comparison 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
comparison
equal deleted inserted replaced
48259:84f6b0c41b90 48260:269ff8978086
1 # Copyright Mercurial Contributors
2 #
3 # This software may be used and distributed according to the terms of the
4 # GNU General Public License version 2 or any later version.
5
6 from __future__ import absolute_import
7
8 import stat
9
10
11 rangemask = 0x7FFFFFFF
12
13
14 class timestamp(tuple):
15 """
16 A Unix timestamp with nanoseconds precision,
17 modulo 2**31 seconds.
18
19 A 2-tuple containing:
20
21 `truncated_seconds`: seconds since the Unix epoch,
22 truncated to its lower 31 bits
23
24 `subsecond_nanoseconds`: number of nanoseconds since `truncated_seconds`.
25 """
26
27 def __new__(cls, value):
28 truncated_seconds, subsec_nanos = value
29 value = (truncated_seconds & rangemask, subsec_nanos)
30 return super(timestamp, cls).__new__(cls, value)
31
32
33 def zero():
34 """
35 Returns the `timestamp` at the Unix epoch.
36 """
37 return tuple.__new__(timestamp, (0, 0))
38
39
40 def mtime_of(stat_result):
41 """
42 Takes an `os.stat_result`-like object and returns a `timestamp` object
43 for its modification time.
44 """
45 # https://docs.python.org/2/library/os.html#os.stat_float_times
46 # "For compatibility with older Python versions,
47 # accessing stat_result as a tuple always returns integers."
48 secs = stat_result[stat.ST_MTIME]
49
50 # For now
51 subsec_nanos = 0
52
53 return timestamp((secs, subsec_nanos))