Boost.Locale
|
There are several important flaws in the standard C, C++ and Boost libraries that handle dates and time:
boost::date_time
, std::tm
, and standard functions like localtime
and gmtime
, all assume the Gregorian calendar.mktime
and localtime
, but they do not give user the information about the first day of week. This information is locale dependent. It is Monday in France and it is Sunday in United States.Boost.Locale provides generic date_time, and calendar classes that allow you to perform operations on dates and times for non-Gregorian calendars such as Hebrew, Islamic, Japanese and others.
Non-ICU based backends support the Gregorian calendar only. Unlike boost::date_time
, they are fully aware of the local first day of week. Thus, if the current day of week is Monday, then setting "current day of week" to Sunday would move the actual date 6 days forward in Russian or French locales and move one day backward in USA and Israeli locales.
For example:
You can calculate the difference between dates by dividing the difference by a period:
You can also use different syntax (less operator overloading)
date_time – provides the member functions minimum and maximum to get the information about smallest and largest possible values of a certain period for a specific time.
For example, for February the maximum(period::day())
would be 28 (or 29 for a leap year), and for January it would be 31.
We recommend you to look at the calendar.cpp
example provided with this library to get an understanding of how to manipulate dates and times using these classes.
To convert between various calendar dates, you may get the current POSIX time via the time member function.
For example:
Non-ICU based backends support the same date-time range as mktime
and localtime
C library functions.
time_t
is 32 bits wide (mingw), then the upper limit is year 2038The current operating system's time zone is used by default, however the time zone can be modified at several different levels:
Non-ICU based backends support only two kinds of time zones:
localtime
and mktime
the standard library functions - the default time zoneWriting a date_time is equivalent to:
For example this code:
Would print something like this (using the default format):
2/3/2011 12:00 am
However if you need to change the default behavior (for example show only date), then you need to use specific iostream manipulators in order to display current date or time.
For example:
Would print something like:
2/3/2011
This is important to remember that date_time
object is always rendered and parsed in the context of the iostream's
locale and time zone and not in the context of specific date_time
object.
Why should I use Boost.Locale over Boost.DateTime when I only need the Gregorian calendar?
date_time(some_time.time() + 3600)
may be not equal to some_time + hour()
because of daylight savings time.Why don't you use Boost.DateTime time zone support?
Boost.DateTime's time zone support is broken. Time zones can not be represented with a simple table of rules where daylight saving depend only on certain n'th day of week in month. The daylight savings time may vary by year, political issues, and many other things.
Most of the modern operating systems (Linux, *BSD, Mac OS X, OpenVMS) and many important software packages (ICU, Java, Python) use so called Olson database in order to handle daylight saving time correctly.
If you need full time zone database support, then you should use ICU library.