/usr/lib/python3/dist-packages/persistent/timestamp.py is in python3-persistent 4.1.1-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | ##############################################################################
#
# Copyright (c) 2011 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
__all__ = ('TimeStamp',)
from ctypes import c_long
import datetime
import math
import struct
import sys
_RAWTYPE = bytes
def _makeOctets(s):
if sys.version_info < (3,):
return bytes(s)
return bytes(s, 'ascii') #pragma NO COVERAGE
_ZERO = _makeOctets('\x00' * 8)
class _UTC(datetime.tzinfo):
def tzname(self):
return 'UTC'
def utcoffset(self, when):
return datetime.timedelta(0, 0, 0)
def dst(self):
return 0
def fromutc(self, dt):
return dt
def _makeUTC(y, mo, d, h, mi, s):
usec, sec = math.modf(s)
sec = int(sec)
usec = int(usec * 1e6)
return datetime.datetime(y, mo, d, h, mi, sec, usec, tzinfo=_UTC())
_EPOCH = _makeUTC(1970, 1, 1, 0, 0, 0)
_SCONV = 60.0 / (1<<16) / (1<<16)
def _makeRaw(year, month, day, hour, minute, second):
a = (((year - 1900) * 12 + month - 1) * 31 + day - 1)
a = (a * 24 + hour) * 60 + minute
b = int(second / _SCONV) # Don't round() this; the C version does simple truncation
return struct.pack('>II', a, b)
def _parseRaw(octets):
a, b = struct.unpack('>II', octets)
minute = a % 60
hour = a // 60 % 24
day = a // (60 * 24) % 31 + 1
month = a // (60 * 24 * 31) % 12 + 1
year = a // (60 * 24 * 31 * 12) + 1900
second = round(b * _SCONV, 6) #microsecond precision
return (year, month, day, hour, minute, second)
class pyTimeStamp(object):
__slots__ = ('_raw', '_elements')
def __init__(self, *args):
if len(args) == 1:
raw = args[0]
if not isinstance(raw, _RAWTYPE):
raise TypeError('Raw octets must be of type: %s' % _RAWTYPE)
if len(raw) != 8:
raise TypeError('Raw must be 8 octets')
self._raw = raw
self._elements = _parseRaw(raw)
elif len(args) == 6:
self._raw = _makeRaw(*args)
self._elements = args
else:
raise TypeError('Pass either a single 8-octet arg '
'or 5 integers and a float')
def raw(self):
return self._raw
def __repr__(self):
return repr(self._raw)
def __str__(self):
return "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%09.6f" % (
self.year(), self.month(), self.day(),
self.hour(), self.minute(),
self.second())
def year(self):
return self._elements[0]
def month(self):
return self._elements[1]
def day(self):
return self._elements[2]
def hour(self):
return self._elements[3]
def minute(self):
return self._elements[4]
def second(self):
return self._elements[5]
def timeTime(self):
""" -> seconds since epoch, as a float.
"""
delta = _makeUTC(*self._elements) - _EPOCH
return delta.days * 86400.0 + delta.seconds
def laterThan(self, other):
""" Return a timestamp instance which is later than 'other'.
If self already qualifies, return self.
Otherwise, return a new instance one moment later than 'other'.
"""
if not isinstance(other, self.__class__):
raise ValueError()
if self._raw > other._raw:
return self
a, b = struct.unpack('>II', other._raw)
later = struct.pack('>II', a, b + 1)
return self.__class__(later)
def __eq__(self, other):
try:
return self.raw() == other.raw()
except AttributeError:
return NotImplemented
def __ne__(self, other):
try:
return self.raw() != other.raw()
except AttributeError:
return NotImplemented
def __hash__(self):
# Match the C implementation
a = bytearray(self._raw)
x = a[0] << 7
for i in a:
x = (1000003 * x) ^ i
x ^= 8
# Make sure to overflow and wraparound just
# like the C code does.
x = c_long(x).value
if x == -1: #pragma: no cover
# The C version has this condition, but it's not clear
# why; it's also not immediately obvious what bytestring
# would generate this---hence the no-cover
x = -2
return x
# Now the rest of the comparison operators
# Sigh. Python 2.6 doesn't have functools.total_ordering
# so we have to do it by hand
def __lt__(self, other):
try:
return self.raw() < other.raw()
except AttributeError:
return NotImplemented
def __gt__(self, other):
try:
return self.raw() > other.raw()
except AttributeError:
return NotImplemented
def __le__(self, other):
try:
return self.raw() <= other.raw()
except AttributeError:
return NotImplemented
def __ge__(self, other):
try:
return self.raw() >= other.raw()
except AttributeError:
return NotImplemented
try:
from persistent._timestamp import TimeStamp
except ImportError: #pragma NO COVER
TimeStamp = pyTimeStamp
|