Time Module

Custom date/time input handlers for Parameters.

Warning

Uses locale.setlocale() if alternate locales are specified, which may cause problems on some systems. Using alternate locales in this manner should not be used in a multi-threaded application, as it will lead to unexpected output from other parts of the program.

If you need to handle multiple locales and this is a problem for your application, then you should leave the locale parameters empty / None and use a proper i18n library like babel for localization.

author:

Doug Skrypa

class cli_command_parser.inputs.time.different_locale(locale: str | Tuple[str | None, str | None] | None)[source]

Bases: object

Context manager that allows the temporary use of an alternate locale for date/time parsing/formatting.

Not using python:calendar.different_locale because it results in incorrect string encoding for some locales (at least on Windows).

locale
original
class cli_command_parser.inputs.time.DTInput(locale: str | Tuple[str | None, str | None] = None, fix_default: bool | Any = True)[source]

Bases: InputType[T], ABC

dt_type: str
classmethod __init_subclass__(dt_type: str = None, **kwargs)[source]
Parameters:

dt_type – Used in InvalidChoiceError / ValueError messages

locale: str | Tuple[str | None, str | None] | None
abstract choice_str(choice_delim: str = ',', sort_choices: bool = False) str[source]
fix_default(value: str | T | None) T | None[source]
class cli_command_parser.inputs.time.DTFormatMode(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: MissingMixin, Enum

FULL = 'full'

The full name of a given date/time unit

ABBREVIATION = 'abbreviation'

The abbreviation of a given date/time unit

NUMERIC = 'numeric'

The numeric representation of a given date/time unit

NUMERIC_ISO = 'numeric_iso'

The ISO numeric representation of a given date/time unit

class cli_command_parser.inputs.time.CalendarUnitInput(*, full: bool | Any = True, abbreviation: bool | Any = True, numeric: bool | Any = False, locale: str | Tuple[str | None, str | None] = None, out_format: str | DTFormatMode = DTFormatMode.FULL, out_locale: str | Tuple[str | None, str | None] = None, fix_default: bool | Any = True)[source]

Bases: DTInput[str | int], ABC

classmethod __init_subclass__(min_index: int = 0, **kwargs)[source]
full
abbreviation
numeric
out_format
out_locale
choices(sort: bool = False) Sequence[str][source]
choice_str(choice_delim: str = ',', sort_choices: bool = False) str[source]
format_metavar(choice_delim: str = ',', sort_choices: bool = False) str[source]
abstract parse_numeric(value: str) int[source]
parse(value: str) int[source]

Parse the date/time unit from the given value.

This method does not use python:datetime.strptime() because it is not accurate for standalone numeric weekdays.

Parameters:

value – The value provided as input

Returns:

The numeric unit value

__call__(value: str) str | int[source]
dt_type: str = None
class cli_command_parser.inputs.time.Day(*, full: bool | Any = True, abbreviation: bool | Any = True, numeric: bool | Any = False, iso: bool | Any = False, locale: str | Tuple[str | None, str | None] = None, out_format: str | DTFormatMode = DTFormatMode.FULL, out_locale: str | Tuple[str | None, str | None] = None, fix_default: bool | Any = True)[source]

Bases: CalendarUnitInput

iso
parse_numeric(value: str) int[source]
dt_type: str = 'day of the week'
class cli_command_parser.inputs.time.Month(*, full: bool | Any = True, abbreviation: bool | Any = True, numeric: bool | Any = True, locale: str | Tuple[str | None, str | None] = None, out_format: str | DTFormatMode = DTFormatMode.FULL, out_locale: str | Tuple[str | None, str | None] = None, fix_default: bool | Any = True)[source]

Bases: CalendarUnitInput

parse_numeric(value: str) int[source]
dt_type: str = 'month'
class cli_command_parser.inputs.time.TimeDelta(unit: Literal['days', 'seconds', 'microseconds', 'milliseconds', 'minutes', 'hours', 'weeks'], fix_default: bool | Any = True)[source]

Bases: InputType[timedelta]

unit
__call__(value: str | int | float) timedelta[source]
fix_default(value: int | float | timedelta | None) timedelta | None[source]
format_metavar(choice_delim: str = ',', sort_choices: bool = False) str[source]
class cli_command_parser.inputs.time.DateTimeInput(formats: Collection[str], locale: str | Tuple[str | None, str | None] = None, earliest: datetime | date | time | timedelta | None = None, latest: datetime | date | time | timedelta | None = None, fix_default: bool | Any = True)[source]

Bases: DTInput[DT], ABC

classmethod __init_subclass__(type: Type[DT], **kwargs)[source]
formats: Collection[str]
property earliest: DT | None[source]
property latest: DT | None[source]
parse_dt(value: str) datetime[source]
parse(value: str) DT[source]
choice_str(choice_delim: str = ' | ', sort_choices: bool = False) str[source]
format_metavar(choice_delim: str = ' | ', sort_choices: bool = False) str[source]
__call__(value: str) DT[source]
locale: str | Tuple[str | None, str | None] | None
dt_type: str = None
class cli_command_parser.inputs.time.DateTime(*formats: str, locale: str | Tuple[str | None, str | None] = None, earliest: datetime | date | time | timedelta | None = None, latest: datetime | date | time | timedelta | None = None, fix_default: bool | Any = True)[source]

Bases: DateTimeInput[datetime]

locale: str | Tuple[str | None, str | None] | None
dt_type: str = 'datetime'
class cli_command_parser.inputs.time.Date(*formats: str, locale: str | Tuple[str | None, str | None] = None, earliest: datetime | date | time | timedelta | None = None, latest: datetime | date | time | timedelta | None = None, fix_default: bool | Any = True)[source]

Bases: DateTimeInput[date]

locale: str | Tuple[str | None, str | None] | None
dt_type: str = 'date'
class cli_command_parser.inputs.time.Time(*formats: str, locale: str | Tuple[str | None, str | None] = None, earliest: datetime | date | time | timedelta | None = None, latest: datetime | date | time | timedelta | None = None, fix_default: bool | Any = True)[source]

Bases: DateTimeInput[time]

locale: str | Tuple[str | None, str | None] | None
dt_type: str = 'time'
cli_command_parser.inputs.time.dt_repr(dt: datetime | date | time, use_repr: bool = True) str[source]
cli_command_parser.inputs.time.normalize_dt(value: datetime | date | time | timedelta | None, now: datetime = None) datetime | None[source]