CHAPTER 3 cdtime Module

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:

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.

Calendars

A calendar specifies the number of days in each month, for a given year. cdtime supports these calendars:

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

Time Constructors

The following table describes the methods for creating time types.

 

 

Time Constructors

Type

Definition

Reltime

cdtime.reltime(value, relunits)

Create a relative time type.

value is an integer or floating point value.

relunits is a string of the form " unit (s) [since basetime ]" where

unit = [second | minute | hour | day | week | month | season | year]

basetime has the form yyyy-mm-dd hh:mi:ss . The default basetime is 1979-1-1 , if no since clause is specified.

Example:

r = cdtime.reltime(28, "days since 1996-1-1")

Comptime

cdtime.comptime(year, month=1, day=1, hour=0, minute=0, second=0.0)

Create a component time type.

year is an integer.

month is an integer in the range 1 .. 12

day is an integer in the range 1 .. 31

hour is an integer in the range 0 .. 59

minute is an integer in the range 0 .. 59

second is a floating point number in the range 0.0 ,, 60.0

Example: c = cdtime.comptime(1996, 2, 28)

Comptime

cdtime.abstime(absvalue, absunits)

Create a component time from an absolute time representation.

absvalue is a floating-point encoding of an absolute time.

absunits is the units template, a string of the form " unit as format " , where unit is one of second, minute, hour, day, calendar_month, or calendar_year . format is a string of the form "%x[%x[...]][.%f]" , where x is one of the format letters ' Y ' (year, including century), ' m ' (two digit month, 01=January), ' d ' (two-digit day within month), ' H ' (hours since midnight), ' M ' (minutes), or ' S ' (seconds ). The optional ' .%f ' denotes a floating-point fraction of the unit.

Example: c = cdtime.abstime(19960228.0, "day as %Y%m%d.%f")

 

Relative Time

A relative time type has two members, value and units . Both can be set.

 

Relative Time Members

Type

Name

Summary

Float

value

Number of units

String

units

Relative units, of the form "unit(s) since basetime"

Component Time

A component time type has six members, all of which are settable.

 

Component Time Members

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 .. 59

Integer

minute

Minute, in the range 0 .. 59

Float

second

Seconds, in the range 0.0 .. 60.0

Time Methods

The following methods apply both to relative and component times.

 

Time Methods

Type

Definition

Comptime or Reltime

t.add(value, intervalUnits, calendar=cdtime.DefaultCalendar)

Add an interval of time to a time type t. Returns the same type of time.

value is the Float number of interval units.

intervalUnits is cdtime.[Second(s) | Minute(s) | Hour(s) | Day(s) | Week(s) | Month(s) | Season(s) | Year(s)]

calendar is the calendar type.

Example:

>>> from cdtime import *

>>> c = comptime(1996,2,28)

>>> r = reltime(28,"days since 1996-1-1")

>>> print r.add(1,Days)

29.00 days since 1996-1-1

>>> print c.add(36,Hour)

1996-2-29 12:0:0.0

Integer

t.cmp(t2, calendar=cdtime.DefaultCalendar)

Compare time values t and t2. Returns -1, 0, 1 as t is less than, equal to, or greater than t2 respectively.

t2 is the time to compare.

calendar is the calendar type.

Example:

>>> from cdtime import *

>>> r = cdtime.reltime(28,"days since 1996-1-1")

>>> c = comptime(1996,2,28)

>>> print r.cmp(c)

-1

>>> print c.cmp(r)

1

>>> print r.cmp(r)

0

Comptime or Reltime

t.sub(value, intervalUnits, calendar=cdtime.DefaultCalendar)

Subtract an interval of time from a time type t. Returns the same type of time.

value is the Float number of interval units.

intervalUnits is cdtime.[Second(s) | Minute(s) | Hour(s) | Day(s) | Week(s) | Month(s) | Season(s) | Year(s)]

calendar is the calendar type.

Example:

>>> from cdtime import *

>>> r = cdtime.reltime(28,"days since 1996-1-1")

>>> c = comptime(1996,2,28)

>>> print r.sub(10,Days)

18.00 days since 1996-1-1

>>> print c.sub(30,Days)

1996-1-29 0:0:0.0

Comptime

t.tocomp(calendar = cdtime.DefaultCalendar)

Convert to component time. Returns the equivalent component time.

calendar is the calendar type.

Example:

>>> r = cdtime.reltime(28,"days since 1996-1-1")

>>> r.tocomp()

1996-1-29 0:0:0.0

Reltime

t.torel(units, calendar=cdtime.DefaultCalendar)

Convert to relative time. Returns the equivalent relative time.

Example:

>>> c = comptime(1996,2,28)

>>> print c.torel("days since 1996-1-1")

58.00 days since 1996-1-1

>>> r = reltime(28,"days since 1996-1-1")

>>> print r.torel("days since 1995")

393.00 days since 1995

>>> print r.torel("days since 1995").value

393.0

Go to Main Go to Previous Go to Next