phenopacket_mapper.data_standards.date module

class phenopacket_mapper.data_standards.date.Date(year: int = 0, month: int = 0, day: int = 0, hour: int = 0, minute: int = 0, second: int = 0)[source]

Bases: object

Data class for Date

This class defines a date object with many useful utility functions, especially for conversions from and to specific string formats.

Variables:
  • year – the year of the date

  • month – the month of the date

  • day – the day of the date

  • hour – the hour of the date

  • minute – the minute of the date

  • second – the second of the date

year: int
month: int
day: int
hour: int
minute: int
second: int
year_str: str
month_str: str
day_str: str
hour_str: str
minute_str: str
second_str: str
iso_8601_datestring(allow_zeros: bool = True) str[source]

Returns the date in ISO 8601 format

Example: “2021-06-02T16:52:15Z” Format: “{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z” Definition: The format for this is “{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z” where {year} is always expressed using four digits while {month}, {day}, {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution), are optional. The “Z” suffix indicates the timezone (“UTC”); the timezone is required.

protobuf_timestamp() Timestamp[source]

Returns the date in a Google Protobuf Timestamp object

Returns:

the date in a Google Protobuf Timestamp object

formatted_string(fmt: str) str[source]

Returns the date in the specified format

Parameters:

fmt – the format as a string to return the date in

Returns:

the date in the specified format

static from_datetime(dt: datetime) Date[source]

Create a Date object from a datetime object

Parameters:

dt – the datetime object to create the Date object from

Returns:

the Date object created from the datetime object

static from_iso_8601(iso_8601: str) Date | None[source]

Create a Date object from an ISO 8601 formatted string

Parameters:

iso_8601 – the ISO 8601 formatted string to create the Date object from

Returns:

the Date object created from the ISO 8601 formatted string

static parse_date(date_str: str, default_first: Literal['day', 'month'] = 'day', compliance: Literal['lenient', 'strict'] = 'lenient') Date[source]

Parse a date string into a Date object

There is a lot of variation in how dates are formatted, and this function attempts to handle as many of them as possible. The function will first attempt to parse the date string as an ISO 8601 formatted string. If that fails, it will attempt to parse the date string as a date string with separators.

In this process it is sometimes unknowable whether 01-02-2024 is January 2nd or February 1st, so the function will use the default_first parameter to determine this. If the default_first parameter is set to “day”, the function will assume that the day comes first, and if it is set to “month”, the function will assume that the month comes first. If the default_first

Parameters:
  • date_str – the date string to parse

  • default_first – the default unit to use if it is unclear which unit comes first between day and month

  • compliance – the compliance level of the parser

Returns:

the Date object created from the date string