pub struct DateTime { /* private fields */ }
Expand description
Combined date and time. Date is in the proleptic Gregorian calendar and clock time is with nanosecond precision.
See the DateUtilites
and TimeUtilities
implementations for get, set and manipulation methods.
OffsetUtilities
implements methods for setting and getting the offset.
Range: 30. June -5879611 00:00:00
..=12. July 5879611 23:59:59
. Please note that year 0 does not exist. After year -1 follows year 1.
Implementations§
source§impl DateTime
impl DateTime
sourcepub fn now() -> Self
pub fn now() -> Self
Creates a new DateTime
instance with SystemTime::now()
.
let date_time = DateTime::now();
assert!(2021 < date_time.year());
sourcepub fn now_local() -> Self
pub fn now_local() -> Self
Creates a new DateTime
instance with SystemTime::now()
with the local timezone as the offset.
let date_time = DateTime::now_local();
assert!(2021 < date_time.year());
assert_eq!(date_time.get_offset(), Offset::Local);
sourcepub fn from_ymdhms(
year: i32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32
) -> Result<Self, AstrolabeError>
pub fn from_ymdhms( year: i32, month: u32, day: u32, hour: u32, minute: u32, second: u32 ) -> Result<Self, AstrolabeError>
Creates a new DateTime
instance from year, month, day (day of month), hour, minute and seconds.
Returns an OutOfRange
error if the provided values are invalid.
let date_time = DateTime::from_ymdhms(2022, 05, 02, 12, 32, 1).unwrap();
assert_eq!("2022/05/02 12:32:01", date_time.format("yyyy/MM/dd HH:mm:ss"));
sourcepub fn as_ymdhms(&self) -> (i32, u32, u32, u32, u32, u32)
pub fn as_ymdhms(&self) -> (i32, u32, u32, u32, u32, u32)
Returns the DateTime as year, month, day (day of month), hour, minute and seconds.
let date_time = DateTime::from_ymdhms(2022, 05, 02, 12, 32, 1).unwrap();
let (year, month, day, hour, minute, second) = date_time.as_ymdhms();
assert_eq!(2022, year);
assert_eq!(5, month);
assert_eq!(2, day);
assert_eq!(12, hour);
assert_eq!(32, minute);
assert_eq!(1, second);
sourcepub fn from_ymd(year: i32, month: u32, day: u32) -> Result<Self, AstrolabeError>
pub fn from_ymd(year: i32, month: u32, day: u32) -> Result<Self, AstrolabeError>
Creates a new DateTime
instance from year, month and day (day of month).
Returns an OutOfRange
error if the provided values are invalid.
let date_time = DateTime::from_ymd(2022, 05, 02).unwrap();
assert_eq!("2022/05/02", date_time.format("yyyy/MM/dd"));
sourcepub fn as_ymd(&self) -> (i32, u32, u32)
pub fn as_ymd(&self) -> (i32, u32, u32)
Returns the DateTime as year, month and day (day of month).
let date_time = DateTime::from_ymd(2022, 05, 02).unwrap();
let (year, month, day) = date_time.as_ymd();
assert_eq!(2022, year);
assert_eq!(5, month);
assert_eq!(2, day);
sourcepub fn from_hms(
hour: u32,
minute: u32,
second: u32
) -> Result<Self, AstrolabeError>
pub fn from_hms( hour: u32, minute: u32, second: u32 ) -> Result<Self, AstrolabeError>
Creates a new DateTime
instance from hour, minute and seconds.
Returns an OutOfRange
error if the provided values are invalid.
let date_time = DateTime::from_hms(12, 32, 12).unwrap();
assert_eq!("0001/01/01 12:32:12", date_time.format("yyyy/MM/dd HH:mm:ss"));
sourcepub fn as_hms(&self) -> (u32, u32, u32)
pub fn as_hms(&self) -> (u32, u32, u32)
Returns the DateTime as hour, minute and seconds.
let date_time = DateTime::from_hms(12, 12, 12).unwrap();
let (hour, minute, second) = date_time.as_hms();
assert_eq!(12, hour);
assert_eq!(12, minute);
assert_eq!(12, second);
sourcepub fn set_time(&self, time: Time) -> Self
pub fn set_time(&self, time: Time) -> Self
Creates a new DateTime
with the specified time.
let time = Time::from_hms(12, 32, 1).unwrap();
let date_time = DateTime::from_ymd(2022, 5, 2).unwrap().set_time(time);
assert_eq!("2022/05/02 12:32:01", date_time.format("yyyy/MM/dd HH:mm:ss"));
sourcepub fn parse_rfc3339(string: &str) -> Result<Self, AstrolabeError>
pub fn parse_rfc3339(string: &str) -> Result<Self, AstrolabeError>
Creates a new DateTime
instance from an RFC 3339 timestamp string.
let date_time = DateTime::parse_rfc3339("2022-05-02T15:30:20Z").unwrap();
assert_eq!("2022/05/02 15:30:20", date_time.format("yyyy/MM/dd HH:mm:ss"));
sourcepub fn format_rfc3339(&self, precision: Precision) -> String
pub fn format_rfc3339(&self, precision: Precision) -> String
Format as an RFC 3339 timestamp (2022-05-02T15:30:20Z
).
Use the Precision
enum to specify decimal places after seconds:
Precision::Seconds
->2022-05-02T15:30:20Z
Precision::Centis
->2022-05-02T15:30:20.00Z
Precision::Millis
->2022-05-02T15:30:20.000Z
Precision::Micros
->2022-05-02T15:30:20.000000Z
Precision::Nanos
->2022-05-02T15:30:20.000000000Z
let date_time = DateTime::from_ymdhms(2022, 5, 2, 15, 30, 20).unwrap();
assert_eq!("2022-05-02T15:30:20Z", date_time.format_rfc3339(Precision::Seconds));
// Equivalent to:
assert_eq!("2022-05-02T15:30:20Z", date_time.format("yyyy-MM-ddTHH:mm:ssXXX"));
sourcepub fn parse(string: &str, format: &str) -> Result<Self, AstrolabeError>
pub fn parse(string: &str, format: &str) -> Result<Self, AstrolabeError>
Parses a string with a given format and creates a new DateTime
instance from it. See DateTime::format
for a list of available symbols.
Returns an InvalidFormat
error if the given string could not be parsed with the given format.
let date_time = DateTime::parse("2022-05-02 12:32:01", "yyyy-MM-dd HH:mm:ss").unwrap();
assert_eq!("2022/05/02 12:32:01", date_time.format("yyyy/MM/dd HH:mm:ss"));
sourcepub fn format(&self, format: &str) -> String
pub fn format(&self, format: &str) -> String
Formatting with format strings based on Unicode Date Field Symbols.
Please note that not all symbols are implemented. If you need something that is not implemented, please open an issue on GitHub describing your need.
§Available Symbols:
Field Type | Pattern | Examples | Hint |
---|---|---|---|
era | G..GGG | AD | |
GGGG | Anno Domini | * | |
GGGGG | A | ||
year | y | 2, 20, 201, 2017, 20173 | |
yy | 02, 20, 01, 17, 73 | ||
yyy | 002, 020, 201, 2017, 20173 | ||
yyyy | 0002, 0020, 0201, 2017, 20173 | ||
yyyyy+ | … | Unlimited length, padded with zeros. | |
quarter | q | 2 | * |
02 | |||
qqq | Q2 | ||
qqqq | 2nd quarter | ||
qqqqq | 2 | ||
month | M | 9, 12 | |
MM | 09, 12 | ||
MMM | Sep | ||
MMMM | September | * | |
MMMMM | S | ||
week | w | 8, 27 | Week of year |
ww | 08, 27 | * | |
days | d | 1 | Day of month |
dd | 01 | * | |
D | 1, 24, 135 | Day of year, * | |
DD | 01, 24, 135 | ||
DDD | 001, 024, 135 | ||
week day | e | 3 | 1-7, 1 is Sunday, * |
ee | 03 | 1-7, 1 is Sunday | |
eee | Tue | ||
eeee | Tuesday | ||
eeeee | T | ||
eeeeee | Tu | ||
eeeeeee | 2 | 1-7, 1 is Monday | |
eeeeeeee | 02 | 1-7, 1 is Monday | |
AM, PM | a..aa | AM, PM | |
aaa | am, pm | * | |
aaaa | a.m., p.m. | ||
aaaaa | a, p | ||
AM, PM, noon, midnight | b..bb | AM, PM, noon, midnight | |
bbb | am, pm, noon, midnight | * | |
bbbb | a.m., p.m., noon, midnight | ||
bbbbb | a, p, n, mi | ||
hour | h | 1, 12 | [1-12] |
hh | 01, 12 | * | |
H | 0, 23 | [0-23] | |
HH | 00, 23 | * | |
K | 0, 11 | [0-11] | |
KK | 00, 11 | * | |
k | 1, 24 | [1-24] | |
kk | 01, 24 | * | |
minute | m | 0, 59 | |
mm | 00, 59 | * | |
second | s | 0, 59 | |
ss | 00, 59 | * | |
subsecond values | n | 1, 9 | Deciseconds |
nn | 01, 99 | Centiseconds | |
nnn | 001, 999 | Milliseconds, * | |
nnnn | 000001, 999999 | Microseconds | |
nnnnn | 000000001, 999999999 | Nanoseconds | |
zone | X | -08, +0530, Z | |
XX | -0800, Z | ||
XXX | -08:00, Z | * | |
XXXX | -0800, -075258, Z | ||
XXXXX | -08:00, -07:52:58, Z | ||
x | -08, +0530, +00 | Like X but without Z | |
xx | -0800, +0000 | ||
xxx | -08:00, +00:00 | * | |
xxxx | -0800, -075258, +0000 | ||
xxxxx | -08:00, -07:52:58, +00:00 |
*
= Default
If the sequence is longer than listed in the table, the output will be the same as the default pattern for this unit (marked with *
).
Surround any character with apostrophes ('
) to escape them.
If you want escape '
, write ''
.
let date_time = DateTime::from_ymdhms(2022, 5, 2, 12, 32, 1).unwrap();
assert_eq!("2022/05/02 12:32:01", date_time.format("yyyy/MM/dd HH:mm:ss"));
// Escape characters
assert_eq!("2022/MM/dd 12:32:01", date_time.format("yyyy/'MM/dd' HH:mm:ss"));
assert_eq!("2022/'05/02' 12:32:01", date_time.format("yyyy/''MM/dd'' HH:mm:ss"));
sourcepub fn duration_between(&self, compare: &Self) -> Duration
pub fn duration_between(&self, compare: &Self) -> Duration
Returns the duration between the provided DateTime.
Trait Implementations§
source§impl AddAssign<Duration> for DateTime
impl AddAssign<Duration> for DateTime
source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+=
operation. Read moresource§impl AddAssign<Time> for DateTime
impl AddAssign<Time> for DateTime
source§fn add_assign(&mut self, rhs: Time)
fn add_assign(&mut self, rhs: Time)
+=
operation. Read moresource§impl DateUtilities for DateTime
impl DateUtilities for DateTime
source§fn day_of_year(&self) -> u32
fn day_of_year(&self) -> u32
1-365
or 1-366
).source§fn from_timestamp(timestamp: i64) -> Self
fn from_timestamp(timestamp: i64) -> Self
source§fn timestamp(&self) -> i64
fn timestamp(&self) -> i64
source§fn set_year(&self, year: i32) -> Result<Self, AstrolabeError>
fn set_year(&self, year: i32) -> Result<Self, AstrolabeError>
-5879611..=5879611
.source§fn set_month(&self, month: u32) -> Result<Self, AstrolabeError>
fn set_month(&self, month: u32) -> Result<Self, AstrolabeError>
1..=12
. Read moresource§fn set_day(&self, day: u32) -> Result<Self, AstrolabeError>
fn set_day(&self, day: u32) -> Result<Self, AstrolabeError>
1..=31
and cannot be greater than the number of days in the current month. Read moresource§fn set_day_of_year(&self, day_of_year: u32) -> Result<Self, AstrolabeError>
fn set_day_of_year(&self, day_of_year: u32) -> Result<Self, AstrolabeError>
1..=365
or 1..=366
in case of a leap year. Read moresource§fn add_years(&self, years: u32) -> Self
fn add_years(&self, years: u32) -> Self
source§fn add_months(&self, months: u32) -> Self
fn add_months(&self, months: u32) -> Self
source§fn sub_years(&self, years: u32) -> Self
fn sub_years(&self, years: u32) -> Self
source§fn sub_months(&self, months: u32) -> Self
fn sub_months(&self, months: u32) -> Self
source§fn sub_days(&self, days: u32) -> Self
fn sub_days(&self, days: u32) -> Self
source§fn clear_until_year(&self) -> Self
fn clear_until_year(&self) -> Self
source§fn clear_until_month(&self) -> Self
fn clear_until_month(&self) -> Self
source§fn clear_until_day(&self) -> Self
fn clear_until_day(&self) -> Self
source§fn years_since(&self, compare: &Self) -> i32
fn years_since(&self, compare: &Self) -> i32
source§fn months_since(&self, compare: &Self) -> i32
fn months_since(&self, compare: &Self) -> i32
source§fn days_since(&self, compare: &Self) -> i64
fn days_since(&self, compare: &Self) -> i64
source§impl<'de> Deserialize<'de> for DateTime
Available on crate feature serde
only.
impl<'de> Deserialize<'de> for DateTime
serde
only.Deserialize an RFC 3339 string into a DateTime
instance.
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl OffsetUtilities for DateTime
impl OffsetUtilities for DateTime
source§fn set_offset(&self, offset: Offset) -> Self
fn set_offset(&self, offset: Offset) -> Self
source§fn as_offset(&self, offset: Offset) -> Self
fn as_offset(&self, offset: Offset) -> Self
UTC
. Read moresource§fn get_offset(&self) -> Offset
fn get_offset(&self) -> Offset
source§impl Ord for DateTime
impl Ord for DateTime
source§impl PartialOrd for DateTime
impl PartialOrd for DateTime
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Serialize for DateTime
Available on crate feature serde
only.
impl Serialize for DateTime
serde
only.Serialize a DateTime
instance as an RFC 3339 string.
source§impl SubAssign<Duration> for DateTime
impl SubAssign<Duration> for DateTime
source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-=
operation. Read moresource§impl SubAssign<Time> for DateTime
impl SubAssign<Time> for DateTime
source§fn sub_assign(&mut self, rhs: Time)
fn sub_assign(&mut self, rhs: Time)
-=
operation. Read moresource§impl TimeUtilities for DateTime
impl TimeUtilities for DateTime
source§fn add_hours(&self, hours: u32) -> Self
fn add_hours(&self, hours: u32) -> Self
Panics if the provided value would result in an out of range datetime.
source§fn add_minutes(&self, minutes: u32) -> Self
fn add_minutes(&self, minutes: u32) -> Self
Panics if the provided value would result in an out of range datetime.
source§fn add_seconds(&self, seconds: u32) -> Self
fn add_seconds(&self, seconds: u32) -> Self
Panics if the provided value would result in an out of range datetime.
source§fn set_hour(&self, hour: u32) -> Result<Self, AstrolabeError>
fn set_hour(&self, hour: u32) -> Result<Self, AstrolabeError>
0..=23
. Read moresource§fn set_minute(&self, minute: u32) -> Result<Self, AstrolabeError>
fn set_minute(&self, minute: u32) -> Result<Self, AstrolabeError>
0..=59
. Read moresource§fn set_second(&self, second: u32) -> Result<Self, AstrolabeError>
fn set_second(&self, second: u32) -> Result<Self, AstrolabeError>
0..=59
. Read moresource§fn set_milli(&self, milli: u32) -> Result<Self, AstrolabeError>
fn set_milli(&self, milli: u32) -> Result<Self, AstrolabeError>
0..=100
. Read moresource§fn set_micro(&self, micro: u32) -> Result<Self, AstrolabeError>
fn set_micro(&self, micro: u32) -> Result<Self, AstrolabeError>
0..=100_000
. Read moresource§fn set_nano(&self, nano: u32) -> Result<Self, AstrolabeError>
fn set_nano(&self, nano: u32) -> Result<Self, AstrolabeError>
0..=100_000_000
. Read more