Datetime reference
Friendly interface for datetime manipulations.
Classes
timeless.datetime.Datetime(year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0, zone: str = 'UTC') -> None
Bases: _datetime
Timeless datetime.
Init a Timeless Datetime.
Parameters:
-
year
(
int
) –description
-
month
(
int
) –description
-
day
(
int
) –description
-
hour
(
int, optional
) –description, by default 0
-
minute
(
int, optional
) –description, by default 0
-
second
(
int, optional
) –description, by default 0
-
microsecond
(
int, optional
) –description, by default 0
-
zone
(
str, optional
) –description, by default "UTC"
Source code in timeless/datetime.py
Functions
__iter__() -> Iterator[int]
Allow iteration over the instance.
Yields:
-
Iterator[int]
–Instance values.
Source code in timeless/datetime.py
__new__(year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0, zone: str = 'UTC') -> Datetime
Control the instance creation.
Source code in timeless/datetime.py
add(years: int = 0, months: int = 0, days: int = 0, hours: int = 0, minutes: int = 0, seconds: int = 0, microseconds: int = 0) -> Datetime
Add duration to the instance.
Returns:
-
Datetime
–New datetime instance with added value.
Source code in timeless/datetime.py
days_in_month() -> int
property
Get the number of days in the month.
Returns:
-
int
–Total days in the instance month.
diff(other: Datetime) -> relativedelta.relativedelta
Get the difference between the instance and another.
Parameters:
-
other
(
Datetime
) –Other datetime instance to compare to.
Returns:
-
relativedelta
–Delta between the two instances.
Source code in timeless/datetime.py
format(format: Optional[str] = None) -> str
Format the instance as a string to isoformat or custom format.
Parameters:
-
format
(
Optional[str], optional
) –Follows the same rules as the python strftime, by default None
Returns:
-
str
–Datetime formated string.
Source code in timeless/datetime.py
get_days_in_month() -> int
Equivalent function of days_in_month property.
Returns:
-
int
–Total days in the instance month.
get_last(weekday: str) -> Datetime
Get the last instance of a given weekday.
Returns:
-
Datetime
–Last closest given weekday.
Source code in timeless/datetime.py
get_month_end() -> Datetime
Get a Datetime instance on the last day of the month.
Returns:
-
Datetime
–Kast day of the month
Source code in timeless/datetime.py
get_month_start() -> Datetime
Get a Datetime instance on the first day of the month.
Only for semantic purposes.
Returns:
-
Datetime
–First day of the month
Source code in timeless/datetime.py
get_next(weekday: str) -> Datetime
Get the next instance of a given weekday.
Does't consider the current day.
Returns:
-
Datetime
–Next closest given weekday.
Source code in timeless/datetime.py
get_utc_offset() -> int
Get UTC offset in hours.
Source code in timeless/datetime.py
get_weekday_name(week_start: Optional[str] = None) -> str
Get the weekday name of the instance.
Parameters:
-
week_start
(
Optional[str], optional
) –First day of the week, by default None (monday)
Returns:
-
str
–[description]
Source code in timeless/datetime.py
is_future() -> bool
Check if the instance is in the future (within the same timezone).
Returns:
-
bool
–is the instance in the future (relative to now).
is_leap() -> bool
is_past() -> bool
Check if the instance is in the past (within the same timezone).
Returns:
-
bool
–is the instance in the past (relative to now).
is_today(weekday: str, week_start: Optional[str] = None) -> bool
Check if the instance weekday is at a given weekday.
Parameters:
-
weekday
(
str
) –Weekday to check against.
-
week_start
(
Optional[str], optional
) –First day of the week, by default None (monday).
Returns:
-
bool
–True if the instance weekday is at the given weekday.
Source code in timeless/datetime.py
set(year: Optional[int] = None, month: Optional[int] = None, day: Optional[int] = None, hour: Optional[int] = None, minute: Optional[int] = None, second: Optional[int] = None, microsecond: Optional[int] = None, zone: Optional[str] = 'UTC') -> Datetime
Override the instance values.
Parameters:
-
year
(
Optional[int], optional
) –new year value, by default None
-
month
(
Optional[int], optional
) –new month value, by default None
-
day
(
Optional[int], optional
) –new day value, by default None
-
hour
(
Optional[int], optional
) –new hour value, by default None
-
minute
(
Optional[int], optional
) –new minute value, by default None
-
second
(
Optional[int], optional
) –new second value, by default None
-
microsecond
(
Optional[int], optional
) –new microsecond value, by default None
-
zone
(
Optional[Union[str, ZoneInfo]], optional
) –new timezone value
Returns:
-
Datetime
–New instance with the new values.
Source code in timeless/datetime.py
set_utc() -> Datetime
set_zero() -> Datetime
subtract(years: int = 0, months: int = 0, days: int = 0, hours: int = 0, minutes: int = 0, seconds: int = 0, microseconds: int = 0) -> Datetime
Remove duration to the instance.
Returns:
-
Datetime
–New datetime instance with the subtracted value.
Source code in timeless/datetime.py
timeless.datetime.Weekdays
dataclass
Weekdays mapping for easy acess.
Functions
timeless.datetime.get_first_weekday_in_month(datetime: Datetime, weekday: str, week_start: Optional[str] = None) -> Datetime
Get the first occourance of a weekday at the instance month.
Parameters:
-
datetime
(
Datetime
) –Datetime instance.
-
weekday
(
str
) –weekday name.
-
week_start
(
Optional[str], optional
) –week start day, by default None (monday)
Returns:
-
Datetime
–First occourance of the given weekday at the instance month.
Source code in timeless/datetime.py
timeless.datetime.now(zone: str = 'UTC', microseconds: bool = False) -> Datetime
Get a DateTime instance for the current date and time.
Parameters:
-
zone
(
Optional[str], optional
) –Instance timezone, by default "UTC"
Returns:
-
Datetime
–Current date and time.
Source code in timeless/datetime.py
timeless.datetime.parse(string: str, format: Optional[str] = None, zone: Optional[str] = None, day_first: bool = False, year_first: bool = False) -> Datetime
Parse a string into a Datetime.
If no format is provided, the string is parsed using Dateutil's parser. Otherwise, the string is parsed using strptime. In the latter case dateutil's parser arguments (nominally day_first, year_first) are ignored.
The "ignoretz" parameter is not supported, since "zone" can override the timezone value and Timeless does not accept naive datetimes.
See https://dateutil.readthedocs.io/en/stable/parser.html for more information on dautil's parser arguments.
Parameters:
-
string
(
str
) –datetime string to parse.
-
format
(
Optional[str]
) –dateutil format string, by default None.
-
zone
(
Optional[str]
) –timezone name (overrides parsed value), by default None.
-
day_first
(
bool, optional
) –Whether to interpret the first value in an ambiguous 3-integer date as the day, by default False
-
year_first
(
bool, optional
) –Whether to interpret the first value in an ambiguous 3-integer date as the year, by default False.
Returns:
-
Datetime
–Parsed datetime.
Source code in timeless/datetime.py
timeless.datetime.today(zone: str = 'UTC') -> Datetime
Get a DateTime instance for the current date.
Hours, minutes, seconds and microseconds are set to 0.
Parameters:
-
zone
(
str, optional
) –Instance timezone, by default "UTC"
Returns:
-
Datetime
–Current date.