2.0.0b10
catchment modelling framework
Loading...
Searching...
No Matches
Time Class Reference

A time class, used to pass around current modelling times. More...

Detailed Description

A time class, used to pass around current modelling times.

Timespans and dates in cmf are used with a special object, called Time. An extra class has the advantage, that the user does not have to remember, which unit of time he or she uses or what time unit is accepted by a specific function of the model. Arithmetic and boolean operators are supported by Time. Internally the time classes stores the time as integer milliseconds, therefore rounding issues will only appear at very small time ranges. Absolute time (like dates) are represented as milliseconds gone by from Dec, 31st 1899. Microsoft Excel dates are represented as days from that time, using floating point numbers, therefore it is very simple to convert Excel time representations to cmf time.

Another object is Date, which is doesn't provide the operators, but has a nice printed version and some special date functions, like day of year (DOY) and provides access to the current hour of day and so on, which only applyto dates and not to time spans. You can convert Time to Date an vice versa. The printing is not culture aware and uses the European representation. If you use the Python standard library datetime, conversion between Python time and cmf time is possible

Creating absolute time values (dates)

import cmf
Create the time: Jan, 5th 2001, 2:30 pm, 20s, 412 ms
t=cmf.Time(5,1,2001,14,30,20,412)
print t # Prints: '36896d:14:30:20.412h'
print t.AsDate() # Prints: '05.01.2001 14:30:20.412'

Creating time spans

In principle, there are three ways to create time spans. One is to use one of the static functions, another is to multiply an existing time span (like one of the build in constants) or to substrate two absolute times.

Available constants

  • cmf.sec * 4.1
    The main namespace of the model framework. Contains the other namespaces and the project class.
    Definition __init__.py:1
    : 4.1 seconds
  • cmf.min * 138
    : 2.3 hours (138 min)
  • cmf.h * 2.3
    : 2.3 hours (138 min)
  • cmf.day * 2.5
    : 60 hours (2.5 days)
  • cmf.week
    : 7 days
  • cmf.month
    : 365/12 days (30.4167 days)
  • cmf.year
    : 365 days

Available operators:

  • time + time = time, time - time = time
  • time * float = time ,time / float = time
  • time/time=float
  • >, <, ==, !=

Conversions

Converting to python datetime

import cmf
pythontime = cmf.year.AsPython()
cmftime=cmf.AsCMFTime(pythontime)
print type(pythontime) # type 'datetime.datetime'
print type(cmftime) # '<class 'cmf.Time'>

Converting to numbers

  • t.AsMilliseconds()
  • t.AsSeconds()
  • t.AsMinutes()
  • t.AsHours()
  • t.AsDays()
  • t.AsYears()

Creating time ranges

import cmf
start=cmf.Time(5,1,2001)
end=cmf.Time(6,1,2001)
step=cmf.h * 6
for t in cmf.timerange(start,end,step):
print t.AsDate()
# Prints:
# 05.01.2001
# 05.01.2001 06:00
# 05.01.2001 12:00
# 05.01.2001 18:00

Public Member Functions

Constructors
 Time (int day, int month, int year, int hour=0, int minute=0, int second=0, int ms=0)
 Construction from date and stores the time as Excel-Time (0 = 31.12.1899 0:00:00)
 
 Time (Date date)
 Conversion constructor.
 
 Time (const Time &t)
 Copy constructor.
 
 Time ()
 Standard constructor.
 
Time unit conversion
double AsDays () const
 Time in days.
 
double AsHours () const
 Time in hours.
 
double AsMinutes () const
 Time in minutes.
 
double AsSeconds () const
 Time in seconds.
 
long long AsMilliseconds () const
 Time in milliseconds.
 
Arithmetic operators
Time operator+ (const Time &t1) const
 Time = Time + Time.
 
Time operator- (const Time &t1) const
 Time = Time - Time.
 
Time operator+= (const Time &t1)
 Time += Time.
 
Time operator-= (const Time &t1)
 Time -= Time.
 
Time operator* (double x) const
 Time = Time * double.
 
Time operator/ (double x) const
 Time = Time / double.
 
Time operator% (const Time &t1) const
 Time = Time % Time.
 
Timeoperator*= (double x)
 Time *= double.
 
Timeoperator/= (double x)
 Time /= double.
 
Time operator* (long long x) const
 Time = Time * int.
 
Time operator/ (long long x) const
 Time = Time / int.
 
Timeoperator*= (long long x)
 Time *= int.
 
Timeoperator/= (long long x)
 Time /= int.
 
Time operator* (int x) const
 Time = Time * int.
 
Time operator/ (int x) const
 Time = Time / int.
 
Timeoperator*= (int x)
 Time *= int.
 
Timeoperator/= (int x)
 Time /= int.
 
Timeoperator%= (const Time &t1)
 Time %= Time.
 
double operator/ (const Time &t1) const
 double = Time / Time
 
long long times_in (const Time &t1) const
 Returns the number of times this is included in t1.
 
Boolean Operators
bool operator< (const Time &t1) const
 bool = Time < Time
 
bool operator> (const Time &t1) const
 bool = Time > Time
 
bool operator<= (const Time &t1) const
 bool = Time <= Time
 
bool operator>= (const Time &t1) const
 bool = Time >= Time
 
bool operator== (const Time &t1) const
 bool = Time == Time
 
bool operator!= (const Time &t1) const
 bool = Time != Time