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

Unified Diff: Modules/_datetimemodule.c

Issue 17267: datetime.time support for '+' and 'now'
Patch Set: Created 6 years, 2 months ago
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 side-by-side diff with in-line comments
Download patch
« Lib/datetime.py ('K') | « Lib/test/datetimetester.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
index 7c7170d..608f4b4 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -3821,6 +3821,71 @@ time_bool(PyObject *self)
return (TIME_GET_MINUTE(self)*60 - offsecs + TIME_GET_HOUR(self)*3600) != 0;
}
+/* Force all the time fields into range
+ * the day argument is there just because I don't know how to normalize the
+ * hours without it.
+ */
+
+static void
+normalize_time(int *hour, int *minute, int *second, int *microsecond)
+{
+ int day = 0; /* Used to normalize the hour */
+ normalize_pair(second, microsecond, 1000000);
+ normalize_pair(minute, second, 60);
+ normalize_pair(hour, minute, 60);
+ normalize_pair(&day, hour, 24);
+}
+
+/*
+ * Time arithmetic
+ */
+
+/* time + timedelta -> time
+ *
+.*/
+static PyObject *
+add_time_timedelta(PyDateTime_Time *time, PyDateTime_Delta *delta,
+ int factor)
+{
+ int hour = TIME_GET_HOUR(time);
+ int minute = TIME_GET_MINUTE(time);
+ int second = TIME_GET_SECOND(time) + GET_TD_SECONDS(delta) * factor;
+ int microsecond = TIME_GET_MICROSECOND(time) +
+ GET_TD_MICROSECONDS(delta) * factor;
+
+ assert(factor == 1 || factor == -1);
+
+ normalize_time(&hour, &minute, &second, &microsecond);
+
+ return new_time(hour, minute, second, microsecond,
+ HASTZINFO(time) ? time->tzinfo : Py_None);
+}
+
+
+static PyObject *
+time_add(PyObject *left, PyObject *right)
+{
+ if (PyDelta_Check(right)) {
+ /* time + delta */
+ return add_time_timedelta((PyDateTime_Time *) left,
+ (PyDateTime_Delta *) right,
+ 1);
+ }
+ Py_RETURN_NOTIMPLEMENTED;
+}
+
+static PyObject *
+time_subtract(PyObject *left, PyObject *right)
+{
+ if (PyDelta_Check(right)) {
+ /* time - delta */
+ return add_time_timedelta((PyDateTime_Time *) left,
+ (PyDateTime_Delta *) right,
+ -1);
+ }
+ Py_RETURN_NOTIMPLEMENTED;
+}
+
/* Pickle support, a simple use of __reduce__. */
/* Let basestate be the non-tzinfo data string.
@@ -3889,8 +3954,8 @@ All arguments are optional. tzinfo may be None, or an instance of\n\
a tzinfo subclass. The remaining arguments may be ints or longs.\n");
static PyNumberMethods time_as_number = {
- 0, /* nb_add */
- 0, /* nb_subtract */
+ time_add, /* nb_add */
+ time_subtract, /* nb_subtract */
0, /* nb_multiply */
0, /* nb_remainder */
0, /* nb_divmod */
« 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+