Timespan

Parent Previous Next

Timespan.

The Timespan type represents a duration of time expressed in days, hours, minutes, seconds and milliseconds. A timespan can be negative or positive. Timespan variables can be used in expressions to calculate datetime or timespan values using simple arithmetic.


Type Specification.

Details of the type and the supported operators:


Category

Description

Default

Standard Type

Value range: 0 00:00:00.000 to ±99999999 23:59:59.999.

Empty


Category

Operators

Primary

x.y   foo(x)

Unary

+   -

Additive

+   -

Relational

<   >    <=   >=

Equality

==   <>


Assignment

:=    +=    -=


Please note that the Datetime and Timespan types provide a basic approach to handle and calculate datetime and timespan values. Advanced aspects related to earth rotation irregularities (leap seconds) etc. are not fully supported.


Using Timespan and Datetime Variables.

A timespan variable default has no value - it is initially empty. It must be assigned a value before being part of an expression, otherwise a runtime error will occur during the VM execution.


Value assignments are done using one of the built-in standard functions or as a result of a calculation as presented in the next example, which demonstrates working with a mix of datetime and timespan variables:



 Datetime dtStart, dtEnd;

 Timespan tsDiff;


 //Thus far all variables are empty.


 dtStart.SetNow();

 //..do some lengthy processing..

 dtEnd.SetNow();


 tsDiff := dtEnd - dtStart; //Calculate duration.

 Integer milliSec := tsDiff.MilliSec();



Supported Operations.

When doing arithmetic or comparisons with a mix of datetime and timespan variables certain value limitations and some common sense rules dictates, how to use additive and relational operators in expressions and assignments. It can be summed up like this:


Violating these rules will generate a compilation error.


In accordance with the stated rules and limitations please take special notice of these situations:


Op1

Operation

Op2

Result

datetime

:=

datetime

datetime

datetime

+=

datetime

ERROR

datetime

-=

datetime

ERROR

datetime

+

datetime

ERROR

datetime

-

datetime

timespan


datetime

:=

timespan

ERROR

datetime

+=

timespan

datetime

datetime

-=

timespan

datetime

datetime

+

timespan

datetime

datetime

-

timespan

datetime


timespan

:=

datetime

ERROR

timespan

+=

datetime

ERROR

timespan

-=

datetime

ERROR

timespan

+

datetime

datetime

timespan

-

datetime

ERROR


timespan

:=

timespan

timespan

timespan

+=

timespan

timespan

timespan

-=

timespan

timespan

timespan

+

timespan

timespan

timespan

-

timespan

timespan


These limitations also apply to internal temporary results. Due to this it can be necessary to reorder the operands of an expression or use parentheses to control the compilation and execution flow:



 Datetime dtA, dtB;

 Timespan tsX, tsY;


 dtA.SetNow();

 dtB.SetNow();

 tsY.Set(0,0,0,0,500);


 //Warning! This statement does not compile!

 tsX := tsY - dtA  + dtB;


 //Instead reorder operands and/or use brackets.

 tsX := tsY + (dtB - dtA); //This will compile!



Standard Functions.

The next pages describe all the standard functions recognized by the compiler as member-functions of the Timespan type.