-
-
Notifications
You must be signed in to change notification settings - Fork 32k
The time library documentation should include the field to catch microseconds in a date time string #115315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
So your use case is something like this, using Python 3.12.2 (v3.12.2:6abddd9f6a, Feb 6 2024, 17:02:06) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(.startup.py)
(imported collections, datetime as dt, dis
8000
, itertools, json, math, os, pprint, re, sys, time)
>>> import time
>>> from datetime import datetime
>>> datetime.now().isoformat()
'2024-02-12T14:29:06.152729' This doesn't work without >>> time.strptime(datetime.now().isoformat(), "%Y-%m-%dT%H:%M:%S")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/_strptime.py", line 548, in _strptime_time
tt = _strptime(data_string, format)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/_strptime.py", line 336, in _strptime
raise ValueError("unconverted data remains: %s" %
ValueError: unconverted data remains: .704075 And it works with >>> time.strptime(datetime.now().isoformat(), "%Y-%m-%dT%H:%M:%S.%f")
time.struct_time(tm_year=2024, tm_mon=2, tm_mday=12, tm_hour=14, tm_min=29, tm_sec=15, tm_wday=0, tm_yday=43, tm_isdst=-1) However, On macOS: Python 3.12.2 (v3.12.2:6abddd9f6a, Feb 6 2024, 17:02:06) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Mon, 12 Feb 2024 12:04:37 +0000'
>>> strftime("%Y-%m-%dT%H:%M:%S.%f%z", gmtime())
'2024-02-12T12:07:19.f+0200' On Ubuntu: Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Mon, 12 Feb 2024 12:19:19 +0000'
>>> strftime("%Y-%m-%dT%H:%M:%S.%f%z", gmtime())
'2024-02-12T12:19:25.%f+0000' Is The table where |
Thank you Hugo for your comment! I'll look at strftime; If the %f doesn't apply to that function, definitely a note should be added to clarify. |
Hugo, you are right. Would adding a new note 4 to that chart work, where note 4 reads something like: Thank you! |
Yes, something like that sounds good, please can you update the PR? |
Will do, thank you! |
PR updated with requested modifications. |
#115316) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
…n chart (pythonGH-115316) (cherry picked from commit 3a72fc3) Co-authored-by: Tahoma Software <bacon@tahoma.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
…n chart (pythonGH-115316) (cherry picked from commit 3a72fc3) Co-authored-by: Tahoma Software <bacon@tahoma.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Thank you all! Very pleased to having been
able to contribute in some way.
|
…n chart (python#115316) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
…n chart (python#115316) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
…n chart (python#115316) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Uh oh!
There was an error while loading. Please reload this page.
Documentation
Summary
Add documentation of the
%f
field to convert microseconds in the time library.Details
On this webpage (https://docs.python.org/3.10/library/time.html), the discussion of
strptime
and the conversion fields used (%H
for hour,%Y
for year, etc) doesn't include mention of the%f
field to match microseconds. On my system, this is needed to convert back and forth fromdatetime
totime.struct_time
.For example, the command sequence:
Yields:
2024-02-12T00:18:44.621471+00:00
To convert this to unix time or the python time tuple, I need to use the
strptime
format string of"%Y-%m-%dT%H:%M:%S.%f%z"
In short, I advocate adding a brief discussion of the
%f
field, perhaps just in the table, to this section of the documentation.This probably impacts documentation for other versions of python too.
Thank you!
Linked PRs
The text was updated successfully, but these errors were encountered: