mirror of
https://github.com/kennethreitz/maya.git
synced 2026-06-05 23:00:18 +00:00
iso8601
This commit is contained in:
@@ -16,6 +16,7 @@ from datetime import datetime as Datetime
|
||||
import pytz
|
||||
import humanize
|
||||
import dateparser
|
||||
import iso8601
|
||||
|
||||
__epoch_start = (1970, 1, 1)
|
||||
|
||||
@@ -29,12 +30,23 @@ class MayaDT(object):
|
||||
def __repr__(self):
|
||||
return '<MayaDT epoch={}>'.format(self._epoch)
|
||||
|
||||
@staticmethod
|
||||
def __dt_to_epoch(dt):
|
||||
epoch_start = Datetime(*__epoch_start, tzinfo=pytz.timezone('UTC'))
|
||||
return (dt - epoch_start).total_seconds()
|
||||
|
||||
@classmethod
|
||||
def from_datetime(klass, dt):
|
||||
return klass(klass.__dt_to_epoch(dt))
|
||||
|
||||
def datetime(self, to_timezone=None):
|
||||
"""Returns a timezone-aware datetime...
|
||||
Defaulting to UTC (as it should).
|
||||
|
||||
# self.timezone.localize(dt)
|
||||
|
||||
Keyword Arguments:
|
||||
to_timezone {string} -- timezone to convert to (default: {None/UTC})
|
||||
"""
|
||||
if to_timezone:
|
||||
|
||||
return self.datetime().astimezone(pytz.timezone(to_timezone))
|
||||
|
||||
dt = Datetime.utcfromtimestamp(self._epoch)
|
||||
@@ -102,7 +114,8 @@ def when(string, timezone='UTC'):
|
||||
if dt is None:
|
||||
raise ValueError('invalid datetime input specified.')
|
||||
|
||||
epoch_start = Datetime(*__epoch_start, tzinfo=pytz.timezone('UTC'))
|
||||
epoch = (dt - epoch_start).total_seconds()
|
||||
return MayaDT.from_datetime(dt)
|
||||
|
||||
return MayaDT(epoch)
|
||||
def from_iso8601(string):
|
||||
dt = iso8601.parse_date(string)
|
||||
return MayaDT.from_datetime(dt)
|
||||
@@ -1,5 +1,6 @@
|
||||
dateparser==0.5.0
|
||||
humanize==0.5.1
|
||||
iso8601==0.1.11
|
||||
jdatetime==1.8.1
|
||||
python-dateutil==2.6.0
|
||||
pytz==2016.10
|
||||
|
||||
Reference in New Issue
Block a user