date — Date Classes and Functions#

Date data and manipulation module.

class date.MonthFormat(index, format_key)[source]#

Bases: MutableMapping

A Mapping which creates months formats.

Initializer of MonthFormat mapping.

Parameters:
  • index (int) – month number

  • format_key (str) – formats key like Day_January or Year_December

day_formats = {'af': ('%d {}', True), 'ang': ('%d {}', True), 'ar': ('%d {}', True), 'arz': ('%d {}', True), 'bg': ('%d {}', False), 'bn': ('{} %%B', None), 'ceb': ('{} %d', True), 'csb': ('%d {}a', False), 'cv': ('{}, %d', True), 'cy': ('%d {}', True), 'de': ('%d. {}', True), 'en': ('{} %d', True), 'eo': ('%d-a de {}', False), 'es': ('%d de {}', False), 'eu': ('{}aren %d', True), 'fi': ('%d. {}ta', False), 'fur': ('%d di {}', True), 'fy': ('%d {}', False), 'gl': ('%d de {}', False), 'gsw': ('%d. {}', True), 'he': ('%d ב{}', None), 'hu': ('{} %d.', True), 'ia': ('%d de {}', False), 'id': ('%d {}', True), 'ie': ('%d {}', False), 'io': ('%d di {}', False), 'it': ('%d {}', False), 'jv': ('%d {}', True), 'ka': ('%d {}', None), 'lb': ('%d. {}', True), 'mhr': ('%d {}', False), 'ml': ('{} %d', None), 'ms': ('%d {}', True), 'nap': ("%d 'e {}", False), 'nds': ('%d. {}', True), 'pt': ('%d de {}', True), 'ro': ('%d {}', False), 'scn': ('%d di {}', False), 'sco': ('%d {}', True), 'su': ('%d {}', True), 'sv': ('%d {}', False), 'ta': ('{} %d', None), 'te': ('{} %d', None), 'th': ('%d {}', None), 'tl': ('{} %d', None), 'tr': ('%d {}', True), 'tt': ('%d. {}', True), 'vec': ('%d de {}', False), 'vo': ('{} %d', False)}#
year_formats = {'cs': ('{} %d', None), 'eo': ('{} de %d', None), 'es': ('{} de %d', True), 'it': ('Attualità/Anno %d - {}', True), 'ka': ('{}, %d', None), 'sk': ('{} %d', None), 'th': ('{} พ.ศ. %%T', None), 'tl': ('{} %d', None)}#
class date.MonthNames[source]#

Bases: Mapping

A Mapping which reads month names from MediaWiki messages.

months = {'br': <function MonthNames.<lambda>>, 'en': <function MonthNames.<lambda>>, 'ja': <function MonthNames.<lambda>>, 'ko': <function MonthNames.<lambda>>, 'wa': <function MonthNames.<lambda>>, 'zh': <function MonthNames.<lambda>>}#
date.addFmt1(lang, isMnthOfYear, patterns)[source]#

Add 12 month formats for a specific type (‘January’, ‘Feb.’).

The function must accept one parameter for the ->int or ->string conversions, just like everywhere else in the formats map. The patterns parameter is a list of 12 elements to be used for each month.

Parameters:
  • lang (str) – language code

  • isMnthOfYear (bool) –

  • patterns (Sequence[str | None]) –

Return type:

None

date.alwaysTrue(x)[source]#

Return True, always.

Used for multiple value selection function to accept all other values.

Parameters:

x (Any) – not used

Returns:

True

Return type:

bool

date.apply_month_delta(date, month_delta=1, add_overlap=False)[source]#

Add or subtract months from the date.

By default if the new month has less days then the day of the date it chooses the last day in the new month. For example a date in the March 31st added by one month will result in April 30th.

When the overlap is enabled, and there is overlap, then the new_date will be one month off and get_month_delta will report a number one higher.

It does only work on calendars with 12 months per year, and where the months are numbered consecutively beginning by 1.

Parameters:
  • date (date) – The starting date

  • month_delta (int) – The amount of months added or subtracted.

  • add_overlap (bool) – Add any missing days to the date, increasing the month once more.

Returns:

The end date

Return type:

date

date.decSinglVal(v)[source]#

Return first item in list v.

Parameters:

v (Sequence[Any]) –

Return type:

Any

date.dh(value, pattern, encf, decf, filter_func=None, filter='[deprecated name of filter_func]')[source]#
date.dh(value, pattern, encf, decf, filter_func=None)

Function to help with year parsing.

Usually it will be used as a lambda call in a map:

lambda v: dh(v, 'pattern string', encf, decf)
Parameters:
  • encf (encf_type) –

    Converts from an integer parameter to another integer or a tuple of integers. Depending on the pattern, each integer will be converted to a proper string representation, and will be passed as a format argument to the pattern:

    pattern % encf(value)
    

    This function is a complement of decf.

  • value (int) –

  • pattern (str) –

  • decf (decf_type) –

  • filter_func (Callable[[int], bool] | None) –

Changed in version 9.0: filter parameter was renamed to filter_func

Parameters:
  • decf (decf_type) – Converts a tuple/list of non-negative integers found in the original value string into a normalized value. The normalized value can be passed right back into dh() to produce the original string. This function is a complement of encf. dh() interprets %d as a decimal and %s as a roman numeral number.

  • value (int) –

  • pattern (str) –

  • encf (encf_type) –

  • filter_func (Callable[[int], bool] | None) –

date.dh_centuryAD(value, pattern)[source]#

Helper for decoding an AD century.

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.dh_centuryBC(value, pattern)[source]#

Helper for decoding an BC century.

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.dh_constVal(value, ind, match)[source]#
date.dh_constVal(value, ind, match)

Helper function to match a single value to a constant.

formats[‘CurrEvents’][‘en’](ind) => ‘Current Events’ formats[‘CurrEvents’][‘en’](‘Current Events’) => ind

Parameters:
  • value (int) –

  • ind (int) –

  • match (str) –

Return type:

str

date.dh_dayOfMnth(value, pattern)[source]#

Helper for decoding a single integer value.

The single integer should be <=31, no conversion, no rounding (used in days of month).

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.dh_decAD(value, pattern)[source]#

Helper for decoding a single integer value.

It should be no conversion, round to decimals (used in decades)

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.dh_decBC(value, pattern)[source]#

Helper for decoding a single integer value.

It should be no conversion, round to decimals (used in decades)

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.dh_millenniumAD(value, pattern)[source]#

Helper for decoding an AD millennium.

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.dh_millenniumBC(value, pattern)[source]#

Helper for decoding an BC millennium.

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.dh_mnthOfYear(value, pattern)[source]#

Helper for decoding a single integer value.

The value should be >=1000, no conversion, no rounding (used in month of the year)

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.dh_noConv(value, pattern, limit)[source]#

Helper for decoding an integer value, no conversion, no rounding.

Parameters:
  • value (int) –

  • pattern (str) –

  • limit (Callable[[int], bool]) –

Return type:

str

date.dh_number(value, pattern)[source]#

Helper for decoding a number.

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.dh_simpleYearAD(value)[source]#

Helper for decoding a single integer value.

This value should be representing a year with no extra symbols.

Parameters:

value (int) –

Return type:

str

date.dh_singVal(value, match)[source]#
date.dh_singVal(value, match)

Helper function to match a single value to a constant.

Parameters:
  • value (int) –

  • match (str) –

Return type:

str

date.dh_yearAD(value, pattern)[source]#

Helper for decoding a year value.

The value should have no conversion, no rounding, limits to 3000.

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.dh_yearBC(value, pattern)[source]#

Helper for decoding a year value.

The value should have no conversion, no rounding, limits to 3000.

Parameters:
  • value (int) –

  • pattern (str) –

Return type:

str

date.encDec0(i)[source]#

Round to the nearest decade, decade starts with a ‘0’-ending year.

Parameters:

i (int) –

Return type:

int

date.encDec1(i)[source]#

Round to the nearest decade, decade starts with a ‘1’-ending year.

Parameters:

i (int) –

Return type:

int

date.escapePattern2(pattern)[source]#

Convert a string pattern into a regex expression and cache.

Allows matching of any _digitDecoders inside the string. Returns a compiled regex object and a list of digit decoders.

Parameters:

pattern (str) –

Return type:

tuple[Pattern[str], str, list[decoder_type]]

date.formatYear(lang, year)[source]#

Return year name in a language.

Parameters:
  • lang (str) –

  • year (int) –

Return type:

str

date.format_date(month, day, lang=None, year=2000)[source]#

Format a date localized to given lang.

Parameters:
  • month (int) – month in range of 1..12

  • day (int) – day of month in range of 1..31

  • lang (str | BaseSite | None) – a site object or language key. Defaults to current site.

  • year (int) – year for which the date is to be formatted. always 29 will be given For February except the year is given. Default is leap year 2000.

Returns:

localized date like “January 11”

Raises:
  • ValueError – Wrong day value; must be 1-28/29/30/31

  • IllegalMonthError – bad month number; must be 1-12

Return type:

str

date.getAutoFormat(lang, title, ignoreFirstLetterCase=True)[source]#

Return first matching formatted date value.

Parameters:
  • lang (str) – language code

  • title (str) – value to format

  • ignoreFirstLetterCase (bool) –

Returns:

dictName (‘YearBC’, ‘December’, …) and value (a year, date, …)

Return type:

tuple[str | None, str | None]

date.get_month_delta(date1, date2)[source]#

Return the difference between two dates in months.

It does only work on calendars with 12 months per year, and where the months are consecutive and non-negative numbers.

Parameters:
  • date1 (date) –

  • date2 (date) –

Return type:

int

date.intToLocalDigitsStr(value, digitsToLocalDict)[source]#

Encode an integer value into a textual form.

Parameters:
  • value (int) –

  • digitsToLocalDict (Mapping[int, str]) –

Return type:

str

date.intToRomanNum(i)[source]#

Convert integer to roman numeral.

Parameters:

i (int) –

Return type:

str

date.localDigitsStrToInt(value, digitsToLocalDict, localToDigitsDict)[source]#

Convert digits to integer.

Parameters:
  • value (str) –

  • digitsToLocalDict (Mapping[int, str]) –

  • localToDigitsDict (Mapping[int, str]) –

Return type:

int

date.makeMonthList(pattern)[source]#

Return a list of 12 elements based on the number of the month.

Parameters:

pattern (str) –

Return type:

list[str]

date.makeMonthNamedList(lang, pattern='%s', makeUpperCase=None)[source]#

Create a list of 12 elements based on the name of the month.

The language-dependent month name is used as a formatting argument to the pattern. The pattern must be have one %s that will be replaced by the localized month name. Use %% for any other parameters that should be preserved.

Parameters:
  • lang (str) –

  • pattern (str) –

  • makeUpperCase (bool | None) –

Return type:

list[str]

date.monthName(lang, ind)[source]#

Return the month name for a language.

Parameters:
  • lang (str) –

  • ind (int) –

Return type:

str

date.multi(value, tuplst)[source]#
date.multi(value, tuplst)
date.multi(value, tuplst)

Run multiple pattern checks for the same entry.

For example: 1st century, 2nd century, etc.

The tuplst is a list of tuples. Each tuple must contain two functions: first to encode/decode a single value (e.g. simpleInt), second is a predicate function with an integer parameter that returns true or false. When the 2nd function evaluates to true, the 1st function is used.

Parameters:

tuplst (tuplst_type) –

Return type:

Any

date.romanNumToInt(v)[source]#

Convert roman numeral to integer.

Parameters:

v (str) –

Return type:

int

date.slh(value, lst)[source]#
date.slh(value, lst)

Helper function for simple list value matching.

!!!!! The index starts at 1, so 1st element has index 1, not 0 !!!!!

Usually it will be used as a lambda call in a map:

lambda v: slh(v, ['January','February',...])

Usage scenarios:

formats['MonthName']['en'](1) => 'January'
formats['MonthName']['en']('January') => 1
formats['MonthName']['en']('anything else') => raise ValueError
Parameters:
  • value (int) –

  • lst (Sequence[str]) –

Return type:

str