3. Module: CdTime¶
3.1. Time Types¶
The cdtime
module implements the CDMS time types, methods, and
calendars. These are made available with the command:
>>> import cdtime
Two time types are available: relative time and component time. Relative time is time relative to a fixed base time. It consists of:
a units string, of the form ‘units since basetime’, and
a floating-point value
For example, the time “28.0 days since 1996-1-1” has value=28.0, and units=’days since 1996-1-1’
Component time consists of the integer fields year, month, day, hour,
minute, and the floating-point field second. A sample component time is
1996-2-28 12:10:30.0
The cdtime
module contains functions for converting between these
forms, based on the common calendars used in climate simulation. Basic
arithmetic and comparison operators are also available.
3.2. Calendars¶
A calendar specifies the number of days in each month, for a given year. cdtime supports these calendars:
cdtime.GregorianCalendar
: years evenly divisible by four are leap years, except century years not evenly divisible by 400. This is sometimes called the proleptic Gregorian calendar, meaning that the algorithm for leap years applies for all years.cdtime.MixedCalendar
: mixed Julian/Gregorian calendar. Dates before 158210-15 are encoded with the Julian calendar, otherwise are encoded with the Gregorian calendar. The day immediately following 1582-10-4 is 1582-10-15. This is the default calendar.cdtime.JulianCalendar
: years evenly divisible by four are leap years,cdtime.NoLeapCalendar
: all years have 365 days,cdtime.Calendar360
: all months have 30 days.
Several cdtime
functions have an optional calendar argument. The
default calendar is the MixedCalendar
. The default calendar may be
changed with the command:
cdtime.DefaultCalendar = newCalendar
3.3. Time Constructors¶
The following table describes the methods for creating time types.
3.3.1. Time Constructors¶
A relative time type has two members, value and units. Both can be set.
3.5. Component Time¶
A component time type has six members, all of which are settable.
3.5.1. Component Time¶
Type |
Name |
Summary |
---|---|---|
Integer |
year |
Year value |
Integer |
month |
Month, in the range 1..12 |
Integer |
day |
Day of month, in the range 1 .. 31 |
Integer |
hour |
Hour, in the range 0 .. 23 |
Integer |
minute |
Minute, in the range 0 .. 59 |
Float |
second |
Seconds, in the range 0.0 .. 60.0 |
3.6. Time Methods¶
The following methods apply both to relative and component times.
3.6.1. Time Methods¶
Type |
Method |
Definition |
---|---|---|
Comptime or Reltime |
|
|
Integer |
|
|
Comptime or Reltime |
|
|
Comptime |
|
|
Reltime |
|
|
3.7. Examples¶
>>> import cdtime
>>> c = cdtime.comptime(1996,2,28)
>>> r = cdtime.reltime(28,"days since 1996-1-1")
>>> print r.add(1, cdtime.Day)
29.000000 days since 1996-1-1
>>> print c.add(36, cdtime.Hours)
1996-2-29 12:0:0.0
Note: When adding or subtracting intervals of months or years, only the month and year of the result are significant. The reason is that intervals in months/years are not commensurate with intervals in days or fractional days. This leads to results that may be surprising.
>>> c = comptime(1979,8,31)
>>> c.add(1, cdtime.Month)
1979-9-1 0:0:0.0
In other words, the day component of c was ignored in the addition, and the day/hour/minute components of the results are just the defaults. If the interval is in years, the interval is converted internally to months:
>>> c = comptime(1979,8,31)
>>> c.add(2, cdtime.Years)
1981-8-1 0:0:0.0
Compare time values.
>>> import cdtime
>>> r = cdtime.reltime(28,"days since 1996-1-1")
>>> c = cdtime.comptime(1996,2,28)
>>> print c.cmp(r)
1
>>> print r.cmp(c)
-1
>>> print r.cmp(r)
1
Subtract an interval of time.
>>> import cdtime
>>> r = cdtime.reltime(28, "days since 1996-1-1")
>>> c = cdtime.comptime(1996, 2, 28)
>>> print r.sub(10, cdtime.Days)
18.000000 days since 1996-1-1
>>> print c.sub(30, cditme.Days)
1996-1-29 0:0:0.0
For intervals of years or months, see the note under add() in the example above.
Convert to component time.
>>> r = cdtime.reltime(28,"days since 1996-1-1")
>>> r.tocomp()
1996-1-29 0:0:0.0
Convert to relative time.
>>> c = comptime(1996,2,28)
>>> print c.torel("days since 1996-1-1")
58.000000 days since 1996-1-1
>>> r = reltime(28,"days since 1996-1-1")
>>> print r.torel("days since 1995")
393.000000 days since 1995
>>> print r.torel("days since 1995").value
393.0