time — Time Classes and Functions#

Time handling module.

New in version 7.5.

pywikibot.time.MW_KEYS = mappingproxy({'s': 'seconds', 'h': 'hours', 'd': 'days', 'w': 'weeks', 'y': 'years'})#

New in version 7.5.

class pywikibot.time.TZoneFixedOffset(offset, name)[source]#

Bases: tzinfo

Class building tzinfo objects for fixed-offset time zones.

Parameters:
  • offset (int) – a number indicating fixed offset in minutes east from UTC

  • name (str) – a string with name of the timezone

dst(dt)[source]#

Return no daylight savings time.

tzname(dt)[source]#

Return the name of the timezone.

utcoffset(dt)[source]#

Return the offset to UTC.

class pywikibot.time.Timestamp[source]#

Bases: datetime

Class for handling MediaWiki timestamps.

This inherits from datetime.datetime, so it can use all of the methods and operations of a datetime object. To ensure that the results of any operation are also a Timestamp object, be sure to use only Timestamp objects (and datetime.timedeltas) in any operation.

Use Timestamp.fromISOformat() and Timestamp.fromtimestampformat() to create Timestamp objects from MediaWiki string formats. As these constructors are typically used to create objects using data passed provided by site and page methods, some of which return a Timestamp when previously they returned a MediaWiki string representation, these methods also accept a Timestamp object, in which case they return a clone.

Alternatively, Timestamp.set_timestamp() can create Timestamp objects from Timestamp, datetime.datetime object, or strings compliant with ISO8601, MW, or POSIX formats.

Use Site.server_time() for the current time; this is more reliable than using Timestamp.utcnow().

Changed in version 7.5: moved to time module

ISO8601Format = '%Y-%m-%dT%H:%M:%SZ'#
clone()[source]#

Clone this instance.

Return type:

Timestamp

classmethod fromISOformat(ts, sep='T')[source]#

Convert an ISO 8601 timestamp to a Timestamp object.

Parameters:
  • ts (Union[str, Timestamp]) – ISO 8601 timestamp or a Timestamp object already

  • sep (str) – one-character separator, placed between the date and time

Returns:

Timestamp object

Return type:

Timestamp

classmethod fromtimestampformat(ts)[source]#

Convert a MediaWiki internal timestamp to a Timestamp object.

Parameters:

ts (Union[str, Timestamp]) –

Return type:

Timestamp

isoformat(sep='T')[source]#

Convert object to an ISO 8601 timestamp accepted by MediaWiki.

datetime.datetime.isoformat does not postfix the ISO formatted date with a ‘Z’ unless a timezone is included, which causes MediaWiki ~1.19 and earlier to fail.

Parameters:

sep (str) –

Return type:

str

mediawikiTSFormat = '%Y%m%d%H%M%S'#
posix_timestamp()[source]#

Convert object to a POSIX timestamp.

See Note in datetime.timestamp().

Return type:

float

posix_timestamp_format()[source]#

Convert object to a POSIX timestamp format.

Return type:

str

classmethod set_timestamp(ts)[source]#

Set Timestamp from input object.

ts is converted to a datetime naive object representing UTC time. String shall be compliant with: - Mediwiki timestamp format: YYYYMMDDHHMMSS - ISO8601 format: YYYY-MM-DD[T ]HH:MM:SS[Z|±HH[MM[SS[.ffffff]]]] - POSIX format: seconds from Unix epoch S{1,13}[.ffffff]]

Parameters:

ts (Union[str, datetime]) – Timestamp, datetime.datetime or str

Returns:

Timestamp object

Raises:

ValueError – conversion failed

Return type:

Timestamp

totimestampformat()[source]#

Convert object to a MediaWiki internal timestamp.

Return type:

str

pywikibot.time.parse_duration(string)[source]#

Return the key and duration extracted from the string.

Parameters:

string (str) – a string defining a time period

Return type:

Tuple[str, int]

Examples:

300s - 300 seconds
36h - 36 hours
7d - 7 days
2w - 2 weeks (14 days)
1y - 1 year
Returns:

key and duration extracted form the string

Parameters:

string (str) –

Return type:

Tuple[str, int]

pywikibot.time.str2timedelta(string, timestamp=None)[source]#

Return a timedelta for a shorthand duration.

Parameters:

string (str) – a string defining a time period:

Return type:

timedelta

Examples:

300s - 300 seconds
36h - 36 hours
7d - 7 days
2w - 2 weeks (14 days)
1y - 1 year
Parameters:
  • timestamp (datetime.datetime) – a timestamp to calculate a more accurate duration offset used by years

  • string (str) –

Returns:

the corresponding timedelta object

Return type:

timedelta