bff.parse_date

bff.parse_date(func=None, date_fields='date')

Cast str date into datetime format.

This decorator casts string arguments of a function to datetime.datetime type. This allows to specify either string of datetime format for a function argument. The name of the parameters to cast must be specified in the date_fields.

The cast is done using the parse function from the dateutil package. All supported format are those from the library and may evolve.

In order to use the decorator both with or without parenthesis when calling it without parameter, the date_fields argument is keyword only. This allows checking if the parameter was given or not.

Parameters
  • func (Callable) – Function with the arguments to parse.

  • date_fields (Sequence of str, default 'date') – Sequence containing the fields with dates.

Returns

Function with the date fields cast to datetime.datetime type.

Return type

Callable

Examples

>>> @parse_date
... def dummy_function(**kwargs):
...     print(f'Args {kwargs}')
...
>>> dummy_function(date='20190325')
Args {'date': datetime.datetime(2019, 3, 25, 0, 0)}
>>> dummy_function(date='Mon, 21 March, 2015')
Args {'date': datetime.datetime(2015, 3, 21, 0, 0)}
>>> dummy_function(date='2019-03-09 08:03:00')
Args {'date': datetime.datetime(2019, 3, 9, 8, 3)}
>>> dummy_function(date='March 27 2019')
Args {'date': datetime.datetime(2019, 3, 27, 0, 0)}
>>> dummy_function(date='wrong string')
Value `wrong string` for field `date` is not convertible to a date format.
Args {'date': 'wrong string'}