mirror of
https://github.com/kennethreitz/maya.git
synced 2026-06-05 14:50:19 +00:00
Merge branch 'master' into mayadt_subtraction
This commit is contained in:
+1
-1
@@ -23,4 +23,4 @@ In chronological order:
|
||||
- Sébastien Eustace <sebastien@eustace.io> (`@sdispater <https://github.com/sdispater>`_)
|
||||
- Evan Mattiza <emattiza@gmail.com> (`@emattiza <https://github.com/emattiza>`_)
|
||||
- Dima Spivak <dima@spivak.ch> (`@dimaspivak <https://github.com/dimaspivak>`_)
|
||||
- Tom Barron <tusculum@gmail.com> (`@tbarron <https://github.com/tbarron>`_)
|
||||
- Tom Barron <tusculum@gmail.com> (`@dtbarron <https://github.com/tbarron>`_)
|
||||
|
||||
+17
-3
@@ -183,12 +183,12 @@ class MayaDT(object):
|
||||
def from_datetime(klass, dt):
|
||||
"""Returns MayaDT instance from datetime."""
|
||||
return klass(klass.__dt_to_epoch(dt))
|
||||
|
||||
|
||||
@classmethod
|
||||
@validate_arguments_type_of_function(time.struct_time)
|
||||
def from_struct(klass, struct, timezone=pytz.UTC):
|
||||
"""Returns MayaDT instance from a 9-tuple struct"""
|
||||
struct_time = time.mktime(struct)
|
||||
"""Returns MayaDT instance from a 9-tuple struct (assumed to be from gmtime())"""
|
||||
struct_time = time.mktime(struct) - utc_offset()
|
||||
dt = Datetime.fromtimestamp(struct_time, timezone)
|
||||
return klass(klass.__dt_to_epoch(dt))
|
||||
|
||||
@@ -261,6 +261,10 @@ class MayaDT(object):
|
||||
@property
|
||||
def day(self):
|
||||
return self.datetime().day
|
||||
|
||||
@property
|
||||
def date(self):
|
||||
return self.datetime().date()
|
||||
|
||||
@property
|
||||
def week(self):
|
||||
@@ -305,6 +309,16 @@ class MayaDT(object):
|
||||
return humanize.naturaltime(dt)
|
||||
|
||||
|
||||
def utc_offset():
|
||||
"""Returns the current local time offset from UTC accounting for DST """
|
||||
ts = time.localtime()
|
||||
if ts[-1]:
|
||||
offset = time.altzone
|
||||
else:
|
||||
offset = time.timezone
|
||||
return offset
|
||||
|
||||
|
||||
def to_utc_offset_naive(dt):
|
||||
if dt.tzinfo is None:
|
||||
return dt
|
||||
|
||||
+15
-4
@@ -67,19 +67,30 @@ def test_parse_iso8601():
|
||||
|
||||
|
||||
def test_struct():
|
||||
ts = time.gmtime()
|
||||
now = round(time.time())
|
||||
ts = time.gmtime(now)
|
||||
m = maya.MayaDT.from_struct(ts)
|
||||
dt = Datetime.fromtimestamp(time.mktime(ts), pytz.UTC)
|
||||
dt = Datetime.fromtimestamp(now, pytz.UTC)
|
||||
assert m._epoch != None
|
||||
assert m.datetime() == dt
|
||||
|
||||
ts = time.localtime()
|
||||
ts = time.localtime(now)
|
||||
m = maya.MayaDT.from_struct(ts)
|
||||
dt = Datetime.fromtimestamp(time.mktime(ts), pytz.UTC)
|
||||
dt = Datetime.fromtimestamp(time.mktime(ts) - maya.core.utc_offset(), pytz.UTC)
|
||||
assert m._epoch != None
|
||||
assert m.datetime() == dt
|
||||
|
||||
|
||||
def test_issue_104():
|
||||
e = 1507756331
|
||||
t = Datetime.utcfromtimestamp(e)
|
||||
t = maya.MayaDT.from_datetime(t)
|
||||
assert str(t) == 'Wed, 11 Oct 2017 21:12:11 GMT'
|
||||
t = time.gmtime(e)
|
||||
t = maya.MayaDT.from_struct(t)
|
||||
assert str(t) == 'Wed, 11 Oct 2017 21:12:11 GMT'
|
||||
|
||||
|
||||
def test_human_when():
|
||||
r1 = maya.when('yesterday')
|
||||
r2 = maya.when('today')
|
||||
|
||||
Reference in New Issue
Block a user