Skip to content

Commit 4ad1812

Browse files
author
Christopher Doris
committed
fix(numpydates): correct year/month extraction in DateTime64(Date)
Use Dates.year(d) and Dates.month(d) in the Date-based constructor instead of value(Dates.Year(d)) and value(Dates.Month(d)). The previous approach could error or yield wrong values when computing YEAR/MONTH offsets relative to 1970, affecting YEARS and MONTHS units. Also includes minor whitespace cleanup.
1 parent 2a21d64 commit 4ad1812

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/NumpyDates/DateTime64.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function unitpair(d::DateTime64)
2424
d.unit
2525
end
2626

27+
2728
# constructors
2829

2930
function DateTime64(d::AbstractDateTime64, unit::UnitArg = defaultunit(d))
@@ -88,12 +89,13 @@ function DateTime64(d::Dates.DateTime, unit::UnitArg = defaultunit(d))
8889
DateTime64(v, unit)
8990
end
9091

92+
9193
function DateTime64(d::Dates.Date, unit::UnitArg = defaultunit(d))
9294
u, m = unit = unitpair(unit)
9395
if u == YEARS
94-
v = value(Dates.Year(d)) - 1970
96+
v = Dates.year(d) - 1970
9597
elseif u == MONTHS
96-
v = 12 * (value(Dates.Year(d)) - 1970) + (value(Dates.Month(d)) - 1)
98+
v = 12 * (Dates.year(d) - 1970) + (Dates.month(d) - 1)
9799
else
98100
v = value((d - Dates.Date(1970))::Dates.Day)
99101
if u == WEEKS
@@ -126,6 +128,7 @@ function DateTime64(d::Dates.Date, unit::UnitArg = defaultunit(d))
126128
DateTime64(v, unit)
127129
end
128130

131+
129132
# show
130133

131134
function Base.show(io::IO, d::DateTime64)

0 commit comments

Comments
 (0)