[issue44831] Inconsistency between datetime.now() and datetime.fromtimestamp(time.time(), None)
Raymond Hettinger
report at bugs.python.org
Fri Aug 6 11:27:24 EDT 2021
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
The cause of the problem is inconsistent rounding modes:
end = datetime.datetime.now() # always rounds down
start = datetime.datetime.fromtimestamp(start, None) # sometimes rounds_up
>From the C code in Modules/_datetimemodule.c:
* The fromtimestamp() code uses ROUND_HALF_EVEN which can round-up.
* The datetime.now() code calls datetime_best_possible() which uses ROUND_FLOOR, always rounding down.
-------------------------------
static PyObject *
datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp,
PyObject *tzinfo)
{
time_t timet;
long us;
if (_PyTime_ObjectToTimeval(timestamp,
&timet, &us, _PyTime_ROUND_HALF_EVEN) == -1)
return NULL;
return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo);
}
--------------------------------
static PyObject *
datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz)
/*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/
{
PyObject *self;
/* Return best possible local time -- this isn't constrained by the
* precision of a timestamp.
*/
if (check_tzinfo_subclass(tz) < 0)
return NULL;
self = datetime_best_possible((PyObject *)type,
tz == Py_None ? _PyTime_localtime :
_PyTime_gmtime,
tz);
if (self != NULL && tz != Py_None) {
/* Convert UTC to tzinfo's zone. */
self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self);
}
return self;
}
------------------------
static PyObject *
datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *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);
}
----------
nosy: +rhettinger
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44831>
_______________________________________
More information about the Python-bugs-list
mailing list