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: |
|
|---|
You can make use of a lot of different time things to initialize a
mw.Timestamp.
time_struct or datetime are
provided, a Timestamp will be constructed from their values.'%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
from_datetime(dt)[source]¶Constructs a mw.Timestamp from a datetime.datetime.
| Parameters: |
datetime.datetime` |
|---|---|
| Returns: |
|
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: |
|
|---|---|
| Returns: |
|
from_time_struct(time_struct)[source]¶Constructs a mw.Timestamp from a time.time_struct.
| Parameters: |
time.time_struct |
|---|---|
| Returns: |
|
from_unix(seconds)[source]¶Constructs a mw.Timestamp from a unix timestamp (in seconds
since Jan. 1st, 1970 UTC).
| Parameters: |
|
|---|---|
| Returns: |
|
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: |
|
|---|---|
| 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: |
|
|---|---|
| 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: |
|
|---|---|
| Returns: | A formatted string |
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: |
|
|---|---|
| Returns: |
|