Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3427)

Delta Between Two Patch Sets: Modules/_datetimemodule.c

Issue 1100942: Add datetime.time.strptime and datetime.date.strptime
Left Patch Set: Created 10 months, 1 week ago
Right Patch Set: Created 10 months, 1 week ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/datetimetester.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* C implementation for the date/time type documented at 1 /* C implementation for the date/time type documented at
2 * http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage 2 * http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage
3 */ 3 */
4 4
5 #include "Python.h" 5 #include "Python.h"
6 #include "structmember.h" 6 #include "structmember.h"
7 7
8 #include <time.h> 8 #include <time.h>
9 9
10 /* Differentiate between building the core module and building extension 10 /* Differentiate between building the core module and building extension
(...skipping 2528 matching lines...) Expand 10 before | Expand all | Expand 10 after
2539 /* Return new date from time.strptime(). */ 2539 /* Return new date from time.strptime(). */
2540 static PyObject * 2540 static PyObject *
2541 date_strptime(PyObject *cls, PyObject *args) 2541 date_strptime(PyObject *cls, PyObject *args)
2542 { 2542 {
2543 PyObject *date = NULL; 2543 PyObject *date = NULL;
2544 PyObject *datetime; 2544 PyObject *datetime;
2545 2545
2546 datetime = datetime_strptime((PyObject *)&PyDateTime_DateTimeType, args); 2546 datetime = datetime_strptime((PyObject *)&PyDateTime_DateTimeType, args);
2547 2547
2548 if (datetime == NULL) 2548 if (datetime == NULL)
2549 » return NULL; 2549 return NULL;
2550 2550
2551 if (DATE_GET_HOUR(datetime) || 2551 if (DATE_GET_HOUR(datetime) ||
2552 » DATE_GET_MINUTE(datetime) || 2552 DATE_GET_MINUTE(datetime) ||
2553 » DATE_GET_SECOND(datetime) || 2553 DATE_GET_SECOND(datetime) ||
2554 » DATE_GET_MICROSECOND(datetime)) 2554 DATE_GET_MICROSECOND(datetime))
2555 » PyErr_SetString(PyExc_ValueError, 2555 PyErr_SetString(PyExc_ValueError,
2556 » » » "date.strptime value cannot have a time part"); 2556 "date.strptime value cannot have a time part");
2557 else 2557 else
2558 » date = datetime_getdate((PyDateTime_DateTime *)datetime); 2558 date = datetime_getdate((PyDateTime_DateTime *)datetime);
2559 2559
2560 Py_DECREF(datetime); 2560 Py_DECREF(datetime);
2561 return date; 2561 return date;
2562 } 2562 }
2563 2563
2564 /* 2564 /*
2565 * Date arithmetic. 2565 * Date arithmetic.
2566 */ 2566 */
2567 2567
2568 /* date + timedelta -> date. If arg negate is true, subtract the timedelta 2568 /* date + timedelta -> date. If arg negate is true, subtract the timedelta
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
2852 {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | 2852 {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS |
2853 METH_CLASS, 2853 METH_CLASS,
2854 PyDoc_STR("int -> date corresponding to a proleptic Gregorian " 2854 PyDoc_STR("int -> date corresponding to a proleptic Gregorian "
2855 "ordinal.")}, 2855 "ordinal.")},
2856 2856
2857 {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, 2857 {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS,
2858 PyDoc_STR("Current date or datetime: same as " 2858 PyDoc_STR("Current date or datetime: same as "
2859 "self.__class__.fromtimestamp(time.time()).")}, 2859 "self.__class__.fromtimestamp(time.time()).")},
2860 2860
2861 {"strptime", (PyCFunction)date_strptime, METH_VARARGS | METH_CLASS, 2861 {"strptime", (PyCFunction)date_strptime, METH_VARARGS | METH_CLASS,
2862 PyDoc_STR("string, format -> new date parsed from a string " 2862 PyDoc_STR("string, format -> new date instance parsed from a string "
2863 » "(like time.strptime()).")}, 2863 "(like time.strptime()).\n\n"
2864 ">>> datetime.date.strptime('2012/07/20', '%Y/%m/%d')\n"
2865 "datetime.date(2012, 7, 20)")},
2864 2866
2865 /* Instance methods: */ 2867 /* Instance methods: */
2866 2868
2867 {"ctime", (PyCFunction)date_ctime, METH_NOARGS, 2869 {"ctime", (PyCFunction)date_ctime, METH_NOARGS,
2868 PyDoc_STR("Return ctime() style string.")}, 2870 PyDoc_STR("Return ctime() style string.")},
2869 2871
2870 {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYW ORDS, 2872 {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYW ORDS,
2871 PyDoc_STR("format -> strftime() style string.")}, 2873 PyDoc_STR("format -> strftime() style string.")},
2872 2874
2873 {"__format__", (PyCFunction)date_format, METH_VARARGS, 2875 {"__format__", (PyCFunction)date_format, METH_VARARGS,
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
3549 self = new_time_ex(hour, minute, second, usecond, tzinfo, 3551 self = new_time_ex(hour, minute, second, usecond, tzinfo,
3550 type); 3552 type);
3551 } 3553 }
3552 return self; 3554 return self;
3553 } 3555 }
3554 3556
3555 /* Return new time from time.strptime(). */ 3557 /* Return new time from time.strptime(). */
3556 static PyObject * 3558 static PyObject *
3557 time_strptime(PyObject *cls, PyObject *args) 3559 time_strptime(PyObject *cls, PyObject *args)
3558 { 3560 {
3559 » PyObject *time = NULL; 3561 PyObject *time = NULL;
3560 » PyObject *datetime; 3562 PyObject *datetime;
3561 3563
3562 » static PyObject *emptyDatetime = NULL; 3564 static PyObject *emptyDatetime = NULL;
3563 3565
3564 » /* To ensure that the given string does not contain a date, 3566 /* To ensure that the given string does not contain a date,
3565 » * compare with the result of an empty date string. 3567 * compare with the result of an empty date string.
3566 » */ 3568 */
3567 » if (emptyDatetime == NULL) { 3569 if (emptyDatetime == NULL) {
3568 » » PyObject *emptyStringPair = Py_BuildValue("ss", "", ""); 3570 PyObject *emptyStringPair = Py_BuildValue("ss", "", "");
3569 » » if (emptyStringPair == NULL) 3571 if (emptyStringPair == NULL)
3570 » » » return NULL; 3572 return NULL;
3571 » » emptyDatetime = datetime_strptime( 3573 emptyDatetime = datetime_strptime(
3572 » » » (PyObject *)&PyDateTime_DateTimeType, 3574 (PyObject *)&PyDateTime_DateTimeType,
3573 » » » emptyStringPair); 3575 emptyStringPair);
3574 » » Py_DECREF(emptyStringPair); 3576 Py_DECREF(emptyStringPair);
3575 » » if (emptyDatetime == NULL) 3577 if (emptyDatetime == NULL)
3576 » » » return NULL; 3578 return NULL;
3577 » } 3579 }
3578 3580
3579 » datetime = datetime_strptime( 3581 datetime = datetime_strptime(
3580 » » (PyObject *)&PyDateTime_DateTimeType, 3582 (PyObject *)&PyDateTime_DateTimeType,
3581 » » args); 3583 args);
3582 3584
3583 » if (datetime == NULL) 3585 if (datetime == NULL)
3584 » » return NULL; 3586 return NULL;
3585 3587
3586 » if (GET_YEAR(datetime) != GET_YEAR(emptyDatetime) 3588 if (GET_YEAR(datetime) != GET_YEAR(emptyDatetime)
3587 » || GET_MONTH(datetime) != GET_MONTH(emptyDatetime) 3589 || GET_MONTH(datetime) != GET_MONTH(emptyDatetime)
3588 » || GET_DAY(datetime) != GET_DAY(emptyDatetime)) 3590 || GET_DAY(datetime) != GET_DAY(emptyDatetime))
3589 » » PyErr_SetString(PyExc_ValueError, 3591 PyErr_SetString(PyExc_ValueError,
3590 » » "time.strptime value cannot have a date part"); 3592 "time.strptime value cannot have a date part");
3591 » else 3593 else
3592 » » time = datetime_gettime((PyDateTime_DateTime *)datetime); 3594 time = datetime_gettime((PyDateTime_DateTime *)datetime);
3593 3595
3594 » Py_DECREF(datetime); 3596 Py_DECREF(datetime);
3595 3597
3596 » return time; 3598 return time;
3597 } 3599 }
3598 3600
3599 3601
3600 /* 3602 /*
3601 * Destructor. 3603 * Destructor.
3602 */ 3604 */
3603 3605
3604 static void 3606 static void
3605 time_dealloc(PyDateTime_Time *self) 3607 time_dealloc(PyDateTime_Time *self)
3606 { 3608 {
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
3923 { 3925 {
3924 return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self)); 3926 return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self));
3925 } 3927 }
3926 3928
3927 static PyMethodDef time_methods[] = { 3929 static PyMethodDef time_methods[] = {
3928 3930
3929 /* Class methods: */ 3931 /* Class methods: */
3930 3932
3931 {"strptime", (PyCFunction)time_strptime, METH_VARARGS | METH_CLASS, 3933 {"strptime", (PyCFunction)time_strptime, METH_VARARGS | METH_CLASS,
3932 PyDoc_STR("string, format -> new time parsed from a string " 3934 PyDoc_STR("string, format -> new time parsed from a string "
3933 » "(like time.strptime()).")}, 3935 "(like time.strptime()).\n\n"
3936 ">>> datetime.time.strptime('10:40am', '%H:%M%p')\n"
3937 "datetime.time(10, 40)")},
3934 3938
3935 /* Instance methods: */ 3939 /* Instance methods: */
3936 3940
3937 {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS, 3941 {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS,
3938 PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]" 3942 PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]"
3939 "[+HH:MM].")}, 3943 "[+HH:MM].")},
3940 3944
3941 {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYW ORDS, 3945 {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYW ORDS,
3942 PyDoc_STR("format -> strftime() style string.")}, 3946 PyDoc_STR("format -> strftime() style string.")},
3943 3947
(...skipping 1138 matching lines...) Expand 10 before | Expand all | Expand 10 after
5082 PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, 5086 PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")},
5083 5087
5084 {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, 5088 {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
5085 METH_VARARGS | METH_CLASS, 5089 METH_VARARGS | METH_CLASS,
5086 PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp " 5090 PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
5087 "(like time.time()).")}, 5091 "(like time.time()).")},
5088 5092
5089 {"strptime", (PyCFunction)datetime_strptime, 5093 {"strptime", (PyCFunction)datetime_strptime,
5090 METH_VARARGS | METH_CLASS, 5094 METH_VARARGS | METH_CLASS,
5091 PyDoc_STR("string, format -> new datetime parsed from a string " 5095 PyDoc_STR("string, format -> new datetime parsed from a string "
5092 "(like time.strptime()).")}, 5096 "(like time.strptime()).\n\n"
5093 5097 ">>> datetime.datetime.strptime('2012/07/20 10:40am', "
5094 {"strptime", (PyCFunction)time_strptime, METH_VARARGS | METH_CLASS, 5098 "'%Y/%m/%d %H:%M%p')\n"
5095 PyDoc_STR("string, format -> new time parsed from a string " 5099 "datetime.datetime(2012, 7, 20, 10, 40)")},
5096 "(like time.strptime()).")},
5097 5100
5098 {"combine", (PyCFunction)datetime_combine, 5101 {"combine", (PyCFunction)datetime_combine,
5099 METH_VARARGS | METH_KEYWORDS | METH_CLASS, 5102 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
5100 PyDoc_STR("date, time -> datetime with same date and time fields")}, 5103 PyDoc_STR("date, time -> datetime with same date and time fields")},
5101 5104
5102 /* Instance methods: */ 5105 /* Instance methods: */
5103 5106
5104 {"date", (PyCFunction)datetime_getdate, METH_NOARGS, 5107 {"date", (PyCFunction)datetime_getdate, METH_NOARGS,
5105 PyDoc_STR("Return date object with same year, month and day.")}, 5108 PyDoc_STR("Return date object with same year, month and day.")},
5106 5109
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after
5638 enough to say. 5641 enough to say.
5639 5642
5640 In any case, it's clear that the default fromutc() is strong enough to handle 5643 In any case, it's clear that the default fromutc() is strong enough to handle
5641 "almost all" time zones: so long as the standard offset is invariant, it 5644 "almost all" time zones: so long as the standard offset is invariant, it
5642 doesn't matter if daylight time transition points change from year to year, or 5645 doesn't matter if daylight time transition points change from year to year, or
5643 if daylight time is skipped in some years; it doesn't matter how large or 5646 if daylight time is skipped in some years; it doesn't matter how large or
5644 small dst() may get within its bounds; and it doesn't even matter if some 5647 small dst() may get within its bounds; and it doesn't even matter if some
5645 perverse time zone returns a negative dst()). So a breaking case must be 5648 perverse time zone returns a negative dst()). So a breaking case must be
5646 pretty bizarre, and a tzinfo subclass can override fromutc() if it is. 5649 pretty bizarre, and a tzinfo subclass can override fromutc() if it is.
5647 --------------------------------------------------------------------------- */ 5650 --------------------------------------------------------------------------- */
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7