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

str parse date with single digits #155

Closed
glennpierce opened this issue Jun 2, 2017 · 2 comments
Closed

str parse date with single digits #155

glennpierce opened this issue Jun 2, 2017 · 2 comments

Comments

@glennpierce
Copy link

Hi as a test I am trying to parse a date time in the format

"1/1/06 0:00"
month/day/year time

The single digits are causing issues I think.

what is the format for parsing this ?

I have tried lots of versions using
https://docs.rs/chrono/0.3.1/chrono/format/strftime/index.html
now but can't work it out

DateTime::parse_from_str("1/1/06 0:00", "%D %k:%M").unwrap();

Thanks

@glennpierce
Copy link
Author

OK after more playing I can parse using

NaiveTime::parse_from_str("0:00:00", "%H:%M:%S").unwrap();
NaiveTime::parse_from_str("0:00:00", "%T").unwrap();
NaiveDate::parse_from_str("1/1/06", "%D").unwrap();
NaiveDateTime::parse_from_str("1/1/06 0:00", "%D %H:%M").unwrap();

Main issue was having to use NaiveDateTime

Without knowing the Chrono internal I think DateTime::parse_from_str("1/1/06 0:00", "%D %H:%M").unwrap();
should just work in cases like this maybe assume UTC

@lifthrasiir
Copy link
Contributor

lifthrasiir commented Jun 21, 2017

(Sorry for very delayed reply :S)

This is not because of the single digit. This is rather the fact that DateTime<FixedOffset> (which is the return type of DateTime::parse_from_str) always requires time zones so you need some offset information to get the full DateTime<FixedOffset>.

There are generally two different ways to resolve the problem. One is, as described above, to use the explicit offset in the input:

DateTime::parse_from_str("1/1/06 0:00 +0000", "%D %H:%M %z").unwrap();

Another is to clearly indicate that we will interpret the input as UTC.

Utc.datetime_from_str("1/1/06 0:00", "%D %H:%M").unwrap(); // note: used to be UTC in 0.3

Edit: For the API discussion there is a dedicated issue #93.

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

No branches or pull requests

2 participants