chrono-tz
chrono-tz copied to clipboard
Tz with a FixedOffset
I am getting a fixed offset timezone with the type chrono::FixedOffset. I also have a timezone based on chrono_tz::Tz. I am trying to unify these types to reduce code duplication.
Is there a way to have a variable that is a FixedOffset or a Tz? I have tried a couple of things with no real success.
Can you describe in more detail your use case? There are a bunch of fixed offset timezones defined in chrono_tz::Etc - are these of help?
I did find a solution, but it wasn't obvious. I will pull out some code that shows the problem and how I managed to solve it.
let tz: FixedOffset;
let date_naive: Date<Utc> = Utc::today();
if let Some(ref user) = self.authorization {
//Extract the timezone
if let Some(local_timezone) = self.current_timezone.clone() {
tz = local_timezone;
} else {
let temp_tz:Tz = user.timezone.as_str().parse().expect("Invalid Timezone");
let offset = temp_tz.offset_from_utc_date(&date_naive.naive_utc());
tz = offset.fix();
}
let date_tz = date.with_timezone(&tz);
date_text = date_tz.format("%H:%M %d/%m/%y").to_string();
} else {
let temp_tz: Tz = Tz_UTC;
let offset = temp_tz.offset_from_utc_date(&date_naive.naive_utc());
tz = offset.fix();
let date_tz = date.with_timezone(&tz);
date_text = date_tz.format("%H:%M %d/%m/%y Utc").to_string();
}
So the important thing to note is that tz is used after this if-else statement. As such, it needs to be a single type.
The method to extract the fixed timezone wasn't obvious and took a lot of effort to work out.
How do you detect the local timezone? I see chrono has a Local TimeZone, but if I want it of type Tz I'm not sure how to detect it.
(sorry for the off topic)
@meskio You can get a FixedOffset (which is another kind of TimeZone via the .offset().fix() method chain, if that answers the question.
extern crate chrono; // 0.4.5
use chrono::prelude::*;
fn main() {
let created: DateTime<Local> = Local::now();
println!("{0} {0:?}", created.offset().fix())
}
@quodlibetor Can I get Tz from FixedOffset?
@quodlibetor Can I get
TzfromFixedOffset?
+1
If someone can submit a PR I'll be happy to review it.
@quodlibetor Can I get Tz from FixedOffset?
I tried converting from a FixedOffset to a Tz in a way that I personally thought would work, but think I found what may be considered a bug:
let fixed_offset = FixedOffset::east(0);
let temp_tz = chrono_tz::Tz::UTC;
let datetime_tz = temp_tz.from_utc_datetime(&Utc::now().naive_utc());
datetime_tz.with_timezone(&fixed_offset);
let new_tz = datetime_tz.timezone();
This compiles and runs fine, but there is some strange unexpected behaviour: trying to call new_tz.name() always returns UTC, no matter which fixed offset is supplied. From the DateTime::with_timezone docs:
Changes the associated time zone. The returned DateTime references the same instant of time from the perspective of the provided time zone.
Should this be considered a bug?
@lancelafontaine with_timezone(&fixed_offset) returns a new value and doesn't mutate datetime_tz in place.
It is currently not possible to crate a TzOffset from a FixedOffset, but not fundamentally impossible.
TzOffset is a superset of FixedOffset, with a base offset, DST offset, time zone abbreviation, and TZ enum variant.
We could crate a TzOffset with the base offset set to that of the FixedOffset and with the other fields empty. With some special code for formatting to work around the missing static string for the abbreviation we are mostly there.
Only what to do with the OffsetName trait...