summaryrefslogtreecommitdiff
path: root/Modules/_datetimemodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_datetimemodule.c')
-rw-r--r--Modules/_datetimemodule.c98
1 files changed, 19 insertions, 79 deletions
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 6084ffa201..e3de537a8d 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -7,6 +7,10 @@
#include <time.h>
+#ifdef MS_WINDOWS
+# include <winsock2.h> /* struct timeval */
+#endif
+
/* Differentiate between building the core module and building extension
* modules.
*/
@@ -22,6 +26,8 @@ class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=78142cb64b9e98bc]*/
+#include "clinic/_datetimemodule.c.h"
+
/* We require that C int be at least 32 bits, and use int virtually
* everywhere. In just a few cases we use a temp long, where a Python
* API returns a C long. In such cases, we have to ensure that the
@@ -2459,7 +2465,7 @@ date_local_from_object(PyObject *cls, PyObject *obj)
struct tm *tm;
time_t t;
- if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_DOWN) == -1)
+ if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1)
return NULL;
tm = localtime(&t);
@@ -3805,29 +3811,6 @@ time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw)
return clone;
}
-static int
-time_bool(PyObject *self)
-{
- PyObject *offset, *tzinfo;
- int offsecs = 0;
-
- if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) {
- /* Since utcoffset is in whole minutes, nothing can
- * alter the conclusion that this is nonzero.
- */
- return 1;
- }
- tzinfo = GET_TIME_TZINFO(self);
- if (tzinfo != Py_None) {
- offset = call_utcoffset(tzinfo, Py_None);
- if (offset == NULL)
- return -1;
- offsecs = GET_TD_DAYS(offset)*86400 + GET_TD_SECONDS(offset);
- Py_DECREF(offset);
- }
- return (TIME_GET_MINUTE(self)*60 - offsecs + TIME_GET_HOUR(self)*3600) != 0;
-}
-
/* Pickle support, a simple use of __reduce__. */
/* Let basestate be the non-tzinfo data string.
@@ -3895,19 +3878,6 @@ PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time
All arguments are optional. tzinfo may be None, or an instance of\n\
a tzinfo subclass. The remaining arguments may be ints.\n");
-static PyNumberMethods time_as_number = {
- 0, /* nb_add */
- 0, /* nb_subtract */
- 0, /* nb_multiply */
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- 0, /* nb_negative */
- 0, /* nb_positive */
- 0, /* nb_absolute */
- (inquiry)time_bool, /* nb_bool */
-};
-
static PyTypeObject PyDateTime_TimeType = {
PyVarObject_HEAD_INIT(NULL, 0)
"datetime.time", /* tp_name */
@@ -3919,7 +3889,7 @@ static PyTypeObject PyDateTime_TimeType = {
0, /* tp_setattr */
0, /* tp_reserved */
(reprfunc)time_repr, /* tp_repr */
- &time_as_number, /* tp_as_number */
+ 0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)time_hash, /* tp_hash */
@@ -4194,10 +4164,15 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp,
static PyObject *
datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo)
{
- _PyTime_timeval t;
- _PyTime_gettimeofday(&t);
- return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec,
- tzinfo);
+ _PyTime_t ts = _PyTime_GetSystemClock();
+ time_t secs;
+ int us;
+
+ if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0)
+ return NULL;
+ assert(0 <= us && us <= 999999);
+
+ return datetime_from_timet_and_us(cls, f, secs, us, tzinfo);
}
/*[clinic input]
@@ -4213,43 +4188,9 @@ Returns new datetime object representing current time local to tz.
If no tz is specified, uses local timezone.
[clinic start generated code]*/
-PyDoc_STRVAR(datetime_datetime_now__doc__,
-"now($type, /, tz=None)\n"
-"--\n"
-"\n"
-"Returns new datetime object representing current time local to tz.\n"
-"\n"
-" tz\n"
-" Timezone object.\n"
-"\n"
-"If no tz is specified, uses local timezone.");
-
-#define DATETIME_DATETIME_NOW_METHODDEF \
- {"now", (PyCFunction)datetime_datetime_now, METH_VARARGS|METH_KEYWORDS|METH_CLASS, datetime_datetime_now__doc__},
-
-static PyObject *
-datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz);
-
-static PyObject *
-datetime_datetime_now(PyTypeObject *type, PyObject *args, PyObject *kwargs)
-{
- PyObject *return_value = NULL;
- static char *_keywords[] = {"tz", NULL};
- PyObject *tz = Py_None;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs,
- "|O:now", _keywords,
- &tz))
- goto exit;
- return_value = datetime_datetime_now_impl(type, tz);
-
-exit:
- return return_value;
-}
-
static PyObject *
datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz)
-/*[clinic end generated code: output=583c5637e3c843fa input=80d09869c5267d00]*/
+/*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/
{
PyObject *self;
@@ -5111,8 +5052,7 @@ static PyMethodDef datetime_methods[] = {
{"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
METH_VARARGS | METH_CLASS,
- PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
- "(like time.time()).")},
+ PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")},
{"strptime", (PyCFunction)datetime_strptime,
METH_VARARGS | METH_CLASS,