diff mercurial/dirstateutils/timestamp.py @ 48260:269ff8978086

dirstate: store mtimes with nanosecond precision in memory Keep integer seconds since the Unix epoch, together with integer nanoseconds in the `0 <= n < 1e9` range. For now, nanoseconds are still always zero. This commit is about data structure changes. Differential Revision: https://phab.mercurial-scm.org/D11684
author Simon Sapin <simon.sapin@octobus.net>
date Mon, 18 Oct 2021 11:23:07 +0200
parents
children 68bb472aee9c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/dirstateutils/timestamp.py	Mon Oct 18 11:23:07 2021 +0200
@@ -0,0 +1,53 @@
+# Copyright Mercurial Contributors
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from __future__ import absolute_import
+
+import stat
+
+
+rangemask = 0x7FFFFFFF
+
+
+class timestamp(tuple):
+    """
+    A Unix timestamp with nanoseconds precision,
+    modulo 2**31 seconds.
+
+    A 2-tuple containing:
+
+    `truncated_seconds`: seconds since the Unix epoch,
+    truncated to its lower 31 bits
+
+    `subsecond_nanoseconds`: number of nanoseconds since `truncated_seconds`.
+    """
+
+    def __new__(cls, value):
+        truncated_seconds, subsec_nanos = value
+        value = (truncated_seconds & rangemask, subsec_nanos)
+        return super(timestamp, cls).__new__(cls, value)
+
+
+def zero():
+    """
+    Returns the `timestamp` at the Unix epoch.
+    """
+    return tuple.__new__(timestamp, (0, 0))
+
+
+def mtime_of(stat_result):
+    """
+    Takes an `os.stat_result`-like object and returns a `timestamp` object
+    for its modification time.
+    """
+    # https://docs.python.org/2/library/os.html#os.stat_float_times
+    # "For compatibility with older Python versions,
+    #  accessing stat_result as a tuple always returns integers."
+    secs = stat_result[stat.ST_MTIME]
+
+    # For now
+    subsec_nanos = 0
+
+    return timestamp((secs, subsec_nanos))