std.time.Date
Valuetype pub copy DateA type representing a date in the Gregorian calendar, without a time and timezone.
Static methods
new
Show source codeHide source code
fn pub inline static new(year: Int, month: Int, day: Int) -> Option[Date] {
if
year >= -2147483648
and year <= 2147483647
and month >= 1
and month <= 12
and day >= 1
and day <= 31
{
Option.Some(new_unchecked(year, month, day))
} else {
Option.None
}
}fn pub inline static new(year: Int, month: Int, day: Int) -> Option[Date]Returns a new Date, provided the given components are valid.
The arguments must be values in the following ranges:
year: -2147483648 up to and including 2147483647month: 1 up to and including 12day: 1 up to and including 31
If any of the arguments are out of bounds, an Option.None is returned.
Examples
import std.time (Date)
let date = Date
.new(year: 2024, month: 12, day: 17)
.or_panic_with('the Date is invalid')
date.year # => 2024
Instance methods
!=
Show source codeHide source code
fn pub !=(other: ref Self) -> Bool {
!(self == other)
}fn pub !=(other: ref Self) -> BoolReturns true if self and the given object are not equal to each other.
==
Show source codeHide source code
fn pub ==(other: Date) -> Bool {
@bits == other.bits
}fn pub ==(other: Date) -> BoolReturns true if self and the given object are equal to each other.
This operator is used to perform structural equality. This means two objects residing in different memory locations may be considered equal, provided their structure is equal. For example, two different arrays may be considered to have structural equality if they contain the exact same values.
day
Show source codeHide source code
fn pub inline day -> Int {
@bits & 0x1F
}fn pub inline day -> IntReturns the day in the range 1 to 31.
day_of_week
Show source codeHide source code
fn pub day_of_week -> Int {
# January 1st, 1970 (our anchor date) was on a Thursday. We add 3 so that
# Monday (3 days before Thursday) becomes the anchor date.
#
# We later on add 1 since the % operator will return 0 for Monday (since its
# the first value in the range), but week days range from 1 to 7; not 0 to
# 6.
#
# The following table should help illustrate this:
#
# | Date | Day of week | days_since_unix_epoch
# |:-----------|:------------|:----------------------
# | 1969-12-29 | Monday | -3
# | 1969-12-30 | Tuesday | -2
# | 1969-12-31 | Wednesday | -1
# | 1970-01-01 | Thursday | 0
# | 1970-01-02 | Friday | 1
# | 1970-01-03 | Saturday | 2
# | 1970-01-04 | Sunday | 3
#
# For these dates, the calculations would be as follows:
#
# | Date | Calculation | Simplified | Return value
# |:-----------|:-------------------|:------------|:------------
# | 1969-12-29 | ((-3 + 3) % 7) + 1 | (0 % 7) + 1 | 1
# | 1969-12-30 | ((-2 + 3) % 7) + 1 | (1 % 7) + 1 | 2
# | 1969-12-31 | ((-1 + 3) % 7) + 1 | (2 % 7) + 1 | 3
# | 1970-01-01 | ((0 + 3) % 7) + 1 | (3 % 7) + 1 | 4
# | 1970-01-02 | ((1 + 3) % 7) + 1 | (4 % 7) + 1 | 5
# | 1970-01-03 | ((2 + 3) % 7) + 1 | (5 % 7) + 1 | 6
# | 1970-01-04 | ((3 + 3) % 7) + 1 | (6 % 7) + 1 | 7
days_since_unix_epoch + 3 % DAYS_PER_WEEK + 1
}fn pub day_of_week -> IntReturns the day of the week from 1 to 7.
Per ISO 8601 the first day of the week starts on Monday, not Sunday.
day_of_year
Show source codeHide source code
fn pub day_of_year -> Int {
let days = if leap_year? { LEAP_DAYS } else { NORMAL_DAYS }
days.get(month - 1).or_panic + day
}fn pub day_of_year -> IntReturns the day of the year from 1 to 366 for leap years, and from 1 to 365 for regular years.
days_since_unix_epoch
Show source codeHide source code
fn pub days_since_unix_epoch -> Int {
let year = if month <= 2 { year - 1 } else { year }
let month = month
let era = if year >= 0 { year } else { year - 399 } / 400
let yoe = year - (era * 400)
let doy = (((153 * if month > 2 { month - 3 } else { month + 9 }) + 2) / 5)
+ day
- 1
let doe = (yoe * 365) + (yoe / 4) - (yoe / 100) + doy
(era * 146_097) + doe - 719_468
}fn pub days_since_unix_epoch -> IntReturns the number of days between self and the Unix epoch.
The returned Int is negative if self is before the Unix epoch, and
positive for a value that is on or after the Unix epoch.
fmt
Show source codeHide source code
fn pub fmt(formatter: mut Formatter) {
let year = year.to_string.pad_start(with: '0', chars: 4)
let month = month.to_string.pad_start(with: '0', chars: 2)
let day = day.to_string.pad_start(with: '0', chars: 2)
formatter.write('${year}-${month}-${day}')
}fn pub fmt(formatter: mut Formatter)Formats self in a human-readable format for debugging purposes.
leap_year?
Show source codeHide source code
fn pub leap_year? -> Bool {
let year = year
(year % 4) == 0 and ((year % 100) > 0 or (year % 400) == 0)
}fn pub leap_year? -> BoolReturns true if the current year is a leap year.
month
Show source codeHide source code
fn pub inline month -> Int {
@bits >> 5 & 0xF
}fn pub inline month -> IntReturns the month in the range 1 to 12.
year
Show source codeHide source code
fn pub inline year -> Int {
@bits >> 9
}fn pub inline year -> IntReturns the year.
Implemented traits
Equal
impl Equal for DateFormat
impl Format for Date