1
- # coding: utf-8
2
1
from collections import namedtuple
3
2
import datetime
4
- import sys
5
3
import struct
6
4
7
5
8
- PY2 = sys .version_info [0 ] == 2
9
-
10
- if PY2 :
11
- int_types = (int , long )
12
- _utc = None
13
- else :
14
- int_types = int
15
- try :
16
- _utc = datetime .timezone .utc
17
- except AttributeError :
18
- _utc = datetime .timezone (datetime .timedelta (0 ))
19
-
20
-
21
6
class ExtType (namedtuple ("ExtType" , "code data" )):
22
7
"""ExtType represents ext type in msgpack."""
23
8
@@ -28,14 +13,15 @@ def __new__(cls, code, data):
28
13
raise TypeError ("data must be bytes" )
29
14
if not 0 <= code <= 127 :
30
15
raise ValueError ("code must be 0~127" )
31
- return super (ExtType , cls ).__new__ (cls , code , data )
16
+ return super ().__new__ (cls , code , data )
32
17
33
18
34
- class Timestamp ( object ) :
19
+ class Timestamp :
35
20
"""Timestamp represents the Timestamp extension type in msgpack.
36
21
37
- When built with Cython, msgpack uses C methods to pack and unpack `Timestamp`. When using pure-Python
38
- msgpack, :func:`to_bytes` and :func:`from_bytes` are used to pack and unpack `Timestamp`.
22
+ When built with Cython, msgpack uses C methods to pack and unpack `Timestamp`.
23
+ When using pure-Python msgpack, :func:`to_bytes` and :func:`from_bytes` are used to pack and
24
+ unpack `Timestamp`.
39
25
40
26
This class is immutable: Do not override seconds and nanoseconds.
41
27
"""
@@ -53,31 +39,25 @@ def __init__(self, seconds, nanoseconds=0):
53
39
Number of nanoseconds to add to `seconds` to get fractional time.
54
40
Maximum is 999_999_999. Default is 0.
55
41
56
- Note: Negative times (before the UNIX epoch) are represented as negative seconds + positive ns.
42
+ Note: Negative times (before the UNIX epoch) are represented as neg. seconds + pos. ns.
57
43
"""
58
- if not isinstance (seconds , int_types ):
44
+ if not isinstance (seconds , int ):
59
45
raise TypeError ("seconds must be an integer" )
60
- if not isinstance (nanoseconds , int_types ):
46
+ if not isinstance (nanoseconds , int ):
61
47
raise TypeError ("nanoseconds must be an integer" )
62
48
if not (0 <= nanoseconds < 10 ** 9 ):
63
- raise ValueError (
64
- "nanoseconds must be a non-negative integer less than 999999999."
65
- )
49
+ raise ValueError ("nanoseconds must be a non-negative integer less than 999999999." )
66
50
self .seconds = seconds
67
51
self .nanoseconds = nanoseconds
68
52
69
53
def __repr__ (self ):
70
54
"""String representation of Timestamp."""
71
- return "Timestamp(seconds={0}, nanoseconds={1})" .format (
72
- self .seconds , self .nanoseconds
73
- )
55
+ return f"Timestamp(seconds={ self .seconds } , nanoseconds={ self .nanoseconds } )"
74
56
75
57
def __eq__ (self , other ):
76
58
"""Check for equality with another Timestamp object"""
77
59
if type (other ) is self .__class__ :
78
- return (
79
- self .seconds == other .seconds and self .nanoseconds == other .nanoseconds
80
- )
60
+ return self .seconds == other .seconds and self .nanoseconds == other .nanoseconds
81
61
return False
82
62
83
63
def __ne__ (self , other ):
@@ -140,7 +120,7 @@ def from_unix(unix_sec):
140
120
"""Create a Timestamp from posix timestamp in seconds.
141
121
142
122
:param unix_float: Posix timestamp in seconds.
143
- :type unix_float: int or float.
123
+ :type unix_float: int or float
144
124
"""
145
125
seconds = int (unix_sec // 1 )
146
126
nanoseconds = int ((unix_sec % 1 ) * 10 ** 9 )
@@ -174,20 +154,15 @@ def to_unix_nano(self):
174
154
def to_datetime (self ):
175
155
"""Get the timestamp as a UTC datetime.
176
156
177
- Python 2 is not supported.
178
-
179
- :rtype: datetime.
157
+ :rtype: `datetime.datetime`
180
158
"""
181
- return datetime .datetime .fromtimestamp (0 , _utc ) + datetime .timedelta (
182
- seconds = self .to_unix ()
183
- )
159
+ utc = datetime .timezone .utc
160
+ return datetime .datetime .fromtimestamp (0 , utc ) + datetime .timedelta (seconds = self .to_unix ())
184
161
185
162
@staticmethod
186
163
def from_datetime (dt ):
187
164
"""Create a Timestamp from datetime with tzinfo.
188
165
189
- Python 2 is not supported.
190
-
191
166
:rtype: Timestamp
192
167
"""
193
168
return Timestamp .from_unix (dt .timestamp ())
0 commit comments