mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
allow pything dates and times (#122)
* allow pything dates and times * update history
This commit is contained in:
@@ -3,6 +3,10 @@
|
||||
History
|
||||
-------
|
||||
|
||||
v0.6.4 (2018-02-01)
|
||||
...................
|
||||
* allow python date and times objects #122
|
||||
|
||||
v0.6.3 (2017-11-26)
|
||||
...................
|
||||
* fix direct install without ``README.rst`` present
|
||||
|
||||
@@ -80,13 +80,16 @@ def from_unix_seconds(seconds: int) -> datetime:
|
||||
return dt.replace(tzinfo=timezone.utc)
|
||||
|
||||
|
||||
def parse_date(value: StrIntFloat) -> date:
|
||||
def parse_date(value: Union[date, StrIntFloat]) -> date:
|
||||
"""
|
||||
Parse a string and return a datetime.date.
|
||||
|
||||
Raise ValueError if the input is well formatted but not a valid date.
|
||||
Raise ValueError if the input isn't well formatted.
|
||||
"""
|
||||
if isinstance(value, date):
|
||||
return value
|
||||
|
||||
number = get_numeric(value)
|
||||
if number:
|
||||
return from_unix_seconds(number).date()
|
||||
@@ -99,7 +102,7 @@ def parse_date(value: StrIntFloat) -> date:
|
||||
return date(**kw)
|
||||
|
||||
|
||||
def parse_time(value: StrIntFloat) -> time:
|
||||
def parse_time(value: Union[time, StrIntFloat]) -> time:
|
||||
"""
|
||||
Parse a string and return a datetime.time.
|
||||
|
||||
@@ -108,6 +111,9 @@ def parse_time(value: StrIntFloat) -> time:
|
||||
Raise ValueError if the input is well formatted but not a valid time.
|
||||
Raise ValueError if the input isn't well formatted, in particular if it contains an offset.
|
||||
"""
|
||||
if isinstance(value, time):
|
||||
return value
|
||||
|
||||
match = time_re.match(value)
|
||||
if not match:
|
||||
raise ValueError('Invalid time format')
|
||||
@@ -119,7 +125,7 @@ def parse_time(value: StrIntFloat) -> time:
|
||||
return time(**kw)
|
||||
|
||||
|
||||
def parse_datetime(value: StrIntFloat) -> datetime:
|
||||
def parse_datetime(value: Union[datetime, StrIntFloat]) -> datetime:
|
||||
"""
|
||||
Parse a string and return a datetime.datetime.
|
||||
|
||||
|
||||
+1
-1
@@ -2,4 +2,4 @@ from distutils.version import StrictVersion
|
||||
|
||||
__all__ = ['VERSION']
|
||||
|
||||
VERSION = StrictVersion('0.6.3')
|
||||
VERSION = StrictVersion('0.6.4')
|
||||
|
||||
@@ -25,6 +25,7 @@ def create_tz(minutes):
|
||||
(1494012444, date(2017, 5, 5)),
|
||||
('2012-04-23', date(2012, 4, 23)),
|
||||
('2012-4-9', date(2012, 4, 9)),
|
||||
(date(2012, 4, 9), date(2012, 4, 9)),
|
||||
# Invalid inputs
|
||||
('x20120423', ValueError),
|
||||
('2012-04-56', ValueError),
|
||||
@@ -43,6 +44,7 @@ def test_date_parsing(value, result):
|
||||
('10:10', time(10, 10)),
|
||||
('10:20:30.400', time(10, 20, 30, 400000)),
|
||||
('4:8:16', time(4, 8, 16)),
|
||||
(time(4, 8, 16), time(4, 8, 16)),
|
||||
# Invalid inputs
|
||||
('091500', ValueError),
|
||||
('09:15:90', ValueError),
|
||||
|
||||
Reference in New Issue
Block a user