Questions tagged [rust-chrono]

Date and time handling for Rust. (also known as rust-chrono) It aims to be a feature-complete superset of the time library.

Chrono is a date and time handling for Rust. In particular,

  • Chrono strictly adheres to ISO 8601.
  • Chrono is timezone-aware by default, with separate timezone-naive types.
  • Chrono is space-optimal and (while not being the primary goal) reasonably efficient.

Links:

83 questions
5
votes
2 answers

The trait `std::ops::Add` is not implemented for `chrono::DateTime`

extern crate chrono; use chrono::{DateTime, Utc}; use std::time::Duration; pub fn after(start: DateTime) -> DateTime { start + Duration::from_secs(1) } fails with: error[E0277]: cannot add `std::time::Duration` to…
Victor Basso
  • 5,556
  • 5
  • 42
  • 60
4
votes
1 answer

Why DateTime can not satisfy serde::Serialize?

extern crate serde; extern crate serde_json; #[macro_use] extern crate serde_derive; use chrono::{self, Date,DateTime, TimeZone}; use serde_derive::{Serialize,Deserialize}; // 1.0.91 #[derive(Serialize,Deserialize )] struct Test where…
davyzhang
  • 2,419
  • 3
  • 26
  • 34
3
votes
3 answers

Trouble with strptime() conversion of duration time string

I have some duration type data (lap times) as pl.Utf8 that fails to convert using strptime, whereas regular datetimes work as expected. Minutes (before :) and Seconds (before .) are always padded to two digits, Milliseconds are always 3 digits. Lap…
Dorian
  • 33
  • 8
3
votes
1 answer

How to create a file/directory with a name from DateTime::format()?

I would like to make directory with a name in "%Y%m%y_%H%M%S" format, but trying to use the result of .format() directly causes an error: use std::fs; use chrono; fn main() { let now = chrono::offset::Local::now(); let…
WebOrCode
  • 6,852
  • 9
  • 43
  • 70
3
votes
1 answer

How do I parse a date from RFC2822 allowing a time zone at the end of the string?

I am trying to parse mail headers. I am trying to parse the date with Chrono, by giving it the RFC2822 strings. The problem is that it is not able to parse strings on the format 2 Nov 2021 14:26:12 +0000 (UTC), where the problem seems to be the last…
fevar
  • 107
  • 6
3
votes
1 answer

Idiomatic way to check if a chrono::DateTime is within date and time range?

I am curious if there is an idiomatic way to check if a chrono::DateTime is within a time range. In my use case I just need to check if the DateTime falls within the next half hour, from current time. This is what I put together so far. Its…
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
3
votes
3 answers

How does rust chrono Duration get number of years or months?

I'm quite new to Rust and chrono library. I checked the https://docs.rs/chrono/0.4.19/chrono/struct.Duration.html#method.num_weeks, but there's no num_years() or num_months() API in chrono::Duration. Is there any work around solution for this ?
linrongbin
  • 2,967
  • 6
  • 31
  • 59
3
votes
1 answer

How to serialize and deserialize chrono::Duration?

In my current project I'm trying to store a chrono::Duration in a configuration struct, which will be serialized and deserialized occasionally using serde_json. Unfortunately, it appears that Serialize and Deserialize aren't implemented for…
3
votes
2 answers

Convert Postgres timestamp to Rust Chrono

How to covert a PostgreSQL timestamp (with time zone) to Rust Chrono DateTime? Example: 2020-04-12 22:10:57.0+02
Ralph Bisschops
  • 1,888
  • 1
  • 22
  • 34
3
votes
1 answer

Method exists but the following trait bounds were not satisfied (generics)

I have a working function foo, which compiles without error: use chrono; fn foo() { let now = chrono::offset::Local::now(); let mut buf = String::new(); buf.push_str(&now.format("%Y/%m/%d").to_string()); } When I try to extract the now…
nishanthshanmugham
  • 2,967
  • 1
  • 25
  • 29
3
votes
1 answer

How to transform `chrono::format::strftime` to `chrono::format::Item` statically?

I have a static array of chrono::format::strftime formats that my application supports. I'd like to avoid parsing them during run time, so I defined a lazy_static! block that parses them to a collection of chrono::format::Item. However, when I try…
Shmoopy
  • 5,334
  • 4
  • 36
  • 72
3
votes
1 answer

Rust chrono gives ParseError(NotEnough)

I am trying to parse a datetime string to DateTime object, but when I try this I am getting this ParseError. I don't understand what is going on, can someone help me out? datetime string: 09-January-2018 12:00:00 code: let date =…
Catman155
  • 155
  • 1
  • 9
3
votes
1 answer

Converting a UTC time via Timezones

I am displaying a date on the screen using the chrono crate. The intention is to show the date in the users preferred time or UTC if no is set. I have the UTC default set up, but I am unsure on the best method to record the user's timezone and how…
tl8
  • 537
  • 5
  • 22
2
votes
0 answers

Parse Zulu time suffix in Chrono - which format?

The following runs fine: use chrono::{DateTime}; fn main() { let parse_from_str = DateTime::parse_from_str; let ts = "2000-01-01T01:23+01:00"; let fmt = "%Y-%m-%dT%H:%M%z"; let parsed = parse_from_str(ts, fmt); println!("{:?}",…
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
2
votes
1 answer

Correct way to get accurate time in Rust?

I'm trying to get accurate time with: use chrono::{DateTime, Local, Utc}; use std::time::SystemTime; fn main() { println!( "Local.now() {}", Local::now().format("%H:%m:%S").to_string() ); println!("Utc.now() {}",…
GlinesMome
  • 1,549
  • 1
  • 22
  • 35