calculations.xml 4.6 KB

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