devela::_dep::jiff::civil

Function datetime

pub const fn datetime(
    year: i16,
    month: i8,
    day: i8,
    hour: i8,
    minute: i8,
    second: i8,
    subsec_nanosecond: i32,
) -> DateTime
Available on crate features dep_jiff and alloc only.
Expand description

Creates a new DateTime value in a const context.

This is a convenience free function for DateTime::constant. It is intended to provide a terse syntax for constructing DateTime values from parameters that are known to be valid.

§Panics

This routine panics when DateTime::new would return an error. That is, when the given components do not correspond to a valid datetime. Namely, all of the following must be true:

  • The year must be in the range -9999..=9999.
  • The month must be in the range 1..=12.
  • The day must be at least 1 and must be at most the number of days in the corresponding month. So for example, 2024-02-29 is valid but 2023-02-29 is not.
  • 0 <= hour <= 23
  • 0 <= minute <= 59
  • 0 <= second <= 59
  • 0 <= subsec_nanosecond <= 999,999,999

Similarly, when used in a const context, invalid parameters will prevent your Rust program from compiling.

§Example

use jiff::civil::DateTime;

let d = DateTime::constant(2024, 2, 29, 21, 30, 5, 123_456_789);
assert_eq!(d.date().year(), 2024);
assert_eq!(d.date().month(), 2);
assert_eq!(d.date().day(), 29);
assert_eq!(d.time().hour(), 21);
assert_eq!(d.time().minute(), 30);
assert_eq!(d.time().second(), 5);
assert_eq!(d.time().millisecond(), 123);
assert_eq!(d.time().microsecond(), 456);
assert_eq!(d.time().nanosecond(), 789);