Chris@16: // Chris@16: // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: #ifndef BOOST_LOCALE_DATE_TIME_HPP_INCLUDED Chris@16: #define BOOST_LOCALE_DATE_TIME_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: #ifdef BOOST_MSVC Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable : 4275 4251 4231 4660) Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost { Chris@16: namespace locale { Chris@16: /// Chris@16: /// \defgroup date_time Date, Time, Timezone and Calendar manipulations Chris@16: /// Chris@16: /// This module provides various calendar, timezone and date time services Chris@16: /// Chris@16: /// @{ Chris@16: Chris@16: Chris@16: /// Chris@16: /// \brief This error is thrown in case of invalid state that occurred Chris@16: /// Chris@16: class BOOST_SYMBOL_VISIBLE date_time_error : public std::runtime_error { Chris@16: public: Chris@16: /// Chris@16: /// Constructor of date_time_error class Chris@16: /// Chris@16: date_time_error(std::string const &e) : std::runtime_error(e) {} Chris@16: }; Chris@16: Chris@16: Chris@16: /// Chris@16: /// \brief This class represents a pair of period_type and the integer Chris@16: /// values that describes its amount. For example 3 days or 4 years. Chris@16: /// Chris@16: /// Usually obtained as product of period_type and integer or Chris@16: /// my calling a representative functions Chris@16: /// For example day()*3 == date_time_period(day(),3) == day(3) Chris@16: /// Chris@16: struct date_time_period Chris@16: { Chris@16: period::period_type type; ///< The type of period, i.e. era, year, day etc. Chris@16: int value; ///< The value the actual number of \a periods Chris@16: /// Chris@16: /// Operator + returns copy of itself Chris@16: /// Chris@16: date_time_period operator+() const { return *this; } Chris@16: /// Chris@16: /// Operator -, switches the sign of period Chris@16: /// Chris@16: date_time_period operator-() const { return date_time_period(type,-value); } Chris@16: Chris@16: /// Chris@16: /// Constructor that creates date_time_period from period_type \a f and a value \a v -- default 1. Chris@16: /// Chris@16: date_time_period(period::period_type f=period::period_type(),int v=1) : type(f), value(v) {} Chris@16: }; Chris@16: Chris@16: namespace period { Chris@16: /// Chris@16: /// Get period_type for: special invalid value, should not be used directly Chris@16: /// Chris@16: inline period_type invalid(){ return period_type(marks::invalid); } Chris@16: /// Chris@16: /// Get period_type for: Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1] Chris@16: /// Chris@16: inline period_type era(){ return period_type(marks::era); } Chris@16: /// Chris@16: /// Get period_type for: Year, it is calendar specific, for example 2011 in Gregorian calendar. Chris@16: /// Chris@16: inline period_type year(){ return period_type(marks::year); } Chris@16: /// Chris@16: /// Get period_type for: Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1. Chris@16: /// Chris@16: inline period_type extended_year(){ return period_type(marks::extended_year); } Chris@16: /// Chris@16: /// Get period_type for: The month of year, calendar specific, in Gregorian [0..11] Chris@16: /// Chris@16: inline period_type month(){ return period_type(marks::month); } Chris@16: /// Chris@16: /// Get period_type for: The day of month, calendar specific, in Gregorian [1..31] Chris@16: /// Chris@16: inline period_type day(){ return period_type(marks::day); } Chris@16: /// Chris@16: /// Get period_type for: The number of day in year, starting from 1, in Gregorian [1..366] Chris@16: /// Chris@16: inline period_type day_of_year(){ return period_type(marks::day_of_year); } Chris@16: /// Chris@16: /// Get period_type for: Day of week, Sunday=1, Monday=2,..., Saturday=7. Chris@16: /// Chris@16: /// Note that updating this value respects local day of week, so for example, Chris@16: /// If first day of week is Monday and the current day is Tuesday then setting Chris@16: /// the value to Sunday (1) would forward the date by 5 days forward and not backward Chris@16: /// by two days as it could be expected if the numbers were taken as is. Chris@16: /// Chris@16: inline period_type day_of_week(){ return period_type(marks::day_of_week); } Chris@16: /// Chris@16: /// Get period_type for: Original number of the day of the week in month. For example 1st Sunday, Chris@16: /// 2nd Sunday, etc. in Gregorian [1..5] Chris@16: /// Chris@16: inline period_type day_of_week_in_month(){ return period_type(marks::day_of_week_in_month); } Chris@16: /// Chris@16: /// Get period_type for: Local day of week, for example in France Monday is 1, in US Sunday is 1, [1..7] Chris@16: /// Chris@16: inline period_type day_of_week_local(){ return period_type(marks::day_of_week_local); } Chris@16: /// Chris@16: /// Get period_type for: 24 clock hour [0..23] Chris@16: /// Chris@16: inline period_type hour(){ return period_type(marks::hour); } Chris@16: /// Chris@16: /// Get period_type for: 12 clock hour [0..11] Chris@16: /// Chris@16: inline period_type hour_12(){ return period_type(marks::hour_12); } Chris@16: /// Chris@16: /// Get period_type for: am or pm marker [0..1] Chris@16: /// Chris@16: inline period_type am_pm(){ return period_type(marks::am_pm); } Chris@16: /// Chris@16: /// Get period_type for: minute [0..59] Chris@16: /// Chris@16: inline period_type minute(){ return period_type(marks::minute); } Chris@16: /// Chris@16: /// Get period_type for: second [0..59] Chris@16: /// Chris@16: inline period_type second(){ return period_type(marks::second); } Chris@16: /// Chris@16: /// Get period_type for: The week number in the year Chris@16: /// Chris@16: inline period_type week_of_year(){ return period_type(marks::week_of_year); } Chris@16: /// Chris@16: /// Get period_type for: The week number within current month Chris@16: /// Chris@16: inline period_type week_of_month(){ return period_type(marks::week_of_month); } Chris@16: /// Chris@16: /// Get period_type for: First day of week, constant, for example Sunday in US = 1, Monday in France = 2 Chris@16: /// Chris@16: inline period_type first_day_of_week(){ return period_type(marks::first_day_of_week); } Chris@16: Chris@16: /// Chris@16: /// Get date_time_period for: Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1] Chris@16: /// Chris@16: inline date_time_period era(int v) { return date_time_period(era(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: Year, it is calendar specific, for example 2011 in Gregorian calendar. Chris@16: /// Chris@16: inline date_time_period year(int v) { return date_time_period(year(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1. Chris@16: /// Chris@16: inline date_time_period extended_year(int v) { return date_time_period(extended_year(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: The month of year, calendar specific, in Gregorian [0..11] Chris@16: /// Chris@16: inline date_time_period month(int v) { return date_time_period(month(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: The day of month, calendar specific, in Gregorian [1..31] Chris@16: /// Chris@16: inline date_time_period day(int v) { return date_time_period(day(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: The number of day in year, starting from 1, in Gregorian [1..366] Chris@16: /// Chris@16: inline date_time_period day_of_year(int v) { return date_time_period(day_of_year(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: Day of week, Sunday=1, Monday=2,..., Saturday=7. Chris@16: /// Chris@16: /// Note that updating this value respects local day of week, so for example, Chris@16: /// If first day of week is Monday and the current day is Tuesday then setting Chris@16: /// the value to Sunday (1) would forward the date by 5 days forward and not backward Chris@16: /// by two days as it could be expected if the numbers were taken as is. Chris@16: /// Chris@16: inline date_time_period day_of_week(int v) { return date_time_period(day_of_week(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: Original number of the day of the week in month. For example 1st Sunday, Chris@16: /// 2nd Sunday, etc. in Gregorian [1..5] Chris@16: /// Chris@16: inline date_time_period day_of_week_in_month(int v) { return date_time_period(day_of_week_in_month(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: Local day of week, for example in France Monday is 1, in US Sunday is 1, [1..7] Chris@16: /// Chris@16: inline date_time_period day_of_week_local(int v) { return date_time_period(day_of_week_local(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: 24 clock hour [0..23] Chris@16: /// Chris@16: inline date_time_period hour(int v) { return date_time_period(hour(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: 12 clock hour [0..11] Chris@16: /// Chris@16: inline date_time_period hour_12(int v) { return date_time_period(hour_12(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: am or pm marker [0..1] Chris@16: /// Chris@16: inline date_time_period am_pm(int v) { return date_time_period(am_pm(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: minute [0..59] Chris@16: /// Chris@16: inline date_time_period minute(int v) { return date_time_period(minute(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: second [0..59] Chris@16: /// Chris@16: inline date_time_period second(int v) { return date_time_period(second(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: The week number in the year Chris@16: /// Chris@16: inline date_time_period week_of_year(int v) { return date_time_period(week_of_year(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: The week number within current month Chris@16: /// Chris@16: inline date_time_period week_of_month(int v) { return date_time_period(week_of_month(),v); } Chris@16: /// Chris@16: /// Get date_time_period for: First day of week, constant, for example Sunday in US = 1, Monday in France = 2 Chris@16: /// Chris@16: inline date_time_period first_day_of_week(int v) { return date_time_period(first_day_of_week(),v); } Chris@16: Chris@16: /// Chris@16: /// Get predefined constant for January Chris@16: /// Chris@16: inline date_time_period january() { return date_time_period(month(),0); } Chris@16: /// Chris@16: /// Get predefined constant for February Chris@16: /// Chris@16: inline date_time_period february() { return date_time_period(month(),1); } Chris@16: /// Chris@16: /// Get predefined constant for March Chris@16: /// Chris@16: inline date_time_period march() { return date_time_period(month(),2); } Chris@16: /// Chris@16: /// Get predefined constant for April Chris@16: /// Chris@16: inline date_time_period april() { return date_time_period(month(),3); } Chris@16: /// Chris@16: /// Get predefined constant for May Chris@16: /// Chris@16: inline date_time_period may() { return date_time_period(month(),4); } Chris@16: /// Chris@16: /// Get predefined constant for June Chris@16: /// Chris@16: inline date_time_period june() { return date_time_period(month(),5); } Chris@16: /// Chris@16: /// Get predefined constant for July Chris@16: /// Chris@16: inline date_time_period july() { return date_time_period(month(),6); } Chris@16: /// Chris@16: /// Get predefined constant for August Chris@16: /// Chris@16: inline date_time_period august() { return date_time_period(month(),7); } Chris@16: /// Chris@16: /// Get predefined constant for September Chris@16: /// Chris@16: inline date_time_period september() { return date_time_period(month(),8); } Chris@16: /// Chris@16: /// Get predefined constant for October Chris@16: /// Chris@16: inline date_time_period october() { return date_time_period(month(),9); } Chris@16: /// Chris@16: /// Get predefined constant for November Chris@16: /// Chris@16: inline date_time_period november() { return date_time_period(month(),10); } Chris@16: /// Chris@16: /// Get predefined constant for December Chris@16: /// Chris@16: inline date_time_period december() { return date_time_period(month(),11); } Chris@16: Chris@16: /// Chris@16: /// Get predefined constant for Sunday Chris@16: /// Chris@16: inline date_time_period sunday() { return date_time_period(day_of_week(),1); } Chris@16: /// Chris@16: /// Get predefined constant for Monday Chris@16: /// Chris@16: inline date_time_period monday() { return date_time_period(day_of_week(),2); } Chris@16: /// Chris@16: /// Get predefined constant for Tuesday Chris@16: /// Chris@16: inline date_time_period tuesday() { return date_time_period(day_of_week(),3); } Chris@16: /// Chris@16: /// Get predefined constant for Wednesday Chris@16: /// Chris@16: inline date_time_period wednesday() { return date_time_period(day_of_week(),4); } Chris@16: /// Chris@16: /// Get predefined constant for Thursday Chris@16: /// Chris@16: inline date_time_period thursday() { return date_time_period(day_of_week(),5); } Chris@16: /// Chris@16: /// Get predefined constant for Friday Chris@16: /// Chris@16: inline date_time_period friday() { return date_time_period(day_of_week(),6); } Chris@16: /// Chris@16: /// Get predefined constant for Saturday Chris@16: /// Chris@16: inline date_time_period saturday() { return date_time_period(day_of_week(),7); } Chris@16: /// Chris@16: /// Get predefined constant for AM (Ante Meridiem) Chris@16: /// Chris@16: inline date_time_period am() { return date_time_period(am_pm(),0); } Chris@16: /// Chris@16: /// Get predefined constant for PM (Post Meridiem) Chris@16: /// Chris@16: inline date_time_period pm() { return date_time_period(am_pm(),1); } Chris@16: Chris@16: /// Chris@16: /// convert period_type to date_time_period(f,1) Chris@16: /// Chris@16: inline date_time_period operator+(period::period_type f) Chris@16: { Chris@16: return date_time_period(f); Chris@16: } Chris@16: /// Chris@16: /// convert period_type to date_time_period(f,-1) Chris@16: /// Chris@16: inline date_time_period operator-(period::period_type f) Chris@16: { Chris@16: return date_time_period(f,-1); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Create date_time_period of type \a f with value \a v. Chris@16: /// Chris@16: template Chris@16: date_time_period operator*(period::period_type f,T v) Chris@16: { Chris@16: return date_time_period(f,v); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Create date_time_period of type \a f with value \a v. Chris@16: /// Chris@16: template Chris@16: date_time_period operator*(T v,period::period_type f) Chris@16: { Chris@16: return date_time_period(f,v); Chris@16: } Chris@16: /// Chris@16: /// Create date_time_period of type \a f with value \a v. Chris@16: /// Chris@16: template Chris@16: date_time_period operator*(T v,date_time_period f) Chris@16: { Chris@16: return date_time_period(f.type,f.value*v); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Create date_time_period of type \a f with value \a v. Chris@16: /// Chris@16: template Chris@16: date_time_period operator*(date_time_period f,T v) Chris@16: { Chris@16: return date_time_period(f.type,f.value*v); Chris@16: } Chris@16: Chris@16: Chris@16: } // period Chris@16: Chris@16: Chris@16: /// Chris@16: /// \brief this class that represents a set of periods, Chris@16: /// Chris@16: /// It is generally created by operations on periods: Chris@16: /// 1995*year + 3*month + 1*day. Note: operations are not commutative. Chris@16: /// Chris@16: class date_time_period_set { Chris@16: public: Chris@16: Chris@16: /// Chris@16: /// Default constructor - empty set Chris@16: /// Chris@16: date_time_period_set() Chris@16: { Chris@16: } Chris@16: /// Chris@16: /// Create a set of single period with value 1 Chris@16: /// Chris@16: date_time_period_set(period::period_type f) Chris@16: { Chris@16: basic_[0]=date_time_period(f); Chris@16: } Chris@16: /// Chris@16: /// Create a set of single period \a fl Chris@16: /// Chris@16: date_time_period_set(date_time_period const &fl) Chris@16: { Chris@16: basic_[0]=fl; Chris@16: } Chris@16: /// Chris@16: /// Append date_time_period \a f to the set Chris@16: /// Chris@16: void add(date_time_period f) Chris@16: { Chris@16: size_t n=size(); Chris@16: if(n < 4) Chris@16: basic_[n]=f; Chris@16: else Chris@16: periods_.push_back(f); Chris@16: } Chris@16: /// Chris@16: /// Get number if items in list Chris@16: /// Chris@16: size_t size() const Chris@16: { Chris@16: if(basic_[0].type == period::period_type()) Chris@16: return 0; Chris@16: if(basic_[1].type == period::period_type()) Chris@16: return 1; Chris@16: if(basic_[2].type == period::period_type()) Chris@16: return 2; Chris@16: if(basic_[3].type == period::period_type()) Chris@16: return 3; Chris@16: return 4+periods_.size(); Chris@16: } Chris@16: /// Chris@16: /// Get item at position \a n the set, n should be in range [0,size) Chris@16: /// Chris@16: date_time_period const &operator[](size_t n) const Chris@16: { Chris@16: if(n >= size()) Chris@16: throw std::out_of_range("Invalid index to date_time_period"); Chris@16: if(n < 4) Chris@16: return basic_[n]; Chris@16: else Chris@16: return periods_[n-4]; Chris@16: } Chris@16: private: Chris@16: date_time_period basic_[4]; Chris@16: std::vector periods_; Chris@16: }; Chris@16: Chris@16: Chris@16: /// Chris@16: /// Append two periods sets. Note this operator is not commutative Chris@16: /// Chris@16: inline date_time_period_set operator+(date_time_period_set const &a,date_time_period_set const &b) Chris@16: { Chris@16: date_time_period_set s(a); Chris@16: for(unsigned i=0;i impl_; Chris@16: }; Chris@16: Chris@16: /// Chris@16: /// \brief this class represents a date time and allows to perform various operation according to the Chris@16: /// locale settings. Chris@16: /// Chris@16: /// This class allows to manipulate various aspects of dates and times easily using arithmetic operations with Chris@16: /// periods. Chris@16: /// Chris@16: /// General arithmetic functions: Chris@16: /// Chris@16: /// - date_time + date_time_period_set = date_time: move time point forward by specific periods like date_time + month; Chris@16: /// - date_time - date_time_period_set = date_time: move time point backward by specific periods like date_time - month; Chris@16: /// - date_time << date_time_period_set = date_time: roll time point forward by specific periods with rolling to begin if overflows: like "2010-01-31" << 2* day == "2010-01-02" instead of "2010-02-02" Chris@16: /// - date_time >> date_time_period_set = date_time: roll time point backward by specific periods with rolling to end if overflows: like "2010-01-02" >> 2* day == "2010-01-31" instead of "2009-12-30" Chris@16: /// - date_time / period_type = int - current period value: like "2010-12-21" / month == 12. "2010-12-21" / year = 2010 Chris@16: /// - (date_time - date_time) / period_type = int: distance between dates in period_type. Like ("2010-12-01" - "2008-12-01") / month = 24. Chris@16: /// Chris@16: /// You can also assign specific periods using assignment operator like: Chris@16: /// some_time = year * 1995 that sets the year to 1995. Chris@16: /// Chris@16: /// Chris@16: Chris@16: class BOOST_LOCALE_DECL date_time { Chris@16: public: Chris@16: Chris@16: /// Chris@16: /// Dafault constructor, uses default calendar initialized date_time object to current time. Chris@16: /// Chris@16: /// \note throws std::bad_cast if the global locale does not have \ref calendar_facet facet installed Chris@16: /// Chris@16: date_time(); Chris@16: /// Chris@16: /// copy date_time Chris@16: /// Chris@16: date_time(date_time const &other); Chris@16: /// Chris@16: /// copy date_time and change some fields according to the \a set Chris@16: /// Chris@16: date_time(date_time const &other,date_time_period_set const &set); Chris@16: /// Chris@16: /// assign the date_time Chris@16: /// Chris@16: date_time const &operator=(date_time const &other); Chris@16: ~date_time(); Chris@16: Chris@16: /// Chris@16: /// Create a date_time object using POSIX time \a time and default calendar Chris@16: /// Chris@16: /// \note throws std::bad_cast if the global locale does not have \ref calendar_facet facet installed Chris@16: /// Chris@16: date_time(double time); Chris@16: /// Chris@16: /// Create a date_time object using POSIX time \a time and calendar \a cal Chris@16: /// Chris@16: date_time(double time,calendar const &cal); Chris@16: /// Chris@16: /// Create a date_time object using calendar \a cal and initializes it to current time. Chris@16: /// Chris@16: date_time(calendar const &cal); Chris@16: Chris@16: /// Chris@16: /// Create a date_time object using default calendar and define values given in \a set Chris@16: /// Chris@16: /// \note throws std::bad_cast if the global locale does not have \ref calendar_facet facet installed Chris@16: /// Chris@16: date_time(date_time_period_set const &set); Chris@16: /// Chris@16: /// Create a date_time object using calendar \a cal and define values given in \a set Chris@16: /// Chris@16: date_time(date_time_period_set const &set,calendar const &cal); Chris@16: Chris@16: Chris@16: /// Chris@16: /// assign values to various periods in set \a f Chris@16: /// Chris@16: date_time const &operator=(date_time_period_set const &f); Chris@16: Chris@16: /// Chris@16: /// set specific period \a f value to \a v Chris@16: /// Chris@16: void set(period::period_type f,int v); Chris@16: /// Chris@16: /// get specific period \a f value Chris@16: /// Chris@16: int get(period::period_type f) const; Chris@16: Chris@16: /// Chris@16: /// syntactic sugar for get(f) Chris@16: /// Chris@16: int operator/(period::period_type f) const Chris@16: { Chris@16: return get(f); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// add single period f to the current date_time Chris@16: /// Chris@16: date_time operator+(period::period_type f) const Chris@16: { Chris@16: return *this+date_time_period(f); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// subtract single period f from the current date_time Chris@16: /// Chris@16: date_time operator-(period::period_type f) const Chris@16: { Chris@16: return *this-date_time_period(f); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// add single period f to the current date_time Chris@16: /// Chris@16: date_time const &operator+=(period::period_type f) Chris@16: { Chris@16: return *this+=date_time_period(f); Chris@16: } Chris@16: /// Chris@16: /// subtract single period f from the current date_time Chris@16: /// Chris@16: date_time const &operator-=(period::period_type f) Chris@16: { Chris@16: return *this-=date_time_period(f); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// roll forward a date by single period f. Chris@16: /// Chris@16: date_time operator<<(period::period_type f) const Chris@16: { Chris@16: return *this<>(period::period_type f) const Chris@16: { Chris@16: return *this>>date_time_period(f); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// roll forward a date by single period f. Chris@16: /// Chris@16: date_time const &operator<<=(period::period_type f) Chris@16: { Chris@16: return *this<<=date_time_period(f); Chris@16: } Chris@16: /// Chris@16: /// roll backward a date by single period f. Chris@16: /// Chris@16: date_time const &operator>>=(period::period_type f) Chris@16: { Chris@16: return *this>>=date_time_period(f); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// add date_time_period to the current date_time Chris@16: /// Chris@16: date_time operator+(date_time_period const &v) const; Chris@16: /// Chris@16: /// subtract date_time_period from the current date_time Chris@16: /// Chris@16: date_time operator-(date_time_period const &v) const; Chris@16: /// Chris@16: /// add date_time_period to the current date_time Chris@16: /// Chris@16: date_time const &operator+=(date_time_period const &v); Chris@16: /// Chris@16: /// subtract date_time_period from the current date_time Chris@16: /// Chris@16: date_time const &operator-=(date_time_period const &v); Chris@16: Chris@16: /// Chris@16: /// roll current date_time forward by date_time_period v Chris@16: /// Chris@16: date_time operator<<(date_time_period const &v) const; Chris@16: /// Chris@16: /// roll current date_time backward by date_time_period v Chris@16: /// Chris@16: date_time operator>>(date_time_period const &v) const ; Chris@16: /// Chris@16: /// roll current date_time forward by date_time_period v Chris@16: /// Chris@16: date_time const &operator<<=(date_time_period const &v); Chris@16: /// Chris@16: /// roll current date_time backward by date_time_period v Chris@16: /// Chris@16: date_time const &operator>>=(date_time_period const &v); Chris@16: Chris@16: /// Chris@16: /// add date_time_period_set v to the current date_time Chris@16: /// Chris@16: date_time operator+(date_time_period_set const &v) const; Chris@16: /// Chris@16: /// subtract date_time_period_set v from the current date_time Chris@16: /// Chris@16: date_time operator-(date_time_period_set const &v) const; Chris@16: /// Chris@16: /// add date_time_period_set v to the current date_time Chris@16: /// Chris@16: date_time const &operator+=(date_time_period_set const &v); Chris@16: /// Chris@16: /// subtract date_time_period_set v from the current date_time Chris@16: /// Chris@16: date_time const &operator-=(date_time_period_set const &v); Chris@16: Chris@16: /// Chris@16: /// roll current date_time forward by date_time_period_set v Chris@16: /// Chris@16: date_time operator<<(date_time_period_set const &v) const; Chris@16: /// Chris@16: /// roll current date_time backward by date_time_period_set v Chris@16: /// Chris@16: date_time operator>>(date_time_period_set const &v) const ; Chris@16: /// Chris@16: /// roll current date_time forward by date_time_period_set v Chris@16: /// Chris@16: date_time const &operator<<=(date_time_period_set const &v); Chris@16: /// Chris@16: /// roll current date_time backward by date_time_period_set v Chris@16: /// Chris@16: date_time const &operator>>=(date_time_period_set const &v); Chris@16: Chris@16: /// Chris@16: /// Get POSIX time Chris@16: /// Chris@16: /// The POSIX time is number of seconds since January 1st, 1970 00:00 UTC, ignoring leap seconds. Chris@16: /// Chris@16: double time() const; Chris@16: /// Chris@16: /// set POSIX time Chris@16: /// Chris@16: /// The POSIX time is number of seconds since January 1st, 1970 00:00 UTC, ignoring leap seconds. Chris@16: /// This time can be fetched from Operating system clock using C function time, gettimeofday and others. Chris@16: /// Chris@16: void time(double v); Chris@16: Chris@16: /// Chris@16: /// compare date_time in the timeline (ignores difference in calendar, timezone etc) Chris@16: /// Chris@16: bool operator==(date_time const &other) const; Chris@16: /// Chris@16: /// compare date_time in the timeline (ignores difference in calendar, timezone etc) Chris@16: /// Chris@16: bool operator!=(date_time const &other) const; Chris@16: /// Chris@16: /// compare date_time in the timeline (ignores difference in calendar, timezone etc) Chris@16: /// Chris@16: bool operator<(date_time const &other) const; Chris@16: /// Chris@16: /// compare date_time in the timeline (ignores difference in calendar, timezone etc) Chris@16: /// Chris@16: bool operator>(date_time const &other) const; Chris@16: /// Chris@16: /// compare date_time in the timeline (ignores difference in calendar, timezone etc) Chris@16: /// Chris@16: bool operator<=(date_time const &other) const; Chris@16: /// Chris@16: /// compare date_time in the timeline (ignores difference in calendar, timezone etc) Chris@16: /// Chris@16: bool operator>=(date_time const &other) const; Chris@16: Chris@16: /// Chris@16: /// swaps two dates - efficient, does not throw Chris@16: /// Chris@16: void swap(date_time &other); Chris@16: Chris@16: /// Chris@16: /// calculate the distance from this date_time to \a other in terms of perios \a f Chris@16: /// Chris@16: int difference(date_time const &other,period::period_type f) const; Chris@16: Chris@16: /// Chris@16: /// Get minimal possible value for *this time point for a period \a f. Chris@16: /// Chris@16: int minimum(period::period_type f) const; Chris@16: /// Chris@16: /// Get minimal possible value for *this time point for a period \a f. For example Chris@16: /// in February maximum(day) may be 28 or 29, in January maximum(day)==31 Chris@16: /// Chris@16: int maximum(period::period_type f) const; Chris@16: Chris@16: /// Chris@16: /// Check if *this time point is in daylight saving time Chris@16: /// Chris@16: bool is_in_daylight_saving_time() const; Chris@16: Chris@16: private: Chris@16: hold_ptr impl_; Chris@16: }; Chris@16: Chris@16: /// Chris@16: /// Writes date_time \a t to output stream \a out. Chris@16: /// Chris@16: /// This function uses locale, calendar and time zone of the target stream \a in. Chris@16: /// Chris@16: /// For example: Chris@16: /// \code Chris@16: /// date_time now(time(0),hebrew_calendar) Chris@16: /// cout << "Year: " << period::year(now) <<" Full Date:"<< now; Chris@16: /// \endcode Chris@16: /// Chris@16: /// The output may be Year:5770 Full Date:Jan 1, 2010 Chris@16: /// Chris@16: template Chris@16: std::basic_ostream &operator<<(std::basic_ostream &out,date_time const &t) Chris@16: { Chris@16: double time_point = t.time(); Chris@16: uint64_t display_flags = ios_info::get(out).display_flags(); Chris@16: if ( Chris@16: display_flags == flags::date Chris@16: || display_flags == flags::time Chris@16: || display_flags == flags::datetime Chris@16: || display_flags == flags::strftime Chris@16: ) Chris@16: { Chris@16: out << time_point; Chris@16: } Chris@16: else { Chris@16: ios_info::get(out).display_flags(flags::datetime); Chris@16: out << time_point; Chris@16: ios_info::get(out).display_flags(display_flags); Chris@16: } Chris@16: return out; Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Reads date_time \a t from output stream \a in Chris@16: /// Chris@16: /// This function uses locale, calendar and time zone of the source stream \a in. Chris@16: /// Chris@16: template Chris@16: std::basic_istream &operator>>(std::basic_istream &in,date_time &t) Chris@16: { Chris@16: double v; Chris@16: uint64_t display_flags = ios_info::get(in).display_flags(); Chris@16: if ( Chris@16: display_flags == flags::date Chris@16: || display_flags == flags::time Chris@16: || display_flags == flags::datetime Chris@16: || display_flags == flags::strftime Chris@16: ) Chris@16: { Chris@16: in >> v; Chris@16: } Chris@16: else { Chris@16: ios_info::get(in).display_flags(flags::datetime); Chris@16: in >> v; Chris@16: ios_info::get(in).display_flags(display_flags); Chris@16: } Chris@16: if(!in.fail()) Chris@16: t.time(v); Chris@16: return in; Chris@16: } Chris@16: Chris@16: /// Chris@16: /// \brief This class represents a period: a pair of two date_time objects. Chris@16: /// Chris@16: /// It is generally used as syntactic sugar to calculate difference between two dates. Chris@16: /// Chris@16: /// Note: it stores references to the original objects, so it is not recommended to be used Chris@16: /// outside of the equation you calculate the difference in. Chris@16: /// Chris@16: class date_time_duration { Chris@16: public: Chris@16: Chris@16: /// Chris@16: /// Create an object were \a first represents earlier point on time line and \a second is later Chris@16: /// point. Chris@16: /// Chris@16: date_time_duration(date_time const &first,date_time const &second) : Chris@16: s_(first), Chris@16: e_(second) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Chris@16: /// find a difference in terms of period_type \a f Chris@16: /// Chris@16: int get(period::period_type f) const Chris@16: { Chris@16: return start().difference(end(),f); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Syntactic sugar for get(f) Chris@16: /// Chris@16: int operator / (period::period_type f) const Chris@16: { Chris@16: return start().difference(end(),f); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Get starting point Chris@16: /// Chris@16: date_time const &start() const { return s_; } Chris@16: /// Chris@16: /// Get ending point Chris@16: /// Chris@16: date_time const &end() const { return e_; } Chris@16: private: Chris@16: date_time const &s_; Chris@16: date_time const &e_; Chris@16: }; Chris@16: Chris@16: /// Chris@16: /// Calculates the difference between two dates, the left operand is a later point on time line. Chris@16: /// Returns date_time_duration object. Chris@16: /// Chris@16: inline date_time_duration operator-(date_time const &later,date_time const &earlier) Chris@16: { Chris@16: return date_time_duration(earlier,later); Chris@16: } Chris@16: Chris@16: Chris@16: namespace period { Chris@16: /// Chris@16: /// Extract from date_time numerical value of Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1] Chris@16: /// Chris@16: inline int era(date_time const &dt) { return dt.get(era()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of Year, it is calendar specific, for example 2011 in Gregorian calendar. Chris@16: /// Chris@16: inline int year(date_time const &dt) { return dt.get(year()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1. Chris@16: /// Chris@16: inline int extended_year(date_time const &dt) { return dt.get(extended_year()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of The month of year, calendar specific, in Gregorian [0..11] Chris@16: /// Chris@16: inline int month(date_time const &dt) { return dt.get(month()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of The day of month, calendar specific, in Gregorian [1..31] Chris@16: /// Chris@16: inline int day(date_time const &dt) { return dt.get(day()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of The number of day in year, starting from 1, in Gregorian [1..366] Chris@16: /// Chris@16: inline int day_of_year(date_time const &dt) { return dt.get(day_of_year()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of Day of week, Sunday=1, Monday=2,..., Saturday=7. Chris@16: /// Chris@16: /// Note that updating this value respects local day of week, so for example, Chris@16: /// If first day of week is Monday and the current day is Tuesday then setting Chris@16: /// the value to Sunday (1) would forward the date by 5 days forward and not backward Chris@16: /// by two days as it could be expected if the numbers were taken as is. Chris@16: /// Chris@16: inline int day_of_week(date_time const &dt) { return dt.get(day_of_week()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of Original number of the day of the week in month. For example 1st Sunday, Chris@16: /// 2nd Sunday, etc. in Gregorian [1..5] Chris@16: /// Chris@16: inline int day_of_week_in_month(date_time const &dt) { return dt.get(day_of_week_in_month()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of Local day of week, for example in France Monday is 1, in US Sunday is 1, [1..7] Chris@16: /// Chris@16: inline int day_of_week_local(date_time const &dt) { return dt.get(day_of_week_local()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of 24 clock hour [0..23] Chris@16: /// Chris@16: inline int hour(date_time const &dt) { return dt.get(hour()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of 12 clock hour [0..11] Chris@16: /// Chris@16: inline int hour_12(date_time const &dt) { return dt.get(hour_12()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of am or pm marker [0..1] Chris@16: /// Chris@16: inline int am_pm(date_time const &dt) { return dt.get(am_pm()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of minute [0..59] Chris@16: /// Chris@16: inline int minute(date_time const &dt) { return dt.get(minute()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of second [0..59] Chris@16: /// Chris@16: inline int second(date_time const &dt) { return dt.get(second()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of The week number in the year Chris@16: /// Chris@16: inline int week_of_year(date_time const &dt) { return dt.get(week_of_year()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of The week number within current month Chris@16: /// Chris@16: inline int week_of_month(date_time const &dt) { return dt.get(week_of_month()); } Chris@16: /// Chris@16: /// Extract from date_time numerical value of First day of week, constant, for example Sunday in US = 1, Monday in France = 2 Chris@16: /// Chris@16: inline int first_day_of_week(date_time const &dt) { return dt.get(first_day_of_week()); } Chris@16: Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1] Chris@16: /// Chris@16: inline int era(date_time_duration const &dt) { return dt.get(era()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in years Chris@16: /// Chris@16: inline int year(date_time_duration const &dt) { return dt.get(year()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in extended years (for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1). Chris@16: /// Chris@16: inline int extended_year(date_time_duration const &dt) { return dt.get(extended_year()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in months Chris@16: /// Chris@16: inline int month(date_time_duration const &dt) { return dt.get(month()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in days of month Chris@16: /// Chris@16: inline int day(date_time_duration const &dt) { return dt.get(day()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in days of year Chris@16: /// Chris@16: inline int day_of_year(date_time_duration const &dt) { return dt.get(day_of_year()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in days of week Chris@16: /// Chris@16: inline int day_of_week(date_time_duration const &dt) { return dt.get(day_of_week()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in original number of the day of the week in month Chris@16: /// Chris@16: inline int day_of_week_in_month(date_time_duration const &dt) { return dt.get(day_of_week_in_month()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in local day of week Chris@16: /// Chris@16: inline int day_of_week_local(date_time_duration const &dt) { return dt.get(day_of_week_local()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in hours Chris@16: /// Chris@16: inline int hour(date_time_duration const &dt) { return dt.get(hour()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in 12 clock hours Chris@16: /// Chris@16: inline int hour_12(date_time_duration const &dt) { return dt.get(hour_12()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in am or pm markers Chris@16: /// Chris@16: inline int am_pm(date_time_duration const &dt) { return dt.get(am_pm()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in minutes Chris@16: /// Chris@16: inline int minute(date_time_duration const &dt) { return dt.get(minute()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in seconds Chris@16: /// Chris@16: inline int second(date_time_duration const &dt) { return dt.get(second()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in the week number in the year Chris@16: /// Chris@16: inline int week_of_year(date_time_duration const &dt) { return dt.get(week_of_year()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in The week number within current month Chris@16: /// Chris@16: inline int week_of_month(date_time_duration const &dt) { return dt.get(week_of_month()); } Chris@16: /// Chris@16: /// Extract from date_time_duration numerical value of duration in the first day of week Chris@16: /// Chris@16: inline int first_day_of_week(date_time_duration const &dt) { return dt.get(first_day_of_week()); } Chris@16: Chris@16: Chris@16: } Chris@16: Chris@16: /// @} Chris@16: Chris@16: Chris@16: } // locale Chris@16: } // boost Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: Chris@16: #endif Chris@16: /// Chris@16: /// \example calendar.cpp Chris@16: /// Chris@16: /// Example of using date_time functions for generating calendar for current year. Chris@16: /// Chris@16: Chris@16: // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4