Skip to content

Commit ead4f3f

Browse files
committed
gh-137758: Clarify os.stat_result time fields are seconds since 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>
1 parent 2a6888e commit ead4f3f

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

Doc/library/os.rst

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3177,15 +3177,18 @@ features:
31773177

31783178
.. attribute:: st_atime
31793179

3180-
Time of most recent access expressed in seconds.
3180+
Time of most recent access expressed in seconds since the epoch
3181+
(see the :mod:`time` module).
31813182

31823183
.. attribute:: st_mtime
31833184

3184-
Time of most recent content modification expressed in seconds.
3185+
Time of most recent content modification expressed in seconds since
3186+
the epoch (see the :mod:`time` module).
31853187

31863188
.. attribute:: st_ctime
31873189

3188-
Time of most recent metadata change expressed in seconds.
3190+
Time of most recent metadata change expressed in seconds since the
3191+
epoch (see the :mod:`time` module).
31893192

31903193
.. versionchanged:: 3.12
31913194
``st_ctime`` is deprecated on Windows. Use ``st_birthtime`` for
@@ -3194,21 +3197,22 @@ features:
31943197

31953198
.. attribute:: st_atime_ns
31963199

3197-
Time of most recent access expressed in nanoseconds as an integer.
3200+
Time of most recent access expressed in nanoseconds since the epoch
3201+
as an integer.
31983202

31993203
.. versionadded:: 3.3
32003204

32013205
.. attribute:: st_mtime_ns
32023206

3203-
Time of most recent content modification expressed in nanoseconds as an
3204-
integer.
3207+
Time of most recent content modification expressed in nanoseconds since
3208+
the epoch as an integer.
32053209

32063210
.. versionadded:: 3.3
32073211

32083212
.. attribute:: st_ctime_ns
32093213

3210-
Time of most recent metadata change expressed in nanoseconds as an
3211-
integer.
3214+
Time of most recent metadata change expressed in nanoseconds since the
3215+
epoch as an integer.
32123216

32133217
.. versionadded:: 3.3
32143218

@@ -3219,16 +3223,17 @@ features:
32193223

32203224
.. attribute:: st_birthtime
32213225

3222-
Time of file creation expressed in seconds. This attribute is not
3223-
always available, and may raise :exc:`AttributeError`.
3226+
Time of file creation expressed in seconds since the epoch
3227+
(see the :mod:`time` module). This attribute is not always available,
3228+
and may raise :exc:`AttributeError`.
32243229

32253230
.. versionchanged:: 3.12
32263231
``st_birthtime`` is now available on Windows.
32273232

32283233
.. attribute:: st_birthtime_ns
32293234

3230-
Time of file creation expressed in nanoseconds as an integer.
3231-
This attribute is not always available, and may raise
3235+
Time of file creation expressed in nanoseconds since the epoch as an
3236+
integer. This attribute is not always available, and may raise
32323237
:exc:`AttributeError`.
32333238

32343239
.. versionadded:: 3.12
@@ -3252,6 +3257,17 @@ features:
32523257
:attr:`st_atime_ns`, :attr:`st_mtime_ns`, :attr:`st_ctime_ns` and
32533258
:attr:`st_birthtime_ns`.
32543259

3260+
.. note::
3261+
3262+
These timestamps are seconds (or nanoseconds for the ``*_ns`` variants)
3263+
since the Unix epoch (00:00:00 UTC, January 1, 1970), and are compatible
3264+
with :func:`time.time`. To convert a timestamp ``ts`` to a
3265+
:class:`datetime.datetime`, use
3266+
:func:`datetime.datetime.fromtimestamp` for local time or
3267+
``datetime.datetime.fromtimestamp(ts, tz=datetime.timezone.utc)`` for
3268+
UTC. The function :func:`datetime.datetime.utcfromtimestamp` can also be
3269+
used to get a naive UTC datetime.
3270+
32553271
On some Unix systems (such as Linux), the following attributes may also be
32563272
available:
32573273

0 commit comments

Comments
 (0)