Timepoints -- Durations -- Intervals (Periods) -- Special Value Handling TimepointsThis 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
DurationsDurations 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 HandlingFor 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(∞)
Unavoidable Trade-offsThe library does its best to provide everything a user could want, but there are certain inherent constraints that limit what any temporal library can do. Specifically, a user must choose which two of the following three capabilities are desired in any particular application:
Some libraries may implicitly promise to deliver all three, but if you actually put them to the test, only two can be true at once. This limitation is not a deficiency in the design or implementation of any particular library; rather it is a consequence of the way different time systems are defined by international standards. Let's look at each of the three cases: If you want exact agreement with wall-clock time, you must use either UTC or local time. If you compute a duration by subtracting one UTC time from another and you want an answer accurate to the second, the two times must not be too far in the future because leap seconds affect the count but are only determined about 6 months in advance. With local times a future duration calculation could be off by an entire hour, since legislatures can and do change DST rules at will. If you want to handle wall-clock times in the future, you won't be able (in the general case) to calculate exact durations, for the same reasons described above. If you want accurate calculations with future times, you will have to use TAI or an equivalent, but the mapping from TAI to UTC or local time depends on leap seconds, so you will not have exact agreement with wall-clock time. Stability, Predictability, and ApproximationsHere is some underlying theory that helps to explain what's going on. Remember that a temporal type, like any abstract data type (ADT), is a set of values together with operations on those values. StabilityThe representation of a type is stable if the bit pattern associated with a given value does not change over time. A type with an unstable representation is unlikely to be of much use to anyone, so we will insist that any temporal library use only stable representations. An operation on a type is stable if the result of applying the operation to a particular operand(s) does not change over time. PredictabilitySets are most often classified into two categories: well-defined and ill-defined. Since a type is a set, we can extend these definitions to cover types. For any type T, there must be a predicate is_member( x ) which determines whether a value x is a member of type T. This predicate must return true, false, or dont_know. If for all x, is_member( x ) returns either true or false, we say the set T is well-defined. If for any x, is_member( x ) returns dont_know, we say the set T is ill-defined. Those are the rules normally used in math. However, because of the special characteristics of temporal types, it is useful to refine this view and create a third category as follows: For any temporal type T, there must be a predicate is_member( x, t ) which determines whether a value x is a member of T. The parameter t represents the time when the predicate is evaluated. For each xi, there must be a time ti and a value v such that:
ti is thus the time when we "find out" whether xi is a member of T. Now we can define three categories of temporal types: If for all xi, ti = negative infinity, we say the type T is predictable. If for some xi, ti = positive infinity, we say the type T is ill-formed. Otherwise we say the type T is unpredictable (this implies that for some xi, ti is finite). Ill-formed sets are not of much practical use, so we will not discuss them further. In plain english the above simply says that all the values of a predictable type are known ahead of time, but some values of an unpredictable type are not known until some particular time. Stability of OperationsPredictable types have a couple of important properties:
The practical effect of this is that duration calculations can be implemented with simple integer subtraction. Examples of predictable types are TAI timepoints and Gregorian dates. Unpredictable types have exactly the opposite properties:
Examples of unpredictable types are UTC timepoints and Local Time timepoints. We can refine this a little by saying that a range within an unpredicatable type can be predictable, and operations performed entirely on values within that range will be stable. For example, the range of UTC timepoints from 1970-01-01 through the present is predictable, so calculations of durations within that range will be stable. ApproximationsThese limitations are problematical, because important temporal types like UTC and Local Time are in fact unpredictable, and therefore operations on them are sometimes unstable. Yet as a practical matter we often want to perform this kind of operation, such as computing the duration between two timepoints in the future that are specified in Local Time. The best the library can do is to provide an approximation, which is generally possible and for most purposes will be good enough. Of course the documentation must specify when an answer will be approximate (and thus unstable) and how big the error may be. In many respects calculating with unpredictable sets is analogous to the use of floating point numbers, for which results are expected to only be approximately correct. Calculating with predictable sets would then be analogous to the user of integers, where results are expected to be exact. For situations where exact answers are required or instability cannot be tolerated, the user must be able to specify this, and then the library should throw an exception if the user requests a computation for which an exact, stable answer is not possible. The following are a number of terms relevant to the date-time domain. A taxonomy of temporal types:
And some other terms:
Some standard date-time terminology:
Some more experimental ones:
These are design sorts of terms:
The design of the library is currently being evolved using Wiki and email discussions. You can find more information at: Boost Wiki GDTL Start Page.
Date Calendar References
Time
Other C/C++ Libraries
JAVA Date & Time Library Quick Reference
Scripting Language Libraries
Related Commercial and Fanciful Pages
Resolution, Precision, and Accuracy
Overview -- Compilation Options -- Compiler/Portability Notes -- Directory Structure -- Required Boost Libraries OverviewThe library has a few functions that require the creation of a library file (mostly to_string, from_string functions). Most library users can make effective use of the library WITHOUT building the library, but simply including the required headers. If the library is needed, the Jamfile in the build directory will produce a "static" library (libboost_date_time) and a "dynamic/shared" library (boost_date_time) that contains these functions. Compilation OptionsBy default the posix_time system uses a single 64 bit integer internally to provide a microsecond level resolution. As an alternative, a combination of a 64 bit integer and a 32 bit integer (96 bit resolution) can be used to provide nano-second level resolutions. The default implementation may provide better performance and more compact memory usage for many applications that do not require nano-second resolutions.
To use the alternate resolution (96 bit nanosecond) the variable As of version 1.33, the date_time library introduced a new IO streaming system. Some compilers are not capable of utilizing this new system. For those compilers the earlier ("legacy") IO system is still available. Non-supported compilers will select the legacy system automatically but the user can force the usage of the legacy system by defining As a convenience, Another convenience is the default constructors for Compiler/Portability NotesThe Boost Date-Time library has been built and tested with many compilers and platforms. However, some compilers and standard libraries have issues. While some of these issues can be worked around, others are difficult to work around. The following compilers are known to fully support all aspects of the library:
Unfortunately, the VC8 compiler has some issues with date-time code. The most serious issue is a memory leak which was introduced into the VC8 standard library basic_stream code. Date-time has code has been changed to avoid this as much as possible, but if you are using the legacy IO option (NOT the default with VC8) then the issue can still arise. See the mailing list archive for more details.
In addition to the problem above, some versions of the VC8 library have limited
the range of allowed
values in the
These compilers support all aspects of the library except
In particular, a lack of support for standard locales limits the ability of the library to support iostream based input output. For these compilers a set of more limited string based input-output is provided. Some compilers/standard libraries with this limitation include:
Official support for some older compilers has now been dropped. This includes:
Visual Studio & STLPortThere is a known issue with Visual Studio (7.0 & 7.1) and STLPort. The build errors typically make reference to a type issue or 'no acceptable conversion' and are attempting to instantiate a template with To build date_time with no wide stream/string etc, execute the following command from bjam -a "-sTOOLS=vc-7_1-stlport" "-sSTLPORT_PATH=..." \ "-sBUILD=<define>BOOST_NO_STD_WSTRING" \ --stagedir=... --with-date_time stage
(replace the ellipsis with the correct paths for the build system and adjust the Rebuilding STLPort with bjam -a "-sTOOLS=vc-7_1-stlport" "-sSTLPORT_PATH=..." \ "-sBUILD=&native-wchar_t>on" \ --stagedir=... --with-date_time stage
(replace the ellipsis with the correct paths for the build system and adjust the Directory StructureThe directory tree has the following structure: /boost/date_time -- common headers and template code /boost/date_time/gregorian -- Gregorian date system header files /boost/date_time/posix_time -- Posix time system headers /boost/date_time/local_time -- Local time system headers /libs/date_time/build -- build files and output directory /libs/date_time/test -- test battery for generic code /libs/date_time/test/gregorian -- test battery for the Gregorian system /libs/date_time/test/posix_time -- test battery for the posix_time system /libs/date_time/test/local_time -- test battery for the local_time system /libs/date_time/examples/gregorian -- example programs for dates /libs/date_time/examples/posix_time -- time example programs /libs/date_time/examples/local_time -- nifty example programs /libs/date_time/src/gregorian -- cpp files for libboost_date_time /libs/date_time/src/posix_time -- empty (one file, but no source code...)
Required Boost LibrariesVarious parts of date-time depend on other boost libraries. These include: so these libraries need to be installed. The library provides a large number of tests in the libs/date_time/test libs/date_time/test/gregorian libs/date_time/test/posix_time libs/date_time/test/local_time directories. Building and executing these tests assures that the installation is correct and that the library is functioning correctly. In addition, these tests facilitate the porting to new compilers. Finally, the tests provide examples of many functions not explicitly described in the usage examples. Changes from Boost 1.33 to 1.34 (date_time 1.04 to 1.05)
Changes from Boost 1.32 to 1.33 (date_time 1.03 to 1.04)
Changes from Boost 1.31 to 1.32 (date_time 1.02 to 1.03)
Changes from Boost 1.30 to 1.31 (date_time 1.01 to 1.02)
Changes from Boost 1.29 to 1.30 (date_time 1.00 to 1.01)Notice: The interface to the partial_date class (see date_algorithms) was changed. The order of construction parameters was changed which will cause some code to fail execution. This change was made to facilitate more generic local time adjustment code. Thus instead of specifying partial_date pd(Dec,25) the code needs to be changed to partial_date pd(25, Dec);
Many people have contributed to the development of this library. In particular Hugo Duncan and Joel de Guzman for help with porting to various compilers. For initial development of concepts and design Corwin Joy and Michael Kenniston deserve special thanks. Also extra thanks to Michael for writing up the theory and tradeoffs part of the documentation. Dave Zumbro for initial inspiration and sage thoughts. Many thanks to boost reviewers and users including: William Seymour, Kjell Elster, Beman Dawes, Gary Powell, Andrew Maclean, William Kempf, Peter Dimov, Chris Little, David Moore, Darin Adler, Gennadiy Rozental, Joachim Achtzehnter, Paul Bristow, Jan Langer, Mark Rodgers, Glen Knowles, Matthew Denman, and George Heintzelman. |