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

Side by Side Diff: Modules/_datetimemodule.c

Issue 17267: datetime.time support for '+' and 'now'
Patch Set: Created 6 years, 2 months 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:
View unified diff | Download patch
« Lib/datetime.py ('K') | « Lib/test/datetimetester.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 3801 matching lines...) Expand 10 before | Expand all | Expand 10 after
3812 } 3812 }
3813 tzinfo = GET_TIME_TZINFO(self); 3813 tzinfo = GET_TIME_TZINFO(self);
3814 if (tzinfo != Py_None) { 3814 if (tzinfo != Py_None) {
3815 offset = call_utcoffset(tzinfo, Py_None); 3815 offset = call_utcoffset(tzinfo, Py_None);
3816 if (offset == NULL) 3816 if (offset == NULL)
3817 return -1; 3817 return -1;
3818 offsecs = GET_TD_DAYS(offset)*86400 + GET_TD_SECONDS(offset); 3818 offsecs = GET_TD_DAYS(offset)*86400 + GET_TD_SECONDS(offset);
3819 Py_DECREF(offset); 3819 Py_DECREF(offset);
3820 } 3820 }
3821 return (TIME_GET_MINUTE(self)*60 - offsecs + TIME_GET_HOUR(self)*3600) != 0; 3821 return (TIME_GET_MINUTE(self)*60 - offsecs + TIME_GET_HOUR(self)*3600) != 0;
3822 }
3823
3824 /* Force all the time fields into range
3825 * the day argument is there just because I don't know how to normalize the
3826 * hours without it.
3827 */
3828
3829 static void
3830 normalize_time(int *hour, int *minute, int *second, int *microsecond)
3831 {
3832 int day = 0; /* Used to normalize the hour */
3833 normalize_pair(second, microsecond, 1000000);
3834 normalize_pair(minute, second, 60);
3835 normalize_pair(hour, minute, 60);
3836 normalize_pair(&day, hour, 24);
3837 }
3838
3839 /*
3840 * Time arithmetic
3841 */
3842
3843 /* time + timedelta -> time
3844 *
3845 .*/
3846 static PyObject *
3847 add_time_timedelta(PyDateTime_Time *time, PyDateTime_Delta *delta,
3848 int factor)
Ronald Oussoren 2013/02/25 08:35:59 I'd use "int is_addition" instead of "int factor",
3849 {
3850 int hour = TIME_GET_HOUR(time);
3851 int minute = TIME_GET_MINUTE(time);
3852 int second = TIME_GET_SECOND(time) + GET_TD_SECONDS(delta) * factor;
3853 int microsecond = TIME_GET_MICROSECOND(time) +
3854 GET_TD_MICROSECONDS(delta) * factor;
3855
3856 assert(factor == 1 || factor == -1);
3857
3858 normalize_time(&hour, &minute, &second, &microsecond);
3859
3860 return new_time(hour, minute, second, microsecond,
3861 HASTZINFO(time) ? time->tzinfo : Py_None);
3862 }
3863
3864
3865 static PyObject *
3866 time_add(PyObject *left, PyObject *right)
3867 {
3868 if (PyDelta_Check(right)) {
3869 /* time + delta */
3870 return add_time_timedelta((PyDateTime_Time *) left,
3871 (PyDateTime_Delta *) right,
3872 1);
3873 }
3874 Py_RETURN_NOTIMPLEMENTED;
3875 }
3876
3877 static PyObject *
3878 time_subtract(PyObject *left, PyObject *right)
3879 {
3880 if (PyDelta_Check(right)) {
3881 /* time - delta */
3882 return add_time_timedelta((PyDateTime_Time *) left,
3883 (PyDateTime_Delta *) right,
3884 -1);
3885 }
3886 Py_RETURN_NOTIMPLEMENTED;
3822 } 3887 }
3823 3888
3824 /* Pickle support, a simple use of __reduce__. */ 3889 /* Pickle support, a simple use of __reduce__. */
3825 3890
3826 /* Let basestate be the non-tzinfo data string. 3891 /* Let basestate be the non-tzinfo data string.
3827 * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). 3892 * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo).
3828 * So it's a tuple in any (non-error) case. 3893 * So it's a tuple in any (non-error) case.
3829 * __getstate__ isn't exposed. 3894 * __getstate__ isn't exposed.
3830 */ 3895 */
3831 static PyObject * 3896 static PyObject *
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
3882 {NULL, NULL} 3947 {NULL, NULL}
3883 }; 3948 };
3884 3949
3885 static char time_doc[] = 3950 static char time_doc[] =
3886 PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ 3951 PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\
3887 \n\ 3952 \n\
3888 All arguments are optional. tzinfo may be None, or an instance of\n\ 3953 All arguments are optional. tzinfo may be None, or an instance of\n\
3889 a tzinfo subclass. The remaining arguments may be ints or longs.\n"); 3954 a tzinfo subclass. The remaining arguments may be ints or longs.\n");
3890 3955
3891 static PyNumberMethods time_as_number = { 3956 static PyNumberMethods time_as_number = {
3892 0, /* nb_add */ 3957 time_add, /* nb_add */
3893 0, /* nb_subtract */ 3958 time_subtract, /* nb_subtract */
3894 0, /* nb_multiply */ 3959 0, /* nb_multiply */
3895 0, /* nb_remainder */ 3960 0, /* nb_remainder */
3896 0, /* nb_divmod */ 3961 0, /* nb_divmod */
3897 0, /* nb_power */ 3962 0, /* nb_power */
3898 0, /* nb_negative */ 3963 0, /* nb_negative */
3899 0, /* nb_positive */ 3964 0, /* nb_positive */
3900 0, /* nb_absolute */ 3965 0, /* nb_absolute */
3901 (inquiry)time_bool, /* nb_bool */ 3966 (inquiry)time_bool, /* nb_bool */
3902 }; 3967 };
3903 3968
(...skipping 10 matching lines...) Expand all
3914 (reprfunc)time_repr, /* tp_repr */ 3979 (reprfunc)time_repr, /* tp_repr */
3915 &time_as_number, /* tp_as_number */ 3980 &time_as_number, /* tp_as_number */
3916 0, /* tp_as_sequence */ 3981 0, /* tp_as_sequence */
3917 0, /* tp_as_mapping */ 3982 0, /* tp_as_mapping */
3918 (hashfunc)time_hash, /* tp_hash */ 3983 (hashfunc)time_hash, /* tp_hash */
3919 0, /* tp_call */ 3984 0, /* tp_call */
3920 (reprfunc)time_str, /* tp_str */ 3985 (reprfunc)time_str, /* tp_str */
3921 PyObject_GenericGetAttr, /* tp_getattro */ 3986 PyObject_GenericGetAttr, /* tp_getattro */
3922 0, /* tp_setattro */ 3987 0, /* tp_setattro */
3923 0, /* tp_as_buffer */ 3988 0, /* tp_as_buffer */
3924 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ 3989 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3925 time_doc, /* tp_doc */ 3990 time_doc, /* tp_doc */
3926 0, /* tp_traverse */ 3991 0, /* tp_traverse */
3927 0, /* tp_clear */ 3992 0, /* tp_clear */
3928 time_richcompare, /* tp_richcompare */ 3993 time_richcompare, /* tp_richcompare */
3929 0, /* tp_weaklistoffset */ 3994 0, /* tp_weaklistoffset */
3930 0, /* tp_iter */ 3995 0, /* tp_iter */
3931 0, /* tp_iternext */ 3996 0, /* tp_iternext */
3932 time_methods, /* tp_methods */ 3997 time_methods, /* tp_methods */
3933 0, /* tp_members */ 3998 0, /* tp_members */
3934 time_getset, /* tp_getset */ 3999 time_getset, /* tp_getset */
(...skipping 1619 matching lines...) Expand 10 before | Expand all | Expand 10 after
5554 enough to say. 5619 enough to say.
5555 5620
5556 In any case, it's clear that the default fromutc() is strong enough to handle 5621 In any case, it's clear that the default fromutc() is strong enough to handle
5557 "almost all" time zones: so long as the standard offset is invariant, it 5622 "almost all" time zones: so long as the standard offset is invariant, it
5558 doesn't matter if daylight time transition points change from year to year, or 5623 doesn't matter if daylight time transition points change from year to year, or
5559 if daylight time is skipped in some years; it doesn't matter how large or 5624 if daylight time is skipped in some years; it doesn't matter how large or
5560 small dst() may get within its bounds; and it doesn't even matter if some 5625 small dst() may get within its bounds; and it doesn't even matter if some
5561 perverse time zone returns a negative dst()). So a breaking case must be 5626 perverse time zone returns a negative dst()). So a breaking case must be
5562 pretty bizarre, and a tzinfo subclass can override fromutc() if it is. 5627 pretty bizarre, and a tzinfo subclass can override fromutc() if it is.
5563 --------------------------------------------------------------------------- */ 5628 --------------------------------------------------------------------------- */
OLDNEW
« Lib/datetime.py ('K') | « Lib/test/datetimetester.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+