augur.curate.format_dates module

Format date fields to Augur-compatible formats.

If the provided --expected-date-formats represent incomplete dates then the incomplete dates are masked with β€˜XX’. For example, providing %Y will allow year only dates to be formatted as 2023-XX-XX.

augur.curate.format_dates.directive_is_included(potential_directives, date_format)

Checks if any of the directives in potential_directives is included in date_format string.

If an element within potential_directives is a tuple, then all directives within the tuple must be included in date_format.

Parameters:
  • potential_directives (set[tuple[str, ...]]) -- Set of potential directives to check

  • date_format (str) -- Date format string to check for directives

Returns:

Whether the provided date_format includes any of the potential_directives

Return type:

bool

>>> potential_directives = {('%y', '%b', '%d'), ('%y', '%B', '%d'), ('%y', '%m', '%d'),}
>>> directive_is_included(potential_directives, '%G-%V-%A')
False
>>> directive_is_included(potential_directives, '%y-%m')
False
>>> directive_is_included(potential_directives, '%%y-%m-%d')
False
>>> directive_is_included(potential_directives, '%y-%m-%d')
True
>>> directive_is_included(potential_directives, '%y-%m-%dT%H:%M:%SZ')
True
augur.curate.format_dates.format_date(date_string, expected_formats)

Format date_string to an Augur-compatible format.

>>> format_date("2020-01-15", BUILTIN_DATE_FORMATS)
'2020-01-15'
>>> format_date("[2001 TO 2002]", BUILTIN_DATE_FORMATS)
'2001-01-01/2002-12-31'
augur.curate.format_dates.format_to_iso_date(date_string, expected_formats)

Format date_string to ISO 8601 date (YYYY-MM-DD) by trying to parse it as one of the provided expected_formats.

Parameters:
  • date_string (str) -- Date string to format

  • expected_formats (list[str]) -- List of expected formats for the provided date string

Returns:

  • str or None -- Formatted date string or None if the parsing of the date string failed. If date_string is an incomplete date, the date is masked with β€˜XX’. Dates without year will be formatted as β€˜XXXX-XX-XX’, even if month/day are known. Dates without month will be formatted as β€˜YYYY-XX-XX’, even if day is known. Dates without day will be formatted as β€˜YYYY-MM-XX’.

  • >>> format_to_iso_date(β€œβ€, BUILTIN_DATE_FORMATS)

  • ’XXXX-XX-XX’

  • >>> format_to_iso_date(” β€œ, BUILTIN_DATE_FORMATS)

  • ’XXXX-XX-XX’

  • >>> format_to_iso_date(β€œ01-01”, [β€˜%m-%d’])

  • ’XXXX-XX-XX’

  • >>> format_to_iso_date(β€œ2020”, [β€˜%Y’])

  • ’2020-XX-XX’

  • >>> format_to_iso_date(β€œ2020-01”, [β€˜%Y-%m’])

  • ’2020-01-XX’

  • >>> format_to_iso_date(β€œ2020-1-15”, [β€˜%Y-%m-%d’])

  • ’2020-01-15’

  • >>> format_to_iso_date(β€œ2020-1-1”, [β€˜%Y-%m-%d’])

  • ’2020-01-01’

  • >>> format_to_iso_date(β€œ2020-01-15”, BUILTIN_DATE_FORMATS)

  • ’2020-01-15’

  • >>> format_to_iso_date(β€œ2020-01-15T00 (00:00Z”, [β€˜%Y-%m-%dT%H:%M:%SZ’]))

  • ’2020-01-15’

  • >>> format_to_iso_date(β€œ[2001 TO 2002]”, BUILTIN_DATE_FORMATS) is None

  • True

augur.curate.format_dates.format_to_iso_interval(date_string)

Format date_string to an ISO 8601 interval, compatible with RE_DATE_RANGE in augur.dates.

>>> format_to_iso_interval("[2001 TO 2002]")
'2001-01-01/2002-12-31'
>>> format_to_iso_interval("2001-01-01/2002-12-31")
'2001-01-01/2002-12-31'
augur.curate.format_dates.register_parser(parent_subparsers)
augur.curate.format_dates.run(args, records)