time-rs / time

The most used Rust library for date and time handling.
https://time-rs.github.io
Apache License 2.0
1.09k stars 273 forks source link

Format description doesn't work #574

Closed amokfa closed 1 year ago

amokfa commented 1 year ago

This code fails with the following error:

use anyhow::*;
use time::{OffsetDateTime};

pub fn parse_timestamp(timestamp: &str) -> Result<OffsetDateTime> {
    OffsetDateTime::parse(timestamp, time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]"))
        .context("invalid date format")
}

fn main() {
    let timestamp = "2023-04-30 08:00:55.207";
    dbg!(parse_timestamp(timestamp));
}

Error:

    Error {
        context: "invalid date format",
        source: TryFromParsed(
            InsufficientInformation,
        ),
    },
jhpratt commented 1 year ago

I presume you figured it out since you closed the issue, but the reason it failed is because there is no UTC offset specified.