Comments (7)
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.
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.
@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.
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.
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.
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.
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)
- Deserializing DateTime from unix timestamp is not implemented, despite description HOT 1
- Ergonomic replacements for panicking APIs. HOT 11
- function Utc::now().checked_add_signed raised wrong error HOT 7
- Proper way to build a DateTime from another DateTime's Date HOT 3
- Index out of bounds: the len is 733 but the index is 798 HOT 11
- Consistently name methods that turn into another more complete type HOT 8
- Use `localtime_rz` on Android if available
- Is there a way to instantiate DateTimes over a generic TimeZone type? HOT 3
- "input is not enough" for DateTime::parse_from_str but works for Utc.datetime_from_str HOT 2
- Version 0.4.36 has breaking changes with 0.4.35 HOT 2
- Compile error using most recent version of chrono 0.4.36 HOT 2
- Deprecations causing large amount of churn, and result in less elegant code HOT 3
- the trait bound `DateTime<Utc>: OptionFrom/ToWasmAbi` is not satisfied when targeting wasm32 HOT 3
- 0.5: Consider using smaller types for arguments
- Make Weekday::num_days_from public HOT 2
- Add a method to the `Offset` trait to return DST info HOT 5
- 0.5: Add a `LocalOffset` type HOT 4
- Verification of published packages HOT 2
- `gh-pages` branch HOT 3
- Releases has problematic naming of 0.4.8 release which puts it at the top HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chrono.