BurntSushi / jiff

A date-time library for Rust that encourages you to jump into the pit of success.
The Unlicense
1.64k stars 26 forks source link

add `BrokenDownTime::set_offset` and `BrokenDownTime::set_iana` #78

Closed martintmk closed 2 weeks ago

martintmk commented 1 month ago

Parsing custom UTC timestamp format:

#[test]
fn test_parsing() {
    let time: strtime::BrokenDownTime =
        jiff::fmt::strtime::parse("%Y-%m-%d at %H:%M:%S", "1970-01-01 at 01:00:00").unwrap();

    let ts = time.to_timestamp().unwrap();
}

Fails with the following error:

parsing format did not include time zone offset directive

This surprised me as I thought that zero offset would be used by default for timestamps if time zone information is not found. The BrokenDownTime offers some modification APIs such as set_month,., set_second, however, set_offset is missing here. This would at least allow me to do:

#[test]
fn test_parsing() {
    let mut time: strtime::BrokenDownTime =
        jiff::fmt::strtime::parse("%Y-%m-%d at %H:%M:%S", "1970-01-01 at 01:00:00").unwrap();

    time.set_offset(Some(Offset::UTC));

    let ts = time.to_timestamp().unwrap();
}

Would it make sense to fallback to UTC offset here?

https://github.com/BurntSushi/jiff/blob/d5f5f4518ed4fa4d5b5017d83fddc0932970600c/src/fmt/strtime/mod.rs#L806

If not, exposing BrokenDownTime::set_offset should suffice.

BurntSushi commented 1 month ago

set_offset would be the way to go. It is definitely wildly inappropriate to assume UTC in cases like this. It might be appropriate in very specific circumstances, but implicitly always assuming UTC is I believe widely considered a source of many many many bugs.