| 1 |
import pytest |
| 2 |
|
| 3 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 4 |
from pypy.module.cpyext.test.test_api import BaseApiTest |
| 5 |
from pypy.module.cpyext.cdatetime import * |
| 6 |
from pypy.module.cpyext.cdatetime import ( |
| 7 |
_PyDateTime_Import, _PyDateTime_FromDateAndTime, _PyDate_FromDate, |
| 8 |
_PyTime_FromTime, _PyDelta_FromDelta, _PyTime_FromTimeAndFold, |
| 9 |
_PyDateTime_FromDateAndTimeAndFold) |
| 10 |
import datetime |
| 11 |
|
| 12 |
class TestDatetime(BaseApiTest): |
| 13 |
def test_date(self, space): |
| 14 |
date_api = _PyDateTime_Import(space) |
| 15 |
w_date = _PyDate_FromDate(space, 2010, 6, 3, date_api.c_DateType) |
| 16 |
assert space.unwrap(space.str(w_date)) == '2010-06-03' |
| 17 |
|
| 18 |
assert PyDate_Check(space, w_date) |
| 19 |
assert PyDate_CheckExact(space, w_date) |
| 20 |
|
| 21 |
assert PyDateTime_GET_YEAR(space, w_date) == 2010 |
| 22 |
assert PyDateTime_GET_MONTH(space, w_date) == 6 |
| 23 |
assert PyDateTime_GET_DAY(space, w_date) == 3 |
| 24 |
|
| 25 |
def test_time(self, space): |
| 26 |
date_api = _PyDateTime_Import(space) |
| 27 |
w_time = _PyTime_FromTime( |
| 28 |
space, 23, 15, 40, 123456, space.w_None, date_api.c_TimeType) |
| 29 |
assert space.unwrap(space.str(w_time)) == '23:15:40.123456' |
| 30 |
|
| 31 |
assert PyTime_Check(space, w_time) |
| 32 |
assert PyTime_CheckExact(space, w_time) |
| 33 |
|
| 34 |
assert PyDateTime_TIME_GET_HOUR(space, w_time) == 23 |
| 35 |
assert PyDateTime_TIME_GET_MINUTE(space, w_time) == 15 |
| 36 |
assert PyDateTime_TIME_GET_SECOND(space, w_time) == 40 |
| 37 |
assert PyDateTime_TIME_GET_MICROSECOND(space, w_time) == 123456 |
| 38 |
|
| 39 |
w_time = _PyTime_FromTimeAndFold( |
| 40 |
space, 23, 15, 40, 123456, space.w_None, 0, date_api.c_TimeType) |
| 41 |
assert space.unwrap(space.str(w_time)) == '23:15:40.123456' |
| 42 |
|
| 43 |
assert PyTime_Check(space, w_time) |
| 44 |
assert PyTime_CheckExact(space, w_time) |
| 45 |
|
| 46 |
assert PyDateTime_TIME_GET_HOUR(space, w_time) == 23 |
| 47 |
assert PyDateTime_TIME_GET_MINUTE(space, w_time) == 15 |
| 48 |
assert PyDateTime_TIME_GET_SECOND(space, w_time) == 40 |
| 49 |
assert PyDateTime_TIME_GET_MICROSECOND(space, w_time) == 123456 |
| 50 |
|
| 51 |
|
| 52 |
def test_datetime(self, space): |
| 53 |
date_api = _PyDateTime_Import(space) |
| 54 |
w_date = _PyDateTime_FromDateAndTime( |
| 55 |
space, 2010, 6, 3, 23, 15, 40, 123456, space.w_None, |
| 56 |
date_api.c_DateTimeType) |
| 57 |
assert space.unwrap(space.str(w_date)) == '2010-06-03 23:15:40.123456' |
| 58 |
|
| 59 |
assert PyDateTime_Check(space, w_date) |
| 60 |
assert PyDateTime_CheckExact(space, w_date) |
| 61 |
assert PyDate_Check(space, w_date) |
| 62 |
assert not PyDate_CheckExact(space, w_date) |
| 63 |
|
| 64 |
assert PyDateTime_GET_YEAR(space, w_date) == 2010 |
| 65 |
assert PyDateTime_GET_MONTH(space, w_date) == 6 |
| 66 |
assert PyDateTime_GET_DAY(space, w_date) == 3 |
| 67 |
assert PyDateTime_DATE_GET_HOUR(space, w_date) == 23 |
| 68 |
assert PyDateTime_DATE_GET_MINUTE(space, w_date) == 15 |
| 69 |
assert PyDateTime_DATE_GET_SECOND(space, w_date) == 40 |
| 70 |
assert PyDateTime_DATE_GET_MICROSECOND(space, w_date) == 123456 |
| 71 |
|
| 72 |
w_date = _PyDateTime_FromDateAndTimeAndFold( |
| 73 |
space, 2010, 6, 3, 23, 15, 40, 123456, space.w_None, |
| 74 |
0, date_api.c_DateTimeType) |
| 75 |
assert space.unwrap(space.str(w_date)) == '2010-06-03 23:15:40.123456' |
| 76 |
|
| 77 |
assert PyDateTime_Check(space, w_date) |
| 78 |
assert PyDateTime_CheckExact(space, w_date) |
| 79 |
assert PyDate_Check(space, w_date) |
| 80 |
assert not PyDate_CheckExact(space, w_date) |
| 81 |
|
| 82 |
assert PyDateTime_GET_YEAR(space, w_date) == 2010 |
| 83 |
assert PyDateTime_GET_MONTH(space, w_date) == 6 |
| 84 |
assert PyDateTime_GET_DAY(space, w_date) == 3 |
| 85 |
assert PyDateTime_DATE_GET_HOUR(space, w_date) == 23 |
| 86 |
assert PyDateTime_DATE_GET_MINUTE(space, w_date) == 15 |
| 87 |
assert PyDateTime_DATE_GET_SECOND(space, w_date) == 40 |
| 88 |
assert PyDateTime_DATE_GET_MICROSECOND(space, w_date) == 123456 |
| 89 |
|
| 90 |
def test_delta(self, space): |
| 91 |
date_api = _PyDateTime_Import(space) |
| 92 |
w_delta = space.appexec( |
| 93 |
[space.wrap(3), space.wrap(15)], """(days, seconds): |
| 94 |
from datetime import timedelta |
| 95 |
return timedelta(days, seconds) |
| 96 |
""") |
| 97 |
assert PyDelta_Check(space, w_delta) |
| 98 |
assert PyDelta_CheckExact(space, w_delta) |
| 99 |
|
| 100 |
w_delta = _PyDelta_FromDelta(space, 10, 20, 30, True, date_api.c_DeltaType) |
| 101 |
assert PyDelta_Check(space, w_delta) |
| 102 |
assert PyDelta_CheckExact(space, w_delta) |
| 103 |
|
| 104 |
assert PyDateTime_DELTA_GET_DAYS(space, w_delta) == 10 |
| 105 |
assert PyDateTime_DELTA_GET_SECONDS(space, w_delta) == 20 |
| 106 |
assert PyDateTime_DELTA_GET_MICROSECONDS(space, w_delta) == 30 |
| 107 |
|
| 108 |
def test_fromtimestamp(self, space): |
| 109 |
w_args = space.wrap((0,)) |
| 110 |
w_date = PyDate_FromTimestamp(space, w_args) |
| 111 |
date = datetime.date.fromtimestamp(0) |
| 112 |
assert space.unwrap(space.str(w_date)) == str(date) |
| 113 |
|
| 114 |
w_args = space.wrap((0,)) |
| 115 |
w_date = PyDateTime_FromTimestamp(space, w_args) |
| 116 |
date = datetime.datetime.fromtimestamp(0) |
| 117 |
assert space.unwrap(space.str(w_date)) == str(date) |
| 118 |
|
| 119 |
@pytest.mark.parametrize('name', ['Time', 'DateTime', 'Date', 'Delta']) |
| 120 |
def test_basicsize(self, space, name): |
| 121 |
datetime = _PyDateTime_Import(space) |
| 122 |
py_size = getattr(datetime, "c_%sType" % name).c_tp_basicsize |
| 123 |
c_size = rffi.sizeof(cts.gettype("PyDateTime_%s" % name)) |
| 124 |
assert py_size == c_size |
| 125 |
|
| 126 |
|
| 127 |
class AppTestDatetime(AppTestCpythonExtensionBase): |
| 128 |
def test_CAPI(self): |
| 129 |
module = self.import_extension('foo', [ |
| 130 |
("get_types", "METH_NOARGS", |
| 131 |
""" |
| 132 |
PyDateTime_IMPORT; |
| 133 |
if (!PyDateTimeAPI) { |
| 134 |
PyErr_SetString(PyExc_RuntimeError, "No PyDateTimeAPI"); |
| 135 |
return NULL; |
| 136 |
} |
| 137 |
return PyTuple_Pack(5, |
| 138 |
PyDateTimeAPI->DateType, |
| 139 |
PyDateTimeAPI->DateTimeType, |
| 140 |
PyDateTimeAPI->TimeType, |
| 141 |
PyDateTimeAPI->DeltaType, |
| 142 |
PyDateTimeAPI->TZInfoType); |
| 143 |
"""), |
| 144 |
("clear_types", "METH_NOARGS", |
| 145 |
""" |
| 146 |
Py_DECREF(PyDateTimeAPI->DateType); |
| 147 |
Py_DECREF(PyDateTimeAPI->DateTimeType); |
| 148 |
Py_DECREF(PyDateTimeAPI->TimeType); |
| 149 |
Py_DECREF(PyDateTimeAPI->DeltaType); |
| 150 |
Py_DECREF(PyDateTimeAPI->TZInfoType); |
| 151 |
Py_RETURN_NONE; |
| 152 |
""" |
| 153 |
) |
| 154 |
], prologue='#include "datetime.h"\n') |
| 155 |
import datetime |
| 156 |
assert module.get_types() == (datetime.date, |
| 157 |
datetime.datetime, |
| 158 |
datetime.time, |
| 159 |
datetime.timedelta, |
| 160 |
datetime.tzinfo) |
| 161 |
module.clear_types() |
| 162 |
|
| 163 |
def test_constructors(self): |
| 164 |
module = self.import_extension('foo', [ |
| 165 |
("new_date", "METH_NOARGS", |
| 166 |
""" PyDateTime_IMPORT; |
| 167 |
return PyDateTimeAPI->Date_FromDate( |
| 168 |
2000, 6, 6, PyDateTimeAPI->DateType); |
| 169 |
"""), |
| 170 |
("new_time", "METH_NOARGS", |
| 171 |
""" PyDateTime_IMPORT; |
| 172 |
return PyDateTimeAPI->Time_FromTime( |
| 173 |
6, 6, 6, 6, Py_None, PyDateTimeAPI->TimeType); |
| 174 |
"""), |
| 175 |
("new_datetime", "METH_NOARGS", |
| 176 |
""" PyDateTime_IMPORT; |
| 177 |
return PyDateTimeAPI->DateTime_FromDateAndTime( |
| 178 |
2000, 6, 6, 6, 6, 6, 6, Py_None, |
| 179 |
PyDateTimeAPI->DateTimeType); |
| 180 |
"""), |
| 181 |
("new_datetime_fromtimestamp", "METH_NOARGS", |
| 182 |
""" PyDateTime_IMPORT; |
| 183 |
PyObject *ts = PyFloat_FromDouble(200000.0); |
| 184 |
Py_INCREF(Py_None); |
| 185 |
PyObject *tsargs = PyTuple_Pack(2, ts, Py_None); |
| 186 |
PyObject *rv = PyDateTimeAPI->DateTime_FromTimestamp( |
| 187 |
(PyObject *)PyDateTimeAPI->DateTimeType, tsargs, NULL); |
| 188 |
Py_DECREF(tsargs); |
| 189 |
return rv; |
| 190 |
"""), |
| 191 |
("new_dt_fromts_tzinfo", "METH_O", |
| 192 |
""" PyDateTime_IMPORT; |
| 193 |
PyObject *ts = PyFloat_FromDouble(200000.0); |
| 194 |
PyObject *tsargs = PyTuple_Pack(1, ts); |
| 195 |
PyObject *tskwargs = PyDict_New(); |
| 196 |
|
| 197 |
Py_INCREF(args); |
| 198 |
PyDict_SetItemString(tskwargs, "tz", args); |
| 199 |
PyObject *rv = PyDateTimeAPI->DateTime_FromTimestamp( |
| 200 |
(PyObject *)PyDateTimeAPI->DateTimeType, tsargs, tskwargs); |
| 201 |
Py_DECREF(tsargs); |
| 202 |
Py_DECREF(tskwargs); |
| 203 |
return rv; |
| 204 |
"""), |
| 205 |
("new_date_fromtimestamp", "METH_NOARGS", |
| 206 |
""" PyDateTime_IMPORT; |
| 207 |
PyObject *ts = PyFloat_FromDouble(1430366400.0); |
| 208 |
Py_INCREF(Py_None); |
| 209 |
PyObject *tsargs = PyTuple_Pack(1, ts); |
| 210 |
PyObject *rv = PyDateTimeAPI->Date_FromTimestamp( |
| 211 |
(PyObject *)PyDateTimeAPI->DateType, tsargs); |
| 212 |
Py_DECREF(tsargs); |
| 213 |
return rv; |
| 214 |
"""), |
| 215 |
], prologue='#include "datetime.h"\n') |
| 216 |
import datetime |
| 217 |
assert module.new_date() == datetime.date(2000, 6, 6) |
| 218 |
assert module.new_time() == datetime.time(6, 6, 6, 6) |
| 219 |
assert module.new_datetime() == datetime.datetime( |
| 220 |
2000, 6, 6, 6, 6, 6, 6) |
| 221 |
|
| 222 |
utc = datetime.timezone.utc |
| 223 |
|
| 224 |
|
| 225 |
assert (module.new_datetime_fromtimestamp() == |
| 226 |
datetime.datetime.fromtimestamp(200000.0)) |
| 227 |
|
| 228 |
assert (module.new_dt_fromts_tzinfo(utc) == |
| 229 |
datetime.datetime.fromtimestamp(200000.0, tz=utc)) |
| 230 |
|
| 231 |
assert (module.new_date_fromtimestamp() == |
| 232 |
datetime.date.fromtimestamp(1430366400.0)) |
| 233 |
|
| 234 |
def test_timezone_constructors(self): |
| 235 |
|
| 236 |
module = self.import_extension('foo', [ |
| 237 |
("new_timezone_fromoffset", "METH_NOARGS", |
| 238 |
""" PyDateTime_IMPORT; |
| 239 |
PyObject *delta = PyDelta_FromDSU(0, 60 * 60, 0); |
| 240 |
PyObject *tzinfo = PyTimeZone_FromOffset(delta); |
| 241 |
Py_DECREF(delta); |
| 242 |
return tzinfo; |
| 243 |
"""), |
| 244 |
("new_timezone_fromoffset_and_name", "METH_NOARGS", |
| 245 |
""" PyDateTime_IMPORT; |
| 246 |
PyObject *delta = PyDelta_FromDSU(0, 60 * 60, 0); |
| 247 |
PyObject *name = PyUnicode_FromString("spam"); |
| 248 |
PyObject *tzinfo = PyTimeZone_FromOffsetAndName(delta, name); |
| 249 |
Py_DECREF(delta); |
| 250 |
Py_DECREF(name); |
| 251 |
return tzinfo; |
| 252 |
"""), |
| 253 |
("utc_singleton_access", "METH_NOARGS", |
| 254 |
""" PyDateTime_IMPORT; |
| 255 |
Py_INCREF(PyDateTime_TimeZone_UTC); |
| 256 |
return PyDateTime_TimeZone_UTC; |
| 257 |
"""), |
| 258 |
], prologue='#include "datetime.h"\n') |
| 259 |
import datetime |
| 260 |
|
| 261 |
one_hour = datetime.timedelta(hours=1) |
| 262 |
expected = datetime.timezone(one_hour) |
| 263 |
assert module.new_timezone_fromoffset() == expected |
| 264 |
|
| 265 |
expected = datetime.timezone(one_hour, "spam") |
| 266 |
assert module.new_timezone_fromoffset_and_name() == expected |
| 267 |
|
| 268 |
assert module.utc_singleton_access() == datetime.timezone.utc |
| 269 |
|
| 270 |
def test_macros(self): |
| 271 |
module = self.import_extension('foo', [ |
| 272 |
("test_date_macros", "METH_NOARGS", |
| 273 |
""" |
| 274 |
PyObject* obj; |
| 275 |
PyDateTime_Date* d; |
| 276 |
PyDateTime_IMPORT; |
| 277 |
if (!PyDateTimeAPI) { |
| 278 |
PyErr_SetString(PyExc_RuntimeError, "No PyDateTimeAPI"); |
| 279 |
return NULL; |
| 280 |
} |
| 281 |
obj = PyDate_FromDate(2000, 6, 6); |
| 282 |
d = (PyDateTime_Date*)obj; |
| 283 |
|
| 284 |
PyDateTime_GET_YEAR(obj); |
| 285 |
PyDateTime_GET_YEAR(d); |
| 286 |
|
| 287 |
PyDateTime_GET_MONTH(obj); |
| 288 |
PyDateTime_GET_MONTH(d); |
| 289 |
|
| 290 |
PyDateTime_GET_DAY(obj); |
| 291 |
PyDateTime_GET_DAY(d); |
| 292 |
|
| 293 |
return obj; |
| 294 |
"""), |
| 295 |
("test_datetime_macros", "METH_NOARGS", |
| 296 |
""" |
| 297 |
PyObject* obj; |
| 298 |
PyDateTime_DateTime *dt; |
| 299 |
PyDateTime_IMPORT; |
| 300 |
if (!PyDateTimeAPI) { |
| 301 |
PyErr_SetString(PyExc_RuntimeError, "No PyDateTimeAPI"); |
| 302 |
return NULL; |
| 303 |
} |
| 304 |
obj = PyDateTime_FromDateAndTime(2000, 6, 6, 6, 6, 6, 6); |
| 305 |
dt = (PyDateTime_DateTime*)obj; |
| 306 |
|
| 307 |
PyDateTime_GET_YEAR(obj); |
| 308 |
PyDateTime_GET_YEAR(dt); |
| 309 |
|
| 310 |
PyDateTime_GET_MONTH(obj); |
| 311 |
PyDateTime_GET_MONTH(dt); |
| 312 |
|
| 313 |
PyDateTime_GET_DAY(obj); |
| 314 |
PyDateTime_GET_DAY(dt); |
| 315 |
|
| 316 |
PyDateTime_DATE_GET_HOUR(obj); |
| 317 |
PyDateTime_DATE_GET_HOUR(dt); |
| 318 |
|
| 319 |
PyDateTime_DATE_GET_MINUTE(obj); |
| 320 |
PyDateTime_DATE_GET_MINUTE(dt); |
| 321 |
|
| 322 |
PyDateTime_DATE_GET_SECOND(obj); |
| 323 |
PyDateTime_DATE_GET_SECOND(dt); |
| 324 |
|
| 325 |
PyDateTime_DATE_GET_MICROSECOND(obj); |
| 326 |
PyDateTime_DATE_GET_MICROSECOND(dt); |
| 327 |
|
| 328 |
PyDateTime_GET_FOLD(obj); |
| 329 |
PyDateTime_DATE_GET_FOLD(obj); |
| 330 |
|
| 331 |
return obj; |
| 332 |
"""), |
| 333 |
("test_time_macros", "METH_NOARGS", |
| 334 |
""" |
| 335 |
PyObject* obj; |
| 336 |
PyDateTime_Time* t; |
| 337 |
PyDateTime_IMPORT; |
| 338 |
if (!PyDateTimeAPI) { |
| 339 |
PyErr_SetString(PyExc_RuntimeError, "No PyDateTimeAPI"); |
| 340 |
return NULL; |
| 341 |
} |
| 342 |
obj = PyTime_FromTime(6, 6, 6, 6); |
| 343 |
t = (PyDateTime_Time*)obj; |
| 344 |
|
| 345 |
PyDateTime_TIME_GET_HOUR(obj); |
| 346 |
PyDateTime_TIME_GET_HOUR(t); |
| 347 |
|
| 348 |
PyDateTime_TIME_GET_MINUTE(obj); |
| 349 |
PyDateTime_TIME_GET_MINUTE(t); |
| 350 |
|
| 351 |
PyDateTime_TIME_GET_SECOND(obj); |
| 352 |
PyDateTime_TIME_GET_SECOND(t); |
| 353 |
|
| 354 |
PyDateTime_TIME_GET_MICROSECOND(obj); |
| 355 |
PyDateTime_TIME_GET_MICROSECOND(t); |
| 356 |
|
| 357 |
return obj; |
| 358 |
"""), |
| 359 |
("test_delta_macros", "METH_NOARGS", |
| 360 |
""" |
| 361 |
PyObject* obj; |
| 362 |
PyDateTime_Delta* delta; |
| 363 |
PyDateTime_IMPORT; |
| 364 |
if (!PyDateTimeAPI) { |
| 365 |
PyErr_SetString(PyExc_RuntimeError, "No PyDateTimeAPI"); |
| 366 |
return NULL; |
| 367 |
} |
| 368 |
obj = PyDelta_FromDSU(6, 6, 6); |
| 369 |
delta = (PyDateTime_Delta*)obj; |
| 370 |
|
| 371 |
#if defined(PYPY_VERSION) || PY_VERSION_HEX >= 0x03030000 |
| 372 |
// These macros are only defined in CPython 3.x and PyPy. |
| 373 |
// See: http://bugs.python.org/issue13727 |
| 374 |
PyDateTime_DELTA_GET_DAYS(obj); |
| 375 |
PyDateTime_DELTA_GET_DAYS(delta); |
| 376 |
|
| 377 |
PyDateTime_DELTA_GET_SECONDS(obj); |
| 378 |
PyDateTime_DELTA_GET_SECONDS(delta); |
| 379 |
|
| 380 |
PyDateTime_DELTA_GET_MICROSECONDS(obj); |
| 381 |
PyDateTime_DELTA_GET_MICROSECONDS(delta); |
| 382 |
#endif |
| 383 |
return obj; |
| 384 |
"""), |
| 385 |
], prologue='#include "datetime.h"\n') |
| 386 |
import datetime |
| 387 |
assert module.test_date_macros() == datetime.date(2000, 6, 6) |
| 388 |
assert module.test_datetime_macros() == datetime.datetime( |
| 389 |
2000, 6, 6, 6, 6, 6, 6) |
| 390 |
assert module.test_time_macros() == datetime.time(6, 6, 6, 6) |
| 391 |
assert module.test_delta_macros() == datetime.timedelta(6, 6, 6) |
| 392 |
|
| 393 |
def test_tzinfo(self): |
| 394 |
module = self.import_extension('foo', [ |
| 395 |
("time_with_tzinfo", "METH_O", |
| 396 |
""" PyDateTime_IMPORT; |
| 397 |
return PyDateTimeAPI->Time_FromTime( |
| 398 |
6, 6, 6, 6, args, PyDateTimeAPI->TimeType); |
| 399 |
"""), |
| 400 |
("datetime_with_tzinfo", "METH_O", |
| 401 |
""" |
| 402 |
PyObject * obj; |
| 403 |
int tzrefcnt = args->ob_refcnt; |
| 404 |
PyDateTime_IMPORT; |
| 405 |
obj = PyDateTimeAPI->DateTime_FromDateAndTime( |
| 406 |
2000, 6, 6, 6, 6, 6, 6, args, |
| 407 |
PyDateTimeAPI->DateTimeType); |
| 408 |
if (!((PyDateTime_DateTime*)obj)->hastzinfo) |
| 409 |
{ |
| 410 |
Py_DECREF(obj); |
| 411 |
PyErr_SetString(PyExc_ValueError, "missing tzinfo"); |
| 412 |
return NULL; |
| 413 |
} |
| 414 |
if (((PyDateTime_DateTime*)obj)->tzinfo->ob_refcnt <= tzrefcnt) |
| 415 |
{ |
| 416 |
Py_DECREF(obj); |
| 417 |
PyErr_SetString(PyExc_ValueError, "tzinfo refcnt not incremented"); |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
return obj; |
| 421 |
|
| 422 |
"""), |
| 423 |
], prologue='#include "datetime.h"\n') |
| 424 |
from datetime import tzinfo, datetime, timedelta, time |
| 425 |
|
| 426 |
|
| 427 |
class GMT1(tzinfo): |
| 428 |
def __del__(self): |
| 429 |
print('deleting GMT1') |
| 430 |
def utcoffset(self, dt): |
| 431 |
return timedelta(hours=1) + self.dst(dt) |
| 432 |
def dst(self, dt): |
| 433 |
return timedelta(0) |
| 434 |
def tzname(self,dt): |
| 435 |
return "GMT +1" |
| 436 |
|
| 437 |
gmt1 = GMT1() |
| 438 |
dt1 = module.time_with_tzinfo(gmt1) |
| 439 |
assert dt1 == time(6, 6, 6, 6, gmt1) |
| 440 |
assert '+01' in str(dt1) |
| 441 |
dt_tz = module.datetime_with_tzinfo(gmt1) |
| 442 |
assert dt_tz == datetime(2000, 6, 6, 6, 6, 6, 6, gmt1) |
| 443 |
|
| 444 |
def test_checks(self): |
| 445 |
module = self.import_extension('foo', [ |
| 446 |
("checks", "METH_O", |
| 447 |
""" PyDateTime_IMPORT; |
| 448 |
return PyTuple_Pack(10, |
| 449 |
PyBool_FromLong(PyDateTime_Check(args)), |
| 450 |
PyBool_FromLong(PyDateTime_CheckExact(args)), |
| 451 |
PyBool_FromLong(PyDate_Check(args)), |
| 452 |
PyBool_FromLong(PyDate_CheckExact(args)), |
| 453 |
PyBool_FromLong(PyTime_Check(args)), |
| 454 |
PyBool_FromLong(PyTime_CheckExact(args)), |
| 455 |
PyBool_FromLong(PyDelta_Check(args)), |
| 456 |
PyBool_FromLong(PyDelta_CheckExact(args)), |
| 457 |
PyBool_FromLong(PyTZInfo_Check(args)), |
| 458 |
PyBool_FromLong(PyTZInfo_CheckExact(args)) |
| 459 |
); |
| 460 |
"""), |
| 461 |
], prologue='#include "datetime.h"\n') |
| 462 |
from datetime import tzinfo, datetime, timedelta, time, date |
| 463 |
o = date(1, 1, 1) |
| 464 |
assert module.checks(o) == (False,) * 2 + (True,) * 2 + (False,) * 6 |
| 465 |
o = time(1, 1, 1) |
| 466 |
assert module.checks(o) == (False,) * 4 + (True,) * 2 + (False,) * 4 |
| 467 |
o = timedelta(1, 1, 1) |
| 468 |
assert module.checks(o) == (False,) * 6 + (True,) * 2 + (False,) * 2 |
| 469 |
o = datetime(1, 1, 1) |
| 470 |
assert module.checks(o) == (True,) * 3 + (False,) * 7 |
| 471 |
o = tzinfo() |
| 472 |
assert module.checks(o) == (False,) * 8 + (True,) * 2 |
| 473 |
|
| 474 |
def test_misc(self): |
| 475 |
|
| 476 |
module = self.import_extension('foo', [ |
| 477 |
("getsystemclock", "METH_NOARGS", |
| 478 |
""" PyDateTime_IMPORT; |
| 479 |
_PyTime_t tic = _PyTime_GetSystemClock(); |
| 480 |
double r = _PyTime_AsSecondsDouble(tic); |
| 481 |
return PyFloat_FromDouble(r); |
| 482 |
"""), |
| 483 |
("get_qualname", "METH_NOARGS", |
| 484 |
""" |
| 485 |
return PyType_GetQualName(PyDateTimeAPI->DateType); |
| 486 |
"""), |
| 487 |
], prologue='#include "datetime.h"\n') |
| 488 |
import time |
| 489 |
|
| 490 |
for _ in range(2000): |
| 491 |
tic1 = time.time() |
| 492 |
tic2 = module.getsystemclock() |
| 493 |
tic3 = time.time() |
| 494 |
assert tic1 <= tic2, "%s, %s" % (tic1, tic2) |
| 495 |
assert tic2 <= tic3, "%s, %s" % (tic2, tic3) |
| 496 |
assert module.get_qualname() == "date" |