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]
- class cli_command_parser.inputs.time.DTFormatMode(*values)[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
Input type representing a date/time unit.
- Parameters:
full¶ – Allow the full unit name to be provided
abbreviation¶ – Allow abbreviations of unit names to be provided
numeric¶ – Allow unit values to be specified as a decimal number
locale¶ – An alternate locale to use when parsing input
out_format¶ – A
DTFormatMode
or str that matches a format mode. Defaults to full weekday name.out_locale¶ – Alternate locale to use for output. Defaults to the same value as
locale
.fix_default¶ – Whether default values should be normalized using
fix_default()
.
- full
- abbreviation
- numeric
- out_format
- out_locale
- 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
Input type representing a day of the week.
- Parameters:
full¶ – Allow the full day name to be provided
abbreviation¶ – Allow abbreviations of day names to be provided
numeric¶ – Allow weekdays to be specified as a decimal number
iso¶ – Ignored if
numeric
is False. If True, then numeric weekdays are treated as ISO 8601 weekdays, where 1 is Monday and 7 is Sunday. If False, then 0 is Monday and 6 is Sunday.locale¶ – An alternate locale to use when parsing input
out_format¶ – A
DTFormatMode
or str that matches a format mode. Defaults to full weekday name.out_locale¶ – Alternate locale to use for output. Defaults to the same value as
locale
.fix_default¶ – Whether default values should be normalized using
fix_default()
.
- iso
- 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
Input type representing a month.
- Parameters:
full¶ – Allow the full month name to be provided
abbreviation¶ – Allow abbreviations of month names to be provided
numeric¶ – Allow months to be specified as a decimal number
locale¶ – An alternate locale to use when parsing input
out_format¶ – A
DTFormatMode
or str that matches a format mode. Defaults to full month name.out_locale¶ – Alternate locale to use for output. Defaults to the same value as
locale
.fix_default¶ – Whether default values should be normalized using
fix_default()
.
- class cli_command_parser.inputs.time.TimeDelta(unit: Literal['microseconds', 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks'], *, min: NT | None = None, max: NT | None = None, include_min: bool | Any = True, include_max: bool | Any = False, int_only: bool | Any = False, fix_default: bool | Any = True)[source]
Bases:
RangeMixin
,InputType
[timedelta
]- unit
- min: Number
- max: Number
- int_only
- 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]
-
- formats: Collection[str]
- 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
]Input type that accepts any number of datetime format strings for parsing input. Parsing results in returning a
datetime.datetime
object.- Parameters:
formats¶ – One or more datetime format strings. Defaults to
DEFAULT_DT_FMT
.locale¶ – An alternate locale to use when parsing input
earliest¶ – If specified, the parsed value must be later than or equal to this
latest¶ – If specified, the parsed value must be earlier than or equal to this
fix_default¶ – Whether default values should be normalized using
fix_default()
.
- 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
]Input type that accepts any number of datetime format strings for parsing input. Parsing results in returning a
datetime.date
object.- Parameters:
formats¶ – One or more datetime format strings. Defaults to
DEFAULT_DT_FMT
.locale¶ – An alternate locale to use when parsing input
earliest¶ – If specified, the parsed value must be later than or equal to this
latest¶ – If specified, the parsed value must be earlier than or equal to this
fix_default¶ – Whether default values should be normalized using
fix_default()
.
- 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
]Input type that accepts any number of datetime format strings for parsing input. Parsing results in returning a
datetime.time
object.- Parameters:
formats¶ – One or more datetime format strings. Defaults to
DEFAULT_DT_FMT
.locale¶ – An alternate locale to use when parsing input
earliest¶ – If specified, the parsed value must be later than or equal to this
latest¶ – If specified, the parsed value must be earlier than or equal to this
fix_default¶ – Whether default values should be normalized using
fix_default()
.