xtquant.xtbson.bson36.timestamp

Tools for representing MongoDB internal Timestamps.

  1# Copyright 2010-2015 MongoDB, Inc.
  2#
  3# Licensed under the Apache License, Version 2.0 (the "License");
  4# you may not use this file except in compliance with the License.
  5# You may obtain a copy of the License at
  6#
  7# http://www.apache.org/licenses/LICENSE-2.0
  8#
  9# Unless required by applicable law or agreed to in writing, software
 10# distributed under the License is distributed on an "AS IS" BASIS,
 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12# See the License for the specific language governing permissions and
 13# limitations under the License.
 14
 15"""Tools for representing MongoDB internal Timestamps.
 16"""
 17
 18import calendar
 19import datetime
 20
 21from ._helpers import _getstate_slots, _setstate_slots
 22from .tz_util import utc
 23
 24UPPERBOUND = 4294967296
 25
 26
 27class Timestamp(object):
 28    """MongoDB internal timestamps used in the opLog."""
 29
 30    __slots__ = ("__time", "__inc")
 31
 32    __getstate__ = _getstate_slots
 33    __setstate__ = _setstate_slots
 34
 35    _type_marker = 17
 36
 37    def __init__(self, time, inc):
 38        """Create a new :class:`Timestamp`.
 39
 40        This class is only for use with the MongoDB opLog. If you need
 41        to store a regular timestamp, please use a
 42        :class:`~datetime.datetime`.
 43
 44        Raises :class:`TypeError` if `time` is not an instance of
 45        :class: `int` or :class:`~datetime.datetime`, or `inc` is not
 46        an instance of :class:`int`. Raises :class:`ValueError` if
 47        `time` or `inc` is not in [0, 2**32).
 48
 49        :Parameters:
 50          - `time`: time in seconds since epoch UTC, or a naive UTC
 51            :class:`~datetime.datetime`, or an aware
 52            :class:`~datetime.datetime`
 53          - `inc`: the incrementing counter
 54        """
 55        if isinstance(time, datetime.datetime):
 56            if time.utcoffset() is not None:
 57                time = time - time.utcoffset()
 58            time = int(calendar.timegm(time.timetuple()))
 59        if not isinstance(time, int):
 60            raise TypeError("time must be an instance of int")
 61        if not isinstance(inc, int):
 62            raise TypeError("inc must be an instance of int")
 63        if not 0 <= time < UPPERBOUND:
 64            raise ValueError("time must be contained in [0, 2**32)")
 65        if not 0 <= inc < UPPERBOUND:
 66            raise ValueError("inc must be contained in [0, 2**32)")
 67
 68        self.__time = time
 69        self.__inc = inc
 70
 71    @property
 72    def time(self):
 73        """Get the time portion of this :class:`Timestamp`."""
 74        return self.__time
 75
 76    @property
 77    def inc(self):
 78        """Get the inc portion of this :class:`Timestamp`."""
 79        return self.__inc
 80
 81    def __eq__(self, other):
 82        if isinstance(other, Timestamp):
 83            return self.__time == other.time and self.__inc == other.inc
 84        else:
 85            return NotImplemented
 86
 87    def __hash__(self):
 88        return hash(self.time) ^ hash(self.inc)
 89
 90    def __ne__(self, other):
 91        return not self == other
 92
 93    def __lt__(self, other):
 94        if isinstance(other, Timestamp):
 95            return (self.time, self.inc) < (other.time, other.inc)
 96        return NotImplemented
 97
 98    def __le__(self, other):
 99        if isinstance(other, Timestamp):
100            return (self.time, self.inc) <= (other.time, other.inc)
101        return NotImplemented
102
103    def __gt__(self, other):
104        if isinstance(other, Timestamp):
105            return (self.time, self.inc) > (other.time, other.inc)
106        return NotImplemented
107
108    def __ge__(self, other):
109        if isinstance(other, Timestamp):
110            return (self.time, self.inc) >= (other.time, other.inc)
111        return NotImplemented
112
113    def __repr__(self):
114        return "Timestamp(%s, %s)" % (self.__time, self.__inc)
115
116    def as_datetime(self):
117        """Return a :class:`~datetime.datetime` instance corresponding
118        to the time portion of this :class:`Timestamp`.
119
120        The returned datetime's timezone is UTC.
121        """
122        return datetime.datetime.fromtimestamp(self.__time, utc)
UPPERBOUND = 4294967296
class Timestamp:
 28class Timestamp(object):
 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, inc):
 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        :Parameters:
 51          - `time`: time in seconds since epoch UTC, or a naive UTC
 52            :class:`~datetime.datetime`, or an aware
 53            :class:`~datetime.datetime`
 54          - `inc`: the incrementing counter
 55        """
 56        if isinstance(time, datetime.datetime):
 57            if time.utcoffset() is not None:
 58                time = time - time.utcoffset()
 59            time = int(calendar.timegm(time.timetuple()))
 60        if not isinstance(time, int):
 61            raise TypeError("time must be an instance of int")
 62        if not isinstance(inc, int):
 63            raise TypeError("inc must be an instance of int")
 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):
 74        """Get the time portion of this :class:`Timestamp`."""
 75        return self.__time
 76
 77    @property
 78    def inc(self):
 79        """Get the inc portion of this :class:`Timestamp`."""
 80        return self.__inc
 81
 82    def __eq__(self, other):
 83        if isinstance(other, Timestamp):
 84            return self.__time == other.time and self.__inc == other.inc
 85        else:
 86            return NotImplemented
 87
 88    def __hash__(self):
 89        return hash(self.time) ^ hash(self.inc)
 90
 91    def __ne__(self, other):
 92        return not self == other
 93
 94    def __lt__(self, other):
 95        if isinstance(other, Timestamp):
 96            return (self.time, self.inc) < (other.time, other.inc)
 97        return NotImplemented
 98
 99    def __le__(self, other):
100        if isinstance(other, Timestamp):
101            return (self.time, self.inc) <= (other.time, other.inc)
102        return NotImplemented
103
104    def __gt__(self, other):
105        if isinstance(other, Timestamp):
106            return (self.time, self.inc) > (other.time, other.inc)
107        return NotImplemented
108
109    def __ge__(self, other):
110        if isinstance(other, Timestamp):
111            return (self.time, self.inc) >= (other.time, other.inc)
112        return NotImplemented
113
114    def __repr__(self):
115        return "Timestamp(%s, %s)" % (self.__time, self.__inc)
116
117    def as_datetime(self):
118        """Return a :class:`~datetime.datetime` instance corresponding
119        to the time portion of this :class:`Timestamp`.
120
121        The returned datetime's timezone is UTC.
122        """
123        return datetime.datetime.fromtimestamp(self.__time, utc)

MongoDB internal timestamps used in the opLog.

Timestamp(time, inc)
38    def __init__(self, time, inc):
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        :Parameters:
51          - `time`: time in seconds since epoch UTC, or a naive UTC
52            :class:`~datetime.datetime`, or an aware
53            :class:`~datetime.datetime`
54          - `inc`: the incrementing counter
55        """
56        if isinstance(time, datetime.datetime):
57            if time.utcoffset() is not None:
58                time = time - time.utcoffset()
59            time = int(calendar.timegm(time.timetuple()))
60        if not isinstance(time, int):
61            raise TypeError("time must be an instance of int")
62        if not isinstance(inc, int):
63            raise TypeError("inc must be an instance of int")
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

Create a new Timestamp.

This class is only for use with the MongoDB opLog. If you need to store a regular timestamp, please use a ~datetime.datetime.

Raises TypeError if time is not an instance of :class: int or ~datetime.datetime, or inc is not an instance of int. Raises ValueError if time or inc is not in [0, 2**32).

:Parameters:

  • time: time in seconds since epoch UTC, or a naive UTC ~datetime.datetime, or an aware ~datetime.datetime
  • inc: the incrementing counter
time

Get the time portion of this Timestamp.

inc

Get the inc portion of this Timestamp.

def as_datetime(self):
117    def as_datetime(self):
118        """Return a :class:`~datetime.datetime` instance corresponding
119        to the time portion of this :class:`Timestamp`.
120
121        The returned datetime's timezone is UTC.
122        """
123        return datetime.datetime.fromtimestamp(self.__time, utc)

Return a ~datetime.datetime instance corresponding to the time portion of this Timestamp.

The returned datetime's timezone is UTC.