00001
00002
00003
00004
00005
00006
00007
00008 #ifndef BOOST_LOCALE_DATE_TIME_HPP_INCLUDED
00009 #define BOOST_LOCALE_DATE_TIME_HPP_INCLUDED
00010
00011 #include <boost/locale/config.hpp>
00012 #ifdef BOOST_MSVC
00013 # pragma warning(push)
00014 # pragma warning(disable : 4275 4251 4231 4660)
00015 #endif
00016
00017 #include <boost/locale/time_zone.hpp>
00018 #include <locale>
00019 #include <vector>
00020 #include <stdexcept>
00021
00022
00023 namespace boost {
00024 namespace locale {
00031
00032
00036 class date_time_error : public std::runtime_error {
00037 public:
00041 date_time_error(std::string const &e) : std::runtime_error(e) {}
00042 };
00043
00047 namespace period {
00053 typedef enum {
00054 invalid,
00055 era,
00056 year,
00057 extended_year,
00058 month,
00059 day,
00060 day_of_year,
00061 day_of_week,
00062 day_of_week_in_month,
00063 day_of_week_local,
00064 hour,
00065 hour_12,
00066 am_pm,
00067 minute,
00068 second,
00069 week_of_year,
00070 week_of_month,
00071 } period_type;
00072 }
00073
00080 struct date_time_period
00081 {
00082 period::period_type type;
00083 int value;
00084
00085
00086
00087 date_time_period operator+() const { return *this; }
00091 date_time_period operator-() const { return date_time_period(type,-value); }
00092
00096 date_time_period(period::period_type f=period::invalid,int v=1) : type(f), value(v) {}
00097 };
00098
00099 namespace period {
00103 static const date_time_period january(month,0);
00107 static const date_time_period february(month,1);
00111 static const date_time_period march(month,2);
00115 static const date_time_period april(month,3);
00119 static const date_time_period may(month,4);
00123 static const date_time_period june(month,5);
00127 static const date_time_period july(month,6);
00131 static const date_time_period august(month,7);
00135 static const date_time_period september(month,8);
00139 static const date_time_period october(month,9);
00143 static const date_time_period november(month,10);
00147 static const date_time_period december(month,11);
00148
00152 static const date_time_period sunday(day_of_week,1);
00156 static const date_time_period monday(day_of_week,2);
00160 static const date_time_period tuesday(day_of_week,3);
00164 static const date_time_period wednesday(day_of_week,4);
00168 static const date_time_period thursday(day_of_week,5);
00172 static const date_time_period friday(day_of_week,6);
00176 static const date_time_period saturday(day_of_week,7);
00180 static const date_time_period am(am_pm,0);
00184 static const date_time_period pm(am_pm,1);
00185
00186 }
00187
00191 inline date_time_period operator+(period::period_type f)
00192 {
00193 return date_time_period(f);
00194 }
00198 inline date_time_period operator-(period::period_type f)
00199 {
00200 return date_time_period(f,-1);
00201 }
00202
00206 inline date_time_period operator*(period::period_type f,char v)
00207 {
00208 return date_time_period(f,v);
00209 }
00210
00214 inline date_time_period operator*(char v,period::period_type f)
00215 {
00216 return date_time_period(f,v);
00217 }
00218
00222 inline date_time_period operator*(char v,date_time_period f)
00223 {
00224 return date_time_period(f.type,f.value*v);
00225 }
00226
00230 inline date_time_period operator*(date_time_period f,char v)
00231 {
00232 return date_time_period(f.type,f.value*v);
00233 }
00234
00238 inline date_time_period operator*(period::period_type f,short int v)
00239 {
00240 return date_time_period(f,v);
00241 }
00242
00246 inline date_time_period operator*(short int v,period::period_type f)
00247 {
00248 return date_time_period(f,v);
00249 }
00250
00254 inline date_time_period operator*(short int v,date_time_period f)
00255 {
00256 return date_time_period(f.type,f.value*v);
00257 }
00258
00262 inline date_time_period operator*(date_time_period f,short int v)
00263 {
00264 return date_time_period(f.type,f.value*v);
00265 }
00266
00267
00271 inline date_time_period operator*(period::period_type f,int v)
00272 {
00273 return date_time_period(f,v);
00274 }
00275
00279 inline date_time_period operator*(int v,period::period_type f)
00280 {
00281 return date_time_period(f,v);
00282 }
00283
00287 inline date_time_period operator*(int v,date_time_period f)
00288 {
00289 return date_time_period(f.type,f.value*v);
00290 }
00291
00295 inline date_time_period operator*(date_time_period f,int v)
00296 {
00297 return date_time_period(f.type,f.value*v);
00298 }
00299
00303 inline date_time_period operator*(period::period_type f,long int v)
00304 {
00305 return date_time_period(f,v);
00306 }
00307
00311 inline date_time_period operator*(long int v,period::period_type f)
00312 {
00313 return date_time_period(f,v);
00314 }
00318 inline date_time_period operator*(long int v,date_time_period f)
00319 {
00320 return date_time_period(f.type,f.value*v);
00321 }
00322
00326 inline date_time_period operator*(date_time_period f,long int v)
00327 {
00328 return date_time_period(f.type,f.value*v);
00329 }
00330
00334 inline date_time_period operator*(period::period_type f,unsigned char v)
00335 {
00336 return date_time_period(f,v);
00337 }
00338
00342 inline date_time_period operator*(unsigned char v,period::period_type f)
00343 {
00344 return date_time_period(f,v);
00345 }
00346
00350 inline date_time_period operator*(unsigned char v,date_time_period f)
00351 {
00352 return date_time_period(f.type,f.value*v);
00353 }
00354
00358 inline date_time_period operator*(date_time_period f,unsigned char v)
00359 {
00360 return date_time_period(f.type,f.value*v);
00361 }
00362
00366 inline date_time_period operator*(period::period_type f,unsigned short int v)
00367 {
00368 return date_time_period(f,v);
00369 }
00370
00374 inline date_time_period operator*(unsigned short int v,period::period_type f)
00375 {
00376 return date_time_period(f,v);
00377 }
00378
00382 inline date_time_period operator*(unsigned short int v,date_time_period f)
00383 {
00384 return date_time_period(f.type,f.value*v);
00385 }
00386
00390 inline date_time_period operator*(date_time_period f,unsigned short int v)
00391 {
00392 return date_time_period(f.type,f.value*v);
00393 }
00394
00398 inline date_time_period operator*(period::period_type f,unsigned int v)
00399 {
00400 return date_time_period(f,v);
00401 }
00402
00406 inline date_time_period operator*(unsigned int v,period::period_type f)
00407 {
00408 return date_time_period(f,v);
00409 }
00410
00414 inline date_time_period operator*(unsigned int v,date_time_period f)
00415 {
00416 return date_time_period(f.type,f.value*v);
00417 }
00418
00422 inline date_time_period operator*(date_time_period f,unsigned int v)
00423 {
00424 return date_time_period(f.type,f.value*v);
00425 }
00426
00430 inline date_time_period operator*(period::period_type f,unsigned long int v)
00431 {
00432 return date_time_period(f,v);
00433 }
00434
00438 inline date_time_period operator*(unsigned long int v,period::period_type f)
00439 {
00440 return date_time_period(f,v);
00441 }
00442
00446 inline date_time_period operator*(unsigned long int v,date_time_period f)
00447 {
00448 return date_time_period(f.type,f.value*v);
00449 }
00450
00454 inline date_time_period operator*(date_time_period f,unsigned long int v)
00455 {
00456 return date_time_period(f.type,f.value*v);
00457 }
00458
00459
00466 class date_time_period_set {
00467 public:
00468
00472 date_time_period_set()
00473 {
00474 }
00478 date_time_period_set(period::period_type f)
00479 {
00480 basic_[0]=date_time_period(f);
00481 }
00485 date_time_period_set(date_time_period const &fl)
00486 {
00487 basic_[0]=fl;
00488 }
00492 void add(date_time_period f)
00493 {
00494 size_t n=size();
00495 if(n < 4)
00496 basic_[n]=f;
00497 else
00498 periods_.push_back(f);
00499 }
00503 size_t size() const
00504 {
00505 if(basic_[0].type == period::invalid)
00506 return 0;
00507 if(basic_[1].type == period::invalid)
00508 return 1;
00509 if(basic_[2].type == period::invalid)
00510 return 2;
00511 if(basic_[3].type == period::invalid)
00512 return 3;
00513 return 4+periods_.size();
00514 }
00518 date_time_period const &operator[](unsigned n) const
00519 {
00520 if(n >= size())
00521 throw std::out_of_range("Invalid index to date_time_period");
00522 if(n < 4)
00523 return basic_[n];
00524 else
00525 return periods_[n-4];
00526 }
00527 private:
00528 date_time_period basic_[4];
00529 std::vector<date_time_period> periods_;
00530 };
00531
00532
00536 inline date_time_period_set operator+(date_time_period_set const &a,date_time_period_set const &b)
00537 {
00538 date_time_period_set s(a);
00539 for(unsigned i=0;i<b.size();i++)
00540 s.add(b[i]);
00541 return s;
00542 }
00543
00547 inline date_time_period_set operator-(date_time_period_set const &a,date_time_period_set const &b)
00548 {
00549 date_time_period_set s(a);
00550 for(unsigned i=0;i<b.size();i++)
00551 s.add(-b[i]);
00552 return s;
00553 }
00554
00555
00563 class BOOST_LOCALE_DECL calendar {
00564 public:
00565
00569 calendar(std::ios_base &ios);
00573 calendar(std::locale const &l,time_zone const &zone);
00577 calendar(std::locale const &l);
00581 calendar(time_zone const &zone);
00585 calendar();
00586 ~calendar();
00587
00591 calendar(calendar const &other);
00595 calendar const &operator=(calendar const &other);
00596
00600 int minimum(period::period_type f) const;
00604 int greatest_minimum(period::period_type f) const;
00608 int maximum(period::period_type f) const;
00612 int least_maximum(period::period_type f) const;
00613
00616 int first_day_of_week() const;
00617
00621 std::locale get_locale() const;
00625 time_zone get_time_zone() const;
00626
00630 bool is_gregorian() const;
00631
00635 bool operator==(calendar const &other) const;
00639 bool operator!=(calendar const &other) const;
00640
00641 private:
00642 friend class date_time;
00643 std::locale locale_;
00644 boost::locale::time_zone tz_;
00645 void *impl_;
00646 };
00647
00668
00669 class BOOST_LOCALE_DECL date_time {
00670 public:
00671
00675 date_time();
00679 date_time(date_time const &other);
00683 date_time(date_time const &other,date_time_period_set const &set);
00687 date_time const &operator=(date_time const &other);
00688 ~date_time();
00689
00693 date_time(double time);
00697 date_time(double time,calendar const &cal);
00701 date_time(calendar const &cal);
00702
00706 date_time(date_time_period_set const &set);
00710 date_time(date_time_period_set const &set,calendar const &cal);
00711
00712
00716 date_time const &operator=(date_time_period_set const &f);
00717
00721 void set(period::period_type f,int v);
00725 int get(period::period_type f) const;
00726
00730 int operator/(period::period_type f) const
00731 {
00732 return get(f);
00733 }
00734
00738 date_time operator+(period::period_type f) const
00739 {
00740 return *this+date_time_period(f);
00741 }
00742
00746 date_time operator-(period::period_type f) const
00747 {
00748 return *this-date_time_period(f);
00749 }
00750
00754 date_time const &operator+=(period::period_type f)
00755 {
00756 return *this+=date_time_period(f);
00757 }
00761 date_time const &operator-=(period::period_type f)
00762 {
00763 return *this-=date_time_period(f);
00764 }
00765
00769 date_time operator<<(period::period_type f) const
00770 {
00771 return *this<<date_time_period(f);
00772 }
00773
00777 date_time operator>>(period::period_type f) const
00778 {
00779 return *this>>date_time_period(f);
00780 }
00781
00785 date_time const &operator<<=(period::period_type f)
00786 {
00787 return *this<<=date_time_period(f);
00788 }
00792 date_time const &operator>>=(period::period_type f)
00793 {
00794 return *this>>=date_time_period(f);
00795 }
00796
00800 date_time operator+(date_time_period const &v) const;
00804 date_time operator-(date_time_period const &v) const;
00808 date_time const &operator+=(date_time_period const &v);
00812 date_time const &operator-=(date_time_period const &v);
00813
00817 date_time operator<<(date_time_period const &v) const;
00821 date_time operator>>(date_time_period const &v) const ;
00825 date_time const &operator<<=(date_time_period const &v);
00829 date_time const &operator>>=(date_time_period const &v);
00830
00834 date_time operator+(date_time_period_set const &v) const;
00838 date_time operator-(date_time_period_set const &v) const;
00842 date_time const &operator+=(date_time_period_set const &v);
00846 date_time const &operator-=(date_time_period_set const &v);
00847
00851 date_time operator<<(date_time_period_set const &v) const;
00855 date_time operator>>(date_time_period_set const &v) const ;
00859 date_time const &operator<<=(date_time_period_set const &v);
00863 date_time const &operator>>=(date_time_period_set const &v);
00864
00870 double time() const;
00877 void time(double v);
00878
00882 bool operator==(date_time const &other) const;
00886 bool operator!=(date_time const &other) const;
00890 bool operator<(date_time const &other) const;
00894 bool operator>(date_time const &other) const;
00898 bool operator<=(date_time const &other) const;
00902 bool operator>=(date_time const &other) const;
00903
00907 void swap(date_time &other);
00908
00912 int difference(date_time const &other,period::period_type f) const;
00913
00917 int difference(date_time const &other,period::period_type f);
00918
00922 int minimum(period::period_type f) const;
00927 int maximum(period::period_type f) const;
00928
00929 private:
00930 void *impl_;
00931 };
00932
00946 template<typename CharType>
00947 std::basic_ostream<CharType> &operator<<(std::basic_ostream<CharType> &out,date_time const &t)
00948 {
00949 out << t.time();
00950 return out;
00951 }
00952
00958 template<typename CharType>
00959 std::basic_istream<CharType> &operator>>(std::basic_istream<CharType> &in,date_time &t)
00960 {
00961 double v;
00962 in >> v;
00963 t.time(v);
00964 return in;
00965 }
00966
00975 class date_time_duration {
00976 public:
00977
00982 date_time_duration(date_time const &first,date_time const &second) :
00983 s_(first),
00984 e_(second)
00985 {
00986 }
00987
00991 int operator / (period::period_type f) const
00992 {
00993 return start().difference(end(),f);
00994 }
00995
00999 date_time const &start() const { return s_; }
01003 date_time const &end() const { return e_; }
01004 private:
01005 date_time const &s_;
01006 date_time const &e_;
01007 };
01008
01013 inline date_time_duration operator-(date_time const &later,date_time const &earlier)
01014 {
01015 return date_time_duration(earlier,later);
01016 }
01017
01019
01020 }
01021 }
01022
01023 #ifdef BOOST_MSVC
01024 #pragma warning(pop)
01025 #endif
01026
01027
01028 #endif
01034
01035