BurntSushi/jiff

add `BrokenDownTime::set_offset` and `BrokenDownTime::set_iana`

Closed this issue · 1 comments

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?

let offset =

If not, exposing BrokenDownTime::set_offset should suffice.

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.