mirror of
https://github.com/kennethreitz/maya.git
synced 2026-06-05 23:00:18 +00:00
Merge pull request #20 from jerry2yu/master
Add datafirst parameter to parse method. Set datafirst true to parse …
This commit is contained in:
@@ -196,13 +196,14 @@ def when(string, timezone='UTC'):
|
||||
|
||||
return MayaDT.from_datetime(dt)
|
||||
|
||||
def parse(string):
|
||||
def parse(string, dayfirst=False):
|
||||
""""Returns a MayaDT instance for the machine-produced moment specified.
|
||||
|
||||
Powered by dateutil. Accepts most known formats. Useful for working with data.
|
||||
|
||||
Keyword Arguments:
|
||||
string -- string to be parsed
|
||||
dayfirst -- if true, the first value in date string (e.g. 01/05/2016) is parsed as day; otherwise it is parsed as month (default: False)
|
||||
"""
|
||||
dt = dateutil.parser.parse(string)
|
||||
dt = dateutil.parser.parse(string, dayfirst=dayfirst)
|
||||
return MayaDT.from_datetime(dt)
|
||||
|
||||
@@ -83,4 +83,10 @@ def test_format():
|
||||
d = maya.parse('February 21, 1994')
|
||||
assert format(d) == '1994-02-21 00:00:00+00:00'
|
||||
|
||||
d = maya.parse('01/05/2016')
|
||||
assert format(d) == '2016-01-05 00:00:00+00:00'
|
||||
|
||||
d = maya.parse('01/05/2016', dayfirst=True)
|
||||
assert format(d) == '2016-05-01 00:00:00+00:00'
|
||||
|
||||
# rand_day = maya.when('2011-02-07', timezone='US/Eastern')
|
||||
|
||||
Reference in New Issue
Block a user