forked from rc/aircox
!112 Co-authored-by: bkfox <thomas bkfox net> Reviewed-on: rc/aircox#113
This commit is contained in:
@ -4,7 +4,6 @@ import django.utils.timezone as tz
|
||||
__all__ = (
|
||||
"Redirect",
|
||||
"redirect",
|
||||
"date_range",
|
||||
"cast_date",
|
||||
"date_or_default",
|
||||
"to_timedelta",
|
||||
@ -29,31 +28,17 @@ def redirect(url):
|
||||
|
||||
|
||||
def str_to_date(value, sep="/"):
|
||||
"""Return a date from the provided `value` string, formated as "yyyy/mm/dd"
|
||||
(or "dd/mm/yyyy" if `reverse` is True).
|
||||
"""Return a date from the provided `value` string, formated as
|
||||
"yyyy/mm/dd".
|
||||
|
||||
Raises ValueError for incorrect value format.
|
||||
:raises: ValueError for incorrect value format.
|
||||
"""
|
||||
value = value.split(sep)[:3]
|
||||
if len(value) < 3:
|
||||
return ValueError("incorrect date format")
|
||||
raise ValueError("incorrect date format")
|
||||
return datetime.date(int(value[0]), int(value[1]), int(value[2]))
|
||||
|
||||
|
||||
def date_range(date, delta=None, **delta_kwargs):
|
||||
"""Return a range of provided date such as `[date-delta, date+delta]`.
|
||||
|
||||
:param date: the reference date
|
||||
:param delta: timedelta
|
||||
:param **delta_kwargs: timedelta init arguments
|
||||
|
||||
Return a datetime range for a given day, as:
|
||||
```(date, 0:0:0:0; date, 23:59:59:999)```.
|
||||
"""
|
||||
delta = tz.timedelta(**delta_kwargs) if delta is None else delta
|
||||
return [date - delta, date + delta]
|
||||
|
||||
|
||||
def cast_date(date, into=datetime.date):
|
||||
"""Cast a given date into the provided class' instance.
|
||||
|
||||
|
Reference in New Issue
Block a user