Timedateutils¶
Class¶
- class badidatetime._timedateutils.TimeDateUtils[source]¶
Bases:
BahaiCalendarThis class provides utility functionality to the datetime package. Its use is through a pre-instantiated object _td_utils.
- A(ttup, org, mod)[source]¶
Full weekday name.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The full weekday.
- Return type:
str
- B(ttup, org, mod)[source]¶
Full month name.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The full month name.
- Return type:
str
- C(ttup, org, mod)[source]¶
Century as a space-padded decimal number. (The year divided by 100 then truncated to an integer.)
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The century as a space-padded decimal number.
- Return type:
str
- D(ttup, org, mod)[source]¶
Date where year is without century. Equivalent to a localized %m/%d/%y.
Note
Return a locale dependent Badí’ short date. Badí’ long dates are converted to short dates first. This method does not take into account format extenders, in other words the - or : after the %. They should never show up in the locale.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The date where year is without century.
- Return type:
str
- DAYNAMES = ('Jalál', 'Jamál', 'Kamál', 'Fiḍāl', '`Idāl', 'Istijlāl', 'Istiqlāl')¶
The full day names.
- Type:
tuple
- DAYNAMES_ABV = ('Jal', 'Jam', 'Kam', 'Fiḍ', 'Idā', 'Isj', 'Isq')¶
The abbreviated day names.
- Type:
tuple
- DAYS_BEFORE_1ST_YEAR = 77¶
Keeps the Badí’ day count in par with the Gregorian day count for ordinals.
- Type:
int
- G(ttup, org, mod)[source]¶
ISO 8601 year with century as a zero-padded decimal number.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The ISO 8601 year with century as a zero-padded decimal number.
- Return type:
str
- H(ttup, org, mod)[source]¶
Hour (24-hour clock) as a decimal number. Either zero-padded if %H or not zero-padded if %-H.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The hour (24-hour clock) as a decimal number.
- Return type:
str
- I(ttup, org, mod)[source]¶
Hour (12-hour clock) as a zero-padded decimal number.
Note
If we assume that sunset was at 1800 hrs UTC then the Badí’ noon would be about 0600 hrs UTC the next morning. This changes on a daily bases because sunset changes and there is seldom exactly 24 hours between two sunsets.
1st we need to find sunset for the provided date and the day after. Subtract these two times and divide the results by 2 to determine noon. Then determine which side of noon the current Badí’ time is on.
Does a 12-hour clock make sense in a Badí’ time?
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The hour (12-hour clock) as a zero-padded decimal number.
- Return type:
str
- K(ttup, org, mod)[source]¶
Kull-i-Shay as a negative or positive decimal number.
Note
If the mod is not a : (colon) them return an empty string.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The Kull-i-Shay as a decimal number.
- Return type:
str
- M(ttup, org, mod)[source]¶
Minute as a zero-padded decimal number. Either zero-padded if %M or not zero-padded if %-M.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The minute as a zero-padded decimal number.
- Return type:
str
- MONTHNAMES = {0: 'Ayyám-i-Há', 1: 'Bahá', 2: 'Jalál', 3: 'Jamál', 4: "'Aẓamat", 5: 'Núr', 6: 'Raḥmat', 7: 'Kalimát', 8: 'Kamál', 9: "Asmá'", 10: "'Izzat", 11: 'Mashíyyat', 12: "'Ilm", 13: 'Qudrat', 14: 'Qawl', 15: 'Masá’il', 16: 'Sharaf', 17: 'Sulṭán', 18: 'Mulk', 19: "'Alá'"}¶
The full month names.
- Type:
dict
- MONTHNAMES_ABV = {0: 'Ayy', 1: 'Bah', 2: 'Jal', 3: 'Jam', 4: 'Aẓa', 5: 'Núr', 6: 'Raḥ', 7: 'Kal', 8: 'Kam', 9: 'Asm', 10: 'Izz', 11: 'Mas', 12: 'Ilm', 13: 'Qud', 14: 'Qaw', 15: 'Mas', 16: 'Sha', 17: 'Sul', 18: 'Mul', 19: 'Alá'}¶
The abbreviated month names.
- Type:
dict
- ORDINAL_1ST_YEAR_EPOCH = 1721501.260417¶
The first day that we start our count, Julian year 1, March 19th.
- Type:
float
- S(ttup, org, mod)[source]¶
Second as a decimal number. Either zero-padded if %S or not zero-padded if %-S.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The second as a decimal number.
- Return type:
str
- U(ttup, org, mod)[source]¶
Week number of the year (Jalál as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first `Idāl are considered to be in week 0.
Note
It makes no sense to start a week on different day in the Badí’ Calendar. So the %W format is the same as %U.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The week number of the year.
- Return type:
str
- V(ttup, org, mod)[source]¶
ISO 8601 week as a decimal number with Jalál as the first day of the week. Week 01 is the week containing the 4th of Bahá. Either zero-padded if %V or not zero-padded if %-V.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The ISO 8601 week as a decimal number.
- Return type:
str
- X(ttup, org, mod)[source]¶
Locale defined time representation.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The locale defined time representation.
- Return type:
str
- Y(ttup, org, mod)[source]¶
Year with century as a zero-padded decimal number.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The year with century as a decimal number.
- Return type:
str
- Z(ttup, org, mod)[source]¶
Time zone name (empty string if the object is naive).
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The time zone name.
- Return type:
str
- _BADI_MONTH_NUM_DAYS = [(1, 19), (2, 19), (3, 19), (4, 19), (5, 19), (6, 19), (7, 19), (8, 19), (9, 19), (10, 19), (11, 19), (12, 19), (13, 19), (14, 19), (15, 19), (16, 19), (17, 19), (18, 19), (0, 0), (19, 19)]¶
Provides a list of month number and the number of days in that month. Month 0 (zero) is in the 19th position and is modified depending on if a leap year.
- Type:
list
- _LONG_STRUCT_TM_ITEMS = 8¶
Length of the long form Badí’ date and time portion of the struct time.
- Type:
int
- _SHORT_STRUCT_TM_ITEMS = 6¶
Length of the short form Badí’ date and time portion of the struct time.
- Type:
int
- _VALID_FORMAT_CHRS = 'aAbBcCdDefGhHIjkKlmMnprSTuUVWxXyYzZ%'¶
A list of all the format characters.
- Type:
str
- __METHOD_LOOKUP = {'%': <function TimeDateUtils.percent>, 'A': <function TimeDateUtils.A>, 'B': <function TimeDateUtils.B>, 'C': <function TimeDateUtils.C>, 'D': <function TimeDateUtils.D>, 'G': <function TimeDateUtils.G>, 'H': <function TimeDateUtils.H>, 'I': <function TimeDateUtils.I>, 'K': <function TimeDateUtils.K>, 'M': <function TimeDateUtils.M>, 'S': <function TimeDateUtils.S>, 'T': <function TimeDateUtils.r>, 'U': <function TimeDateUtils.U>, 'V': <function TimeDateUtils.V>, 'W': <function TimeDateUtils.U>, 'X': <function TimeDateUtils.X>, 'Y': <function TimeDateUtils.Y>, 'Z': <function TimeDateUtils.Z>, 'a': <function TimeDateUtils.a>, 'b': <function TimeDateUtils.b>, 'c': <function TimeDateUtils.c>, 'd': <function TimeDateUtils.d>, 'e': <function TimeDateUtils.d>, 'f': <function TimeDateUtils.f>, 'h': <function TimeDateUtils.b>, 'j': <function TimeDateUtils.j>, 'k': <function TimeDateUtils.H>, 'l': <function TimeDateUtils.I>, 'm': <function TimeDateUtils.m>, 'n': <function TimeDateUtils.n>, 'p': <function TimeDateUtils.p>, 'r': <function TimeDateUtils.r>, 'u': <function TimeDateUtils.u>, 'w': <function TimeDateUtils.u>, 'x': <function TimeDateUtils.x>, 'y': <function TimeDateUtils.y>, 'z': <function TimeDateUtils.z>}¶
An internal list of the format methods.
- Type:
dict
- _build_struct_time(date: tuple, dstflag: int, *, tzinfo=None, short_in: bool = False) NamedTuple[source]¶
Build either a ShortFormStruct or LongFormStruct struct_time.
- Parameters:
date (tuple) – A tuple date and time object.
dstflag (int) – A flag indicating daylight savings time. May be set to 1 when daylight savings time is in effect, and 0 when it is not. A value of -1 indicates that this is not known.
tzinfo (tzinfo) – If provided a tzinfo object.
- Returns:
A struct_time object.
- Return type:
- _check_date_fields(a: int, b: int, c: int, d: int = None, e: int = None, *, short_in: bool = False) None[source]¶
Check the validity of the date.
- Parameters:
a (int) – The long form Kull-i-Shay or short form year.
b (int) – The long form Váḥid or short form month.
c (int) – The long form year or short form day.
d (int) – The long form month.
e (int) – The long form day.
short_in (bool) – If True then parse for a short date else if False parse for a long date. This is for incoming dates not outgoing dates as in most other uses of ‘short’.
- Returns:
Nothing
- Return type:
None
- Raises:
AssertionError – If any of the date values are out of range.
- _check_format(format: str) None[source]¶
Check that the correct format was provided.
- Parameters:
format (str) – A string format.
- _check_time_fields(hour: int, minute: int, second: int, microsecond: int, fold: int) None[source]¶
Check the validity of the time.
- Parameters:
hour (int) – The hour.
minute (int) – The minute.
second (int) – The second.
microsecond (int) – The microsecond.
fold (int) – The value of 1 if the time is in the time fold when the time falls back one hour in the Autumn or 0 any other time of the year.
- _checktm(ttup: tuple) None[source]¶
Check that the fields in the tuple are of the correct type. This check on date information is different than what is found inn the badi_calendar.py module as it needs to conform with ISO standards.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A time tuple.
- _date_and_time_locale()[source]¶
Set the _locale_data dictionary instance object to locale date and time information.
- _day_of_week(year: int, month: int, day: int) int[source]¶
Find the day of the week where 0 == Jalál (Saturday) and 6 == Istiqlāl (Friday).
- Parameters:
year (int) – Badí’ year
month (int) – Badí’ month (0..19)
day (int) – Badí’ day
- Returns:
The numerical day of the week.
- Return type:
int
- _days_before_month(year: int, month: int) int[source]¶
The number of days in the year preceding the first day of month.
- Parameters:
year (int) – Badí’ year
month (int) – Badí’ month (0..19)
- Returns:
The number in the year preceding the first day of month.
- Return type:
int
- _days_before_year(year: int) float[source]¶
Get the number of days before the 1st of Bahá of the year.
- Parameters:
year (int) – Badí’ year
- Returns:
The number of days since (-1841, 19, 19) of the Badí’ calendar.
- Return type:
int
- _find_midday(ttup: tuple) tuple[source]¶
Midday time in hours, minutes, and seconds representing the Badí’ midday.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
- Returns:
The Badí’ midday.
- Return type:
tuple
- _find_time_order()[source]¶
Find the locale time delimiter and id 24 or 12 hour time is used.
- Returns:
The time delimiter and 24 or 12 hour time.
- Return type:
str
- _get_year(ttup: tuple) int[source]¶
Convert The Kull-i-Shay’, Váḥid, year to a short mode year.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
- Returns:
The short form year converted if necessary.
- Return type:
int
- _isoweek1jalal(year: int) int[source]¶
Calculate the day number of Jalál (Saturday) starting week 1. It would be the first week with 4 or more days in the year in question.
- Parameters:
year (int) – Badí’ year
- Returns:
The number of the first Jalál in the Badí’ year.
- Return type:
int
- _isoweek_to_badi(year: int, week: int, day: int, *, short: bool = False) tuple[source]¶
The week counts from Jalál (Saturday) as the first day and Istiqlāl (Friday) the last day of the week. This is different from the usual way ISO weeks are counted in the Gregorian Calendar which is Monday to Sunday.
- Parameters:
year (int) – Badí’ year.
month (int) – Badí’ month (0..19)
day (int) – Badí’ day in week.
short (bool) – If True then parse for a short date else if False parse for a long date.
- Returns:
A Badí’ date.
- Return type:
tuple
- Raises:
AssertionError – If the week or weekday is out of range.
- _ord2ymd(n: int, *, short: bool = False) tuple[source]¶
It is difficult to do this in the Badí’ Calendar because a Badí’ day can be more or less than 24 hours depending on when sunset is and the time of the year. From the summer Solstice to the winter Solstice the days get shorter. The day slowly comes down to 24 hours around the Fall Equinox and then below 24 hours. The inverse happens between the Winter Solstice and the Summer Solstice. We just use the BadiCalendar API.
- Parameters:
n (int) – The ordinal number of days from the MINYEAR.
short (bool) – If True then parse for a short date else if False parse for a long date.
- Returns:
The Badí’ date.
- Return type:
tuple
- _order_format(fmt: str, default: str) None[source]¶
Pulls out of the incoming strings data needed to processing date and time formats.
- Parameters:
fmt (str) – The string format to parse.
default (str) – A default format if fmt is invalid.
- Returns:
Data for processing date and time formats.
- Return type:
str
- _parse_isoformat_date(dtstr: str) tuple[source]¶
Parse a date ISO formatted string.
- Parameters:
dtstr (str) – A ISO compliant time string.
- Returns:
The year, month, and day parsed from a ISO string.
- Return type:
tuple
- Raises:
AssertionError – Raised when the year is out of range or when too many hyphens are used.
IndexError – When a string index is out of range.
ValueError – Raised when an invalid string is being parsed to an integer or when an invalid ISO string is being parsed.
- _parse_isoformat_date_time_timezone(dtstr: str) tuple[source]¶
Parse both the date and time represented in an ISO string into a date and time tuple.
- Parameters:
dtstr (str) – A ISO compliant time string.
- Returns:
The date, time, and timezone.
- Return type:
tuple, tuple, timezone
- _parse_isoformat_time(tmstr: str) tuple[source]¶
Parse a time ISO formatted string.
- Parameters:
tmstr (str) – A ISO compliant time string.
- Returns:
The hour, minute, and second parsed from an ISO string.
- Return type:
tuple
- Raises:
AssertionError – Raised when there are invalid time designators, when to many colons used, or when too many dots are used.
ValueError – Raised when an invalid string is being parsed to an integer or when an invalid ISO string is being parsed.
- _parse_isoformat_timezone(tzstr: str) tuple[source]¶
Parse a timezone ISO formatted string.
- Parameters:
tzstr (str) – A ISO compliant time string.
- Returns:
A timezone object indicating the offset from UTC.
- Return type:
- Raises:
AssertionError – Raised when there are invalid timezone delimiters are found.
ValueError – Raised when an invalid ISO string is being parsed.
- _wrap_strftime(obj, format: str, timetuple: tuple, tzinfo=None) str[source]¶
Correctly substitute for %z and %Z escapes in strftime formats.
- Parameters:
obj (class) – A class instance that is calling this method.
format (str) – The formatted string.
timetuple
- Returns:
A correctly formatted string.
- Return type:
str
- _year_week_day(year: int, month: int, day: int, week0: bool = False) tuple[source]¶
Return the year, week, and day of the week from a short form Badí’ date.
- Parameters:
year (int,) – The year.
month (int) – The month.
day (int) – The day.
- Returns:
The year, week, and day of the week.
- Return type:
tuple
- _ymd2ord(year: int, month: int, day: int) int[source]¶
Get the number of days since Badí’ year -1842 (Julian 0001-03-19) including the current day.
year, month, day -> ordinal, considering -1842-01-01 as day 1
Note
We add 77 days to the total so that the ordinal number can be compared to the ordinals in the standard datetime package.
- Parameters:
year (int) – Badí’ year
month (int) – Badí’ month [0, 19]
day (int) – Badí’ day
- Returns:
The number of days since Badí’ year -1842 including the current day.
- Return type:
int
- a(ttup, org, mod)[source]¶
Abbreviated weekday.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The abbreviated weekday.
- Return type:
str
- property am¶
Returns the AM designator.
- b(ttup, org, mod)[source]¶
Abbreviated month name.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The abbreviated month name.
- Return type:
str
- c(ttup, org, mod)[source]¶
Locale specific date and time. Equivalent to “%a %b %d %H:%M:%S %Y”.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The locale specific date and time.
- Return type:
str
- d(ttup, org, mod)[source]¶
Day of month as a zero-padded decimal number.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The day of month as a zero-padded decimal number.
- Return type:
str
- property date_format¶
Returns the locale’s date format.
- f(ttup, org, mod)[source]¶
Microseconds as a decimal number.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The Microseconds as a decimal number.
- Return type:
str
- j(ttup, org, mod)[source]¶
Day of the year as a decimal number. Either zero-padded if %j or not zero-padded if %-j.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The day of the year as a decimal number.
- Return type:
str
- property locale¶
Returns the current locale.
- m(ttup, org, mod)[source]¶
Month as a decimal number. Either zero-padded if %m or not zero-padded if %-m.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The month as a decimal number.
- Return type:
str
- n(ttup, org, mod)[source]¶
Newline character.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The newline character.
- Return type:
str
- p(ttup, org, mod)[source]¶
Locale defined AM and PM.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The locale defined AM and PM.
- Return type:
str
- percent(ttup, org, mod)[source]¶
The literal ‘%’ character.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The literal ‘%’ character.
- Return type:
str
- property pm¶
Returns the PM designator.
- r(ttup, org, mod)[source]¶
Locale defined 12-hour clock time (am/pm).
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The locale defined 12-hour clock time.
- Return type:
str
- strftime(format: str, ttup: tuple, tzinfo=None) str[source]¶
Convert a struct_time object into a string according to a specified format.
- Parameters:
format (str) – A string format.
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
tzinfo (tzinfo) – A tzinfo object.
- Returns:
A struck_time object converted to a formatted string.
- Return type:
str
- property time_format¶
Returns the locale’s time format.
- u(ttup, org, mod)[source]¶
Weekday as a decimal number. With Jalál as 1.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The weekday as a decimal number.
- Return type:
str
- x(ttup, org, mod)[source]¶
Locale defined date representation.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The locale defined date representation.
- Return type:
str
- y(ttup, org, mod)[source]¶
Year without century as a decimal number. Either zero-padded if %y or not zero-padded if %-y.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The year without century as a decimal number.
- Return type:
str
- z(ttup, org, mod)[source]¶
UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).
Note
Some equivalents from offset to ISO standard.
-14400.0 == -0400
37080 == +1030
22829.4 == +063415
11056.44427776 == +030712.
- Parameters:
ttup (ShortFormStruct or LongFormStruct) – A struct_time object.
org (str,) – The original text code.
mod (str) – Code modifier.
- Returns:
The UTC offset in the form ±HHMM[SS[.ffffff]].
- Return type:
str