datetime.pxd 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. from cpython.object cimport PyObject
  2. cdef extern from "Python.h":
  3. ctypedef struct PyTypeObject:
  4. pass
  5. cdef extern from "datetime.h":
  6. ctypedef extern class datetime.date[object PyDateTime_Date]:
  7. pass
  8. ctypedef extern class datetime.time[object PyDateTime_Time]:
  9. pass
  10. ctypedef extern class datetime.datetime[object PyDateTime_DateTime]:
  11. pass
  12. ctypedef extern class datetime.timedelta[object PyDateTime_Delta]:
  13. pass
  14. ctypedef extern class datetime.tzinfo[object PyDateTime_TZInfo]:
  15. pass
  16. ctypedef struct PyDateTime_Date:
  17. pass
  18. ctypedef struct PyDateTime_Time:
  19. char hastzinfo
  20. PyObject *tzinfo
  21. ctypedef struct PyDateTime_DateTime:
  22. char hastzinfo
  23. PyObject *tzinfo
  24. ctypedef struct PyDateTime_Delta:
  25. int days
  26. int seconds
  27. int microseconds
  28. # Define structure for C API.
  29. ctypedef struct PyDateTime_CAPI:
  30. # type objects
  31. PyTypeObject *DateType
  32. PyTypeObject *DateTimeType
  33. PyTypeObject *TimeType
  34. PyTypeObject *DeltaType
  35. PyTypeObject *TZInfoType
  36. # constructors
  37. object (*Date_FromDate)(int, int, int, PyTypeObject*)
  38. object (*DateTime_FromDateAndTime)(int, int, int, int, int, int, int, object, PyTypeObject*)
  39. object (*Time_FromTime)(int, int, int, int, object, PyTypeObject*)
  40. object (*Delta_FromDelta)(int, int, int, int, PyTypeObject*)
  41. # constructors for the DB API
  42. object (*DateTime_FromTimestamp)(object, object, object)
  43. object (*Date_FromTimestamp)(object, object)
  44. # Check type of the object.
  45. bint PyDate_Check(object op)
  46. bint PyDate_CheckExact(object op)
  47. bint PyDateTime_Check(object op)
  48. bint PyDateTime_CheckExact(object op)
  49. bint PyTime_Check(object op)
  50. bint PyTime_CheckExact(object op)
  51. bint PyDelta_Check(object op)
  52. bint PyDelta_CheckExact(object op)
  53. bint PyTZInfo_Check(object op)
  54. bint PyTZInfo_CheckExact(object op)
  55. # Getters for date and datetime (C macros).
  56. int PyDateTime_GET_YEAR(object o)
  57. int PyDateTime_GET_MONTH(object o)
  58. int PyDateTime_GET_DAY(object o)
  59. # Getters for datetime (C macros).
  60. int PyDateTime_DATE_GET_HOUR(object o)
  61. int PyDateTime_DATE_GET_MINUTE(object o)
  62. int PyDateTime_DATE_GET_SECOND(object o)
  63. int PyDateTime_DATE_GET_MICROSECOND(object o)
  64. # Getters for time (C macros).
  65. int PyDateTime_TIME_GET_HOUR(object o)
  66. int PyDateTime_TIME_GET_MINUTE(object o)
  67. int PyDateTime_TIME_GET_SECOND(object o)
  68. int PyDateTime_TIME_GET_MICROSECOND(object o)
  69. # Getters for timedelta (C macros).
  70. int PyDateTime_DELTA_GET_DAYS(object o)
  71. int PyDateTime_DELTA_GET_SECONDS(object o)
  72. int PyDateTime_DELTA_GET_MICROSECONDS(object o)
  73. # PyDateTime CAPI object.
  74. PyDateTime_CAPI *PyDateTimeAPI
  75. void PyDateTime_IMPORT()
  76. # Datetime C API initialization function.
  77. # You have to call it before any usage of DateTime CAPI functions.
  78. cdef inline void import_datetime():
  79. PyDateTime_IMPORT
  80. # Create date object using DateTime CAPI factory function.
  81. # Note, there are no range checks for any of the arguments.
  82. cdef inline object date_new(int year, int month, int day):
  83. return PyDateTimeAPI.Date_FromDate(year, month, day, PyDateTimeAPI.DateType)
  84. # Create time object using DateTime CAPI factory function
  85. # Note, there are no range checks for any of the arguments.
  86. cdef inline object time_new(int hour, int minute, int second, int microsecond, object tz):
  87. return PyDateTimeAPI.Time_FromTime(hour, minute, second, microsecond, tz, PyDateTimeAPI.TimeType)
  88. # Create datetime object using DateTime CAPI factory function.
  89. # Note, there are no range checks for any of the arguments.
  90. cdef inline object datetime_new(int year, int month, int day, int hour, int minute, int second, int microsecond, object tz):
  91. return PyDateTimeAPI.DateTime_FromDateAndTime(year, month, day, hour, minute, second, microsecond, tz, PyDateTimeAPI.DateTimeType)
  92. # Create timedelta object using DateTime CAPI factory function.
  93. # Note, there are no range checks for any of the arguments.
  94. cdef inline object timedelta_new(int days, int seconds, int useconds):
  95. return PyDateTimeAPI.Delta_FromDelta(days, seconds, useconds, 1, PyDateTimeAPI.DeltaType)
  96. # More recognizable getters for date/time/datetime/timedelta.
  97. # There are no setters because datetime.h hasn't them.
  98. # This is because of immutable nature of these objects by design.
  99. # If you would change time/date/datetime/timedelta object you need to recreate.
  100. # Get tzinfo of time
  101. cdef inline object time_tzinfo(object o):
  102. if (<PyDateTime_Time*>o).hastzinfo:
  103. return <object>(<PyDateTime_Time*>o).tzinfo
  104. else:
  105. return None
  106. # Get tzinfo of datetime
  107. cdef inline object datetime_tzinfo(object o):
  108. if (<PyDateTime_DateTime*>o).hastzinfo:
  109. return <object>(<PyDateTime_DateTime*>o).tzinfo
  110. else:
  111. return None
  112. # Get year of date
  113. cdef inline int date_year(object o):
  114. return PyDateTime_GET_YEAR(o)
  115. # Get month of date
  116. cdef inline int date_month(object o):
  117. return PyDateTime_GET_MONTH(o)
  118. # Get day of date
  119. cdef inline int date_day(object o):
  120. return PyDateTime_GET_DAY(o)
  121. # Get year of datetime
  122. cdef inline int datetime_year(object o):
  123. return PyDateTime_GET_YEAR(o)
  124. # Get month of datetime
  125. cdef inline int datetime_month(object o):
  126. return PyDateTime_GET_MONTH(o)
  127. # Get day of datetime
  128. cdef inline int datetime_day(object o):
  129. return PyDateTime_GET_DAY(o)
  130. # Get hour of time
  131. cdef inline int time_hour(object o):
  132. return PyDateTime_TIME_GET_HOUR(o)
  133. # Get minute of time
  134. cdef inline int time_minute(object o):
  135. return PyDateTime_TIME_GET_MINUTE(o)
  136. # Get second of time
  137. cdef inline int time_second(object o):
  138. return PyDateTime_TIME_GET_SECOND(o)
  139. # Get microsecond of time
  140. cdef inline int time_microsecond(object o):
  141. return PyDateTime_TIME_GET_MICROSECOND(o)
  142. # Get hour of datetime
  143. cdef inline int datetime_hour(object o):
  144. return PyDateTime_DATE_GET_HOUR(o)
  145. # Get minute of datetime
  146. cdef inline int datetime_minute(object o):
  147. return PyDateTime_DATE_GET_MINUTE(o)
  148. # Get second of datetime
  149. cdef inline int datetime_second(object o):
  150. return PyDateTime_DATE_GET_SECOND(o)
  151. # Get microsecond of datetime
  152. cdef inline int datetime_microsecond(object o):
  153. return PyDateTime_DATE_GET_MICROSECOND(o)
  154. # Get days of timedelta
  155. cdef inline int timedelta_days(object o):
  156. return (<PyDateTime_Delta*>o).days
  157. # Get seconds of timedelta
  158. cdef inline int timedelta_seconds(object o):
  159. return (<PyDateTime_Delta*>o).seconds
  160. # Get microseconds of timedelta
  161. cdef inline int timedelta_microseconds(object o):
  162. return (<PyDateTime_Delta*>o).microseconds