Skip to content

Commit 04694c5

Browse files
author
Mike Dirolf
committed
more docs for timestamp module
1 parent 5371b62 commit 04694c5

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

doc/api/pymongo/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ Sub-modules:
3030
message
3131
son
3232
son_manipulator
33+
timestamp
3334
json_util
3435
cursor_manager

doc/api/pymongo/timestamp.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:mod:`timestamp` -- Tools for representing MongoDB internal Timestamps
2+
======================================================================
3+
.. versionadded:: 1.4+
4+
5+
.. automodule:: pymongo.timestamp
6+
:synopsis: Tools for representing MongoDB internal Timestamps
7+
:members:

pymongo/timestamp.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@
1919

2020
class 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

Comments
 (0)