Skip to content

gh-137758: Clarify os.stat_result time fields (since Unix epoch) and add datetime conversion note#137761

Open
tangyuan0821 wants to merge 9 commits into
python:mainfrom
tangyuan0821:main2
Open

gh-137758: Clarify os.stat_result time fields (since Unix epoch) and add datetime conversion note#137761
tangyuan0821 wants to merge 9 commits into
python:mainfrom
tangyuan0821:main2

Conversation

@tangyuan0821

@tangyuan0821 tangyuan0821 commented Aug 14, 2025

Copy link
Copy Markdown
Contributor

tangyuan0821 and others added 2 commits August 14, 2025 20:26
… the Unix epoch

Clarify that `os.stat_result` time fields (`st_atime`, `st_mtime`, `st_ctime`,
`st_birthtime`) and their `*_ns` variants are measured since the Unix epoch
(00:00:00 UTC, 1970-01-01). Add a short note showing how to convert these
timestamps to `datetime` using `datetime.datetime.fromtimestamp`, including a
UTC example. Documentation-only; no behavior change.

Files touched: Doc/library/os.rst

Co-authored-by: tangyuan0821 <tangyuan0821@hotmail.com>
…indicate that it is the number of seconds since the Unix epoch.
Comment thread Doc/library/os.rst Outdated

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation has still not been fixed. In the preview you can see the rendered attributes with proper indentation (top of image), and the ones with wrong indentation:

image

@StanFromIreland

Copy link
Copy Markdown
Member

Please do not use the Update Branch button unless necessary (e.g. fixing conflicts, jogging the CI, or very old PRs) as it uses valuable resources. For more information see the devguide.

@tangyuan0821

This comment was marked as outdated.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 28, 2026
@tangyuan0821

Copy link
Copy Markdown
Contributor Author

@StanFromIreland The indentation fix is in. Mind taking another look?

Comment thread Doc/library/os.rst Outdated

Time of most recent access expressed in seconds.
Time of most recent access expressed in seconds since the Unix epoch
(see the :mod:`time` module).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these suggestions are helpful. It sends a user to the top of the time documentation, and while there are functions that deal with timestamps, the majority don't.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Will remove those.

Comment thread Doc/library/os.rst

.. note::

These timestamps are seconds (or nanoseconds for the ``*_ns`` variants)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optionally, we can add an anchor to this note and make "seconds since the Unix epoch" above link here.

Comment thread Doc/library/os.rst Outdated
:class:`datetime.datetime`, use
:func:`datetime.datetime.fromtimestamp` for local time or
``datetime.datetime.fromtimestamp(ts, tz=datetime.timezone.utc)`` for
UTC. The function :func:`datetime.datetime.utcfromtimestamp` can also be

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't recommend deprecated functions please.

@read-the-docs-community

read-the-docs-community Bot commented Jun 29, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33352980 | 📁 Comparing 7805c8f against main (7aae0e5)

  🔍 Preview build  

4 files changed
± library/numbers.html
± library/os.html
± library/tkinter.html
± whatsnew/changelog.html

Comment thread Doc/library/os.rst Outdated
.. attribute:: st_atime

Time of most recent access expressed in seconds.
Time of most recent access expressed in :ref:`seconds since the Unix epoch <stat-result-timestamps>`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap these lines.

- Remove unhelpful (see the :mod:`time` module) cross-references
- Add .. _stat-result-timestamps: anchor and use :ref: from attributes
- Remove deprecated utcfromtimestamp recommendation
Comment thread Doc/library/os.rst

These timestamps are seconds (or nanoseconds for the ``*_ns`` variants)
since the Unix epoch (00:00:00 UTC, January 1, 1970), and are compatible
with :func:`time.time`. To convert a timestamp ``ts`` to a

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this won't work for nanosecond timestamps.

Comment thread Doc/library/os.rst

Time of most recent metadata change expressed in nanoseconds as an
integer.
Time of most recent metadata change expressed in nanoseconds since the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also link to the note here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge docs Documentation in the Doc dir skip news stale Stale PR or inactive for long period of time.

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants