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

Support changing OffsetDateTime's offset without changing the actual instant in time. #437

Closed
vincentdephily opened this issue Jan 25, 2022 · 2 comments

Comments

@vincentdephily
Copy link

I'm parsing a unix timestamp and formatting it for the current timezone. This involves calling replace_offset() then checked_add() :

let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory]:[offset_minute]");
let offset_secs = Duration::new(offset.whole_seconds().try_into().unwrap(), 0);
OffsetDateTime::from_unix_timestamp(ts).unwrap()
                                       .replace_offset(offset)
                                       .checked_add(offset_secs)
                                       .unwrap()
                                       .format(&fmt)
                                       .unwrap()

It would be nice and less error-prone to have a set_offset() function that covers both operations.

It would be even nicer to have a set_timezone() function as this code is not DST-aware (and my program is multithreaded at this stage), but I understand that this is a much larger ask (#380).

@jhpratt
Copy link
Member

jhpratt commented Jan 25, 2022

Is this not OffsetDateTime::to_offset?

@vincentdephily
Copy link
Author

D'oh ! Yes, to_offset, I looked for it hard but missed it. Sorry for the noise, closing.

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

2 participants