mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-20 12:23:16 +00:00
Support datetime. (#394)
This commit is contained in:
parent
5fd6119093
commit
2186455d15
8 changed files with 222 additions and 24 deletions
|
@ -1,12 +1,18 @@
|
|||
# coding: utf-8
|
||||
from collections import namedtuple
|
||||
import datetime
|
||||
import sys
|
||||
import struct
|
||||
|
||||
|
||||
PY2 = sys.version_info[0] == 2
|
||||
|
||||
if not PY2:
|
||||
long = int
|
||||
try:
|
||||
_utc = datetime.timezone.utc
|
||||
except AttributeError:
|
||||
_utc = datetime.timezone(datetime.timedelta(0))
|
||||
|
||||
|
||||
class ExtType(namedtuple("ExtType", "code data")):
|
||||
|
@ -131,7 +137,7 @@ class Timestamp(object):
|
|||
data = struct.pack("!Iq", self.nanoseconds, self.seconds)
|
||||
return data
|
||||
|
||||
def to_float_s(self):
|
||||
def to_float(self):
|
||||
"""Get the timestamp as a floating-point value.
|
||||
|
||||
:returns: posix timestamp
|
||||
|
@ -139,6 +145,12 @@ class Timestamp(object):
|
|||
"""
|
||||
return self.seconds + self.nanoseconds / 1e9
|
||||
|
||||
@staticmethod
|
||||
def from_float(unix_float):
|
||||
seconds = int(unix_float)
|
||||
nanoseconds = int((unix_float % 1) * 1000000000)
|
||||
return Timestamp(seconds, nanoseconds)
|
||||
|
||||
def to_unix_ns(self):
|
||||
"""Get the timestamp as a unixtime in nanoseconds.
|
||||
|
||||
|
@ -146,3 +158,16 @@ class Timestamp(object):
|
|||
:rtype: int
|
||||
"""
|
||||
return int(self.seconds * 1e9 + self.nanoseconds)
|
||||
|
||||
if not PY2:
|
||||
|
||||
def to_datetime(self):
|
||||
"""Get the timestamp as a UTC datetime.
|
||||
|
||||
:rtype: datetime.
|
||||
"""
|
||||
return datetime.datetime.fromtimestamp(self.to_float(), _utc)
|
||||
|
||||
@staticmethod
|
||||
def from_datetime(dt):
|
||||
return Timestamp.from_float(dt.timestamp())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue