8000 The time library documentation should include the field to catch microseconds in a date time string · Issue #115315 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
TahomaSoft opened this issue Feb 12, 2024 · 7 comments
Labels
docs Documentation in the Doc dir

Comments

@TahomaSoft
Copy link
Contributor
TahomaSoft commented Feb 12, 2024

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 from datetime to time.struct_time.

For example, the command sequence:

now =  datetime.now(timezone.utc).isoformat()
print (now)

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

@hugovk
Copy link
Member
hugovk commented Feb 12, 2024

So your use case is something like this, using strptime?

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 %f:

>>> 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 %f:

>>> 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, %f doesn't seem to be available as microseconds for strftime?

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 %f always available for strptime? Is it never available for strftime?

The table where %f is added in #115316 applies to both strptime and strftime, so a note is needed to clarify.

@TahomaSoft
Copy link
Contributor Author

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.

@TahomaSoft
Copy link
Contributor Author

Hugo, you are right.

Would adding a new note 4 to that chart work, where note 4 reads something like:
"The %f format directive only applies to time.strptime, not to time.strftime. However, see also datetime.strptime and datetime.strftime."

Thank you!

@hugovk
Copy link
Member
hugovk commented Feb 12, 2024

Yes, something like that sounds good, please can you update the PR?

@TahomaSoft
Copy link
Contributor Author

Will do, thank you!

@TahomaSoft
Copy link
Contributor Author

PR updated with requested modifications.

hugovk added a commit that referenced this issue Feb 27, 2024
#115316)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 27, 2024
…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>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 27, 2024
…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>
hugovk added a commit that referenced this issue Feb 27, 2024
…in chart (GH-115316) (#115990)

Co-authored-by: Tahoma Software <bacon@tahoma.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
hugovk added a commit that referenced this issue Feb 27, 2024
…in chart (GH-115316) (#115991)

Co-authored-by: Tahoma Software <bacon@tahoma.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@hugovk hugovk closed this as completed Feb 27, 2024
@TahomaSoft
Copy link
Contributor Author
TahomaSoft commented Feb 27, 2024 via email

woodruffw pushed a commit to woodruffw-forks/cpython that referenced this issue Mar 4, 2024
…n chart (python#115316)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
…n chart (python#115316)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
…n chart (python#115316)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
Archived in project
Development

No branches or pull requests

2 participants
0