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

Delta Between Two Patch Sets: Modules/_datetimemodule.c

Issue 17267: datetime.time support for '+' and 'now'
Left Patch Set: Created 6 years, 6 months ago
Right Patch Set: Created 6 years, 6 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« Lib/datetime.py ('K') | « 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 3808 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 } 3822 }
3823 3823
3824 /* Force all the time fields into range 3824 /* Force all the time fields into range
3825 * the day argument is there just because I don't know how to normalize the 3825 * the day argument is there just because I don't know how to normalize the
3826 * hours without it. 3826 * hours without it.
3827 */ 3827 */
3828 3828
3829 static int 3829 static void
Petri Lehtinen 2013/02/23 20:57:19 Should be static void, because cannot fail
3830 normalize_time(int *hour, int *minute, int *second, 3830 normalize_time(int *hour, int *minute, int *second, int *microsecond)
3831 int *microsecond, int *day) 3831 {
3832 { 3832 int day = 0; /* Used to normalize the hour */
Petri Lehtinen 2013/02/23 20:57:19 You should say int day = 0; and not take it
3833 normalize_pair(second, microsecond, 1000000); 3833 normalize_pair(second, microsecond, 1000000);
3834 normalize_pair(minute, second, 60); 3834 normalize_pair(minute, second, 60);
3835 normalize_pair(hour, minute, 60); 3835 normalize_pair(hour, minute, 60);
3836 normalize_pair(day, hour, 24); 3836 normalize_pair(&day, hour, 24);
3837 } 3837 }
3838 3838
3839 /* 3839 /*
3840 * Time arithmetic 3840 * Time arithmetic
3841 */ 3841 */
3842 3842
3843 /* time + timedelta -> time 3843 /* time + timedelta -> time
3844 * 3844 *
3845 .*/ 3845 .*/
3846 static PyObject * 3846 static PyObject *
3847 add_time_timedelta(PyDateTime_Time *time, PyDateTime_Delta *delta) 3847 add_time_timedelta(PyDateTime_Time *time, PyDateTime_Delta *delta,
3848 { 3848 int factor)
3849 PyObject *result = NULL; 3849 {
3850 int hour = TIME_GET_HOUR(time); 3850 int hour = TIME_GET_HOUR(time);
3851 int minute = TIME_GET_MINUTE(time); 3851 int minute = TIME_GET_MINUTE(time);
3852 int second = TIME_GET_SECOND(time) + GET_TD_SECONDS(delta); 3852 int second = TIME_GET_SECOND(time) + GET_TD_SECONDS(delta) * factor;
3853 int microsecond = TIME_GET_MICROSECOND(time) + GET_TD_MICROSECONDS(delta); 3853 int microsecond = TIME_GET_MICROSECOND(time) +
3854 int day = GET_TD_DAYS(delta); /* Is this ugly? */ 3854 GET_TD_MICROSECONDS(delta) * factor;
Petri Lehtinen 2013/02/23 20:57:19 It's useless! :)
3855 3855
3856 /* Passing the day to be able to normalize the hour */ 3856 assert(factor == 1 || factor == -1);
3857 if (normalize_time(&hour, &minute, &second, &microsecond, &day) >= 0) 3857
Petri Lehtinen 2013/02/23 20:57:19 This test is also useless.
3858 result = new_time(hour, minute, second, microsecond, 3858 normalize_time(&hour, &minute, &second, &microsecond);
3859 HASTZINFO(time) ? time->tzinfo : Py_None); 3859
3860 return result; 3860 return new_time(hour, minute, second, microsecond,
3861 } 3861 HASTZINFO(time) ? time->tzinfo : Py_None);
3862 }
3863
3862 3864
3863 static PyObject * 3865 static PyObject *
3864 time_add(PyObject *left, PyObject *right) 3866 time_add(PyObject *left, PyObject *right)
3865 { 3867 {
3866 if (PyDelta_Check(right)) { 3868 if (PyDelta_Check(right)) {
3867 /* time + delta */ 3869 /* time + delta */
3868 return add_time_timedelta((PyDateTime_Time *) left, 3870 return add_time_timedelta((PyDateTime_Time *) left,
3869 (PyDateTime_Delta *) right); 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);
3870 } 3885 }
3871 Py_RETURN_NOTIMPLEMENTED; 3886 Py_RETURN_NOTIMPLEMENTED;
3872 } 3887 }
3873 3888
3874 /* Pickle support, a simple use of __reduce__. */ 3889 /* Pickle support, a simple use of __reduce__. */
3875 3890
3876 /* Let basestate be the non-tzinfo data string. 3891 /* Let basestate be the non-tzinfo data string.
3877 * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). 3892 * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo).
3878 * So it's a tuple in any (non-error) case. 3893 * So it's a tuple in any (non-error) case.
3879 * __getstate__ isn't exposed. 3894 * __getstate__ isn't exposed.
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
3933 }; 3948 };
3934 3949
3935 static char time_doc[] = 3950 static char time_doc[] =
3936 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\
3937 \n\ 3952 \n\
3938 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\
3939 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");
3940 3955
3941 static PyNumberMethods time_as_number = { 3956 static PyNumberMethods time_as_number = {
3942 time_add, /* nb_add */ 3957 time_add, /* nb_add */
3943 0, /* nb_subtract */ 3958 time_subtract, /* nb_subtract */
3944 0, /* nb_multiply */ 3959 0, /* nb_multiply */
3945 0, /* nb_remainder */ 3960 0, /* nb_remainder */
3946 0, /* nb_divmod */ 3961 0, /* nb_divmod */
3947 0, /* nb_power */ 3962 0, /* nb_power */
3948 0, /* nb_negative */ 3963 0, /* nb_negative */
3949 0, /* nb_positive */ 3964 0, /* nb_positive */
3950 0, /* nb_absolute */ 3965 0, /* nb_absolute */
3951 (inquiry)time_bool, /* nb_bool */ 3966 (inquiry)time_bool, /* nb_bool */
3952 }; 3967 };
3953 3968
(...skipping 1650 matching lines...) Expand 10 before | Expand all | Expand 10 after
5604 enough to say. 5619 enough to say.
5605 5620
5606 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
5607 "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
5608 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
5609 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
5610 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
5611 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
5612 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.
5613 --------------------------------------------------------------------------- */ 5628 --------------------------------------------------------------------------- */
LEFTRIGHT

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