Struct astrolabe::DateTime

source ·
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

source

pub fn now() -> Self

Creates a new DateTime instance with SystemTime::now().

let date_time = DateTime::now();
assert!(2021 < date_time.year());
source

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);
source

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"));
source

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);
source

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"));
source

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);
source

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"));
source

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);
source

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"));
source

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"));
source

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:

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"));
source

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"));
source

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 TypePatternExamplesHint
eraG..GGGAD
GGGGAnno Domini*
GGGGGA
yeary2, 20, 201, 2017, 20173
yy02, 20, 01, 17, 73
yyy002, 020, 201, 2017, 20173
yyyy0002, 0020, 0201, 2017, 20173
yyyyy+Unlimited length,
padded with zeros.
quarterq2*
qq02
qqqQ2
qqqq2nd quarter
qqqqq2
monthM9, 12
MM09, 12
MMMSep
MMMMSeptember*
MMMMMS
weekw8, 27Week of year
ww08, 27*
daysd1Day of month
dd01*
D1, 24, 135Day of year, *
DD01, 24, 135
DDD001, 024, 135
week daye31-7, 1 is Sunday, *
ee031-7, 1 is Sunday
eeeTue
eeeeTuesday
eeeeeT
eeeeeeTu
eeeeeee21-7, 1 is Monday
eeeeeeee021-7, 1 is Monday
AM, PMa..aaAM, PM
aaaam, pm*
aaaaa.m., p.m.
aaaaaa, p
AM, PM,
noon, midnight
b..bbAM, PM,
noon, midnight
bbbam, pm,
noon, midnight
*
bbbba.m., p.m.,
noon, midnight
bbbbba, p, n, mi
hourh1, 12[1-12]
hh01, 12*
H0, 23[0-23]
HH00, 23*
K0, 11[0-11]
KK00, 11*
k1, 24[1-24]
kk01, 24*
minutem0, 59
mm00, 59*
seconds0, 59
ss00, 59*
subsecond valuesn1, 9Deciseconds
nn01, 99Centiseconds
nnn001, 999Milliseconds, *
nnnn000001, 999999Microseconds
nnnnn000000001, 999999999Nanoseconds
zoneX-08, +0530, Z
XX-0800, Z
XXX-08:00, Z*
XXXX-0800, -075258, Z
XXXXX-08:00, -07:52:58, Z
x-08, +0530, +00Like 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"));
source

pub fn duration_between(&self, compare: &Self) -> Duration

Returns the duration between the provided DateTime.

Trait Implementations§

source§

impl Add<Duration> for DateTime

§

type Output = DateTime

The resulting type after applying the + operator.
source§

fn add(self, rhs: Duration) -> Self::Output

Performs the + operation. Read more
source§

impl Add<Time> for DateTime

§

type Output = DateTime

The resulting type after applying the + operator.
source§

fn add(self, rhs: Time) -> Self::Output

Performs the + operation. Read more
source§

impl AddAssign<Duration> for DateTime

source§

fn add_assign(&mut self, rhs: Duration)

Performs the += operation. Read more
source§

impl AddAssign<Time> for DateTime

source§

fn add_assign(&mut self, rhs: Time)

Performs the += operation. Read more
source§

impl Clone for DateTime

source§

fn clone(&self) -> DateTime

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl DateUtilities for DateTime

source§

fn year(&self) -> i32

Returns the year.
source§

fn month(&self) -> u32

Returns the month of the year (1-12).
source§

fn day(&self) -> u32

Returns the day of the month (1-31).
source§

fn day_of_year(&self) -> u32

Returns the day of the year (1-365 or 1-366).
source§

fn weekday(&self) -> u8

Returns the day of the week (0-6, 0 is Sunday).
source§

fn from_timestamp(timestamp: i64) -> Self

Creates a date from a unix timestamp (non-leap seconds since January 1, 1970 00:00:00 UTC). Read more
source§

fn timestamp(&self) -> i64

Returns the number of non-leap seconds since January 1, 1970 00:00:00 UTC. (Negative if date is before)
source§

fn set_year(&self, year: i32) -> Result<Self, AstrolabeError>

Sets the year to the provided value. Has to be in range -5879611..=5879611.
source§

fn set_month(&self, month: u32) -> Result<Self, AstrolabeError>

Sets the month of the year to the provided value. Has to be in range 1..=12. Read more
source§

fn set_day(&self, day: u32) -> Result<Self, AstrolabeError>

Sets the day of the month to the provided value. Has to be in range 1..=31 and cannot be greater than the number of days in the current month. Read more
source§

fn set_day_of_year(&self, day_of_year: u32) -> Result<Self, AstrolabeError>

Sets the day of the year to the provided value. Has to be in range 1..=365 or 1..=366 in case of a leap year. Read more
source§

fn add_years(&self, years: u32) -> Self

Adds the provided years to the current date. Read more
source§

fn add_months(&self, months: u32) -> Self

Adds the provided months to the current date. Read more
source§

fn add_days(&self, days: u32) -> Self

Adds the provided days to the current date. Read more
source§

fn sub_years(&self, years: u32) -> Self

Subtracts the provided years from the current date. Read more
source§

fn sub_months(&self, months: u32) -> Self

Subtracts the provided months from the current date. Read more
source§

fn sub_days(&self, days: u32) -> Self

Subtracts the provided days from the current date. Read more
source§

fn clear_until_year(&self) -> Self

Clears date/time units until the year (inclusive).
source§

fn clear_until_month(&self) -> Self

Clears date/time units until the month (inclusive).
source§

fn clear_until_day(&self) -> Self

Clears date/time units until the day (inclusive).
source§

fn years_since(&self, compare: &Self) -> i32

Returns full years since the provided date.
source§

fn months_since(&self, compare: &Self) -> i32

Returns full months since the provided date.
source§

fn days_since(&self, compare: &Self) -> i64

Returns full days since the provided date.
source§

impl Debug for DateTime

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for DateTime

source§

fn default() -> DateTime

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for DateTime

Available on crate feature 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>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for DateTime

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<&Date> for DateTime

source§

fn from(value: &Date) -> Self

Converts to this type from the input type.
source§

impl From<&DateTime> for Date

source§

fn from(value: &DateTime) -> Self

Converts to this type from the input type.
source§

impl From<&DateTime> for DateTime

source§

fn from(date_time: &DateTime) -> Self

Converts to this type from the input type.
source§

impl From<&DateTime> for Time

source§

fn from(value: &DateTime) -> Self

Converts to this type from the input type.
source§

impl From<&Time> for DateTime

source§

fn from(time: &Time) -> Self

Converts to this type from the input type.
source§

impl From<Date> for DateTime

source§

fn from(value: Date) -> Self

Converts to this type from the input type.
source§

impl From<DateTime> for Date

source§

fn from(value: DateTime) -> Self

Converts to this type from the input type.
source§

impl From<DateTime> for Time

source§

fn from(value: DateTime) -> Self

Converts to this type from the input type.
source§

impl From<Time> for DateTime

source§

fn from(value: Time) -> Self

Converts to this type from the input type.
source§

impl FromStr for DateTime

§

type Err = AstrolabeError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl OffsetUtilities for DateTime

source§

fn set_offset(&self, offset: Offset) -> Self

Sets the offset Read more
source§

fn as_offset(&self, offset: Offset) -> Self

Sets the offset, assuming the current instance has the provided offset applied. The new instance will have the specified offset and the datetime itself will be converted to UTC. Read more
source§

fn get_offset(&self) -> Offset

Returns the offset
source§

impl Ord for DateTime

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for DateTime

source§

fn eq(&self, rhs: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for DateTime

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for DateTime

Available on crate feature serde only.

Serialize a DateTime instance as an RFC 3339 string.

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Sub<Duration> for DateTime

§

type Output = DateTime

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Duration) -> Self::Output

Performs the - operation. Read more
source§

impl Sub<Time> for DateTime

§

type Output = DateTime

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Time) -> Self::Output

Performs the - operation. Read more
source§

impl SubAssign<Duration> for DateTime

source§

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation. Read more
source§

impl SubAssign<Time> for DateTime

source§

fn sub_assign(&mut self, rhs: Time)

Performs the -= operation. Read more
source§

impl TimeUtilities for DateTime

source§

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

Panics if the provided value would result in an out of range datetime.

source§

fn add_seconds(&self, seconds: u32) -> Self

Panics if the provided value would result in an out of range datetime.

source§

fn hour(&self) -> u32

Returns the hour (0-23).
source§

fn minute(&self) -> u32

Returns the minute of the hour (0-59).
source§

fn second(&self) -> u32

Returns the second of the minute (0-59).
source§

fn milli(&self) -> u32

Returns the millisecond of the second (0-999).
source§

fn micro(&self) -> u32

Returns the microsecond of the second (0-999_999).
source§

fn nano(&self) -> u32

Returns the nanosecond of the second (0-999_999_999).
source§

fn set_hour(&self, hour: u32) -> Result<Self, AstrolabeError>

Sets the hour to the provided value. Has to be in range 0..=23. Read more
source§

fn set_minute(&self, minute: u32) -> Result<Self, AstrolabeError>

Sets the minute to the provided value. Has to be in range 0..=59. Read more
source§

fn set_second(&self, second: u32) -> Result<Self, AstrolabeError>

Sets the second to the provided value. Has to be in range 0..=59. Read more
source§

fn set_milli(&self, milli: u32) -> Result<Self, AstrolabeError>

Sets the millisecond to the provided value. Has to be in range 0..=100. Read more
source§

fn set_micro(&self, micro: u32) -> Result<Self, AstrolabeError>

Sets the microsecond to the provided value. Has to be in range 0..=100_000. Read more
source§

fn set_nano(&self, nano: u32) -> Result<Self, AstrolabeError>

Sets the nanosecond to the provided value. Has to be in range 0..=100_000_000. Read more
source§

fn add_millis(&self, millis: u32) -> Self

Adds the provided milliseconds.
source§

fn add_micros(&self, micros: u32) -> Self

Adds the provided microseconds.
source§

fn add_nanos(&self, nanos: u32) -> Self

Adds the provided nanoseconds.
source§

fn sub_hours(&self, hours: u32) -> Self

Subtracts the provided hours.
source§

fn sub_minutes(&self, minutes: u32) -> Self

Subtracts the provided minutes.
source§

fn sub_seconds(&self, seconds: u32) -> Self

Subtracts the provided seconds.
source§

fn sub_millis(&self, millis: u32) -> Self

Subtracts the provided milliseconds.
source§

fn sub_micros(&self, micros: u32) -> Self

Subtracts the provided microseconds.
source§

fn sub_nanos(&self, nanos: u32) -> Self

Subtracts the provided nanoseconds.
source§

fn clear_until_hour(&self) -> Self

Clears date/time units until the hour (inclusive).
source§

fn clear_until_minute(&self) -> Self

Clears date/time units until the minute (inclusive).
source§

fn clear_until_second(&self) -> Self

Clears date/time units until the second (inclusive).
source§

fn clear_until_milli(&self) -> Self

Clears date/time units until the millisecond (inclusive).
source§

fn clear_until_micro(&self) -> Self

Clears date/time units until the microsecond (inclusive).
source§

fn clear_until_nano(&self) -> Self

Clears date/time units until the nanosecond (inclusive).
§

type SubDayReturn = i64

Return type for the hour_, minutes_ and seconds_since functions. Is i32 for Time and i64 for DateTime.
source§

fn hours_since(&self, compare: &Self) -> Self::SubDayReturn

Returns full hours since the provided time.
source§

fn minutes_since(&self, compare: &Self) -> Self::SubDayReturn

Returns full minutes since the provided time.
source§

fn seconds_since(&self, compare: &Self) -> Self::SubDayReturn

Returns full seconds since the provided time.
§

type SubSecReturn = i128

Return type for the millis_, micros_ and manos_since functions. Is i64 for Time and i128 for DateTime.
source§

fn millis_since(&self, compare: &Self) -> Self::SubSecReturn

Returns full milliseconds since the provided time.
source§

fn micros_since(&self, compare: &Self) -> Self::SubSecReturn

Returns full microseconds since the provided time.
source§

fn nanos_since(&self, compare: &Self) -> Self::SubSecReturn

Returns full nanoseconds since the provided time.
source§

impl Copy for DateTime

source§

impl Eq for DateTime

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,