chrono-tz icon indicating copy to clipboard operation
chrono-tz copied to clipboard

Tz with a FixedOffset

Open tl8roy opened this issue 8 years ago • 11 comments

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.

tl8roy avatar Jun 12 '17 04:06 tl8roy

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?

djzin avatar Jun 25 '17 21:06 djzin

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.

tl8roy avatar Jun 25 '17 22:06 tl8roy

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.

tl8roy avatar Jun 27 '17 06:06 tl8roy

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 avatar Aug 28 '18 18:08 meskio

@meskio You can get a FixedOffset (which is another kind of TimeZone via the .offset().fix() method chain, if that answers the question.

playground

extern crate chrono; // 0.4.5
use chrono::prelude::*;
fn main() {
    let created: DateTime<Local> = Local::now();
    println!("{0} {0:?}", created.offset().fix())
}

quodlibetor avatar Nov 27 '18 01:11 quodlibetor

@quodlibetor Can I get Tz from FixedOffset?

nooberfsh avatar May 24 '21 13:05 nooberfsh

@quodlibetor Can I get Tz from FixedOffset?

+1

vkill avatar Mar 25 '22 10:03 vkill

If someone can submit a PR I'll be happy to review it.

djc avatar Mar 29 '22 08:03 djc

@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 avatar Jan 06 '23 19:01 lancelafontaine

@lancelafontaine with_timezone(&fixed_offset) returns a new value and doesn't mutate datetime_tz in place.

AhmedSoliman avatar May 22 '23 12:05 AhmedSoliman

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...

pitdicker avatar Apr 05 '24 13:04 pitdicker