Boost.Locale
date_time.hpp
1 //
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_LOCALE_DATE_TIME_HPP_INCLUDED
9 #define BOOST_LOCALE_DATE_TIME_HPP_INCLUDED
10 
11 #include <boost/locale/config.hpp>
12 #ifdef BOOST_MSVC
13 # pragma warning(push)
14 # pragma warning(disable : 4275 4251 4231 4660)
15 #endif
16 
17 #include <boost/locale/hold_ptr.hpp>
18 #include <boost/locale/date_time_facet.hpp>
19 #include <boost/locale/formatting.hpp>
20 #include <boost/locale/time_zone.hpp>
21 #include <locale>
22 #include <vector>
23 #include <stdexcept>
24 
25 
26 namespace boost {
27  namespace locale {
34 
35 
39  class BOOST_SYMBOL_VISIBLE date_time_error : public std::runtime_error {
40  public:
44  date_time_error(std::string const &e) : std::runtime_error(e) {}
45  };
46 
47 
57  {
59  int value;
60  date_time_period operator+() const { return *this; }
68 
73  };
74 
75  namespace period {
83  inline period_type era(){ return period_type(marks::era); }
87  inline period_type year(){ return period_type(marks::year); }
99  inline period_type day(){ return period_type(marks::day); }
154 
158  inline date_time_period era(int v) { return date_time_period(era(),v); }
162  inline date_time_period year(int v) { return date_time_period(year(),v); }
170  inline date_time_period month(int v) { return date_time_period(month(),v); }
174  inline date_time_period day(int v) { return date_time_period(day(),v); }
200  inline date_time_period hour(int v) { return date_time_period(hour(),v); }
204  inline date_time_period hour_12(int v) { return date_time_period(hour_12(),v); }
208  inline date_time_period am_pm(int v) { return date_time_period(am_pm(),v); }
212  inline date_time_period minute(int v) { return date_time_period(minute(),v); }
216  inline date_time_period second(int v) { return date_time_period(second(),v); }
229 
233  inline date_time_period january() { return date_time_period(month(),0); }
241  inline date_time_period march() { return date_time_period(month(),2); }
245  inline date_time_period april() { return date_time_period(month(),3); }
249  inline date_time_period may() { return date_time_period(month(),4); }
253  inline date_time_period june() { return date_time_period(month(),5); }
257  inline date_time_period july() { return date_time_period(month(),6); }
261  inline date_time_period august() { return date_time_period(month(),7); }
269  inline date_time_period october() { return date_time_period(month(),9); }
273  inline date_time_period november() { return date_time_period(month(),10); }
277  inline date_time_period december() { return date_time_period(month(),11); }
278 
310  inline date_time_period am() { return date_time_period(am_pm(),0); }
314  inline date_time_period pm() { return date_time_period(am_pm(),1); }
315 
320  {
321  return date_time_period(f);
322  }
327  {
328  return date_time_period(f,-1);
329  }
330 
334  template<typename T>
336  {
337  return date_time_period(f,v);
338  }
339 
343  template<typename T>
345  {
346  return date_time_period(f,v);
347  }
351  template<typename T>
353  {
354  return date_time_period(f.type,f.value*v);
355  }
356 
360  template<typename T>
362  {
363  return date_time_period(f.type,f.value*v);
364  }
365 
366 
367  } // period
368 
369 
377  public:
378 
383  {
384  }
389  {
390  basic_[0]=date_time_period(f);
391  }
396  {
397  basic_[0]=fl;
398  }
403  {
404  size_t n=size();
405  if(n < 4)
406  basic_[n]=f;
407  else
408  periods_.push_back(f);
409  }
413  size_t size() const
414  {
415  if(basic_[0].type == period::period_type())
416  return 0;
417  if(basic_[1].type == period::period_type())
418  return 1;
419  if(basic_[2].type == period::period_type())
420  return 2;
421  if(basic_[3].type == period::period_type())
422  return 3;
423  return 4+periods_.size();
424  }
428  date_time_period const &operator[](size_t n) const
429  {
430  if(n >= size())
431  throw std::out_of_range("Invalid index to date_time_period");
432  if(n < 4)
433  return basic_[n];
434  else
435  return periods_[n-4];
436  }
437  private:
438  date_time_period basic_[4];
439  std::vector<date_time_period> periods_;
440  };
441 
442 
447  {
449  for(unsigned i=0;i<b.size();i++)
450  s.add(b[i]);
451  return s;
452  }
453 
458  {
460  for(unsigned i=0;i<b.size();i++)
461  s.add(-b[i]);
462  return s;
463  }
464 
465 
473  class BOOST_LOCALE_DECL calendar {
474  public:
475 
482  calendar(std::ios_base &ios);
488  calendar(std::locale const &l,std::string const &zone);
494  calendar(std::locale const &l);
500  calendar(std::string const &zone);
506  calendar();
507  ~calendar();
508 
512  calendar(calendar const &other);
516  calendar const &operator=(calendar const &other);
517 
521  int minimum(period::period_type f) const;
525  int greatest_minimum(period::period_type f) const;
529  int maximum(period::period_type f) const;
533  int least_maximum(period::period_type f) const;
534 
537  int first_day_of_week() const;
538 
542  std::locale get_locale() const;
546  std::string get_time_zone() const;
547 
551  bool is_gregorian() const;
552 
556  bool operator==(calendar const &other) const;
560  bool operator!=(calendar const &other) const;
561 
562  private:
563  friend class date_time;
564  std::locale locale_;
565  std::string tz_;
567  };
568 
589 
590  class BOOST_LOCALE_DECL date_time {
591  public:
592 
598  date_time();
602  date_time(date_time const &other);
606  date_time(date_time const &other,date_time_period_set const &set);
610  date_time const &operator=(date_time const &other);
611  ~date_time();
612 
618  date_time(double time);
622  date_time(double time,calendar const &cal);
626  date_time(calendar const &cal);
627 
633  date_time(date_time_period_set const &set);
637  date_time(date_time_period_set const &set,calendar const &cal);
638 
639 
643  date_time const &operator=(date_time_period_set const &f);
644 
648  void set(period::period_type f,int v);
652  int get(period::period_type f) const;
653 
658  {
659  return get(f);
660  }
661 
666  {
667  return *this+date_time_period(f);
668  }
669 
674  {
675  return *this-date_time_period(f);
676  }
677 
682  {
683  return *this+=date_time_period(f);
684  }
689  {
690  return *this-=date_time_period(f);
691  }
692 
697  {
698  return *this<<date_time_period(f);
699  }
700 
705  {
706  return *this>>date_time_period(f);
707  }
708 
713  {
714  return *this<<=date_time_period(f);
715  }
720  {
721  return *this>>=date_time_period(f);
722  }
723 
727  date_time operator+(date_time_period const &v) const;
731  date_time operator-(date_time_period const &v) const;
735  date_time const &operator+=(date_time_period const &v);
739  date_time const &operator-=(date_time_period const &v);
740 
744  date_time operator<<(date_time_period const &v) const;
748  date_time operator>>(date_time_period const &v) const ;
752  date_time const &operator<<=(date_time_period const &v);
756  date_time const &operator>>=(date_time_period const &v);
757 
761  date_time operator+(date_time_period_set const &v) const;
765  date_time operator-(date_time_period_set const &v) const;
769  date_time const &operator+=(date_time_period_set const &v);
773  date_time const &operator-=(date_time_period_set const &v);
774 
778  date_time operator<<(date_time_period_set const &v) const;
782  date_time operator>>(date_time_period_set const &v) const ;
786  date_time const &operator<<=(date_time_period_set const &v);
790  date_time const &operator>>=(date_time_period_set const &v);
791 
797  double time() const;
804  void time(double v);
805 
809  bool operator==(date_time const &other) const;
813  bool operator!=(date_time const &other) const;
817  bool operator<(date_time const &other) const;
821  bool operator>(date_time const &other) const;
825  bool operator<=(date_time const &other) const;
829  bool operator>=(date_time const &other) const;
830 
834  void swap(date_time &other);
835 
839  int difference(date_time const &other,period::period_type f) const;
840 
844  int minimum(period::period_type f) const;
849  int maximum(period::period_type f) const;
850 
854  bool is_in_daylight_saving_time() const;
855 
856  private:
858  };
859 
873  template<typename CharType>
874  std::basic_ostream<CharType> &operator<<(std::basic_ostream<CharType> &out,date_time const &t)
875  {
876  double time_point = t.time();
877  uint64_t display_flags = ios_info::get(out).display_flags();
878  if (
879  display_flags == flags::date
880  || display_flags == flags::time
881  || display_flags == flags::datetime
882  || display_flags == flags::strftime
883  )
884  {
885  out << time_point;
886  }
887  else {
888  ios_info::get(out).display_flags(flags::datetime);
889  out << time_point;
890  ios_info::get(out).display_flags(display_flags);
891  }
892  return out;
893  }
894 
900  template<typename CharType>
901  std::basic_istream<CharType> &operator>>(std::basic_istream<CharType> &in,date_time &t)
902  {
903  double v;
904  uint64_t display_flags = ios_info::get(in).display_flags();
905  if (
906  display_flags == flags::date
907  || display_flags == flags::time
908  || display_flags == flags::datetime
909  || display_flags == flags::strftime
910  )
911  {
912  in >> v;
913  }
914  else {
915  ios_info::get(in).display_flags(flags::datetime);
916  in >> v;
917  ios_info::get(in).display_flags(display_flags);
918  }
919  if(!in.fail())
920  t.time(v);
921  return in;
922  }
923 
933  public:
934 
939  date_time_duration(date_time const &first,date_time const &second) :
940  s_(first),
941  e_(second)
942  {
943  }
944 
948  int get(period::period_type f) const
949  {
950  return start().difference(end(),f);
951  }
952 
957  {
958  return start().difference(end(),f);
959  }
960 
964  date_time const &start() const { return s_; }
968  date_time const &end() const { return e_; }
969  private:
970  date_time const &s_;
971  date_time const &e_;
972  };
973 
978  inline date_time_duration operator-(date_time const &later,date_time const &earlier)
979  {
980  return date_time_duration(earlier,later);
981  }
982 
983 
984  namespace period {
988  inline int era(date_time const &dt) { return dt.get(era()); }
992  inline int year(date_time const &dt) { return dt.get(year()); }
996  inline int extended_year(date_time const &dt) { return dt.get(extended_year()); }
1000  inline int month(date_time const &dt) { return dt.get(month()); }
1004  inline int day(date_time const &dt) { return dt.get(day()); }
1008  inline int day_of_year(date_time const &dt) { return dt.get(day_of_year()); }
1017  inline int day_of_week(date_time const &dt) { return dt.get(day_of_week()); }
1022  inline int day_of_week_in_month(date_time const &dt) { return dt.get(day_of_week_in_month()); }
1026  inline int day_of_week_local(date_time const &dt) { return dt.get(day_of_week_local()); }
1030  inline int hour(date_time const &dt) { return dt.get(hour()); }
1034  inline int hour_12(date_time const &dt) { return dt.get(hour_12()); }
1038  inline int am_pm(date_time const &dt) { return dt.get(am_pm()); }
1042  inline int minute(date_time const &dt) { return dt.get(minute()); }
1046  inline int second(date_time const &dt) { return dt.get(second()); }
1050  inline int week_of_year(date_time const &dt) { return dt.get(week_of_year()); }
1054  inline int week_of_month(date_time const &dt) { return dt.get(week_of_month()); }
1058  inline int first_day_of_week(date_time const &dt) { return dt.get(first_day_of_week()); }
1059 
1063  inline int era(date_time_duration const &dt) { return dt.get(era()); }
1067  inline int year(date_time_duration const &dt) { return dt.get(year()); }
1071  inline int extended_year(date_time_duration const &dt) { return dt.get(extended_year()); }
1075  inline int month(date_time_duration const &dt) { return dt.get(month()); }
1079  inline int day(date_time_duration const &dt) { return dt.get(day()); }
1083  inline int day_of_year(date_time_duration const &dt) { return dt.get(day_of_year()); }
1087  inline int day_of_week(date_time_duration const &dt) { return dt.get(day_of_week()); }
1091  inline int day_of_week_in_month(date_time_duration const &dt) { return dt.get(day_of_week_in_month()); }
1095  inline int day_of_week_local(date_time_duration const &dt) { return dt.get(day_of_week_local()); }
1099  inline int hour(date_time_duration const &dt) { return dt.get(hour()); }
1103  inline int hour_12(date_time_duration const &dt) { return dt.get(hour_12()); }
1107  inline int am_pm(date_time_duration const &dt) { return dt.get(am_pm()); }
1111  inline int minute(date_time_duration const &dt) { return dt.get(minute()); }
1115  inline int second(date_time_duration const &dt) { return dt.get(second()); }
1119  inline int week_of_year(date_time_duration const &dt) { return dt.get(week_of_year()); }
1123  inline int week_of_month(date_time_duration const &dt) { return dt.get(week_of_month()); }
1127  inline int first_day_of_week(date_time_duration const &dt) { return dt.get(first_day_of_week()); }
1128 
1129 
1130  }
1131 
1133 
1134 
1135  } // locale
1136 } // boost
1137 
1138 #ifdef BOOST_MSVC
1139 #pragma warning(pop)
1140 #endif
1141 
1142 
1143 #endif
1144 
1150 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
period_type am_pm()
Definition: date_time.hpp:133
period_type week_of_year()
Definition: date_time.hpp:145
period_type minute()
Definition: date_time.hpp:137
The week number within current month.
Definition: date_time_facet.hpp:53
period::period_type type
The type of period, i.e. era, year, day etc.
Definition: date_time.hpp:58
Definition: date_time_facet.hpp:44
period_type extended_year()
Definition: date_time.hpp:91
date_time_period saturday()
Definition: date_time.hpp:306
date_time_period_set()
Definition: date_time.hpp:382
date_time const & end() const
Definition: date_time.hpp:968
date_time_period const & operator[](size_t n) const
Definition: date_time.hpp:428
Definition: date_time_facet.hpp:39
Year, it is calendar specific, for example 2011 in Gregorian calendar.
Definition: date_time_facet.hpp:34
this class that represents a set of periods,
Definition: date_time.hpp:376
date_time_period january()
Definition: date_time.hpp:233
date_time_period november()
Definition: date_time.hpp:273
date_time operator+(period::period_type f) const
Definition: date_time.hpp:665
size_t size() const
Definition: date_time.hpp:413
date_time_error(std::string const &e)
Definition: date_time.hpp:44
This class represents a period: a pair of two date_time objects.
Definition: date_time.hpp:932
date_time const & operator>>=(period::period_type f)
Definition: date_time.hpp:719
date_time_period march()
Definition: date_time.hpp:241
period_type day_of_week()
Definition: date_time.hpp:112
The number of day in year, starting from 1, in Gregorian [1..366].
Definition: date_time_facet.hpp:38
int get(period::period_type f) const
Definition: date_time.hpp:948
date_time_period monday()
Definition: date_time.hpp:286
date_time_period february()
Definition: date_time.hpp:237
date_time_period april()
Definition: date_time.hpp:245
bool operator<(segment< IteratorL > const &l, segment< IteratorR > const &r)
Compare two segments.
Definition: segment.hpp:255
period_type first_day_of_week()
Definition: date_time.hpp:153
bool operator<=(segment< IteratorL > const &l, segment< IteratorR > const &r)
Compare two segments.
Definition: segment.hpp:261
this class provides an access to general calendar information.
Definition: date_time.hpp:473
date_time_period august()
Definition: date_time.hpp:261
date_time_period am()
Definition: date_time.hpp:310
bool operator>=(segment< IteratorL > const &l, segment< IteratorR > const &r)
Compare two segments.
Definition: segment.hpp:273
date_time_period october()
Definition: date_time.hpp:269
void add(date_time_period f)
Definition: date_time.hpp:402
bool operator==(BaseIterator const &l, boundary_point< BaseIterator > const &r)
Definition: boundary_point.hpp:142
int operator/(period::period_type f) const
Definition: date_time.hpp:657
date_time_period december()
Definition: date_time.hpp:277
period_type era()
Definition: date_time.hpp:83
period_type hour()
Definition: date_time.hpp:125
a smart pointer similar to std::auto_ptr but it is non-copyable and the underlying object has the sam...
Definition: hold_ptr.hpp:18
void display_flags(uint64_t flags)
static ios_info & get(std::ios_base &ios)
date_time const & operator-=(period::period_type f)
Definition: date_time.hpp:688
The week number in the year.
Definition: date_time_facet.hpp:52
24 clock hour [0..23]
Definition: date_time_facet.hpp:47
Special invalid value, should not be used directly.
Definition: date_time_facet.hpp:32
The day of month, calendar specific, in Gregorian [1..31].
Definition: date_time_facet.hpp:37
period_type day_of_week_local()
Definition: date_time.hpp:121
minute [0..59]
Definition: date_time_facet.hpp:50
date_time_period operator+() const
Definition: date_time.hpp:63
The month of year, calendar specific, in Gregorian [0..11].
Definition: date_time_facet.hpp:36
Local day of week, for example in France Monday is 1, in US Sunday is 1, [1..7].
Definition: date_time_facet.hpp:46
date_time_period operator-(period::period_type f)
Definition: date_time.hpp:326
period_type day_of_week_in_month()
Definition: date_time.hpp:117
date_time_period operator*(period::period_type f, T v)
Definition: date_time.hpp:335
12 clock hour [0..11]
Definition: date_time_facet.hpp:48
This class represents a pair of period_type and the integer values that describes its amount...
Definition: date_time.hpp:56
date_time_period pm()
Definition: date_time.hpp:314
period_type day()
Definition: date_time.hpp:99
int get(period::period_type f) const
period_type year()
Definition: date_time.hpp:87
period_type week_of_month()
Definition: date_time.hpp:149
date_time_period_set operator-(date_time_period_set const &a, date_time_period_set const &b)
Definition: date_time.hpp:457
date_time_period wednesday()
Definition: date_time.hpp:294
date_time const & operator<<=(period::period_type f)
Definition: date_time.hpp:712
This error is thrown in case of invalid state that occurred.
Definition: date_time.hpp:39
date_time_period thursday()
Definition: date_time.hpp:298
am or pm marker [0..1]
Definition: date_time_facet.hpp:49
date_time_period_set(period::period_type f)
Definition: date_time.hpp:388
date_time_period_set(date_time_period const &fl)
Definition: date_time.hpp:395
date_time_period sunday()
Definition: date_time.hpp:282
std::basic_ostream< CharType > & operator<<(std::basic_ostream< CharType > &out, date_time const &t)
Definition: date_time.hpp:874
date_time_period friday()
Definition: date_time.hpp:302
date_time operator>>(period::period_type f) const
Definition: date_time.hpp:704
int operator/(period::period_type f) const
Definition: date_time.hpp:956
date_time_period tuesday()
Definition: date_time.hpp:290
int value
Definition: date_time.hpp:59
date_time_period operator+(period::period_type f)
Definition: date_time.hpp:319
date_time_duration(date_time const &first, date_time const &second)
Definition: date_time.hpp:939
period_type day_of_year()
Definition: date_time.hpp:103
date_time_period operator-() const
Definition: date_time.hpp:67
Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1].
Definition: date_time_facet.hpp:33
First day of week, constant, for example Sunday in US = 1, Monday in France = 2.
Definition: date_time_facet.hpp:54
double time() const
date_time_period_set operator+(date_time_period_set const &a, date_time_period_set const &b)
Definition: date_time.hpp:446
period_type hour_12()
Definition: date_time.hpp:129
std::basic_istream< CharType > & operator>>(std::basic_istream< CharType > &in, date_time &t)
Definition: date_time.hpp:901
Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1.
Definition: date_time_facet.hpp:35
period_type invalid()
Definition: date_time.hpp:79
date_time const & operator+=(period::period_type f)
Definition: date_time.hpp:681
date_time_period june()
Definition: date_time.hpp:253
date_time operator<<(period::period_type f) const
Definition: date_time.hpp:696
date_time_period may()
Definition: date_time.hpp:249
this class represents a date time and allows to perform various operation according to the locale set...
Definition: date_time.hpp:590
std::ios_base & time(std::ios_base &ios)
Definition: formatting.hpp:328
date_time const & start() const
Definition: date_time.hpp:964
date_time operator-(period::period_type f) const
Definition: date_time.hpp:673
int difference(date_time const &other, period::period_type f) const
date_time_period(period::period_type f=period::period_type(), int v=1)
Definition: date_time.hpp:72
bool operator>(segment< IteratorL > const &l, segment< IteratorR > const &r)
Compare two segments.
Definition: segment.hpp:267
period_type second()
Definition: date_time.hpp:141
second [0..59]
Definition: date_time_facet.hpp:51
period_type month()
Definition: date_time.hpp:95
date_time_period july()
Definition: date_time.hpp:257
bool operator!=(BaseIterator const &l, boundary_point< BaseIterator > const &r)
Definition: boundary_point.hpp:150
date_time_period september()
Definition: date_time.hpp:265
This class holds a type that represents certain period of time like year, hour, second and so on...
Definition: date_time_facet.hpp:70