This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Feature: extend strftime to accept milliseconds
Type: enhancement Stage: resolved
Components: Extension Modules Versions: Python 3.2
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: belopolsky Nosy List: baloan, belopolsky, christian.heimes, giampaolo.rodola, guettli, magicbadger, mdt, r.david.murray, wangchun, zseil
Priority: normal Keywords:

Created on 2008-01-31 18:59 by baloan, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (12)
msg61924 - (view) Author: Andreas Balogh (baloan) Date: 2008-01-31 18:59
Currently serializing datetime objects into isoformat string is well
possible. The reverse process - parsing an isoformat string into a
datetime object - doesn't work due to the missing %-tag for the strftime
format string.

Proposal:
Add new tag to strftime like format strings allowing to print and parse
milliseconds.
msg61930 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-31 21:21
Your chances are going to increase if you can come up with a decent
patch. It's going to be harder than you might think. Python uses the
system's strftime function. You'd have to implement a strftime
replacement which supports miliseconds.

Can't you use another like TAI64
(http://cr.yp.to/libtai/tai64.html#tai64n) or JDN
(http://en.wikipedia.org/wiki/Julian_day) instead?
msg75313 - (view) Author: Wang Chun (wangchun) Date: 2008-10-29 09:53
Ruby recently added support of millisecond and nanosecond to strftime.

This is their changeset:

http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=18731

To use the extended strftime, one can do:

>> Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z')
.. "2008-10-29T17:46:03.895+0800"

In the current implementation of Python, both datetime and time modules 
have strftime. Like in Ruby, the strftime in datetime module is a 
method. But the strftime in time module is a function, which takes time 
value to be formatted from argument, and which must be a 9-tuple 
returned by gmtime or localtime. No microsecond data in the tuple, 
unfortunately.

I think as the first step we can make datetime.datetime.strftime do 
microsecond. I prefer microsecond to milli- or micro- second because it 
is something from the the system.

The current Ruby implementation use %L or %3N for millisecond, %6N for 
microsecond, and %N or %9N for nanosecond. I am not sure where they came 
from. Hope there can be some widely accepted standard.
msg75976 - (view) Author: Ziga Seilnacht (zseil) * (Python committer) Date: 2008-11-17 19:00
Do you require millisecond support or would microsecond support be
enough? r61402, which is included in Python 2.6, added support for %f
format to datetime.strftime() and datetime.strptime(). See also #1158.
msg75981 - (view) Author: Andreas Balogh (baloan) Date: 2008-11-17 20:57
Yes, microsecond support is fine. Elaborating on my initial comment.

The following should assert:

a_datetime = datetime.now()
a_datetime.replace(microsecond = 1)

iso_str = adatetime.isoformat()
b_datetime = datetime.strptime(iso_str, "%Y%m%dT%H%M%S.%f")

assert(a_datetime = b_datetime)

The datetime shall be an invariant. 

Fixed from my point of view, Andreas
msg75982 - (view) Author: Andreas Balogh (baloan) Date: 2008-11-17 20:59
Some typos corrected. Sorry for any inconvenience.

a_datetime = datetime.now()
a_datetime.replace(microsecond = 1)

iso_str = a_datetime.isoformat()
b_datetime = datetime.strptime(iso_str, "%Y-%m-%dT%H:%M:%S.%f")

assert(a_datetime = b_datetime)
msg84075 - (view) Author: M. Dietrich (mdt) Date: 2009-03-24 11:30
i am not shure what the code snippet shall proove but shouldnt't it read

from datetime import datetime
a_datetime = datetime.now()
a_datetime = a_datetime.replace(microsecond = 1)

iso_str = a_datetime.isoformat()
b_datetime = datetime.strptime(iso_str, "%Y-%m-%dT%H:%M:%S.%f")

assert(a_datetime == b_datetime)

? 

i general i would expect for each operation a reverse if possible to be
complete. so if there is a strftime, i look for a strptime, if there is
a isoformat, i like a isoparse. and to be complete i need a format tag
for ms if there is a microseconds member in datatime object.
msg85084 - (view) Author: Andreas Balogh (baloan) Date: 2009-04-01 20:05
- a_datetime.replace(microsecond = 1)
+ a_datetime = a_datetime.replace(microsecond = 1)

Thanks for spotting the bug.
msg107174 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-06-06 02:13
With %f support in, isn't this issue out of date?
msg114044 - (view) Author: Thomas Guettler (guettli) * Date: 2010-08-16 13:31
Yes, I think this can be closed, too.
msg140019 - (view) Author: Ben P (magicbadger) Date: 2011-07-08 09:46
Sorry to chime in on an old issue. Whilst it is good to have the ability to format the string up to microsecond precision, it would be better to be able to control the precision used.

For instance, the ISO8601 specification states that there is no strictly defined precision to be used, and that any such precision should be agreed between parties exchanging ISO8601 datetimes.

Would it be possible to state the precision when formatting ISO8601 datetimes - e.g. %3f to only print up to millisecond precision? presumable %f would keep the original functionality of printing right up to the microseconds.
msg140109 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-07-11 11:48
You are better off opening a new issue as a feature request.  Do add at least belopolsky as nosy on the new issue.
History
Date User Action Args
2022-04-11 14:56:30adminsetgithub: 46274
2011-07-11 11:48:07r.david.murraysetnosy: + r.david.murray
messages: + msg140109
2011-07-08 09:46:58magicbadgersetnosy: + magicbadger
messages: + msg140019
2010-08-16 16:00:56belopolskysetstatus: open -> closed
resolution: out of date
stage: test needed -> resolved
2010-08-16 13:31:24guettlisetnosy: + guettli
messages: + msg114044
2010-06-07 20:26:47giampaolo.rodolasetnosy: + giampaolo.rodola
2010-06-06 02:13:10belopolskysetversions: + Python 3.2, - Python 2.6
nosy: + belopolsky

messages: + msg107174

assignee: belopolsky
stage: test needed
2009-04-01 20:05:09baloansetmessages: + msg85084
2009-03-24 11:30:20mdtsetnosy: + mdt
messages: + msg84075
2008-11-17 20:59:51baloansetmessages: + msg75982
2008-11-17 20:57:08baloansetmessages: + msg75981
2008-11-17 19:00:50zseilsetnosy: + zseil
messages: + msg75976
2008-10-29 09:53:55wangchunsetnosy: + wangchun
messages: + msg75313
2008-01-31 21:21:19christian.heimessetpriority: normal
nosy: + christian.heimes
messages: + msg61930
versions: + Python 2.6, - Python 2.5
2008-01-31 19:17:48brett.cannonsettype: enhancement
2008-01-31 18:59:16baloancreate