1919
2020class Timestamp (object ):
2121 """MongoDB internal timestamps used in the opLog.
22+ """
2223
23- This class is only for use with the MongoDB opLog. If you need to
24- store a regular timestamp, please use a
25- :class:`datetime.datetime`.
24+ def __init__ (self , time , inc ):
25+ """Create a new :class:`Timestamp`.
2626
27- Raises : class:`TypeError` if `time` and `inc` are not instances of
28- :class:`int`. Raises :class:`ValueError` if `time` or `inc` is not
29- in [0, 2**32) .
27+ This class is only for use with the MongoDB opLog. If you need
28+ to store a regular timestamp, please use a
29+ :class:`~datetime.datetime` .
3030
31- :Parameters:
32- - `time`: time in seconds since epoch UTC
33- - `inc`: the incrementing counter
34- """
31+ Raises :class:`TypeError` if `time` and `inc` are not
32+ instances of :class:`int`. Raises :class:`ValueError` if
33+ `time` or `inc` is not in [0, 2**32).
3534
36- def __init__ (self , time , inc ):
35+ :Parameters:
36+ - `time`: time in seconds since epoch UTC
37+ - `inc`: the incrementing counter
38+ """
3739 if not isinstance (time , int ):
3840 raise TypeError ("time must be an instance of int" )
3941 if not isinstance (inc , int ):
@@ -43,17 +45,32 @@ def __init__(self, time, inc):
4345 if not 0 <= inc < 2 ** 32 :
4446 raise ValueError ("inc must be contained in [0, 2**32)" )
4547
46- self .time = time
47- self .inc = inc
48+ self .__time = time
49+ self .__inc = inc
50+
51+ @property
52+ def time (self ):
53+ """Get the time portion of this :class:`Timestamp`.
54+ """
55+ return self .__time
56+
57+ @property
58+ def inc (self ):
59+ """Get the inc portion of this :class:`Timestamp`.
60+ """
61+ return self .__inc
4862
4963 def __eq__ (self , other ):
5064 if isinstance (other , Timestamp ):
51- return (self .time == other .time and self .inc == other .inc )
65+ return (self .__time == other .time and self .__inc == other .inc )
5266 else :
5367 return NotImplemented
5468
5569 def __repr__ (self ):
56- return "Timestamp(%s, %s)" % (self .time , self .inc )
70+ return "Timestamp(%s, %s)" % (self .__time , self .__inc )
5771
5872 def as_datetime (self ):
59- return datetime .datetime .utcfromtimestamp (self .time )
73+ """Return a :class:`~datetime.datetime` instance corresponding
74+ to the time portion of this :class:`Timestamp`.
75+ """
76+ return datetime .datetime .utcfromtimestamp (self .__time )
0 commit comments