Reputation: 1786
I learned that Dates.value(Date)
returns an integer representation of that date.
using Dates
dt = Date(2020, 01, 01)
>>> println(Dates.value(dt))
737425
How does it go from 2020-01-01 to 737425?
Upvotes: 2
Views: 274
Reputation: 69939
The value
is a field in this structure:
julia> dump(dt)
Date
instant: Dates.UTInstant{Day}
periods: Day
value: Int64 737425
The Dates.value
function extracts this field.
For the Dates.Date
object Date(1, 1, 1)
is associated with value
equal to 1, and then consecutive days have value
increasing by 1.
However, Dates.value
is defined for other objects. E.g.:
julia> Dates.value(Time(1))
3600000000000
julia> Dates.value(Second(10))
10
julia> Dates.value(Minute(20))
20
The fact that Dates.value
does not perform this computation is important as it ensures that the operation is fast.
If you want to see how this is calculated for Date
, check the source code of Dates.totaldays
function which calculates this using Rata Die system:
# Convert y,m,d to # of Rata Die days
# Works by shifting the beginning of the year to March 1,
# so a leap day is the very last day of the year
const SHIFTEDMONTHDAYS = (306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275)
function totaldays(y, m, d)
# If we're in Jan/Feb, shift the given year back one
z = m < 3 ? y - 1 : y
mdays = SHIFTEDMONTHDAYS[m]
# days + month_days + year_days
return d + mdays + 365z + fld(z, 4) - fld(z, 100) + fld(z, 400) - 306
end
Upvotes: 4