rust-chrono

How do I add days to a Chrono UTC?

…衆ロ難τιáo~ 提交于 2020-12-04 15:43:20
问题 I am trying to find the preferred way to add days to a Chrono UTC . I want to add 137 days to the current time: let dt = UTC::now(); 回答1: Just use Duration and appropriate operator: use chrono::{Duration, Utc}; fn main() { let dt = Utc::now() + Duration::days(137); println!("today date + 137 days {}", dt); } Test on playground. 回答2: I just wanted to improve on @Stargateur answer. There is no need to use time crate, since chrono crate has Duration struct in it: extern crate chrono; use chrono:

How do I add days to a Chrono UTC?

这一生的挚爱 提交于 2020-12-04 15:34:11
问题 I am trying to find the preferred way to add days to a Chrono UTC . I want to add 137 days to the current time: let dt = UTC::now(); 回答1: Just use Duration and appropriate operator: use chrono::{Duration, Utc}; fn main() { let dt = Utc::now() + Duration::days(137); println!("today date + 137 days {}", dt); } Test on playground. 回答2: I just wanted to improve on @Stargateur answer. There is no need to use time crate, since chrono crate has Duration struct in it: extern crate chrono; use chrono:

How to use a custom serde deserializer for chrono timestamps?

六眼飞鱼酱① 提交于 2020-07-30 05:58:07
问题 I'm trying to parse JSON into a struct which has a chrono::DateTime field. The JSON has the timestamps saved in a custom format that I wrote a deserializer for. How do I connect the two and get it working using #[serde(deserialize_with)] ? I'm using NaiveDateTime for simpler code extern crate serde; extern crate serde_json; use serde::Deserialize; extern crate chrono; use chrono::NaiveDateTime; fn from_timestamp(time: &String) -> NaiveDateTime { NaiveDateTime::parse_from_str(time, "%Y-%m-%dT

Rust Chrono parse date String, ParseError(NotEnough) and ParseError(TooShort)

倖福魔咒の 提交于 2020-04-14 06:17:46
问题 How to convert a String to a chrono::DateTime or chrono::NaiveDateTime And what does the ParseError(NotEnough) or ParseError(TooShort) mean? 回答1: When converting a String into a Chrono object you have to know what parts the input format of the string has. The parts are: Date, Time, TimeZone Examples: "2020-04-12" => Date = NaiveDate "22:10" => Time = NaiveTime "2020-04-12 22:10:57" => Date + Time = NaiveDateTime "2020-04-12 22:10:57+02:00" => Date + Time + TimeZone = DateTime<Tz> The

How to convert Unix time / time since the epoch to standard date and time?

坚强是说给别人听的谎言 提交于 2019-12-01 15:29:39
问题 I'm using the chrono crate; after some digging I discovered the DateTime type has a function timestamp() which could generate epoch time of type i64 . However, I couldn't find out how to convert it back to DateTime . extern crate chrono; use chrono::*; fn main() { let date = chrono::UTC.ymd(2020, 1, 1).and_hms(0, 0, 0); println!("{}", start_date.timestamp()); // ...how to convert it back? } 回答1: You first need to create a NaiveDateTime and then use it to create a DateTime again: extern crate

What does ParseError(NotEnough) from rust-chrono mean?

混江龙づ霸主 提交于 2019-12-01 06:03:49
I'm using rust-chrono and I'm trying to parse a date like this: extern crate chrono; use chrono::*; fn main() { let date_str = "2013-02-14 15:41:07"; let date = DateTime::parse_from_str(&date_str, "%Y-%m-%d %H:%M:%S"); match date { Ok(v) => println!("{:?}", v), Err(e) => println!("{:?}", e) } } And this is the output: ParseError(NotEnough) What does this mean? Not enough of what? Should I be using some other library? You should use UTC.datetime_from_str(&date_str, "%Y-%m-%d %H:%M:%S"); Like: extern crate chrono; use chrono::*; fn main() { let date_str = "2013-02-14 15:41:07"; let date = UTC

What does ParseError(NotEnough) from rust-chrono mean?

别等时光非礼了梦想. 提交于 2019-12-01 03:31:49
问题 I'm using rust-chrono and I'm trying to parse a date like this: extern crate chrono; use chrono::*; fn main() { let date_str = "2013-02-14 15:41:07"; let date = DateTime::parse_from_str(&date_str, "%Y-%m-%d %H:%M:%S"); match date { Ok(v) => println!("{:?}", v), Err(e) => println!("{:?}", e) } } And this is the output: ParseError(NotEnough) What does this mean? Not enough of what? Should I be using some other library? 回答1: You should use UTC.datetime_from_str(&date_str, "%Y-%m-%d %H:%M:%S");

How to find the difference between 2 NaiveDateTimes?

十年热恋 提交于 2019-11-29 18:49:26
I am using chrono . I have now() and some other NaiveDateTime . How can I find a difference between them? let now = Utc::now().naive_utc(); let dt1 = get_my_naive_datetime(); Use NaiveDateTime::signed_duration_since : println!("{:?}", dt1.signed_duration_since(now)) ( playground ) It returns a Duration , which has &self -taking methods to yield whatever units you like, e.g. dt1.signed_duration_since(now).num_days() . 来源: https://stackoverflow.com/questions/48312801/how-to-find-the-difference-between-2-naivedatetimes

How to find the difference between 2 NaiveDateTimes?

折月煮酒 提交于 2019-11-28 12:57:52
问题 I am using chrono. I have now() and some other NaiveDateTime . How can I find a difference between them? let now = Utc::now().naive_utc(); let dt1 = get_my_naive_datetime(); 回答1: Use NaiveDateTime::signed_duration_since: println!("{:?}", dt1.signed_duration_since(now)) (playground) It returns a Duration, which has &self -taking methods to yield whatever units you like, e.g. dt1.signed_duration_since(now).num_days() . 来源: https://stackoverflow.com/questions/48312801/how-to-find-the-difference