Timestamp

class mwtypes.Timestamp(*args, **kwargs)[source]

Provides a set of convenience functions for working with MediaWiki timestamps. This class can interpret and return multiple formats as well as perform basic mathematical operations.

Parameters:
time_thing
: time.time_struct | datetime.datetime | str | int

The timestamp type from which to construct the timestamp class.

You can make use of a lot of different time things to initialize a mw.Timestamp.

  • If a time_struct or datetime are provided, a Timestamp will be constructed from their values.
  • If an int or float are provided, they will be assumed to a unix timestamp in seconds since Jan. 1st, 1970 UTC.
  • If a str is provided, it will be be checked against known MediaWiki timestamp formats. E.g., '%Y%m%d%H%M%S' and '%Y-%m-%dT%H:%M:%SZ'.

For example:

>>> import datetime, time
>>> from mwtypes import Timestamp
>>> Timestamp(1234567890)
Timestamp('2009-02-13T23:31:30Z')
>>> Timestamp(1234567890) == Timestamp("2009-02-13T23:31:30Z")
True
>>> Timestamp(1234567890) == Timestamp("20090213233130")
True
>>> Timestamp(1234567890) == Timestamp(datetime.datetime.utcfromtimestamp(1234567890))
True
>>> Timestamp(1234567890) == Timestamp(time.strptime("2009-02-13T23:31:30Z", "%Y-%m-%dT%H:%M:%SZ"))
True

You can also do math and comparisons of timestamps.:

>>> from mw import Timestamp
>>> t = Timestamp(1234567890)
>>> t
Timestamp('2009-02-13T23:31:30Z')
>>> t2 = t + 10
>>> t2
Timestamp('2009-02-13T23:31:40Z')
>>> t += 1
>>> t
Timestamp('2009-02-13T23:31:31Z')
>>> t2 - t
9
>>> t < t2
True
classmethod from_datetime(dt)[source]

Constructs a mw.Timestamp from a datetime.datetime.

Parameters:
dt
: datetime.datetime`

A datetime.

Returns:

mw.Timestamp

classmethod from_string(string)[source]

Constructs a mw.Timestamp from a MediaWiki formatted string. This method is provides a convenient way to construct from common MediaWiki timestamp formats. E.g., %Y%m%d%H%M%S and %Y-%m-%dT%H:%M:%SZ.

Parameters:
string
: str

A formatted timestamp

Returns:

mw.Timestamp

classmethod from_time_struct(time_struct)[source]

Constructs a mw.Timestamp from a time.time_struct.

Parameters:
time_struct
: time.time_struct

A time structure

Returns:

mw.Timestamp

classmethod from_unix(seconds)[source]

Constructs a mw.Timestamp from a unix timestamp (in seconds since Jan. 1st, 1970 UTC).

Parameters:
seconds
: int

A unix timestamp

Returns:

mw.Timestamp

long_format()[source]

Constructs a long, '%Y-%m-%dT%H:%M:%SZ' formatted string common to the API. This method is roughly equivalent to calling strftime('%Y-%m-%dT%H:%M:%SZ').

Parameters:
format
: str

The string format

Returns:

A formatted string

short_format()[source]

Constructs a long, '%Y%m%d%H%M%S' formatted string common to the database. This method is roughly equivalent to calling strftime('%Y%m%d%H%M%S').

Parameters:
format
: str

The string format

Returns:

A formatted string

strftime(format)[source]

Constructs a formatted string. See https://docs.python.org/3/library/time.html#time.strftime for a discussion of formats descriptors.

Parameters:
format
: str

The format description

Returns:

A formatted string

classmethod strptime(string, format)[source]

Constructs a mw.Timestamp from an explicitly formatted string. See https://docs.python.org/3/library/time.html#time.strftime for a discussion of formats descriptors.

Parameters:
string
: str

A formatted timestamp

format
: str

The format description

Returns:

mw.Timestamp

unix()[source]
Returns:the number of seconds since Jan. 1st, 1970 UTC.