# Calculations

Timepoints -- Durations -- Intervals (Periods) -- Special Value Handling

Timepoints

This section describes some of basic arithmetic rules that can be performed with timepoints. In general, Timepoints support basic arithmetic in conjunction with Durations as follows:

          Timepoint + Duration  --> Timepoint
          Timepoint - Duration  --> Timepoint
          Timepoint - Timepoint --> Duration

Unlike regular numeric types, the following operations are undefined:

          Duration + Timepoint  --> Undefined
          Duration - Timepoint  --> Undefined
          Timepoint + Timepoint --> Undefined

Durations

Durations represent a length of time and can have positive and negative values. It is frequently useful to be able to perform calculations with other durations and with simple integral values. The following describes these calculations:

          Duration + Duration  --> Duration
          Duration - Duration  --> Duration

          Duration * Integer   --> Duration
          Integer  * Duration  --> Duration
          Duration / Integer   --> Duration  (Integer Division rules)

Intervals (Periods)

Interval logic is extremely useful for simplifying many 'calculations' for dates and times. The following describes the operations provided by periods which are based on half-open range. The following operations calculate new time periods based on two input time periods:

    Timeperiod intersection Timeperiod --> Timeperiod
      (null interval if no intersection)
    Timeperiod merge Timeperiod        --> Timeperiod
      (null interval if no intersection)
    Timeperiod shift Duration          --> Timeperiod
      (shift start and end by duration amount)

In addition, periods support various queries that calculate boolean results. The first set is caluculations with other time periods:

      Timeperiod == Timeperiod           --> bool
      Timeperiod < Timeperiod            --> bool (true if lhs.last <= rhs.begin)
      Timeperiod intersects Timeperiod   --> bool
      Timeperiod contains Timeperiod     --> bool
      Timeperiod is_adjacent Timeperiod  --> bool

The following calculations are performed versus the Timepoint.

      Timeperiod contains Timepoint      --> bool
      Timeperiod is_before Timepoint     --> bool
      Timeperiod is_after Timepoint      --> bool

Special Value Handling

For many temporal problems it is useful for Duration and Timepoint types to support special values such as Not A Date Time (NADT) and infinity. In general special values such as Not A Date Time (NADT) and infinity should follow rules like floating point values. Note that it should be possible to configure NADT based systems to throw an exception instead of result in NADT.

      Timepoint(NADT) + Duration --> Timepoint(NADT)
      Timepoint(∞) + Duration    --> Timepoint(∞)
      Timepoint + Duration(∞)    --> Timepoint(∞)
      Timepoint - Duration(∞)    --> Timepoint(-∞)

When performing operations on both positive and negative infinities, the library will produce results consistent with the following.

      Timepoint(+∞) + Duration(-∞) --> NADT
      Duration(+∞) + Duration(-∞)  --> NADT
      Duration(±∞) * Zero          --> NADT

      Duration(∞) * Integer(Not Zero) --> Duration(∞)
      Duration(+∞) * -Integer         --> Duration(-∞)
      Duration(∞) / Integer           --> Duration(∞)