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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
|
#include <Python.h>
#include <ctype.h>
#include <datetime.h>
#include "isocalendar.h"
#include "timezone.h"
#define STRINGIZE(x) #x
#define EXPAND_AND_STRINGIZE(x) STRINGIZE(x)
#define PY_VERSION_AT_LEAST_33 \
((PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 3) || PY_MAJOR_VERSION > 3)
#define PY_VERSION_AT_LEAST_36 \
((PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 6) || PY_MAJOR_VERSION > 3)
#define PY_VERSION_AT_LEAST_37 \
((PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 7) || PY_MAJOR_VERSION > 3)
#define PY_VERSION_AT_LEAST_38 \
((PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || PY_MAJOR_VERSION > 3)
/* PyPy compatibility for cPython 3.7's Timezone API was added to PyPy 7.3.6
* https://foss.heptapod.net/pypy/pypy/-/merge_requests/826
* But was then reverted in 7.3.7 for PyPy 3.7:
* https://foss.heptapod.net/pypy/pypy/-/commit/eeeafcf905afa0f26049ac29dc00f5b295171f99
* It is still present in 7.3.7 for PyPy 3.8+
*/
#ifdef PYPY_VERSION
#define SUPPORTS_37_TIMEZONE_API \
(PYPY_VERSION_NUM >= 0x07030600) && PY_VERSION_AT_LEAST_38
#else
#define SUPPORTS_37_TIMEZONE_API PY_VERSION_AT_LEAST_37
#endif
static PyObject *utc;
#if CISO8601_CACHING_ENABLED
/* 2879 = (1439 * 2) + 1, number of offsets from UTC possible in
* Python (i.e., [-1439, 1439]).
*
* 0 - 1438 = Negative offsets [-1439..-1]
* 1439 = Zero offset
* 1440 - 2878 = Positive offsets [1...1439]
*/
static PyObject *tz_cache[2879] = {NULL};
#endif
#define PARSE_INTEGER(field, length, field_name) \
for (i = 0; i < length; i++) { \
if (*c >= '0' && *c <= '9') { \
field = 10 * field + *c++ - '0'; \
} \
else { \
return format_unexpected_character_exception( \
field_name, c, (c - str) / sizeof(char), length - i); \
} \
}
#define PARSE_FRACTIONAL_SECOND() \
for (i = 0; i < 6; i++) { \
if (*c >= '0' && *c <= '9') { \
usecond = 10 * usecond + *c++ - '0'; \
} \
else if (i == 0) { \
/* We need at least one digit. */ \
/* Trailing '.' or ',' is not allowed */ \
return format_unexpected_character_exception( \
"subsecond", c, (c - str) / sizeof(char), 1); \
} \
else \
break; \
} \
\
/* Omit excessive digits */ \
while (*c >= '0' && *c <= '9') c++; \
\
/* If we break early, fully expand the usecond */ \
while (i++ < 6) usecond *= 10;
#if PY_VERSION_AT_LEAST_33
#define PARSE_SEPARATOR(separator, field_name) \
if (separator) { \
c++; \
} \
else { \
PyObject *unicode_str = PyUnicode_FromString(c); \
PyObject *unicode_char = PyUnicode_Substring(unicode_str, 0, 1); \
PyErr_Format(PyExc_ValueError, \
"Invalid character while parsing %s ('%U', Index: %lu)", \
field_name, unicode_char, (c - str) / sizeof(char)); \
Py_DECREF(unicode_str); \
Py_DECREF(unicode_char); \
return NULL; \
}
#else
#define PARSE_SEPARATOR(separator, field_name) \
if (separator) { \
c++; \
} \
else { \
if (isascii((int)*c)) { \
PyErr_Format( \
PyExc_ValueError, \
"Invalid character while parsing %s ('%c', Index: %lu)", \
field_name, *c, (c - str) / sizeof(char)); \
} \
else { \
PyErr_Format(PyExc_ValueError, \
"Invalid character while parsing %s (Index: %lu)", \
field_name, (c - str) / sizeof(char)); \
} \
return NULL; \
}
#endif
static void *
format_unexpected_character_exception(char *field_name, const char *c,
size_t index,
int expected_character_count)
{
if (*c == '\0') {
PyErr_Format(
PyExc_ValueError,
"Unexpected end of string while parsing %s. Expected %d more "
"character%s",
field_name, expected_character_count,
(expected_character_count != 1) ? "s" : "");
}
else if (*c == '-' && index == 0 && strcmp(field_name, "year") == 0) {
PyErr_Format(
PyExc_ValueError,
"Invalid character while parsing %s ('-', Index: 0). "
"While valid ISO 8601 years, BCE years are not supported by "
"Python's `datetime` objects.",
field_name);
}
else {
#if PY_VERSION_AT_LEAST_33
PyObject *unicode_str = PyUnicode_FromString(c);
PyObject *unicode_char = PyUnicode_Substring(unicode_str, 0, 1);
PyErr_Format(PyExc_ValueError,
"Invalid character while parsing %s ('%U', Index: %zu)",
field_name, unicode_char, index);
Py_DECREF(unicode_str);
Py_DECREF(unicode_char);
#else
if (isascii((int)*c)) {
PyErr_Format(
PyExc_ValueError,
"Invalid character while parsing %s ('%c', Index: %zu)",
field_name, *c, index);
}
else {
PyErr_Format(PyExc_ValueError,
"Invalid character while parsing %s (Index: %zu)",
field_name, index);
}
#endif
}
return NULL;
}
#define IS_CALENDAR_DATE_SEPARATOR (*c == '-')
#define IS_ISOCALENDAR_SEPARATOR (*c == 'W')
#define IS_DATE_AND_TIME_SEPARATOR (*c == 'T' || *c == ' ' || *c == 't')
#define IS_TIME_SEPARATOR (*c == ':')
#define IS_TIME_ZONE_SEPARATOR \
(*c == 'Z' || *c == '-' || *c == '+' || *c == 'z')
#define IS_FRACTIONAL_SEPARATOR (*c == '.' || (*c == ',' && !rfc3339_only))
static PyObject *
_parse(PyObject *self, PyObject *dtstr, int parse_any_tzinfo, int rfc3339_only)
{
PyObject *obj;
PyObject *tzinfo = Py_None;
#if PY_VERSION_AT_LEAST_33
Py_ssize_t len;
#endif
int i;
const char *str;
const char *c;
int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0,
usecond = 0;
int iso_week = 0, iso_day = 0;
int ordinal_day = 0;
int time_is_midnight = 0;
int tzhour = 0, tzminute = 0, tzsign = 0;
#if CISO8601_CACHING_ENABLED
int tz_index = 0;
#endif
PyObject *delta;
PyObject *temp;
int extended_date_format = 0;
#if PY_MAJOR_VERSION >= 3
if (!PyUnicode_Check(dtstr)) {
#else
if (!PyString_Check(dtstr)) {
#endif
PyErr_SetString(PyExc_TypeError, "argument must be str");
return NULL;
}
#if PY_VERSION_AT_LEAST_33
str = c = PyUnicode_AsUTF8AndSize(dtstr, &len);
#else
str = c = PyString_AsString(dtstr);
#endif
/* Year */
PARSE_INTEGER(year, 4, "year")
#if 1
/* Python 3.6+ does this validation as part of datetime's C API
* constructor. See
* https://github.com/python/cpython/commit/b67f0967386a9c9041166d2bbe0a421bd81e10bc
* We skip ` || year < datetime.MAXYEAR)`, since ciso8601 currently doesn't
* support 5 character years, so it is impossible.
*/
if (year <
1) /* datetime.MINYEAR = 1, which is not exposed to the C API. */
{
PyErr_Format(PyExc_ValueError, "year %d is out of range", year);
return NULL;
}
#endif
if (IS_CALENDAR_DATE_SEPARATOR) {
c++;
extended_date_format = 1;
if (IS_ISOCALENDAR_SEPARATOR) { /* Separated ISO Calendar week and day
(i.e., Www-D) */
c++;
if (rfc3339_only) {
PyErr_SetString(PyExc_ValueError,
"Datetime string not in RFC 3339 format.");
return NULL;
}
PARSE_INTEGER(iso_week, 2, "iso_week")
if (*c != '\0' && !IS_DATE_AND_TIME_SEPARATOR) { /* Optional Day */
PARSE_SEPARATOR(IS_CALENDAR_DATE_SEPARATOR,
"date separator ('-')")
PARSE_INTEGER(iso_day, 1, "iso_day")
}
else {
iso_day = 1;
}
int rv = iso_to_ymd(year, iso_week, iso_day, &year, &month, &day);
if (rv) {
PyErr_Format(PyExc_ValueError, "Invalid ISO Calendar date");
return NULL;
}
}
else { /* Separated month and may (i.e., MM-DD) or
ordinal date (i.e., DDD) */
/* For sake of simplicity, we'll assume that it is a month
* If we find out later that it's an ordinal day, then we'll adjust
*/
PARSE_INTEGER(month, 2, "month")
if (*c != '\0' && !IS_DATE_AND_TIME_SEPARATOR) {
if (IS_CALENDAR_DATE_SEPARATOR) { /* Optional day */
c++;
PARSE_INTEGER(day, 2, "day")
}
else { /* Ordinal day */
PARSE_INTEGER(ordinal_day, 1, "ordinal day")
ordinal_day = (month * 10) + ordinal_day;
int rv =
ordinal_to_ymd(year, ordinal_day, &year, &month, &day);
if (rv) {
PyErr_Format(
PyExc_ValueError,
"Invalid ordinal day: %d is %s for year %d",
ordinal_day, rv == -1 ? "too small" : "too large",
year);
return NULL;
}
}
}
else if (rfc3339_only) {
PyErr_SetString(PyExc_ValueError,
"Datetime string not in RFC 3339 format.");
return NULL;
}
else {
day = 1;
}
}
}
else if (rfc3339_only) {
PyErr_SetString(PyExc_ValueError,
"Datetime string not in RFC 3339 format.");
return NULL;
}
else {
if (IS_ISOCALENDAR_SEPARATOR) { /* Non-separated ISO Calendar week and
day (i.e., WwwD) */
c++;
PARSE_INTEGER(iso_week, 2, "iso_week")
if (*c != '\0' && !IS_DATE_AND_TIME_SEPARATOR) { /* Optional Day */
PARSE_INTEGER(iso_day, 1, "iso_day")
}
else {
iso_day = 1;
}
int rv = iso_to_ymd(year, iso_week, iso_day, &year, &month, &day);
if (rv) {
PyErr_Format(PyExc_ValueError, "Invalid ISO Calendar date");
return NULL;
}
}
else { /* Non-separated Month and Day (i.e., MMDD) or
ordinal date (i.e., DDD)*/
/* For sake of simplicity, we'll assume that it is a month
* If we find out later that it's an ordinal day, then we'll adjust
*/
PARSE_INTEGER(month, 2, "month")
PARSE_INTEGER(ordinal_day, 1, "ordinal day")
if (*c == '\0' || IS_DATE_AND_TIME_SEPARATOR) { /* Ordinal day */
ordinal_day = (month * 10) + ordinal_day;
int rv =
ordinal_to_ymd(year, ordinal_day, &year, &month, &day);
if (rv) {
PyErr_Format(PyExc_ValueError,
"Invalid ordinal day: %d is %s for year %d",
ordinal_day,
rv == -1 ? "too small" : "too large", year);
return NULL;
}
}
else { /* Day */
/* Note that YYYYMM is not a valid timestamp. If the calendar
* date is not separated, a day is required (i.e., YYMMDD)
*/
PARSE_INTEGER(day, 1, "day")
day = (ordinal_day * 10) + day;
}
}
}
#if !PY_VERSION_AT_LEAST_36
/* Validation of date fields
* These checks are needed for Python <3.6 support. See
* https://github.com/closeio/ciso8601/pull/30 Python 3.6+ does this
* validation as part of datetime's C API constructor. See
* https://github.com/python/cpython/commit/b67f0967386a9c9041166d2bbe0a421bd81e10bc
*/
if (month < 1 || month > 12) {
PyErr_SetString(PyExc_ValueError, "month must be in 1..12");
return NULL;
}
if (day < 1) {
PyErr_SetString(PyExc_ValueError, "day is out of range for month");
return NULL;
}
/* Validate max day based on month */
switch (month) {
case 2:
/* In the Gregorian calendar three criteria must be taken into
* account to identify leap years:
* -The year can be evenly divided by 4;
* -If the year can be evenly divided by 100, it is NOT a leap
* year, unless;
* -The year is also evenly divisible by 400. Then it is a leap
* year.
*/
if (day > 28) {
unsigned int leap =
(year % 4 == 0) && (year % 100 || (year % 400 == 0));
if (leap == 0 || day > 29) {
PyErr_SetString(PyExc_ValueError,
"day is out of range for month");
return NULL;
}
}
break;
case 4:
case 6:
case 9:
case 11:
if (day > 30) {
PyErr_SetString(PyExc_ValueError,
"day is out of range for month");
return NULL;
}
break;
default:
/* For other months i.e. 1, 3, 5, 7, 8, 10 and 12 */
if (day > 31) {
PyErr_SetString(PyExc_ValueError,
"day is out of range for month");
return NULL;
}
break;
}
#endif
if (*c != '\0') {
/* Date and time separator */
PARSE_SEPARATOR(IS_DATE_AND_TIME_SEPARATOR,
"date and time separator (i.e., 'T', 't', or ' ')")
/* Hour */
PARSE_INTEGER(hour, 2, "hour")
if (*c != '\0' &&
!IS_TIME_ZONE_SEPARATOR) { /* Optional minute and second */
if (IS_TIME_SEPARATOR) { /* Separated Minute and Second
* (i.e., mm:ss)
*/
c++;
/* Minute */
PARSE_INTEGER(minute, 2, "minute")
if (*c != '\0' &&
!IS_TIME_ZONE_SEPARATOR) { /* Optional Second */
PARSE_SEPARATOR(IS_TIME_SEPARATOR, "time separator (':')")
/* Second */
PARSE_INTEGER(second, 2, "second")
/* Optional Fractional Second */
if (IS_FRACTIONAL_SEPARATOR) {
c++;
PARSE_FRACTIONAL_SECOND()
}
}
else if (rfc3339_only) {
PyErr_SetString(PyExc_ValueError,
"RFC 3339 requires the second to be "
"specified.");
return NULL;
}
if (!extended_date_format) {
PyErr_SetString(
PyExc_ValueError,
"Cannot combine \"basic\" date format with"
" \"extended\" time format (Should be either "
"`YYYY-MM-DDThh:mm:ss` or `YYYYMMDDThhmmss`).");
return NULL;
}
}
else if (rfc3339_only) {
PyErr_SetString(PyExc_ValueError,
"Colons separating time components are "
"mandatory in RFC 3339.");
return NULL;
}
else { /* Non-separated Minute and Second (i.e., mmss) */
/* Minute */
PARSE_INTEGER(minute, 2, "minute")
if (*c != '\0' &&
!IS_TIME_ZONE_SEPARATOR) { /* Optional Second */
/* Second */
PARSE_INTEGER(second, 2, "second")
/* Optional Fractional Second */
if (IS_FRACTIONAL_SEPARATOR) {
c++;
PARSE_FRACTIONAL_SECOND()
}
}
if (extended_date_format) {
PyErr_SetString(
PyExc_ValueError,
"Cannot combine \"extended\" date format with"
" \"basic\" time format (Should be either "
"`YYYY-MM-DDThh:mm:ss` or `YYYYMMDDThhmmss`).");
return NULL;
}
}
}
else if (rfc3339_only) {
PyErr_SetString(PyExc_ValueError,
"Minute and second are mandatory in RFC 3339");
return NULL;
}
if (hour == 24 && minute == 0 && second == 0 && usecond == 0) {
/* Special case of 24:00:00, that is allowed in ISO 8601. It is
* equivalent to 00:00:00 the following day
*/
if (rfc3339_only) {
PyErr_SetString(PyExc_ValueError,
"An hour value of 24, while sometimes legal "
"in ISO 8601, is explicitly forbidden by RFC "
"3339.");
return NULL;
}
hour = 0, minute = 0, second = 0, usecond = 0;
time_is_midnight = 1;
}
#if !PY_VERSION_AT_LEAST_36
/* Validate hour/minute/second
* Only needed for Python <3.6 support.
* Python 3.6+ does this validation as part of datetime's constructor).
*/
if (hour > 23) {
PyErr_SetString(PyExc_ValueError, "hour must be in 0..23");
return NULL;
}
if (minute > 59) {
PyErr_SetString(PyExc_ValueError, "minute must be in 0..59");
return NULL;
}
if (second > 59) {
PyErr_SetString(PyExc_ValueError, "second must be in 0..59");
return NULL;
}
#endif
/* Optional tzinfo */
if (IS_TIME_ZONE_SEPARATOR) {
if (*c == '+') {
tzsign = 1;
}
else if (*c == '-') {
tzsign = -1;
}
c++;
if (tzsign != 0) {
/* tz hour */
PARSE_INTEGER(tzhour, 2, "tz hour")
if (IS_TIME_SEPARATOR) { /* Optional separator */
c++;
/* tz minute */
PARSE_INTEGER(tzminute, 2, "tz minute")
}
else if (rfc3339_only) {
PyErr_SetString(PyExc_ValueError,
"Separator between hour and minute in UTC "
"offset is mandatory in RFC 3339");
return NULL;
}
else if (*c != '\0') { /* Optional tz minute */
PARSE_INTEGER(tzminute, 2, "tz minute")
}
}
/* It's not entirely clear whether this validation check is
* necessary under ISO 8601. For now, we will err on the side of
* caution and prevent suspected invalid timestamps. If we need to
* loosen this restriction later, we can.
*/
if (tzminute > 59) {
PyErr_SetString(PyExc_ValueError, "tzminute must be in 0..59");
return NULL;
}
if (parse_any_tzinfo) {
tzminute += 60 * tzhour;
tzminute *= tzsign;
if (tzminute == 0) {
tzinfo = utc;
}
else if (abs(tzminute) >= 1440) {
/* Format the error message as if we were still using pytz
* for Python 2 and datetime.timezone for Python 3.
* This is done to maintain complete backwards
* compatibility with ciso8601 2.0.x. Perhaps change to a
* simpler message in ciso8601 v3.0.0.
*/
#if PY_MAJOR_VERSION >= 3
delta = PyDelta_FromDSU(0, tzminute * 60, 0);
PyErr_Format(PyExc_ValueError,
"offset must be a timedelta"
" strictly between -timedelta(hours=24) and"
" timedelta(hours=24),"
" not %R.",
delta);
Py_DECREF(delta);
#else
PyErr_Format(PyExc_ValueError,
"('absolute offset is too large', %d)",
tzminute);
#endif
return NULL;
}
else {
#if CISO8601_CACHING_ENABLED
tz_index = tzminute + 1439;
if ((tzinfo = tz_cache[tz_index]) == NULL) {
tzinfo = new_fixed_offset(60 * tzminute);
if (tzinfo == NULL) /* i.e., PyErr_Occurred() */
return NULL;
tz_cache[tz_index] = tzinfo;
}
#else
tzinfo = new_fixed_offset(60 * tzminute);
if (tzinfo == NULL) /* i.e., PyErr_Occurred() */
return NULL;
#endif
}
}
}
else if (rfc3339_only) {
PyErr_SetString(PyExc_ValueError,
"UTC offset is mandatory in RFC 3339 format.");
return NULL;
}
}
else if (rfc3339_only) {
PyErr_SetString(PyExc_ValueError,
"Time is mandatory in RFC 3339 format.");
return NULL;
}
/* Make sure that there is no more to parse. */
if (*c != '\0') {
PyErr_Format(PyExc_ValueError, "unconverted data remains: '%s'", c);
#if !CISO8601_CACHING_ENABLED
if (tzinfo != Py_None && tzinfo != utc)
Py_DECREF(tzinfo);
#endif
return NULL;
}
obj = PyDateTimeAPI->DateTime_FromDateAndTime(
year, month, day, hour, minute, second, usecond, tzinfo,
PyDateTimeAPI->DateTimeType);
#if !CISO8601_CACHING_ENABLED
if (tzinfo != Py_None && tzinfo != utc)
Py_DECREF(tzinfo);
#endif
if (obj && time_is_midnight) {
delta = PyDelta_FromDSU(1, 0, 0); /* 1 day */
temp = obj;
obj = PyNumber_Add(temp, delta);
Py_DECREF(delta);
Py_DECREF(temp);
}
return obj;
}
static PyObject *
parse_datetime_as_naive(PyObject *self, PyObject *dtstr)
{
return _parse(self, dtstr, 0, 0);
}
static PyObject *
parse_datetime(PyObject *self, PyObject *dtstr)
{
return _parse(self, dtstr, 1, 0);
}
static PyObject *
parse_rfc3339(PyObject *self, PyObject *dtstr)
{
return _parse(self, dtstr, 1, 1);
}
static PyObject *
_hard_coded_benchmark_timestamp(PyObject *self, PyObject *ignored)
{
return PyDateTimeAPI->DateTime_FromDateAndTime(
2014, 1, 9, 21, 48, 0, 0, Py_None, PyDateTimeAPI->DateTimeType);
}
static PyMethodDef CISO8601Methods[] = {
{"parse_datetime", (PyCFunction)parse_datetime, METH_O,
"Parse a ISO8601 date time string."},
{"parse_datetime_as_naive", parse_datetime_as_naive, METH_O,
"Parse a ISO8601 date time string, ignoring the time zone component."},
{"parse_rfc3339", parse_rfc3339, METH_O,
"Parse an RFC 3339 date time string."},
{"_hard_coded_benchmark_timestamp", _hard_coded_benchmark_timestamp,
METH_NOARGS,
"Return a datetime using hardcoded values (for benchmarking purposes)"},
{NULL, NULL, 0, NULL}};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"ciso8601",
NULL,
-1,
CISO8601Methods,
NULL,
NULL,
NULL,
NULL,
};
#endif
PyMODINIT_FUNC
#if PY_MAJOR_VERSION >= 3
PyInit_ciso8601(void)
#else
initciso8601(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
PyObject *module = Py_InitModule("ciso8601", CISO8601Methods);
#endif
/* CISO8601_VERSION is defined in setup.py */
PyModule_AddStringConstant(module, "__version__",
EXPAND_AND_STRINGIZE(CISO8601_VERSION));
PyDateTime_IMPORT;
// PyMODINIT_FUNC is void in Python 2, returns PyObject* in Python 3
if (initialize_timezone_code(module) < 0) {
#if PY_MAJOR_VERSION >= 3
return NULL;
#else
return;
#endif
}
#if SUPPORTS_37_TIMEZONE_API
utc = PyDateTime_TimeZone_UTC;
#else
utc = new_fixed_offset(0);
#endif
// PyMODINIT_FUNC is void in Python 2, returns PyObject* in Python 3
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}
|