简体   繁体   中英

Python time.ctime() format: 0-padding or space-padding

Two different computers (same python version) return different formatting for time.ctime() . One returns

"Sun May  6 14:04:28 2018"

with 2 spaces before the day of month; the other returns

"Sun May 06 14:04:28 2018"

with a space and a zero. I feel like it's dictated either by OS or by C lib. Does anyone know what this depends upon?

PS: I know how to fix it in the code, I'm looking for a root cause of such behavior.

https://github.com/python/cpython/blob/master/Modules/timemodule.c contains the relevant code:

static PyObject *
_asctime(struct tm *timeptr)
{
    …

    return PyUnicode_FromFormat("%s %s%3d %.2d:%.2d:%.2d %d", …

In words: The said version of Python produces a three character number behind the month, without any zeros. So it is either Dec 9 or Dec 10 .

I could not manage to find a version different, but probably one of the versions you use does something like "%s %s %02d… , putting an explicit space and a two-digit day.

Update: By going back the "blame chain", I could find https://github.com/python/cpython/blame/2427ab9d6f132224d6ee4a2b7f00b9d69ba6c0a3/Modules/timemodule.c which indeed calls the libc asctime() function. Up to then, the function indeed was OS dependent.

The relevant changeset can be reached under https://github.com/python/cpython/commit/b9588b528a48302a4884d0500caec71f1c59280c of 2011-01-04 and, as far as I understand the tags, was backported up to v2.7.4. If your version is older than that, that explains something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM