pub struct DateTime<C: Calendar, S: Standard> { /* private fields */ }Expand description
A calendar date and time, with attosecond precision, representing the
time elapsed since the start of the Common Era in a traditional way
according to a particular time Standard.
DateTimes are type parameterized by a Calendar type which is either
Gregorian or Julian.
Normal ranges for values are as follows:
- year: any i32 value (
-2_147_483_648..2_147_483_647) - month:
1..12 - day:
1..31(or less in some months) - hour:
0..23 - minute:
0..59 - second:
0..60(60 is only used under leap-second time standards) - attosecond:
0..999_999_999_999_999_999
Even when years are negative, the other values must remain in the positive ranges as specified (i.e. negative values are not a reflection against zero, but just an extension further backwards)
Zero and Negative years are handled in accordance with ISO 8601, such that year 0 is 1 B.C., and year -1 is 2 B.C., etc. In general:
- n B.C. is represented by year 1-n
- Year -y represents year y+1 B.C. (for positive y).
This type is proleptic; that is, it allows values for dates outside the
normal usage period of the Calendar.
The oldest date representable is -2147483648-01-01 00:00:00.000000000000000000
The newest date representable is 2147483647-12-31 23:59:59.999999999999999999
Internally this is stored in a packed format and is 128 bits in size.
This represents the same thing that an Instant does, but it makes Calendar data
easier to work with, and has such date precomputed and packed within.
Implementations§
Source§impl<C: Calendar, S: Standard> DateTime<C, S>
impl<C: Calendar, S: Standard> DateTime<C, S>
Sourcepub unsafe fn new_unchecked(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
attosecond: u64,
) -> Self
pub unsafe fn new_unchecked( year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, attosecond: u64, ) -> Self
Create a new DateTime with the given parts.
§Safety
Parameter values must be within normal ranges, or else the results are not defined.
Sourcepub fn new(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
attosecond: u64,
) -> Result<Self, Error>
pub fn new( year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, attosecond: u64, ) -> Result<Self, Error>
Create a new DateTime from the given parts.
Values must be within normal ranges. See DateTime for details.
§Errors
Will return Error::RangeError if any input is outside of the normal
range (months from 1-12, days from 1-31, hours from 0-23, minutes from
0-59, seconds from 0-60, attoseconds from 0-999_999_999_999_999_999)
Sourcepub fn new_bc(
bc_year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
attosecond: u64,
) -> Result<Self, Error>
pub fn new_bc( bc_year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, attosecond: u64, ) -> Result<Self, Error>
Create a new DateTime from the given parts, with BC years.
Values must be within normal ranges. See DateTime for details.
§Errors
Will return Error::RangeError if any input is outside of the normal
range (months from 1-12, days from 1-31, hours from 0-23, minutes from
0-59, seconds from 0-60, attoseconds from 0-999_999_999_999_999_999)
Sourcepub fn new_abnormal(
year: i32,
month: i64,
day: i64,
hour: i64,
minute: i64,
second: i64,
attosecond: i64,
) -> Self
pub fn new_abnormal( year: i32, month: i64, day: i64, hour: i64, minute: i64, second: i64, attosecond: i64, ) -> Self
Create a new DateTime from the given parts.
Values that are out of normal ranges are allowed, including values that are negative. This function will adjust the input your provide into a normal form.
The types we are working with are large i64 types, but they can still overflow. Overflow is not detected or reported (FIXME).
§Panics
Shouldn’t panic but several math assertions may trigger if we have a bug when compiled in development mode.
Sourcepub fn from_day_number(day_number: i64) -> Result<Self, Error>
pub fn from_day_number(day_number: i64) -> Result<Self, Error>
Create a DateTime from a day number (integer).
January 1st of 1 A.D. (Common Era) is the epoch and has a day number of 0.
Hour, minute, second and attosecond will be zero.
§Errors
Will return a Error::RangeError if day_number is out of range.
Sourcepub fn from_day_number_and_fraction(
day_number: i64,
day_fraction: f64,
) -> Result<Self, Error>
pub fn from_day_number_and_fraction( day_number: i64, day_fraction: f64, ) -> Result<Self, Error>
Create a DateTime from a day number (integer) and day fraction (float).
January 1st of 1 A.D. (Common Era) is the epoch and has a day number of 0.
§Errors
Will return a Error::RangeError if day_number is out of range.
Will return Error::RangeError if day_fraction is <0.0 or >=1.0
§Panics
Panics on assertions that should only fail if there is a bug.
Sourcepub fn from_duration_from_epoch(duration: Duration) -> Self
pub fn from_duration_from_epoch(duration: Duration) -> Self
Create a DateTime from a Duration from the calendar epoch
(with the calendar epoch represented in time Standard S, such
that no time Standard conversions are done here).
Sourcepub fn attosecond(&self) -> u64
pub fn attosecond(&self) -> u64
The attosecond part. Ranges from 0 .. 999_999_999_999_999_999
Sourcepub fn time(&self) -> (u8, u8, u8, u64)
pub fn time(&self) -> (u8, u8, u8, u64)
The time part
Returns (hour, minute, second, attosecond)
Sourcepub fn set_year_bc(&mut self, year_bc: i32)
pub fn set_year_bc(&mut self, year_bc: i32)
Set the year with a BC year, leaving other fields unchanged
Sourcepub fn set_month(&mut self, month: u8) -> Result<(), Error>
pub fn set_month(&mut self, month: u8) -> Result<(), Error>
Set the month, leaving other fields unchanged
§Errors
Will return Error::RangeError if month is <1 or >12.
Sourcepub fn set_day(&mut self, day: u8) -> Result<(), Error>
pub fn set_day(&mut self, day: u8) -> Result<(), Error>
Set the day, leaving other fields unchanged
§Errors
Will return Error::RangeError if day is outside of the range of days
for the month.
Sourcepub fn set_hour(&mut self, hour: u8) -> Result<(), Error>
pub fn set_hour(&mut self, hour: u8) -> Result<(), Error>
Set the hour, leaving other fields unchanged
§Errors
Will return Error::RangeError if hour is greater than 23.
Sourcepub fn set_minute(&mut self, minute: u8) -> Result<(), Error>
pub fn set_minute(&mut self, minute: u8) -> Result<(), Error>
Set the minute, leaving other fields unchanged
§Errors
Will return Error::RangeError if minute is greater than 59.
Sourcepub fn set_second(&mut self, second: u8) -> Result<(), Error>
pub fn set_second(&mut self, second: u8) -> Result<(), Error>
Set the second, leaving other fields unchanged
§Errors
Will return Error::RangeError if second is greater than 60.
The second of ‘60’ should only be used for leapsecond situations, but
no error is thrown if used otherwise.
Sourcepub fn set_attosecond(&mut self, attosecond: u64) -> Result<(), Error>
pub fn set_attosecond(&mut self, attosecond: u64) -> Result<(), Error>
Set the attosecond, leaving other fields unchanged
§Errors
Will return Error::RangeError if attosecond are out of the proscribed range
(more than 1 seconds worth of attoseconds)
Sourcepub fn set_date(&mut self, date: (i32, u8, u8)) -> Result<(), Error>
pub fn set_date(&mut self, date: (i32, u8, u8)) -> Result<(), Error>
Set the date part (year, month, day)
§Errors
Will return Error::RangeError if any input values are out of the proscribed range
Sourcepub fn set_time(&mut self, date: (u8, u8, u8, u64)) -> Result<(), Error>
pub fn set_time(&mut self, date: (u8, u8, u8, u64)) -> Result<(), Error>
Set the time part (hour, minute, second, attosecond)
§Errors
Will return Error::RangeError if any input values are out of the proscribed range
Sourcepub fn day_number(&self) -> i64
pub fn day_number(&self) -> i64
Day number (integer).
January 1st of 1 A.D. (Common Era) is the epoch and has a day number of 0.
§Panics
Will only panic on a bug that caused internal values to get out of range.
Sourcepub fn day_fraction(&self) -> f64
pub fn day_fraction(&self) -> f64
Day fraction, fractional part of the day since midnight
This isn’t attosecond accurate because a day contains more attoseconds than can fit in a f64 (which has 52 bits of precision). However, it should be accurate to 10,000 attoseconds.
Sourcepub fn duration_from_epoch(&self) -> Duration
pub fn duration_from_epoch(&self) -> Duration
Duration from the calendar epoch (with the calendar epoch represented
in the time Standard S, such that no time Standard conversions are
done here).
§Panics
Only panics if we have an internal data consistency issue