Skip to content

Commit f9d553c

Browse files
author
Christopher Doris
committed
tweak datetime conversion
1 parent 078a0f1 commit f9d553c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/concrete/datetime.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
const pydatetime111 = pynew()
1+
# We used to use 1/1/1 but pandas.Timestamp is a subclass of datetime and does not include
2+
# this date, so we use 1970 instead.
3+
const _base_datetime = DateTime(1970, 1, 1)
4+
const _base_pydatetime = pynew()
25

36
function init_datetime()
4-
pycopy!(pydatetime111, pydatetimetype(1, 1, 1))
7+
pycopy!(_base_pydatetime, pydatetimetype(1970, 1, 1))
58
end
69

710
pydate(year, month, day) = pydatetype(year, month, day)
@@ -20,10 +23,10 @@ export pytime
2023

2124
pydatetime(year, month, day, _hour=0, _minute=0, _second=0, _microsecond=0, _tzinfo=nothing; hour=_hour, minute=_minute, second=_second, microsecond=_microsecond, tzinfo=_tzinfo, fold=0) = pydatetimetype(year, month, day, hour, minute, second, microsecond, tzinfo, fold=fold)
2225
function pydatetime(x::DateTime)
23-
# compute time since 1/1/1
26+
# compute time since _base_datetime
2427
# this accounts for fold
25-
d = pytimedeltatype(milliseconds = (x - DateTime(1, 1, 1)).value)
26-
ans = pydatetime111 + d
28+
d = pytimedeltatype(milliseconds = (x - _base_datetime).value)
29+
ans = _base_pydatetime + d
2730
pydel!(d)
2831
return ans
2932
end
@@ -80,13 +83,13 @@ end
8083

8184
function pyconvert_rule_datetime(::Type{DateTime}, x::Py)
8285
pydatetime_isaware(x) && return pyconvert_unconverted()
83-
# compute the time since 1/1/1
86+
# compute the time since _base_datetime
8487
# this accounts for fold
85-
d = x - pydatetime111
88+
d = x - _base_pydatetime
8689
days = pyconvert_and_del(Int, d.days)
8790
seconds = pyconvert_and_del(Int, d.seconds)
8891
microseconds = pyconvert_and_del(Int, d.microseconds)
8992
pydel!(d)
9093
iszero(mod(microseconds, 1000)) || return pyconvert_unconverted()
91-
return pyconvert_return(DateTime(1,1,1) + Millisecond(div(microseconds, 1000) + 1000 * (seconds + 60 * 60 * 24 * days)))
94+
return pyconvert_return(_base_datetime + Millisecond(div(microseconds, 1000) + 1000 * (seconds + 60 * 60 * 24 * days)))
9295
end

0 commit comments

Comments
 (0)