Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tz with a FixedOffset #11

Open
tl8roy opened this issue Jun 12, 2017 · 11 comments
Open

Tz with a FixedOffset #11

tl8roy opened this issue Jun 12, 2017 · 11 comments

Comments

@tl8roy
Copy link

tl8roy commented Jun 12, 2017

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.

@djzin
Copy link
Contributor

djzin commented Jun 25, 2017

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?

@tl8roy
Copy link
Author

tl8roy commented Jun 25, 2017

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
Copy link
Author

tl8roy commented Jun 27, 2017

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.

@meskio
Copy link

meskio commented Aug 28, 2018

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)

@quodlibetor
Copy link
Contributor

@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())
}

@nooberfsh
Copy link

nooberfsh commented May 24, 2021

@quodlibetor Can I get Tz from FixedOffset?

@vkill
Copy link

vkill commented Mar 25, 2022

@quodlibetor Can I get Tz from FixedOffset?

+1

@djc
Copy link
Contributor

djc commented Mar 29, 2022

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

@lancelafontaine
Copy link

lancelafontaine commented Jan 6, 2023

@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?

@AhmedSoliman
Copy link

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

@pitdicker
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants