/
pydates.jl
169 lines (148 loc) · 5.86 KB
/
pydates.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Conversion functions for date and time objects in the Python datetime
# module and the Julia Dates module.
# Unfortunately, the Python C API (in Python/Include/datetime.h) is somewhat
# painful to call from Julia because it consists mainly of macros that
# translate to lookups in datastructures that are loaded at runtime by
# a PyDateTime_IMPORT macro. So, we have to mirror those declarations here.
# Fortunately, they don't seem to have changed much since Python 2.7, with
# the biggest difference being the use of a 64-bit hash type.
immutable PyDateTime_CAPI
# type objects:
DateType::PyPtr
DateTimeType::PyPtr
TimeType::PyPtr
DeltaType::PyPtr
TZInfoType::PyPtr
# function pointers:
Date_FromDate::Ptr{Void}
DateTime_FromDateAndTime::Ptr{Void}
Time_FromTime::Ptr{Void}
Delta_FromDelta::Ptr{Void}
DateTime_FromTimestamp::Ptr{Void}
Date_FromTimestamp::Ptr{Void}
end
immutable PyDateTime_Delta{H} # H = Clong in Python 2, Py_hash_t in Python 3
# PyObject_HEAD (for non-Py_TRACE_REFS build):
ob_refcnt::Int
ob_type::PyPtr
hashcode::H
days::Cint
seconds::Cint
microseconds::Cint
end
# the DateTime, Date, and Time objects are a struct with fields:
# ob_refcnt::Int
# ob_type::PyPtr
# hashcode::Py_hash_t
# hastzinfo::UInt8
# unsigned char data[LEN]
# tzinfo::PyPtr
# where LEN = 4 for Date, 6 for Time, and 10 for DateTime. We
# will access this via raw Ptr{UInt8} loads, with the following offset
# for data:
const PyDate_HEAD = sizeof(Int)+sizeof(PyPtr)+sizeof(Py_hash_t)+1
# called from __init__
function init_datetime()
# emulate PyDateTime_IMPORT:
global const PyDateTimeAPI =
unsafe_load(@pycheckn ccall((@pysym :PyCapsule_Import),
Ptr{PyDateTime_CAPI}, (Ptr{UInt8}, Cint),
"datetime.datetime_CAPI", 0))
end
PyObject(d::Dates.Date) =
PyObject(@pycheckn ccall(PyDateTimeAPI.Date_FromDate, PyPtr,
(Cint, Cint, Cint, PyPtr),
Dates.year(d), Dates.month(d), Dates.day(d),
PyDateTimeAPI.DateType))
PyObject(d::Dates.DateTime) =
PyObject(@pycheckn ccall(PyDateTimeAPI.DateTime_FromDateAndTime, PyPtr,
(Cint, Cint, Cint, Cint, Cint, Cint, Cint,
PyPtr, PyPtr),
Dates.year(d), Dates.month(d), Dates.day(d),
Dates.hour(d), Dates.minute(d), Dates.second(d),
Dates.millisecond(d) * 1000,
pynothing, PyDateTimeAPI.DateTimeType))
PyDelta_FromDSU(days, seconds, useconds) =
PyObject(@pycheckn ccall(PyDateTimeAPI.Delta_FromDelta, PyPtr,
(Cint, Cint, Cint, Cint, PyPtr),
days, seconds, useconds,
1, PyDateTimeAPI.DeltaType))
PyObject(p::Dates.Day) = PyDelta_FromDSU(Int(p), 0, 0)
function PyObject(p::Dates.Second)
# normalize to make Cint overflow less likely
s = Int(p)
d = div(s, 86400)
s -= d * 86400
PyDelta_FromDSU(d, s, 0)
end
function PyObject(p::Dates.Millisecond)
# normalize to make Cint overflow less likely
ms = Int(p)
s = div(ms, 1000)
ms -= s * 1000
d = div(s, 86400)
s -= d * 86400
PyDelta_FromDSU(d, s, ms * 1000)
end
for T in (:Date, :DateTime, :Delta)
f = Symbol(string("Py", T, "_Check"))
t = Expr(:., :PyDateTimeAPI, QuoteNode(Symbol(string(T, "Type"))))
@eval $f(o::PyObject) = pyisinstance(o, $t)
end
function pydate_query(o::PyObject)
if PyDate_Check(o)
return PyDateTime_Check(o) ? Dates.DateTime : Dates.Date
elseif PyDelta_Check(o)
return Dates.Millisecond
else
return Union{}
end
end
function convert(::Type{Dates.DateTime}, o::PyObject)
if PyDate_Check(o)
dt = convert(Ptr{UInt8}, o.o) + PyDate_HEAD
if PyDateTime_Check(o)
Dates.DateTime((UInt(unsafe_load(dt,1))<<8)|unsafe_load(dt,2), # Y
unsafe_load(dt,3), unsafe_load(dt,4), # month, day
unsafe_load(dt,5), unsafe_load(dt,6), # hour, minute
unsafe_load(dt,7), # second
div((UInt(unsafe_load(dt,8)) << 16) |
(UInt(unsafe_load(dt,9)) << 8) |
unsafe_load(dt,10), 1000)) # μs ÷ 1000
else
Dates.DateTime((UInt(unsafe_load(dt,1))<<8)|unsafe_load(dt,2), # Y
unsafe_load(dt,3), unsafe_load(dt,4)) # month, day
end
else
throw(ArgumentError("unknown DateTime type $o"))
end
end
function convert(::Type{Dates.Date}, o::PyObject)
if PyDate_Check(o)
dt = convert(Ptr{UInt8}, o.o) + PyDate_HEAD
Dates.Date((UInt(unsafe_load(dt,1)) << 8) | unsafe_load(dt,2), # Y
unsafe_load(dt,3), unsafe_load(dt,4)) # month, day
else
throw(ArgumentError("unknown Date type $o"))
end
end
function delta_dsμ(o::PyObject)
PyDelta_Check(o) || throw(ArgumentError("$o is not a timedelta instance"))
p = unsafe_load(convert(Ptr{PyDateTime_Delta{Py_hash_t}}, o.o))
return (p.days, p.seconds, p.microseconds)
end
# Should we throw an InexactError when converting a period
# that is not an exact multiple of the resulting unit? For now,
# follow the lead of Dates and truncate; see Julia issue #9169.
function convert(::Type{Dates.Millisecond}, o::PyObject)
(d,s,μs) = delta_dsμ(o)
return Dates.Millisecond((86400d + s) * 1000 + div(μs, 1000))
end
function convert(::Type{Dates.Second}, o::PyObject)
(d,s,μs) = delta_dsμ(o)
return Dates.Second(86400d + s + div(μs, 1000000))
end
function convert(::Type{Dates.Day}, o::PyObject)
(d,s,μs) = delta_dsμ(o)
return Dates.Day(d + div(s + div(μs, 1000000), 86400))
end