File indexing completed on 2025-01-18 09:39:16
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_LOCALE_DATE_TIME_HPP_INCLUDED
0008 #define BOOST_LOCALE_DATE_TIME_HPP_INCLUDED
0009
0010 #include <boost/locale/date_time_facet.hpp>
0011 #include <boost/locale/formatting.hpp>
0012 #include <boost/locale/hold_ptr.hpp>
0013 #include <boost/locale/time_zone.hpp>
0014 #include <array>
0015 #include <locale>
0016 #include <stdexcept>
0017 #include <vector>
0018
0019 #ifdef BOOST_MSVC
0020 # pragma warning(push)
0021 # pragma warning(disable : 4275 4251 4231 4660)
0022 #endif
0023
0024 namespace boost { namespace locale {
0025
0026
0027
0028
0029
0030
0031 class BOOST_SYMBOL_VISIBLE date_time_error : public std::runtime_error {
0032 public:
0033
0034 date_time_error(const std::string& e) : std::runtime_error(e) {}
0035 };
0036
0037
0038
0039
0040
0041
0042
0043 struct date_time_period {
0044 period::period_type type;
0045 int value;
0046
0047 date_time_period operator+() const { return *this; }
0048
0049 date_time_period operator-() const { return date_time_period(type, -value); }
0050
0051
0052 date_time_period(period::period_type f = period::period_type(), int v = 1) : type(f), value(v) {}
0053 };
0054
0055 namespace period {
0056
0057 inline period_type invalid()
0058 {
0059 return period_type(marks::invalid);
0060 }
0061
0062 inline period_type era()
0063 {
0064 return period_type(marks::era);
0065 }
0066
0067 inline period_type year()
0068 {
0069 return period_type(marks::year);
0070 }
0071
0072 inline period_type extended_year()
0073 {
0074 return period_type(marks::extended_year);
0075 }
0076
0077 inline period_type month()
0078 {
0079 return period_type(marks::month);
0080 }
0081
0082 inline period_type day()
0083 {
0084 return period_type(marks::day);
0085 }
0086
0087 inline period_type day_of_year()
0088 {
0089 return period_type(marks::day_of_year);
0090 }
0091
0092
0093
0094
0095
0096
0097 inline period_type day_of_week()
0098 {
0099 return period_type(marks::day_of_week);
0100 }
0101
0102
0103 inline period_type day_of_week_in_month()
0104 {
0105 return period_type(marks::day_of_week_in_month);
0106 }
0107
0108 inline period_type day_of_week_local()
0109 {
0110 return period_type(marks::day_of_week_local);
0111 }
0112
0113 inline period_type hour()
0114 {
0115 return period_type(marks::hour);
0116 }
0117
0118 inline period_type hour_12()
0119 {
0120 return period_type(marks::hour_12);
0121 }
0122
0123 inline period_type am_pm()
0124 {
0125 return period_type(marks::am_pm);
0126 }
0127
0128 inline period_type minute()
0129 {
0130 return period_type(marks::minute);
0131 }
0132
0133 inline period_type second()
0134 {
0135 return period_type(marks::second);
0136 }
0137
0138 inline period_type week_of_year()
0139 {
0140 return period_type(marks::week_of_year);
0141 }
0142
0143 inline period_type week_of_month()
0144 {
0145 return period_type(marks::week_of_month);
0146 }
0147
0148 inline period_type first_day_of_week()
0149 {
0150 return period_type(marks::first_day_of_week);
0151 }
0152
0153
0154 inline date_time_period era(int v)
0155 {
0156 return date_time_period(era(), v);
0157 }
0158
0159 inline date_time_period year(int v)
0160 {
0161 return date_time_period(year(), v);
0162 }
0163
0164 inline date_time_period extended_year(int v)
0165 {
0166 return date_time_period(extended_year(), v);
0167 }
0168
0169 inline date_time_period month(int v)
0170 {
0171 return date_time_period(month(), v);
0172 }
0173
0174 inline date_time_period day(int v)
0175 {
0176 return date_time_period(day(), v);
0177 }
0178
0179 inline date_time_period day_of_year(int v)
0180 {
0181 return date_time_period(day_of_year(), v);
0182 }
0183
0184
0185
0186
0187
0188
0189 inline date_time_period day_of_week(int v)
0190 {
0191 return date_time_period(day_of_week(), v);
0192 }
0193
0194
0195 inline date_time_period day_of_week_in_month(int v)
0196 {
0197 return date_time_period(day_of_week_in_month(), v);
0198 }
0199
0200 inline date_time_period day_of_week_local(int v)
0201 {
0202 return date_time_period(day_of_week_local(), v);
0203 }
0204
0205 inline date_time_period hour(int v)
0206 {
0207 return date_time_period(hour(), v);
0208 }
0209
0210 inline date_time_period hour_12(int v)
0211 {
0212 return date_time_period(hour_12(), v);
0213 }
0214
0215 inline date_time_period am_pm(int v)
0216 {
0217 return date_time_period(am_pm(), v);
0218 }
0219
0220 inline date_time_period minute(int v)
0221 {
0222 return date_time_period(minute(), v);
0223 }
0224
0225 inline date_time_period second(int v)
0226 {
0227 return date_time_period(second(), v);
0228 }
0229
0230 inline date_time_period week_of_year(int v)
0231 {
0232 return date_time_period(week_of_year(), v);
0233 }
0234
0235 inline date_time_period week_of_month(int v)
0236 {
0237 return date_time_period(week_of_month(), v);
0238 }
0239
0240 inline date_time_period first_day_of_week(int v)
0241 {
0242 return date_time_period(first_day_of_week(), v);
0243 }
0244
0245
0246 inline date_time_period january()
0247 {
0248 return date_time_period(month(), 0);
0249 }
0250
0251 inline date_time_period february()
0252 {
0253 return date_time_period(month(), 1);
0254 }
0255
0256 inline date_time_period march()
0257 {
0258 return date_time_period(month(), 2);
0259 }
0260
0261 inline date_time_period april()
0262 {
0263 return date_time_period(month(), 3);
0264 }
0265
0266 inline date_time_period may()
0267 {
0268 return date_time_period(month(), 4);
0269 }
0270
0271 inline date_time_period june()
0272 {
0273 return date_time_period(month(), 5);
0274 }
0275
0276 inline date_time_period july()
0277 {
0278 return date_time_period(month(), 6);
0279 }
0280
0281 inline date_time_period august()
0282 {
0283 return date_time_period(month(), 7);
0284 }
0285
0286 inline date_time_period september()
0287 {
0288 return date_time_period(month(), 8);
0289 }
0290
0291 inline date_time_period october()
0292 {
0293 return date_time_period(month(), 9);
0294 }
0295
0296 inline date_time_period november()
0297 {
0298 return date_time_period(month(), 10);
0299 }
0300
0301 inline date_time_period december()
0302 {
0303 return date_time_period(month(), 11);
0304 }
0305
0306
0307 inline date_time_period sunday()
0308 {
0309 return date_time_period(day_of_week(), 1);
0310 }
0311
0312 inline date_time_period monday()
0313 {
0314 return date_time_period(day_of_week(), 2);
0315 }
0316
0317 inline date_time_period tuesday()
0318 {
0319 return date_time_period(day_of_week(), 3);
0320 }
0321
0322 inline date_time_period wednesday()
0323 {
0324 return date_time_period(day_of_week(), 4);
0325 }
0326
0327 inline date_time_period thursday()
0328 {
0329 return date_time_period(day_of_week(), 5);
0330 }
0331
0332 inline date_time_period friday()
0333 {
0334 return date_time_period(day_of_week(), 6);
0335 }
0336
0337 inline date_time_period saturday()
0338 {
0339 return date_time_period(day_of_week(), 7);
0340 }
0341
0342 inline date_time_period am()
0343 {
0344 return date_time_period(am_pm(), 0);
0345 }
0346
0347 inline date_time_period pm()
0348 {
0349 return date_time_period(am_pm(), 1);
0350 }
0351
0352
0353 inline date_time_period operator+(period::period_type f)
0354 {
0355 return date_time_period(f);
0356 }
0357
0358 inline date_time_period operator-(period::period_type f)
0359 {
0360 return date_time_period(f, -1);
0361 }
0362
0363
0364 template<typename T>
0365 date_time_period operator*(period::period_type f, T v)
0366 {
0367 return date_time_period(f, v);
0368 }
0369
0370
0371 template<typename T>
0372 date_time_period operator*(T v, period::period_type f)
0373 {
0374 return date_time_period(f, v);
0375 }
0376
0377 template<typename T>
0378 date_time_period operator*(T v, date_time_period f)
0379 {
0380 return date_time_period(f.type, f.value * v);
0381 }
0382
0383
0384 template<typename T>
0385 date_time_period operator*(date_time_period f, T v)
0386 {
0387 return date_time_period(f.type, f.value * v);
0388 }
0389
0390 }
0391
0392
0393
0394
0395
0396 class date_time_period_set {
0397 public:
0398
0399 date_time_period_set() = default;
0400
0401
0402 date_time_period_set(period::period_type f) { basic_[0] = date_time_period(f); }
0403
0404
0405 date_time_period_set(const date_time_period& fl) { basic_[0] = fl; }
0406
0407
0408 void add(date_time_period f)
0409 {
0410 const size_t n = size();
0411 if(n < basic_.size())
0412 basic_[n] = f;
0413 else
0414 periods_.push_back(f);
0415 }
0416
0417
0418 size_t size() const
0419 {
0420 for(size_t i = 0; i < basic_.size(); ++i) {
0421 if(basic_[i].type == period::period_type())
0422 return i;
0423 }
0424 return basic_.size() + periods_.size();
0425 }
0426
0427
0428 const date_time_period& operator[](size_t n) const
0429 {
0430 if(n >= size())
0431 throw std::out_of_range("Invalid index to date_time_period");
0432 if(n < basic_.size())
0433 return basic_[n];
0434 else
0435 return periods_[n - basic_.size()];
0436 }
0437
0438 private:
0439 std::array<date_time_period, 4> basic_;
0440 std::vector<date_time_period> periods_;
0441 };
0442
0443
0444 inline date_time_period_set operator+(const date_time_period_set& a, const date_time_period_set& b)
0445 {
0446 date_time_period_set s(a);
0447 for(unsigned i = 0; i < b.size(); i++)
0448 s.add(b[i]);
0449 return s;
0450 }
0451
0452
0453 inline date_time_period_set operator-(const date_time_period_set& a, const date_time_period_set& b)
0454 {
0455 date_time_period_set s(a);
0456 for(unsigned i = 0; i < b.size(); i++)
0457 s.add(-b[i]);
0458 return s;
0459 }
0460
0461
0462
0463
0464
0465
0466 class BOOST_LOCALE_DECL calendar {
0467 public:
0468
0469
0470
0471
0472 calendar(std::ios_base& ios);
0473
0474
0475
0476
0477 calendar(const std::locale& l, const std::string& zone);
0478
0479
0480
0481
0482 calendar(const std::locale& l);
0483
0484
0485
0486
0487
0488 calendar(const std::string& zone);
0489
0490
0491
0492
0493 calendar();
0494 ~calendar();
0495
0496
0497 calendar(const calendar& other);
0498
0499 calendar& operator=(const calendar& other);
0500
0501
0502 int minimum(period::period_type f) const;
0503
0504
0505 int greatest_minimum(period::period_type f) const;
0506
0507 int maximum(period::period_type f) const;
0508
0509 int least_maximum(period::period_type f) const;
0510
0511
0512 int first_day_of_week() const;
0513
0514
0515 const std::locale& get_locale() const;
0516
0517 const std::string& get_time_zone() const;
0518
0519
0520 bool is_gregorian() const;
0521
0522
0523 bool operator==(const calendar& other) const;
0524
0525 bool operator!=(const calendar& other) const;
0526
0527 private:
0528 friend class date_time;
0529 std::locale locale_;
0530 std::string tz_;
0531 hold_ptr<abstract_calendar> impl_;
0532 };
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558 class BOOST_LOCALE_DECL date_time {
0559 public:
0560
0561
0562
0563 date_time();
0564
0565
0566 date_time(const date_time& other);
0567
0568 date_time(date_time&&) noexcept = default;
0569
0570
0571 date_time(const date_time& other, const date_time_period_set& set);
0572
0573
0574 date_time& operator=(const date_time& other);
0575
0576 date_time& operator=(date_time&&) noexcept = default;
0577
0578
0579
0580
0581 date_time(double time);
0582
0583
0584 date_time(double time, const calendar& cal);
0585
0586
0587 date_time(const calendar& cal);
0588
0589
0590
0591
0592 date_time(const date_time_period_set& set);
0593
0594
0595 date_time(const date_time_period_set& set, const calendar& cal);
0596
0597
0598 date_time& operator=(const date_time_period_set& f);
0599
0600
0601 void set(period::period_type f, int v);
0602
0603
0604 int get(period::period_type f) const;
0605
0606 int operator/(period::period_type f) const { return get(f); }
0607
0608
0609 date_time operator+(period::period_type f) const { return *this + date_time_period(f); }
0610
0611 date_time operator-(period::period_type f) const { return *this - date_time_period(f); }
0612
0613 date_time& operator+=(period::period_type f) { return *this += date_time_period(f); }
0614
0615 date_time& operator-=(period::period_type f) { return *this -= date_time_period(f); }
0616
0617
0618 date_time operator<<(period::period_type f) const { return *this << date_time_period(f); }
0619
0620 date_time operator>>(period::period_type f) const { return *this >> date_time_period(f); }
0621
0622 date_time& operator<<=(period::period_type f) { return *this <<= date_time_period(f); }
0623
0624 date_time& operator>>=(period::period_type f) { return *this >>= date_time_period(f); }
0625
0626
0627 date_time operator+(const date_time_period& v) const;
0628
0629 date_time operator-(const date_time_period& v) const;
0630
0631 date_time& operator+=(const date_time_period& v);
0632
0633 date_time& operator-=(const date_time_period& v);
0634
0635
0636 date_time operator<<(const date_time_period& v) const;
0637
0638 date_time operator>>(const date_time_period& v) const;
0639
0640 date_time& operator<<=(const date_time_period& v);
0641
0642 date_time& operator>>=(const date_time_period& v);
0643
0644
0645 date_time operator+(const date_time_period_set& v) const;
0646
0647 date_time operator-(const date_time_period_set& v) const;
0648
0649 date_time& operator+=(const date_time_period_set& v);
0650
0651 date_time& operator-=(const date_time_period_set& v);
0652
0653
0654 date_time operator<<(const date_time_period_set& v) const;
0655
0656 date_time operator>>(const date_time_period_set& v) const;
0657
0658 date_time& operator<<=(const date_time_period_set& v);
0659
0660 date_time& operator>>=(const date_time_period_set& v);
0661
0662
0663
0664
0665 double time() const;
0666
0667
0668
0669
0670 void time(double v);
0671
0672
0673 std::string timezone() const;
0674
0675
0676 bool operator==(const date_time& other) const;
0677
0678 bool operator!=(const date_time& other) const;
0679
0680 bool operator<(const date_time& other) const;
0681
0682 bool operator>(const date_time& other) const;
0683
0684 bool operator<=(const date_time& other) const;
0685
0686 bool operator>=(const date_time& other) const;
0687
0688
0689 void swap(date_time& other) noexcept;
0690
0691
0692 int difference(const date_time& other, period::period_type f) const;
0693
0694
0695 int minimum(period::period_type f) const;
0696
0697
0698 int maximum(period::period_type f) const;
0699
0700
0701 bool is_in_daylight_saving_time() const;
0702
0703 private:
0704 hold_ptr<abstract_calendar> impl_;
0705 };
0706
0707 inline void swap(date_time& left, date_time& right) noexcept
0708 {
0709 left.swap(right);
0710 }
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723 template<typename CharType>
0724 std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const date_time& t)
0725 {
0726 const double time_point = t.time();
0727 ios_info& info = ios_info::get(out);
0728 const uint64_t display_flags = info.display_flags();
0729 if(as::detail::is_datetime_display_flags(display_flags)) {
0730 out << time_point;
0731 } else {
0732 info.display_flags(flags::datetime);
0733 out << time_point;
0734 info.display_flags(display_flags);
0735 }
0736 return out;
0737 }
0738
0739
0740
0741
0742 template<typename CharType>
0743 std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, date_time& t)
0744 {
0745 double v;
0746 const uint64_t display_flags = ios_info::get(in).display_flags();
0747 if(as::detail::is_datetime_display_flags(display_flags)) {
0748 in >> v;
0749 } else {
0750 ios_info::get(in).display_flags(flags::datetime);
0751 in >> v;
0752 ios_info::get(in).display_flags(display_flags);
0753 }
0754 if(!in.fail())
0755 t.time(v);
0756 return in;
0757 }
0758
0759 #ifdef BOOST_MSVC
0760 # pragma warning(push)
0761 # pragma warning(disable : 4512)
0762 #endif
0763
0764
0765
0766
0767
0768
0769
0770 class date_time_duration {
0771 public:
0772
0773
0774 date_time_duration(const date_time& first, const date_time& second) : s_(first), e_(second) {}
0775
0776
0777 int get(period::period_type f) const { return start().difference(end(), f); }
0778
0779 int operator/(period::period_type f) const { return start().difference(end(), f); }
0780
0781
0782 const date_time& start() const { return s_; }
0783
0784 const date_time& end() const { return e_; }
0785
0786 private:
0787 const date_time& s_;
0788 const date_time& e_;
0789 };
0790 #ifdef BOOST_MSVC
0791 # pragma warning(pop)
0792 #endif
0793
0794
0795
0796 inline date_time_duration operator-(const date_time& later, const date_time& earlier)
0797 {
0798 return date_time_duration(earlier, later);
0799 }
0800
0801 namespace period {
0802
0803 inline int era(const date_time& dt)
0804 {
0805 return dt.get(era());
0806 }
0807
0808
0809 inline int year(const date_time& dt)
0810 {
0811 return dt.get(year());
0812 }
0813
0814
0815 inline int extended_year(const date_time& dt)
0816 {
0817 return dt.get(extended_year());
0818 }
0819
0820 inline int month(const date_time& dt)
0821 {
0822 return dt.get(month());
0823 }
0824
0825 inline int day(const date_time& dt)
0826 {
0827 return dt.get(day());
0828 }
0829
0830 inline int day_of_year(const date_time& dt)
0831 {
0832 return dt.get(day_of_year());
0833 }
0834
0835
0836
0837
0838
0839
0840 inline int day_of_week(const date_time& dt)
0841 {
0842 return dt.get(day_of_week());
0843 }
0844
0845
0846
0847 inline int day_of_week_in_month(const date_time& dt)
0848 {
0849 return dt.get(day_of_week_in_month());
0850 }
0851
0852
0853 inline int day_of_week_local(const date_time& dt)
0854 {
0855 return dt.get(day_of_week_local());
0856 }
0857
0858 inline int hour(const date_time& dt)
0859 {
0860 return dt.get(hour());
0861 }
0862
0863 inline int hour_12(const date_time& dt)
0864 {
0865 return dt.get(hour_12());
0866 }
0867
0868 inline int am_pm(const date_time& dt)
0869 {
0870 return dt.get(am_pm());
0871 }
0872
0873 inline int minute(const date_time& dt)
0874 {
0875 return dt.get(minute());
0876 }
0877
0878 inline int second(const date_time& dt)
0879 {
0880 return dt.get(second());
0881 }
0882
0883 inline int week_of_year(const date_time& dt)
0884 {
0885 return dt.get(week_of_year());
0886 }
0887
0888 inline int week_of_month(const date_time& dt)
0889 {
0890 return dt.get(week_of_month());
0891 }
0892
0893
0894 inline int first_day_of_week(const date_time& dt)
0895 {
0896 return dt.get(first_day_of_week());
0897 }
0898
0899
0900
0901 inline int era(const date_time_duration& dt)
0902 {
0903 return dt.get(era());
0904 }
0905
0906 inline int year(const date_time_duration& dt)
0907 {
0908 return dt.get(year());
0909 }
0910
0911
0912 inline int extended_year(const date_time_duration& dt)
0913 {
0914 return dt.get(extended_year());
0915 }
0916
0917 inline int month(const date_time_duration& dt)
0918 {
0919 return dt.get(month());
0920 }
0921
0922 inline int day(const date_time_duration& dt)
0923 {
0924 return dt.get(day());
0925 }
0926
0927 inline int day_of_year(const date_time_duration& dt)
0928 {
0929 return dt.get(day_of_year());
0930 }
0931
0932 inline int day_of_week(const date_time_duration& dt)
0933 {
0934 return dt.get(day_of_week());
0935 }
0936
0937
0938 inline int day_of_week_in_month(const date_time_duration& dt)
0939 {
0940 return dt.get(day_of_week_in_month());
0941 }
0942
0943 inline int day_of_week_local(const date_time_duration& dt)
0944 {
0945 return dt.get(day_of_week_local());
0946 }
0947
0948 inline int hour(const date_time_duration& dt)
0949 {
0950 return dt.get(hour());
0951 }
0952
0953 inline int hour_12(const date_time_duration& dt)
0954 {
0955 return dt.get(hour_12());
0956 }
0957
0958 inline int am_pm(const date_time_duration& dt)
0959 {
0960 return dt.get(am_pm());
0961 }
0962
0963 inline int minute(const date_time_duration& dt)
0964 {
0965 return dt.get(minute());
0966 }
0967
0968 inline int second(const date_time_duration& dt)
0969 {
0970 return dt.get(second());
0971 }
0972
0973 inline int week_of_year(const date_time_duration& dt)
0974 {
0975 return dt.get(week_of_year());
0976 }
0977
0978 inline int week_of_month(const date_time_duration& dt)
0979 {
0980 return dt.get(week_of_month());
0981 }
0982
0983 inline int first_day_of_week(const date_time_duration& dt)
0984 {
0985 return dt.get(first_day_of_week());
0986 }
0987
0988 }
0989
0990
0991
0992 }}
0993
0994 #ifdef BOOST_MSVC
0995 # pragma warning(pop)
0996 #endif
0997
0998
0999
1000
1001
1002
1003
1004 #endif