CppCMS
date_time.h
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 BOOSTER_LOCALE_DATE_TIME_H_INCLUDED
9 #define BOOSTER_LOCALE_DATE_TIME_H_INCLUDED
10 
11 #include <booster/config.h>
12 #ifdef BOOSTER_MSVC
13 # pragma warning(push)
14 # pragma warning(disable : 4275 4251 4231 4660)
15 #endif
16 
17 #include <booster/locale/hold_ptr.h>
18 #include <booster/locale/date_time_facet.h>
19 #include <booster/locale/formatting.h>
20 #include <booster/locale/time_zone.h>
21 #include <locale>
22 #include <vector>
23 #include <booster/backtrace.h>
24 
25 
26 namespace booster {
27  namespace locale {
34 
35 
40  public:
44  date_time_error(std::string const &e) : booster::runtime_error(e) {}
45  };
46 
47 
57  {
59  int value;
60  date_time_period operator+() const { return *this; }
67  date_time_period operator-() const { return date_time_period(type,-value); }
68 
72  date_time_period(period::period_type f=period::period_type(),int v=1) : type(f), value(v) {}
73  };
74 
75  namespace period {
79  inline period_type invalid(){ return period_type(marks::invalid); }
83  inline period_type era(){ return period_type(marks::era); }
87  inline period_type year(){ return period_type(marks::year); }
91  inline period_type extended_year(){ return period_type(marks::extended_year); }
95  inline period_type month(){ return period_type(marks::month); }
99  inline period_type day(){ return period_type(marks::day); }
103  inline period_type day_of_year(){ return period_type(marks::day_of_year); }
112  inline period_type day_of_week(){ return period_type(marks::day_of_week); }
117  inline period_type day_of_week_in_month(){ return period_type(marks::day_of_week_in_month); }
121  inline period_type day_of_week_local(){ return period_type(marks::day_of_week_local); }
125  inline period_type hour(){ return period_type(marks::hour); }
129  inline period_type hour_12(){ return period_type(marks::hour_12); }
133  inline period_type am_pm(){ return period_type(marks::am_pm); }
137  inline period_type minute(){ return period_type(marks::minute); }
141  inline period_type second(){ return period_type(marks::second); }
145  inline period_type week_of_year(){ return period_type(marks::week_of_year); }
149  inline period_type week_of_month(){ return period_type(marks::week_of_month); }
153  inline period_type first_day_of_week(){ return period_type(marks::first_day_of_week); }
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); }
166  inline date_time_period extended_year(int v) { return date_time_period(extended_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); }
178  inline date_time_period day_of_year(int v) { return date_time_period(day_of_year(),v); }
187  inline date_time_period day_of_week(int v) { return date_time_period(day_of_week(),v); }
192  inline date_time_period day_of_week_in_month(int v) { return date_time_period(day_of_week_in_month(),v); }
196  inline date_time_period day_of_week_local(int v) { return date_time_period(day_of_week_local(),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); }
220  inline date_time_period week_of_year(int v) { return date_time_period(week_of_year(),v); }
224  inline date_time_period week_of_month(int v) { return date_time_period(week_of_month(),v); }
228  inline date_time_period first_day_of_week(int v) { return date_time_period(first_day_of_week(),v); }
229 
233  inline date_time_period january() { return date_time_period(month(),0); }
237  inline date_time_period february() { return date_time_period(month(),1); }
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); }
265  inline date_time_period september() { return date_time_period(month(),8); }
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 
282  inline date_time_period sunday() { return date_time_period(day_of_week(),1); }
286  inline date_time_period monday() { return date_time_period(day_of_week(),2); }
290  inline date_time_period tuesday() { return date_time_period(day_of_week(),3); }
294  inline date_time_period wednesday() { return date_time_period(day_of_week(),4); }
298  inline date_time_period thursday() { return date_time_period(day_of_week(),5); }
302  inline date_time_period friday() { return date_time_period(day_of_week(),6); }
306  inline date_time_period saturday() { return date_time_period(day_of_week(),7); }
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 booster::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 BOOSTER_API 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 BOOSTER_API 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 
956  int operator / (period::period_type f) const
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 BOOSTER_MSVC
1139 #pragma warning(pop)
1140 #endif
1141 
1142 
1143 #endif
1144 
1150 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
int operator/(period::period_type f) const
Definition: date_time.h:657
date_time const & start() const
Definition: date_time.h:964
date_time_period november()
Definition: date_time.h:273
this class provides an access to general calendar information.
Definition: date_time.h:473
date_time_period monday()
Definition: date_time.h:286
date_time const & operator+=(period::period_type f)
Definition: date_time.h:681
date_time const & end() const
Definition: date_time.h:968
Same as std::runtime_error but records stack trace.
Definition: backtrace.h:158
std::basic_istream< CharType > & operator>>(std::basic_istream< CharType > &in, date_time &t)
Definition: date_time.h:901
date_time_period sunday()
Definition: date_time.h:282
void add(date_time_period f)
Definition: date_time.h:402
date_time_period tuesday()
Definition: date_time.h:290
date_time_period_set(period::period_type f)
Definition: date_time.h:388
date_time operator-(period::period_type f) const
Definition: date_time.h:673
std::basic_ostream< CharType > & operator<<(std::basic_ostream< CharType > &out, date_time const &t)
Definition: date_time.h:874
date_time_duration(date_time const &first, date_time const &second)
Definition: date_time.h:939
date_time_period saturday()
Definition: date_time.h:306
size_t size() const
Definition: date_time.h:413
date_time_period january()
Definition: date_time.h:233
date_time_period pm()
Definition: date_time.h:314
date_time operator<<(period::period_type f) const
Definition: date_time.h:696
This error is thrown in case of invalid state that occurred.
Definition: date_time.h:39
date_time_period september()
Definition: date_time.h:265
a smart pointer similar to std::auto_ptr but it is non-copyable and the underlying object has the sam...
Definition: hold_ptr.h:18
This class represents a period: a pair of two date_time objects.
Definition: date_time.h:932
date_time_period february()
Definition: date_time.h:237
this class represents a date time and allows to perform various operation according to the locale set...
Definition: date_time.h:590
static ios_info & get(std::ios_base &ios)
date_time_period may()
Definition: date_time.h:249
date_time_period march()
Definition: date_time.h:241
date_time_period august()
Definition: date_time.h:261
date_time_period december()
Definition: date_time.h:277
date_time const & operator>>=(period::period_type f)
Definition: date_time.h:719
date_time operator>>(period::period_type f) const
Definition: date_time.h:704
date_time_period operator*(period::period_type f, T v)
Definition: date_time.h:335
date_time_period wednesday()
Definition: date_time.h:294
date_time_period(period::period_type f=period::period_type(), int v=1)
Definition: date_time.h:72
date_time_error(std::string const &e)
Definition: date_time.h:44
Same as std::out_of_range but records stack trace.
Definition: backtrace.h:238
date_time_period_set operator-(date_time_period_set const &a, date_time_period_set const &b)
Definition: date_time.h:457
date_time_period october()
Definition: date_time.h:269
date_time_period_set(date_time_period const &fl)
Definition: date_time.h:395
This is the main namespace that encloses all localization classes.
Definition: locale_fwd.h:14
date_time operator+(period::period_type f) const
Definition: date_time.h:665
date_time_period am()
Definition: date_time.h:310
date_time const & operator-=(period::period_type f)
Definition: date_time.h:688
date_time_period friday()
Definition: date_time.h:302
This class holds a type that represents certain period of time like year, hour, second and so on...
Definition: date_time_facet.h:70
period::period_type type
The type of period, i.e. era, year, day etc.
Definition: date_time.h:58
date_time_period operator-() const
Definition: date_time.h:67
date_time_period const & operator[](size_t n) const
Definition: date_time.h:428
this class that represents a set of periods,
Definition: date_time.h:376
date_time_period april()
Definition: date_time.h:245
date_time_period thursday()
Definition: date_time.h:298
This class represents a pair of period_type and the integer values that describes its amount...
Definition: date_time.h:56
date_time_period june()
Definition: date_time.h:253
date_time_period july()
Definition: date_time.h:257
date_time_period_set operator+(date_time_period_set const &a, date_time_period_set const &b)
Definition: date_time.h:446
int get(period::period_type f) const
Definition: date_time.h:948
date_time const & operator<<=(period::period_type f)
Definition: date_time.h:712
void display_flags(uint64_t flags)
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
date_time_period_set()
Definition: date_time.h:382
int value
Definition: date_time.h:59
int get(period::period_type f) const
std::ios_base & time(std::ios_base &ios)
Definition: formatting.h:328