Skip to content

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

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

tangyuan0821
Copy link

@tangyuan0821 tangyuan0821 commented Aug 14, 2025

Summary:

  • 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 on converting these timestamps to datetime using datetime.fromtimestamp() and UTC example.
  • Documentation-only change; no behavior change.

Linked issue:

Changes:

  • Doc/library/os.rst
    • Update wording for:
      • st_atime, st_mtime, st_ctime → “seconds since the epoch”.
      • st_atime_ns, st_mtime_ns, st_ctime_ns → “nanoseconds since the epoch”.
      • st_birthtime, st_birthtime_ns → explicitly reference the epoch.
    • Add a note: timestamps are since the Unix epoch and compatible with time.time; include datetime conversion examples.

📚 Documentation preview 📚: https://cpython-previews--137761.org.readthedocs.build/

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.
@@ -3177,15 +3177,18 @@ features:

.. attribute:: st_atime

Time of most recent access expressed in seconds.
Time of most recent access expressed in seconds since the Unix epoch
Copy link
Member

Choose a reason for hiding this comment

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

Hello, please correct the indentation, the lines should start in the column of the start of the directive name, here that is the "a"

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your reminder!

Copy link
Member

@StanFromIreland StanFromIreland left a comment

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
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
Copy link
Author

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.

Sorry. Thanks again for your reminder. I'll re-read the devguide carefully. In addition, the previously requested changes have been fixed. You can review it again.

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
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

os.stat_result time fields: seconds since when?
3 participants