Support datetime. (#394)

This commit is contained in:
Inada Naoki 2019-12-11 23:48:16 +09:00 committed by GitHub
parent 5fd6119093
commit 2186455d15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 222 additions and 24 deletions

View file

@ -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())