1 from cpython.ref cimport PyObject
3 cdef extern from "Python.h":
4 ctypedef struct PyTypeObject:
7 cdef extern from "datetime.h":
9 ctypedef extern class datetime.date[object PyDateTime_Date]:
12 ctypedef extern class datetime.time[object PyDateTime_Time]:
15 ctypedef extern class datetime.datetime[object PyDateTime_DateTime]:
18 ctypedef extern class datetime.timedelta[object PyDateTime_Delta]:
21 ctypedef extern class datetime.tzinfo[object PyDateTime_TZInfo]:
24 ctypedef struct PyDateTime_Date:
27 ctypedef struct PyDateTime_Time:
31 ctypedef struct PyDateTime_DateTime:
35 ctypedef struct PyDateTime_Delta:
40 # Define structure for C API.
41 ctypedef struct PyDateTime_CAPI:
43 PyTypeObject *DateType
44 PyTypeObject *DateTimeType
45 PyTypeObject *TimeType
46 PyTypeObject *DeltaType
47 PyTypeObject *TZInfoType
50 object (*Date_FromDate)(int, int, int, PyTypeObject*)
51 object (*DateTime_FromDateAndTime)(int, int, int, int, int, int, int, object, PyTypeObject*)
52 object (*Time_FromTime)(int, int, int, int, object, PyTypeObject*)
53 object (*Delta_FromDelta)(int, int, int, int, PyTypeObject*)
55 # constructors for the DB API
56 object (*DateTime_FromTimestamp)(object, object, object)
57 object (*Date_FromTimestamp)(object, object)
59 # Check type of the object.
60 bint PyDate_Check(object op)
61 bint PyDate_CheckExact(object op)
63 bint PyDateTime_Check(object op)
64 bint PyDateTime_CheckExact(object op)
66 bint PyTime_Check(object op)
67 bint PyTime_CheckExact(object op)
69 bint PyDelta_Check(object op)
70 bint PyDelta_CheckExact(object op)
72 bint PyTZInfo_Check(object op)
73 bint PyTZInfo_CheckExact(object op)
75 # Getters for date and datetime (C macros).
76 int PyDateTime_GET_YEAR(object o)
77 int PyDateTime_GET_MONTH(object o)
78 int PyDateTime_GET_DAY(object o)
80 # Getters for datetime (C macros).
81 int PyDateTime_DATE_GET_HOUR(object o)
82 int PyDateTime_DATE_GET_MINUTE(object o)
83 int PyDateTime_DATE_GET_SECOND(object o)
84 int PyDateTime_DATE_GET_MICROSECOND(object o)
86 # Getters for time (C macros).
87 int PyDateTime_TIME_GET_HOUR(object o)
88 int PyDateTime_TIME_GET_MINUTE(object o)
89 int PyDateTime_TIME_GET_SECOND(object o)
90 int PyDateTime_TIME_GET_MICROSECOND(object o)
92 # Getters for timedelta (C macros).
93 #int PyDateTime_DELTA_GET_DAYS(object o)
94 #int PyDateTime_DELTA_GET_SECONDS(object o)
95 #int PyDateTime_DELTA_GET_MICROSECONDS(object o)
97 # PyDateTime CAPI object.
98 PyDateTime_CAPI *PyDateTimeAPI
100 void PyDateTime_IMPORT()
102 # Datetime C API initialization function.
103 # You have to call it before any usage of DateTime CAPI functions.
104 cdef inline void import_datetime():
107 # Create date object using DateTime CAPI factory function.
108 # Note, there are no range checks for any of the arguments.
109 cdef inline object date_new(int year, int month, int day):
110 return PyDateTimeAPI.Date_FromDate(year, month, day, PyDateTimeAPI.DateType)
112 # Create time object using DateTime CAPI factory function
113 # Note, there are no range checks for any of the arguments.
114 cdef inline object time_new(int hour, int minute, int second, int microsecond, object tz):
115 return PyDateTimeAPI.Time_FromTime(hour, minute, second, microsecond, tz, PyDateTimeAPI.TimeType)
117 # Create datetime object using DateTime CAPI factory function.
118 # Note, there are no range checks for any of the arguments.
119 cdef inline object datetime_new(int year, int month, int day, int hour, int minute, int second, int microsecond, object tz):
120 return PyDateTimeAPI.DateTime_FromDateAndTime(year, month, day, hour, minute, second, microsecond, tz, PyDateTimeAPI.DateTimeType)
122 # Create timedelta object using DateTime CAPI factory function.
123 # Note, there are no range checks for any of the arguments.
124 cdef inline object timedelta_new(int days, int seconds, int useconds):
125 return PyDateTimeAPI.Delta_FromDelta(days, seconds, useconds, 1, PyDateTimeAPI.DeltaType)
127 # More recognizable getters for date/time/datetime/timedelta.
128 # There are no setters because datetime.h hasn't them.
129 # This is because of immutable nature of these objects by design.
130 # If you would change time/date/datetime/timedelta object you need to recreate.
133 cdef inline object time_tzinfo(object o):
134 if (<PyDateTime_Time*>o).hastzinfo:
135 return <object>(<PyDateTime_Time*>o).tzinfo
139 # Get tzinfo of datetime
140 cdef inline object datetime_tzinfo(object o):
141 if (<PyDateTime_DateTime*>o).hastzinfo:
142 return <object>(<PyDateTime_DateTime*>o).tzinfo
147 cdef inline int date_year(object o):
148 return PyDateTime_GET_YEAR(o)
151 cdef inline int date_month(object o):
152 return PyDateTime_GET_MONTH(o)
155 cdef inline int date_day(object o):
156 return PyDateTime_GET_DAY(o)
158 # Get year of datetime
159 cdef inline int datetime_year(object o):
160 return PyDateTime_GET_YEAR(o)
162 # Get month of datetime
163 cdef inline int datetime_month(object o):
164 return PyDateTime_GET_MONTH(o)
166 # Get day of datetime
167 cdef inline int datetime_day(object o):
168 return PyDateTime_GET_DAY(o)
171 cdef inline int time_hour(object o):
172 return PyDateTime_TIME_GET_HOUR(o)
175 cdef inline int time_minute(object o):
176 return PyDateTime_TIME_GET_MINUTE(o)
179 cdef inline int time_second(object o):
180 return PyDateTime_TIME_GET_SECOND(o)
182 # Get microsecond of time
183 cdef inline int time_microsecond(object o):
184 return PyDateTime_TIME_GET_MICROSECOND(o)
186 # Get hour of datetime
187 cdef inline int datetime_hour(object o):
188 return PyDateTime_DATE_GET_HOUR(o)
190 # Get minute of datetime
191 cdef inline int datetime_minute(object o):
192 return PyDateTime_DATE_GET_MINUTE(o)
194 # Get second of datetime
195 cdef inline int datetime_second(object o):
196 return PyDateTime_DATE_GET_SECOND(o)
198 # Get microsecond of datetime
199 cdef inline int datetime_microsecond(object o):
200 return PyDateTime_DATE_GET_MICROSECOND(o)
202 # Get days of timedelta
203 cdef inline int timedelta_days(object o):
204 return (<PyDateTime_Delta*>o).days
206 # Get seconds of timedelta
207 cdef inline int timedelta_seconds(object o):
208 return (<PyDateTime_Delta*>o).seconds
210 # Get microseconds of timedelta
211 cdef inline int timedelta_microseconds(object o):
212 return (<PyDateTime_Delta*>o).microseconds