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
8
votes
1 answer

How can I round a chrono::Datetime to the nearest second?

I want to get the current time rounded to the nearest second using the chrono crate but I don't know how to strip or round the result of chrono::UTC.now(). It doesn't seem like there are any operations to modify an existing…
Samson G.
  • 369
  • 1
  • 3
  • 7
8
votes
1 answer

Retrieve datetime from mySQL database using Diesel

I can't retrieve datetime from a populated mySQL database using Rocket and Diesel. Here is my model: extern crate chrono; use diesel::prelude::*; use diesel::mysql::MysqlConnection; use schema::chrisms; use diesel::sql_types::Datetime; use…
Leonel Sá
  • 81
  • 1
  • 3
8
votes
3 answers

Calculate the duration between now and the next midnight using Chrono

What is the idiomatic way to get the duration between now and the next midnight? I have a function like this: extern crate chrono; use chrono::prelude::*; use time; fn duration_until_next_midnight() -> time::Duration { let now = Local::now();…
cspinetta
  • 443
  • 6
  • 17
7
votes
1 answer

Deserializing a DateTime from a string millisecond timestamp with serde

I receive a millisecond timestamp from an external API as a JSON string attribute. {"time":"1526522699918"} Whats the best way to use Serde to parse the millisecond timestamp as a string? The ts_milliseconds option works with a millisecond…
patrick-fitzgerald
  • 2,561
  • 3
  • 35
  • 49
7
votes
1 answer

How to get a duration of 1 day with Rust chrono?

I am dealing with some Rust code that works with durations of days but the implementation of Duration::days(n) is, per the documentation n * 24 * 60 * 60 seconds, which isn't n days because not all days are 24 * 60 * 60 seconds. This behaviour is…
Ian
  • 2,078
  • 2
  • 19
  • 19
7
votes
1 answer

How to set a chrono DateTime to a specific date and time?

I need to create a chrono::DateTime instance that is set to a specific date and time. For example, I need to create a DateTime instance that is set to something like 3/17/2019 at 4:43 PM (or 3/17/2019 at 16:43). The documentation for…
Factor Three
  • 2,094
  • 5
  • 35
  • 51
7
votes
1 answer

How can I test if a chrono::DateTime falls within certain times of day?

I want to compare the current local time to a constant time range, but my current solution seems more difficult than I'd expect. I can construct a chrono::DateTime with Local::now(). I can then laboriously find out if now lies in a particular…
Matthew Piziak
  • 3,430
  • 4
  • 35
  • 49
6
votes
0 answers

Cargo builds fails: ld framework not found Security

Trying to run a simple program and it fails with the issue note: ld: framework not found Security clang: error: linker command failed with exit code 1 (use -v to see invocation) I tried to do some investigation and I see that chrono 0.4.19 in…
log N
  • 925
  • 9
  • 33
6
votes
1 answer

How do I create a generic Rust struct with a chrono time zone?

Disclaimer: I am new to Rust (previous experience is Python, TypeScript, and Go, in that order), and it is entirely possible that I am missing something really obvious. I am trying to build a Rust clock interface. My basic goal here is that I have a…
Luke Sneeringer
  • 9,270
  • 2
  • 35
  • 32
6
votes
1 answer

How to get the year, month, and date component from Chrono::DateTime?

The documentation doesn't say anything about this topic. Do I need to convert it into Date? Even then, there is no function to get the year component from it. let current_date = chrono::Utc::now(); let year = current_date.year(); //this is not…
DennyHiu
  • 4,861
  • 8
  • 48
  • 80
6
votes
1 answer

How to insert and fetch date in a sqlite database using rusqlite?

I have a struct with NaiveDate field and I want to insert this field to a table. This is my whole code use chrono::naive::NaiveDate; use rusqlite::{params, Connection, Result}; #[derive(Debug)] struct Person { id: i32, name: String, …
Eka
  • 14,170
  • 38
  • 128
  • 212
6
votes
1 answer

How can I get the current weekday in Rust using the Chrono crate?

I am trying to get the current weekday in Rust using the Chrono crate. The JavaScript equivalent would be something like this: new Date().toLocaleDateString('en-US',{weekday: 'long'}); I am getting the current timestamp with the following code: let…
Quddus George
  • 962
  • 2
  • 14
  • 30
6
votes
1 answer

chrono kills my Rust WebAssembly function

I have a Hello World WebAssembly and I tried to add some code to show the time. The following line appears to kill the function and it returns nothing (no text, no error) let dt = Utc::now(); If I comment out the line the function runs as before…
Paul McCarthy
  • 818
  • 9
  • 24
6
votes
2 answers

How to compute the duration between two chrono::DateTime?

I am using the chrono crate and want to compute the Duration between two DateTimes. use chrono::Utc; use chrono::offset::TimeZone; let start_of_period = Utc.ymd(2020, 1, 1).and_hms(0, 0, 0); let end_of_period = Utc.ymd(2021, 1, 1).and_hms(0, 0,…
Erik
  • 755
  • 1
  • 5
  • 17
5
votes
1 answer

Chrono DateTime from u64 unix timestamp in Rust

How does one convert a u64 unix timestamp into a DateTime? let timestamp_u64 = 1657113606; let date_time = ...
alextes
  • 1,817
  • 2
  • 15
  • 22