GithubHelp home page GithubHelp logo

Comments (7)

lifthrasiir avatar lifthrasiir commented on May 13, 2024

Ah, I thought that you've meant milliseconds do not parse correctly, but it turns out that you actually had a typo: %f (not %F, which equals to %Y-%m-%d) is what you need. As my testing with a corrected format string parses your example correctly, I'll close this bug. Thank you for the issue though!

(I should really take a look at SO, but I didn't have much time recently...)

from chrono.

stej avatar stej commented on May 13, 2024

That's great news that it's built in already :)

I didn't find it in official documentation - any mention about parsing milliseconds at https://lifthrasiir.github.io/rust-chrono/chrono/
So I thought I'll look at how to format milliseconds at https://lifthrasiir.github.io/rust-chrono/chrono/format/strftime/index.html to think up what's the correct string but there is no mention about it. That's why I "invented" %F in my question as an example.

from chrono.

lifthrasiir avatar lifthrasiir commented on May 13, 2024

@stej I wonder if the following text is not well visible: "%f 026490000 The number of nanoseconds since last whole second, zero-padded to 9 digits."

from chrono.

stej avatar stej commented on May 13, 2024

It is visible, but it's about parsing nanoseconds. I was looking for milliseconds as it's common in other languages I use
E.g.
"fff" in .NET https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
"S" in Java http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
"SSS" in javascript http://momentjs.com/docs/

As for my original question - I'm talking about milliseconds, not nanoseconds:

let parsed = UTC.datetime_from_str("2015-01-02 11:34:11.123", "%Y-%m-%d %H:%M:%S.%f").unwrap();
let expected = UTC.ymd(2015, 1, 2).and_hms_milli(11, 34, 11, 123);
assert_eq!(parsed, expected)

Is there any way how to parse it? (%f is nano, any other format string for millisecs)?

from chrono.

lifthrasiir avatar lifthrasiir commented on May 13, 2024

Oops! You are right, I've unconsciously thought that you (and I) are talking about... nanoseconds. My bad.

Actually Chrono has a support for parsing left-aligned nanoseconds, so that .3 against .%f will give the nanosecond value of 300,000,000, while .1234 will give that of 123,400,000. The problem is that strftime does not work like this, so Chrono's strftime works just like glibc, i.e. 3 and 1,234 for those strings. Technically speaking you can trigger the left-aligned behavior with... some under-documented way:

use chrono::format::{Item, Numeric, Fixed, Pad, Parsed, parse};
macro_rules! lit  { ($x:expr) => (Item::Literal($x)) }
macro_rules! sp   { ($x:expr) => (Item::Space($x)) }
macro_rules! num0 { ($x:ident) => (Item::Numeric(Numeric::$x, Pad::Zero)) }
macro_rules! fix  { ($x:ident) => (Item::Fixed(Fixed::$x)) }

let mut parsed = Parsed::new();
parse(&mut parsed, "2015-01-02 11:34:11.123",
      // parsed internal representation for "%Y-%m-%d %H:%M:%S." + nanoseconds
      [num0!(Year), lit!("-"), num0!(Month), lit!("-"), num0!(Day), sp!(" "),
       num0!(Hour), lit!(":"), num0!(Minute), lit!(":"), num0!(Second),
       fix!(Nanosecond)].iter().cloned()).unwrap();
let parsed = parsed.to_datetime_with_timezone(&UTC).unwrap();
let expected = UTC.ymd(2015, 1, 2).and_hms_milli(11, 34, 11, 123);
println!("parsed: {}", parsed);
assert_eq!(parsed, expected)

It seems that, however, the current behavior can be confusing even after accepting this caveat. I will at least revise the documentation to note this caveat accurately. Meanwhile, the code above should work throughout any Chrono version since 0.2. Thank you for clarification again.

from chrono.

stej avatar stej commented on May 13, 2024

I looked at your code. Still very new to Rust, so it looks very interesting (for me learning new lang), but still like magic ;)

For me something like

let x = UTC.datetime_from_str("2015-01-02 11:34:11.123", "%Y-%m-%d %H:%M:%S.%f").unwrap();
let y = x.with_nanosecond(x.nanosecond() * 1000000).unwrap();
println!("{} -> {} ", x, y);

is acceptable for some time until I'll understand you workaround.

Thank you for, I like your passion! ;)

from chrono.

lifthrasiir avatar lifthrasiir commented on May 13, 2024

1f40b03 adds a new formatting specifier %.f which accounts for this problem. The following code should work in Chrono 0.2.15 (which I'll publish soon):

let parsed = UTC.datetime_from_str("2015-01-02 11:34:11.123", "%Y-%m-%d %H:%M:%S%.f").unwrap();
//                                                                              ^^^
let expected = UTC.ymd(2015, 1, 2).and_hms_milli(11, 34, 11, 123);
assert_eq!(parsed, expected)

from chrono.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.