MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / Timestamp

Class Timestamp

bson/timestamp.py:28–123  ·  view source on GitHub ↗

MongoDB internal timestamps used in the opLog.

Source from the content-addressed store, hash-verified

26
27
28class Timestamp:
29 """MongoDB internal timestamps used in the opLog."""
30
31 __slots__ = ("__time", "__inc")
32
33 __getstate__ = _getstate_slots
34 __setstate__ = _setstate_slots
35
36 _type_marker = 17
37
38 def __init__(self, time: Union[datetime.datetime, int], inc: int) -> None:
39 """Create a new :class:`Timestamp`.
40
41 This class is only for use with the MongoDB opLog. If you need
42 to store a regular timestamp, please use a
43 :class:`~datetime.datetime`.
44
45 Raises :class:`TypeError` if `time` is not an instance of
46 :class: `int` or :class:`~datetime.datetime`, or `inc` is not
47 an instance of :class:`int`. Raises :class:`ValueError` if
48 `time` or `inc` is not in [0, 2**32).
49
50 :param time: time in seconds since epoch UTC, or a naive UTC
51 :class:`~datetime.datetime`, or an aware
52 :class:`~datetime.datetime`
53 :param inc: the incrementing counter
54 """
55 if isinstance(time, datetime.datetime):
56 offset = time.utcoffset()
57 if offset is not None:
58 time = time - offset
59 time = int(calendar.timegm(time.timetuple()))
60 if not isinstance(time, int):
61 raise TypeError(f"time must be an instance of int, not {type(time)}")
62 if not isinstance(inc, int):
63 raise TypeError(f"inc must be an instance of int, not {type(inc)}")
64 if not 0 <= time < UPPERBOUND:
65 raise ValueError("time must be contained in [0, 2**32)")
66 if not 0 <= inc < UPPERBOUND:
67 raise ValueError("inc must be contained in [0, 2**32)")
68
69 self.__time = time
70 self.__inc = inc
71
72 @property
73 def time(self) -> int:
74 """Get the time portion of this :class:`Timestamp`."""
75 return self.__time
76
77 @property
78 def inc(self) -> int:
79 """Get the inc portion of this :class:`Timestamp`."""
80 return self.__inc
81
82 def __eq__(self, other: Any) -> bool:
83 if isinstance(other, Timestamp):
84 return self.__time == other.time and self.__inc == other.inc
85 else:

Callers 15

test_timestampMethod · 0.90
test_data_timestampMethod · 0.90
send_cluster_timeMethod · 0.90
test_timestampMethod · 0.90
test_datetimeMethod · 0.90
test_exceptionsMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_timestampMethod · 0.72
test_data_timestampMethod · 0.72
send_cluster_timeMethod · 0.72
test_timestampMethod · 0.72
test_datetimeMethod · 0.72
test_exceptionsMethod · 0.72