1 | <?xml version="1.0" encoding="utf-8"?> |
---|
2 | <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" |
---|
3 | "../../../tools/boostbook/dtd/boostbook.dtd"> |
---|
4 | |
---|
5 | <!-- Copyright (c) 2001-2004 CrystalClear Software, Inc. |
---|
6 | Subject to the Boost Software License, Version 1.0. |
---|
7 | (See accompanying file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0) |
---|
8 | --> |
---|
9 | |
---|
10 | <section id="date_time.calculations"> |
---|
11 | <title>Calculations</title> |
---|
12 | |
---|
13 | <para> |
---|
14 | <link linkend="timepoints">Timepoints</link> -- |
---|
15 | <link linkend="durations">Durations</link> -- |
---|
16 | <link linkend="intervals">Intervals (Periods)</link> -- |
---|
17 | <link linkend="special_value_handling">Special Value Handling</link> |
---|
18 | </para> |
---|
19 | <anchor id="timepoints" /> |
---|
20 | <bridgehead renderas="sect3">Timepoints</bridgehead> |
---|
21 | <para> |
---|
22 | 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: |
---|
23 | <programlisting> |
---|
24 | Timepoint + Duration --> Timepoint |
---|
25 | Timepoint - Duration --> Timepoint |
---|
26 | Timepoint - Timepoint --> Duration |
---|
27 | </programlisting> |
---|
28 | Unlike regular numeric types, the following operations are undefined: |
---|
29 | <programlisting> |
---|
30 | Duration + Timepoint --> Undefined |
---|
31 | Duration - Timepoint --> Undefined |
---|
32 | Timepoint + Timepoint --> Undefined |
---|
33 | </programlisting> |
---|
34 | </para> |
---|
35 | <anchor id="durations" /> |
---|
36 | <bridgehead renderas="sect3">Durations</bridgehead> |
---|
37 | <para> |
---|
38 | 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: |
---|
39 | <programlisting> |
---|
40 | Duration + Duration --> Duration |
---|
41 | Duration - Duration --> Duration |
---|
42 | |
---|
43 | Duration * Integer --> Duration |
---|
44 | Integer * Duration --> Duration |
---|
45 | Duration / Integer --> Duration (Integer Division rules) |
---|
46 | </programlisting> |
---|
47 | </para> |
---|
48 | <anchor id="intervals" /> |
---|
49 | <bridgehead renderas="sect3">Intervals (Periods)</bridgehead> |
---|
50 | <para> |
---|
51 | 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: |
---|
52 | <programlisting> |
---|
53 | Timeperiod intersection Timeperiod --> Timeperiod |
---|
54 | (null interval if no intersection) |
---|
55 | Timeperiod merge Timeperiod --> Timeperiod |
---|
56 | (null interval if no intersection) |
---|
57 | Timeperiod shift Duration --> Timeperiod |
---|
58 | (shift start and end by duration amount) |
---|
59 | </programlisting> |
---|
60 | In addition, periods support various queries that calculate boolean results. The first set is caluculations with other time periods: |
---|
61 | <programlisting> |
---|
62 | Timeperiod == Timeperiod --> bool |
---|
63 | Timeperiod < Timeperiod --> bool (true if lhs.last <= rhs.begin) |
---|
64 | Timeperiod intersects Timeperiod --> bool |
---|
65 | Timeperiod contains Timeperiod --> bool |
---|
66 | Timeperiod is_adjacent Timeperiod --> bool |
---|
67 | </programlisting> |
---|
68 | The following calculations are performed versus the Timepoint. |
---|
69 | <programlisting> |
---|
70 | Timeperiod contains Timepoint --> bool |
---|
71 | Timeperiod is_before Timepoint --> bool |
---|
72 | Timeperiod is_after Timepoint --> bool |
---|
73 | </programlisting> |
---|
74 | </para> |
---|
75 | <anchor id="special_value_handling" /> |
---|
76 | <bridgehead renderas="sect3">Special Value Handling</bridgehead> |
---|
77 | <para> |
---|
78 | 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. |
---|
79 | <programlisting> |
---|
80 | Timepoint(NADT) + Duration --> Timepoint(NADT) |
---|
81 | Timepoint(∞) + Duration --> Timepoint(∞) |
---|
82 | Timepoint + Duration(∞) --> Timepoint(∞) |
---|
83 | Timepoint - Duration(∞) --> Timepoint(-∞) |
---|
84 | </programlisting> |
---|
85 | When performing operations on both positive and negative infinities, the library will produce results consistent with the following. |
---|
86 | <programlisting> |
---|
87 | Timepoint(+∞) + Duration(-∞) --> NADT |
---|
88 | Duration(+∞) + Duration(-∞) --> NADT |
---|
89 | Duration(±∞) * Zero --> NADT |
---|
90 | |
---|
91 | Duration(∞) * Integer(Not Zero) --> Duration(∞) |
---|
92 | Duration(+∞) * -Integer --> Duration(-∞) |
---|
93 | Duration(∞) / Integer --> Duration(∞) |
---|
94 | </programlisting> |
---|
95 | </para> |
---|
96 | </section> |
---|