obspy.core.utcdatetime.UTCDateTime¶
-
class
UTCDateTime
(*args, **kwargs)[source]¶ Bases:
object
A UTC-based datetime object.
This datetime class is based on the POSIX time, a system for describing instants in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of Thursday, January 1, 1970. Using a single float timestamp allows higher precision as the default Python
datetime.datetime
class. It features the full ISO8601:2004 specification and some additional string patterns during object initialization.- Parameters
args (int, float, str,
datetime.datetime
, optional) – The creation of a new UTCDateTime object depends from the given input parameters. All possible options are summarized in the Examples section below.iso8601 (bool, optional) – Enforce ISO8601:2004 detection. Works only with a string as first input argument.
precision (int, optional) – Sets the precision used by the rich comparison operators. Defaults to
6
digits after the decimal point. See also Precision section below.
Changed in version 0.5.1: UTCDateTime is no longer based on Python’s datetime.datetime class instead uses timestamp as a single floating point value which allows higher precision.
Supported Operations
UTCDateTime = UTCDateTime + delta
Adds/removes
delta
seconds (given as int or float) to/from the currentUTCDateTime
object and returns a newUTCDateTime
object. See also:__add__()
.delta = UTCDateTime - UTCDateTime
Calculates the time difference in seconds between two
UTCDateTime
objects. The time difference is given as float data type and may also contain a negative number. See also:__sub__()
.
Examples
Using a timestamp.
>>> UTCDateTime(0) UTCDateTime(1970, 1, 1, 0, 0)
>>> UTCDateTime(1240561632) UTCDateTime(2009, 4, 24, 8, 27, 12)
>>> UTCDateTime(1240561632.5) UTCDateTime(2009, 4, 24, 8, 27, 12, 500000)
Using a ISO8601:2004 string. The detection may be enforced by setting the
iso8601
parameter to True.Calendar date representation.
>>> UTCDateTime("2009-12-31T12:23:34.5") UTCDateTime(2009, 12, 31, 12, 23, 34, 500000)
>>> UTCDateTime("20091231T122334.5") # compact UTCDateTime(2009, 12, 31, 12, 23, 34, 500000)
>>> UTCDateTime("2009-12-31T12:23:34.5Z") # w/o time zone UTCDateTime(2009, 12, 31, 12, 23, 34, 500000)
>>> UTCDateTime("2009-12-31T12:23:34+01:15") # w/ time zone UTCDateTime(2009, 12, 31, 11, 8, 34)
Ordinal date representation.
>>> UTCDateTime("2009-365T12:23:34.5") UTCDateTime(2009, 12, 31, 12, 23, 34, 500000)
>>> UTCDateTime("2009365T122334.5") # compact UTCDateTime(2009, 12, 31, 12, 23, 34, 500000)
>>> UTCDateTime("2009001", iso8601=True) # enforce ISO8601 UTCDateTime(2009, 1, 1, 0, 0)
Week date representation.
>>> UTCDateTime("2009-W53-7T12:23:34.5") UTCDateTime(2010, 1, 3, 12, 23, 34, 500000)
>>> UTCDateTime("2009W537T122334.5") # compact UTCDateTime(2010, 1, 3, 12, 23, 34, 500000)
>>> UTCDateTime("2009W011", iso8601=True) # enforce ISO8601 UTCDateTime(2008, 12, 29, 0, 0)
Using not ISO8601 compatible strings.
>>> UTCDateTime("1970-01-01 12:23:34") UTCDateTime(1970, 1, 1, 12, 23, 34)
>>> UTCDateTime("1970,01,01,12:23:34") UTCDateTime(1970, 1, 1, 12, 23, 34)
>>> UTCDateTime("1970,001,12:23:34") UTCDateTime(1970, 1, 1, 12, 23, 34)
>>> UTCDateTime("20090701121212") UTCDateTime(2009, 7, 1, 12, 12, 12)
>>> UTCDateTime("19700101") UTCDateTime(1970, 1, 1, 0, 0)
>>> UTCDateTime("20110818_03:00:00") UTCDateTime(2011, 8, 18, 3, 0)
>>> UTCDateTime("1970/01/17 12:23:34") UTCDateTime(1970, 1, 17, 12, 23, 34)
Using multiple arguments in the following order: year, month, day[, hour[, minute[, second[, microsecond]]]. The year, month and day arguments are required.
>>> UTCDateTime(1970, 1, 1) UTCDateTime(1970, 1, 1, 0, 0)
>>> UTCDateTime(1970, 1, 1, 12, 23, 34, 123456) UTCDateTime(1970, 1, 1, 12, 23, 34, 123456)
Using the following keyword arguments: year, month, day, julday, hour, minute, second, microsecond. Either the combination of year, month and day, or year and Julian day are required.
>>> UTCDateTime(year=1970, month=1, day=1, minute=15, microsecond=20) UTCDateTime(1970, 1, 1, 0, 15, 0, 20)
>>> UTCDateTime(year=2009, julday=234, hour=14, minute=13) UTCDateTime(2009, 8, 22, 14, 13)
Using a Python
datetime.datetime
object.>>> dt = datetime.datetime(2009, 5, 24, 8, 28, 12, 5001) >>> UTCDateTime(dt) UTCDateTime(2009, 5, 24, 8, 28, 12, 5001)
Precision
The
UTCDateTime
class works with a default precision of6
digits which effects the comparison of date/time values, e.g.:>>> dt = UTCDateTime(0) >>> dt2 = UTCDateTime(0.00001) >>> dt3 = UTCDateTime(0.0000001) >>> print(dt.precision) 6 >>> dt == dt2 # 5th digit is within current precision False >>> dt == dt3 # 7th digit will be neglected True
You may change that behavior either by,
using the
precision
keyword during object initialization:>>> dt = UTCDateTime(0, precision=4) >>> dt2 = UTCDateTime(0.00001, precision=4) >>> print(dt.precision) 4 >>> dt == dt2 True
or set it the class attribute
DEFAULT_PRECISION
for all newUTCDateTime
objects using a monkey patch:>>> UTCDateTime.DEFAULT_PRECISION = 4 >>> dt = UTCDateTime(0) >>> dt2 = UTCDateTime(0.00001) >>> print(dt.precision) 4 >>> dt == dt2 True
Don’t forget to reset
DEFAULT_PRECISION
if not needed anymore!>>> UTCDateTime.DEFAULT_PRECISION = 6
Attributes
DEFAULT_PRECISION
__dict__
__doc__
__module__
__weakref__
list of weak references to the object (if defined)
date
Returns a Python date object..
datetime
Returns a Python datetime object.
day
Returns day as an integer.
hour
Returns hour as an integer.
julday
Returns Julian day as an integer.
matplotlib_date
Maplotlib date number representation.
microsecond
Returns microseconds as an integer.
minute
Returns minute as an integer.
month
Returns month as an integer (January is 1, December is 12).
precision
Returns precision of current UTCDateTime object.
second
Returns seconds as an integer.
time
Returns a Python time object.
timestamp
Returns UTC timestamp in seconds.
weekday
Return the day of the week as an integer (Monday is 0, Sunday is 6).
year
Returns year of the current UTCDateTime object.
Public Methods
Return a string representing the date and time.
Returns None (to stay compatible with
datetime.datetime
)Returns string representation for the ArcLink protocol.
Returns string representation for the IRIS Fissures protocol.
Returns string representation usable for the IRIS Web services.
Returns string representation for a SEED volume.
Returns string representation for the SeedLink protocol.
Returns a tuple containing (ISO year, ISO week number, ISO weekday).
Return a string representing the date and time in ISO 8601 format.
Return the day of the week as an integer (Monday is 1, Sunday is 7).
Returns current UTC datetime.
Return a string representing the date and time, controlled by an explicit format string.
Return a UTCDateTime corresponding to date_string, parsed according to given format.
Return a time.struct_time such as returned by time.localtime().
Return time object with same hour, minute, second, microsecond, and tzinfo attributes.
Return proleptic Gregorian ordinal.
Returns None (to stay compatible with
datetime.datetime
)Returns current UTC datetime.
Returns None (to stay compatible with
datetime.datetime
)Return a time.struct_time of current UTCDateTime object.
Private Methods
Warning
Private methods are mainly for internal/developer use and their API might change without notice.
Use Python datetime object to set current time.
Parses an ISO8601:2004 date time string.
Use given timestamp to set current time.
Returns a Python date object..
Returns a Python datetime object.
Returns day as an integer.
Returns hour as an integer.
Calculate foating point hours after midnight.
Returns Julian day as an integer.
Returns microseconds as an integer.
Returns minute as an integer.
Returns month as an integer (January is 1, December is 12).
Returns precision of current UTCDateTime object.
Returns seconds as an integer.
Returns a Python time object.
Returns UTC timestamp in seconds.
Return the day of the week as an integer (Monday is 0, Sunday is 6).
Returns year of the current UTCDateTime object.
Sets current timestamp using kwargs.
Sets day of current UTCDateTime object.
Sets hours of current UTCDateTime object.
Sets Julian day of current UTCDateTime object.
Sets microseconds of current UTCDateTime object.
Sets minutes of current UTCDateTime object.
Sets month of current UTCDateTime object.
Set precision of current UTCDateTime object.
Sets seconds of current UTCDateTime object.
Sets year of current UTCDateTime object.
Replace all simple, year-independent strftime commands
Special Methods
Returns absolute timestamp value of the current UTCDateTime object.
Adds seconds and microseconds to current UTCDateTime object.
Default dir() implementation.
Rich comparison operator ‘==’.
Returns UTC timestamp in seconds.
Default object formatter.
Rich comparison operator ‘>=’.
Rich comparison operator ‘>’.
An object is hashable if it has a hash value which never changes during its lifetime.
Creates a new UTCDateTime object.
This method is called when a class is subclassed.
Rich comparison operator ‘<=’.
Rich comparison operator ‘<’.
Rich comparison operator ‘!=’.
Create and return a new object.
Helper for pickle.
Helper for pickle.
Returns a representation of UTCDatetime object.
Size of object in memory, in bytes.
Returns ISO8601 string representation from current UTCDateTime object.
Subtracts seconds and microseconds from current UTCDateTime object.
Abstract classes can override this to customize issubclass().
Returns ISO8601 unicode representation from current UTCDateTime object.