cf.Datetime

class cf.Datetime(year, month=1, day=1, hour=0, minute=0, second=0, microsecond=0, dayofwk=-1, dayofyr=1, calendar=None)[source]

Bases: cftime._cftime.datetime

A date-time object which supports CF calendars.

Any date-time in any CF calendar is allowed.

Comparion operations

Comparison operations are defined for cf.Datetime and datetime.datetime objects:

>>> cf.Datetime(2004, 2, 1) < cf.Datetime(2004, 2, 30)
True
>>> cf.Datetime(2004, 2, 1) <= cf.Datetime(2004, 2, 30)
True
>>> cf.Datetime(2004, 2, 1) > cf.Datetime(2004, 2, 30)
False
>>> cf.Datetime(2004, 2, 1) >= cf.Datetime(2004, 2, 30)
False
>>> cf.Datetime(2004, 2, 1) == cf.Datetime(2004, 2, 30)
False
>>> cf.Datetime(2004, 2, 1) != cf.Datetime(2004, 2, 30)
True
>>> cf.Datetime(2004, 2, 1, microsecond=1) > cf.Datetime(2004, 2, 1)
True
>>> import datetime
>>> cf.Datetime(2004, 2, 30) > datetime.datetime(2004, 2, 1)
True
>>> datetime.datetime(2004, 2, 1) > cf.Datetime(2004, 2, 30)
False

Arithmetic operations

Addition and subtraction operations are defined for cf.Datetime and cf.TimeDuration objects:

>>> cf.Datetime(2003, 2, 1) + cf.D(30)
<CF Datetime: 2003-03-03T00:00:00Z>
>>> cf.M(30) + cf.Datetime(2003, 2, 1)
<CF Datetime: 2003-03-03T00:00:00Z>
>>> cf.Datetime(2003, 2, 1, calendar='360_day') + cf.D(30)
<CF Datetime: 2003-03-01T00:00:00Z 360_day>
>>> cf.Datetime(2003, 2, 1, calendar='360_day') - cf.D(30)
<CF Datetime: 2003-01-01T00:00:00Z 360_day>
>>> cf.Datetime(2003, 2, 1) - cf.h(30)
<CF Datetime: 2003-01-30T18:00:00Z>
>>> cf.s(456) + cf.Datetime(2003, 2, 1)
<CF Datetime: 2003-02-01T00:07:36Z>
>>> cf.Datetime(2003, 2, 1) + cf.M(1)
<CF Datetime: 2003-03-01T00:00:00Z>
>>> cf.M(1) + cf.Datetime(2003, 2, 1, calendar='360_day') 
<CF Datetime: 2003-03-01T00:00:00Z 360_day>
>>> cf.Datetime(2003, 2, 1, calendar='noleap') + cf.M(245)
<CF Datetime: 2023-07-01T00:00:00Z noleap>
>>> cf.Datetime(2003, 2, 1) - cf.Y(2)
<CF Datetime: 2001-02-01T00:00:00Z>
>>> cf.Datetime(2003, 2, 1) - cf.Y(2) + cf.D(10)
<CF Datetime: 2001-02-11T00:00:00Z>
>>> d = cf.Datetime(2003, 2, 1)
>>> d += cf.s(34567689)
>>> d
<CF Datetime: 2004-03-07T02:08:09Z>

Attributes

Attribute Description
year The year of the date
month The month of the year of the date
day The day of the month of the date
hour The hour of the day of the date
minute The minute of the hour of the date
second The second of the minute of the date
microsecond The microsecond of the second of the date

Constructors

For convenience, the following functions may also be used to create cf.Datetime objects:

Function Description
cf.dt Create a date-time object.

For example:

>>> cf.dt(2001, 12, 3) == cf.dt('2001-12-3') == cf.Datetime(2001, 12, 3)
True
>>> cf.dt(cf.Datetime(2001, 12, 3)) == cf.Datetime(2001, 12, 3)
True
>>> import datetime
>>> cf.dt(datetime.datetime(2001, 12, 3)) == cf.Datetime(2001, 12, 3)
True

Methods

copy Return a deep copy.
inspect Inspect the attributes.
timetuple Return a time.struct_time such as returned by time.localtime().

Class methods

utcnow Return the current Gregorian calendar UTC date and time.