date_time.hpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_LOCALE_DATE_TIME_HPP_INCLUDED
  7. #define BOOST_LOCALE_DATE_TIME_HPP_INCLUDED
  8. #include <boost/locale/date_time_facet.hpp>
  9. #include <boost/locale/formatting.hpp>
  10. #include <boost/locale/hold_ptr.hpp>
  11. #include <boost/locale/time_zone.hpp>
  12. #include <array>
  13. #include <locale>
  14. #include <stdexcept>
  15. #include <vector>
  16. #ifdef BOOST_MSVC
  17. # pragma warning(push)
  18. # pragma warning(disable : 4275 4251 4231 4660)
  19. #endif
  20. namespace boost { namespace locale {
  21. /// \defgroup date_time Date, Time, Timezone and Calendar manipulations
  22. ///
  23. /// This module provides various calendar, timezone and date time services
  24. /// @{
  25. /// \brief This error is thrown in case of invalid state that occurred
  26. class BOOST_SYMBOL_VISIBLE date_time_error : public std::runtime_error {
  27. public:
  28. /// Constructor of date_time_error class
  29. date_time_error(const std::string& e) : std::runtime_error(e) {}
  30. };
  31. /// \brief This class represents a pair of period_type and the integer
  32. /// values that describes its amount. For example 3 days or 4 years.
  33. ///
  34. /// Usually obtained as product of period_type and integer or
  35. /// my calling a representative functions
  36. /// For example day()*3 == date_time_period(day(),3) == day(3)
  37. struct date_time_period {
  38. period::period_type type; ///< The type of period, i.e. era, year, day etc.
  39. int value; ///< The value the actual number of \a periods
  40. /// Operator + returns copy of itself
  41. date_time_period operator+() const { return *this; }
  42. /// Operator -, switches the sign of period
  43. date_time_period operator-() const { return date_time_period(type, -value); }
  44. /// Constructor that creates date_time_period from period_type \a f and a value \a v -- default 1.
  45. date_time_period(period::period_type f = period::period_type(), int v = 1) : type(f), value(v) {}
  46. };
  47. namespace period {
  48. /// Get period_type for: special invalid value, should not be used directly
  49. inline period_type invalid()
  50. {
  51. return period_type(marks::invalid);
  52. }
  53. /// Get period_type for: Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1]
  54. inline period_type era()
  55. {
  56. return period_type(marks::era);
  57. }
  58. /// Get period_type for: Year, it is calendar specific, for example 2011 in Gregorian calendar.
  59. inline period_type year()
  60. {
  61. return period_type(marks::year);
  62. }
  63. /// Get period_type for: Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1.
  64. inline period_type extended_year()
  65. {
  66. return period_type(marks::extended_year);
  67. }
  68. /// Get period_type for: The month of year, calendar specific, in Gregorian [0..11]
  69. inline period_type month()
  70. {
  71. return period_type(marks::month);
  72. }
  73. /// Get period_type for: The day of month, calendar specific, in Gregorian [1..31]
  74. inline period_type day()
  75. {
  76. return period_type(marks::day);
  77. }
  78. /// Get period_type for: The number of day in year, starting from 1, in Gregorian [1..366]
  79. inline period_type day_of_year()
  80. {
  81. return period_type(marks::day_of_year);
  82. }
  83. /// Get period_type for: Day of week, Sunday=1, Monday=2,..., Saturday=7.
  84. ///
  85. /// Note that updating this value respects local day of week, so for example,
  86. /// If first day of week is Monday and the current day is Tuesday then setting
  87. /// the value to Sunday (1) would forward the date by 5 days forward and not backward
  88. /// by two days as it could be expected if the numbers were taken as is.
  89. inline period_type day_of_week()
  90. {
  91. return period_type(marks::day_of_week);
  92. }
  93. /// Get period_type for: Original number of the day of the week in month. For example 1st Sunday,
  94. /// 2nd Sunday, etc. in Gregorian [1..5]
  95. inline period_type day_of_week_in_month()
  96. {
  97. return period_type(marks::day_of_week_in_month);
  98. }
  99. /// Get period_type for: Local day of week, for example in France Monday is 1, in US Sunday is 1, [1..7]
  100. inline period_type day_of_week_local()
  101. {
  102. return period_type(marks::day_of_week_local);
  103. }
  104. /// Get period_type for: 24 clock hour [0..23]
  105. inline period_type hour()
  106. {
  107. return period_type(marks::hour);
  108. }
  109. /// Get period_type for: 12 clock hour [0..11]
  110. inline period_type hour_12()
  111. {
  112. return period_type(marks::hour_12);
  113. }
  114. /// Get period_type for: am or pm marker [0..1]
  115. inline period_type am_pm()
  116. {
  117. return period_type(marks::am_pm);
  118. }
  119. /// Get period_type for: minute [0..59]
  120. inline period_type minute()
  121. {
  122. return period_type(marks::minute);
  123. }
  124. /// Get period_type for: second [0..59]
  125. inline period_type second()
  126. {
  127. return period_type(marks::second);
  128. }
  129. /// Get period_type for: The week number in the year
  130. inline period_type week_of_year()
  131. {
  132. return period_type(marks::week_of_year);
  133. }
  134. /// Get period_type for: The week number within current month
  135. inline period_type week_of_month()
  136. {
  137. return period_type(marks::week_of_month);
  138. }
  139. /// Get period_type for: First day of week, constant, for example Sunday in US = 1, Monday in France = 2
  140. inline period_type first_day_of_week()
  141. {
  142. return period_type(marks::first_day_of_week);
  143. }
  144. /// Get date_time_period for: Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1]
  145. inline date_time_period era(int v)
  146. {
  147. return date_time_period(era(), v);
  148. }
  149. /// Get date_time_period for: Year, it is calendar specific, for example 2011 in Gregorian calendar.
  150. inline date_time_period year(int v)
  151. {
  152. return date_time_period(year(), v);
  153. }
  154. /// Get date_time_period for: Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1.
  155. inline date_time_period extended_year(int v)
  156. {
  157. return date_time_period(extended_year(), v);
  158. }
  159. /// Get date_time_period for: The month of year, calendar specific, in Gregorian [0..11]
  160. inline date_time_period month(int v)
  161. {
  162. return date_time_period(month(), v);
  163. }
  164. /// Get date_time_period for: The day of month, calendar specific, in Gregorian [1..31]
  165. inline date_time_period day(int v)
  166. {
  167. return date_time_period(day(), v);
  168. }
  169. /// Get date_time_period for: The number of day in year, starting from 1, in Gregorian [1..366]
  170. inline date_time_period day_of_year(int v)
  171. {
  172. return date_time_period(day_of_year(), v);
  173. }
  174. /// Get date_time_period for: Day of week, Sunday=1, Monday=2,..., Saturday=7.
  175. ///
  176. /// Note that updating this value respects local day of week, so for example,
  177. /// If first day of week is Monday and the current day is Tuesday then setting
  178. /// the value to Sunday (1) would forward the date by 5 days forward and not backward
  179. /// by two days as it could be expected if the numbers were taken as is.
  180. inline date_time_period day_of_week(int v)
  181. {
  182. return date_time_period(day_of_week(), v);
  183. }
  184. /// Get date_time_period for: Original number of the day of the week in month. For example 1st Sunday,
  185. /// 2nd Sunday, etc. in Gregorian [1..5]
  186. inline date_time_period day_of_week_in_month(int v)
  187. {
  188. return date_time_period(day_of_week_in_month(), v);
  189. }
  190. /// Get date_time_period for: Local day of week, for example in France Monday is 1, in US Sunday is 1, [1..7]
  191. inline date_time_period day_of_week_local(int v)
  192. {
  193. return date_time_period(day_of_week_local(), v);
  194. }
  195. /// Get date_time_period for: 24 clock hour [0..23]
  196. inline date_time_period hour(int v)
  197. {
  198. return date_time_period(hour(), v);
  199. }
  200. /// Get date_time_period for: 12 clock hour [0..11]
  201. inline date_time_period hour_12(int v)
  202. {
  203. return date_time_period(hour_12(), v);
  204. }
  205. /// Get date_time_period for: am or pm marker [0..1]
  206. inline date_time_period am_pm(int v)
  207. {
  208. return date_time_period(am_pm(), v);
  209. }
  210. /// Get date_time_period for: minute [0..59]
  211. inline date_time_period minute(int v)
  212. {
  213. return date_time_period(minute(), v);
  214. }
  215. /// Get date_time_period for: second [0..59]
  216. inline date_time_period second(int v)
  217. {
  218. return date_time_period(second(), v);
  219. }
  220. /// Get date_time_period for: The week number in the year
  221. inline date_time_period week_of_year(int v)
  222. {
  223. return date_time_period(week_of_year(), v);
  224. }
  225. /// Get date_time_period for: The week number within current month
  226. inline date_time_period week_of_month(int v)
  227. {
  228. return date_time_period(week_of_month(), v);
  229. }
  230. /// Get date_time_period for: First day of week, constant, for example Sunday in US = 1, Monday in France = 2
  231. inline date_time_period first_day_of_week(int v)
  232. {
  233. return date_time_period(first_day_of_week(), v);
  234. }
  235. /// Get predefined constant for January
  236. inline date_time_period january()
  237. {
  238. return date_time_period(month(), 0);
  239. }
  240. /// Get predefined constant for February
  241. inline date_time_period february()
  242. {
  243. return date_time_period(month(), 1);
  244. }
  245. /// Get predefined constant for March
  246. inline date_time_period march()
  247. {
  248. return date_time_period(month(), 2);
  249. }
  250. /// Get predefined constant for April
  251. inline date_time_period april()
  252. {
  253. return date_time_period(month(), 3);
  254. }
  255. /// Get predefined constant for May
  256. inline date_time_period may()
  257. {
  258. return date_time_period(month(), 4);
  259. }
  260. /// Get predefined constant for June
  261. inline date_time_period june()
  262. {
  263. return date_time_period(month(), 5);
  264. }
  265. /// Get predefined constant for July
  266. inline date_time_period july()
  267. {
  268. return date_time_period(month(), 6);
  269. }
  270. /// Get predefined constant for August
  271. inline date_time_period august()
  272. {
  273. return date_time_period(month(), 7);
  274. }
  275. /// Get predefined constant for September
  276. inline date_time_period september()
  277. {
  278. return date_time_period(month(), 8);
  279. }
  280. /// Get predefined constant for October
  281. inline date_time_period october()
  282. {
  283. return date_time_period(month(), 9);
  284. }
  285. /// Get predefined constant for November
  286. inline date_time_period november()
  287. {
  288. return date_time_period(month(), 10);
  289. }
  290. /// Get predefined constant for December
  291. inline date_time_period december()
  292. {
  293. return date_time_period(month(), 11);
  294. }
  295. /// Get predefined constant for Sunday
  296. inline date_time_period sunday()
  297. {
  298. return date_time_period(day_of_week(), 1);
  299. }
  300. /// Get predefined constant for Monday
  301. inline date_time_period monday()
  302. {
  303. return date_time_period(day_of_week(), 2);
  304. }
  305. /// Get predefined constant for Tuesday
  306. inline date_time_period tuesday()
  307. {
  308. return date_time_period(day_of_week(), 3);
  309. }
  310. /// Get predefined constant for Wednesday
  311. inline date_time_period wednesday()
  312. {
  313. return date_time_period(day_of_week(), 4);
  314. }
  315. /// Get predefined constant for Thursday
  316. inline date_time_period thursday()
  317. {
  318. return date_time_period(day_of_week(), 5);
  319. }
  320. /// Get predefined constant for Friday
  321. inline date_time_period friday()
  322. {
  323. return date_time_period(day_of_week(), 6);
  324. }
  325. /// Get predefined constant for Saturday
  326. inline date_time_period saturday()
  327. {
  328. return date_time_period(day_of_week(), 7);
  329. }
  330. /// Get predefined constant for AM (Ante Meridiem)
  331. inline date_time_period am()
  332. {
  333. return date_time_period(am_pm(), 0);
  334. }
  335. /// Get predefined constant for PM (Post Meridiem)
  336. inline date_time_period pm()
  337. {
  338. return date_time_period(am_pm(), 1);
  339. }
  340. /// convert period_type to date_time_period(f,1)
  341. inline date_time_period operator+(period::period_type f)
  342. {
  343. return date_time_period(f);
  344. }
  345. /// convert period_type to date_time_period(f,-1)
  346. inline date_time_period operator-(period::period_type f)
  347. {
  348. return date_time_period(f, -1);
  349. }
  350. /// Create date_time_period of type \a f with value \a v.
  351. template<typename T>
  352. date_time_period operator*(period::period_type f, T v)
  353. {
  354. return date_time_period(f, v);
  355. }
  356. /// Create date_time_period of type \a f with value \a v.
  357. template<typename T>
  358. date_time_period operator*(T v, period::period_type f)
  359. {
  360. return date_time_period(f, v);
  361. }
  362. /// Create date_time_period of type \a f with value \a v.
  363. template<typename T>
  364. date_time_period operator*(T v, date_time_period f)
  365. {
  366. return date_time_period(f.type, f.value * v);
  367. }
  368. /// Create date_time_period of type \a f with value \a v.
  369. template<typename T>
  370. date_time_period operator*(date_time_period f, T v)
  371. {
  372. return date_time_period(f.type, f.value * v);
  373. }
  374. } // namespace period
  375. /// \brief This class represents a set of periods.
  376. ///
  377. /// It is generally created by operations on periods:
  378. /// 1995*year + 3*month + 1*day. Note: operations are not commutative.
  379. class date_time_period_set {
  380. public:
  381. /// Default constructor - empty set
  382. date_time_period_set() = default;
  383. /// Create a set of single period with value 1
  384. date_time_period_set(period::period_type f) { basic_[0] = date_time_period(f); }
  385. /// Create a set of single period \a fl
  386. date_time_period_set(const date_time_period& fl) { basic_[0] = fl; }
  387. /// Append date_time_period \a f to the set
  388. void add(date_time_period f)
  389. {
  390. const size_t n = size();
  391. if(n < basic_.size())
  392. basic_[n] = f;
  393. else
  394. periods_.push_back(f);
  395. }
  396. /// Get number of items in list
  397. size_t size() const
  398. {
  399. for(size_t i = 0; i < basic_.size(); ++i) {
  400. if(basic_[i].type == period::period_type())
  401. return i;
  402. }
  403. return basic_.size() + periods_.size();
  404. }
  405. /// Get item at position \a n the set, n should be in range [0,size)
  406. const date_time_period& operator[](size_t n) const
  407. {
  408. if(n >= size())
  409. throw std::out_of_range("Invalid index to date_time_period");
  410. if(n < basic_.size())
  411. return basic_[n];
  412. else
  413. return periods_[n - basic_.size()];
  414. }
  415. private:
  416. std::array<date_time_period, 4> basic_;
  417. std::vector<date_time_period> periods_;
  418. };
  419. /// Append two periods sets. Note this operator is not commutative
  420. inline date_time_period_set operator+(const date_time_period_set& a, const date_time_period_set& b)
  421. {
  422. date_time_period_set s(a);
  423. for(unsigned i = 0; i < b.size(); i++)
  424. s.add(b[i]);
  425. return s;
  426. }
  427. /// Append two period sets when all periods of set \b change their sign
  428. inline date_time_period_set operator-(const date_time_period_set& a, const date_time_period_set& b)
  429. {
  430. date_time_period_set s(a);
  431. for(unsigned i = 0; i < b.size(); i++)
  432. s.add(-b[i]);
  433. return s;
  434. }
  435. /// \brief this class provides an access to general calendar information.
  436. ///
  437. /// This information is not connected to specific date but generic to locale, and timezone.
  438. /// It is used in obtaining general information about calendar and is essential for creation of
  439. /// date_time objects.
  440. class BOOST_LOCALE_DECL calendar {
  441. public:
  442. /// Create calendar taking locale and timezone information from ios_base instance.
  443. ///
  444. /// \throws std::bad_cast: \a ios does not have a locale with installed \ref calendar_facet
  445. /// facet installed
  446. calendar(std::ios_base& ios);
  447. /// Create calendar with locale \a l and time_zone \a zone
  448. ///
  449. /// \throws std::bad_cast: \a l does not have \ref calendar_facet facet installed
  450. calendar(const std::locale& l, const std::string& zone);
  451. /// Create calendar with locale \a l and default timezone
  452. ///
  453. /// \throws std::bad_cast: \a l does not have \ref calendar_facet facet installed
  454. calendar(const std::locale& l);
  455. /// Create calendar with default locale and timezone \a zone
  456. ///
  457. /// \throws std::bad_cast: global locale does not have \ref calendar_facet facet installed
  458. calendar(const std::string& zone);
  459. /// Create calendar with default locale and timezone
  460. ///
  461. /// \throws std::bad_cast: global locale does not have \ref calendar_facet facet installed
  462. calendar();
  463. ~calendar();
  464. /// copy calendar
  465. calendar(const calendar& other);
  466. /// assign calendar
  467. calendar& operator=(const calendar& other);
  468. /// Get minimum value for period f, For example for period::day it is 1.
  469. int minimum(period::period_type f) const;
  470. /// Get greatest possible minimum value for period f, For example for period::day it is 1, but may be different
  471. /// for other calendars.
  472. int greatest_minimum(period::period_type f) const;
  473. /// Get maximum value for period f, For example for Gregorian calendar's maximum period::day it is 31.
  474. int maximum(period::period_type f) const;
  475. /// Get least maximum value for period f, For example for Gregorian calendar's maximum period::day it is 28.
  476. int least_maximum(period::period_type f) const;
  477. /// Get first day of week for specific calendar, for example for US it is 1 - Sunday for France it is 2 - Monday
  478. int first_day_of_week() const;
  479. /// get calendar's locale
  480. const std::locale& get_locale() const;
  481. /// get calendar's time zone
  482. const std::string& get_time_zone() const;
  483. /// Check if the calendar is Gregorian
  484. bool is_gregorian() const;
  485. /// Compare calendars for equivalence: i.e. calendar types, time zones etc.
  486. bool operator==(const calendar& other) const;
  487. /// Opposite of ==
  488. bool operator!=(const calendar& other) const;
  489. private:
  490. friend class date_time;
  491. std::locale locale_;
  492. std::string tz_;
  493. hold_ptr<abstract_calendar> impl_;
  494. };
  495. /// \brief this class represents a date time and allows to perform various operation according to the
  496. /// locale settings.
  497. ///
  498. /// This class allows to manipulate various aspects of dates and times easily using arithmetic operations with
  499. /// periods.
  500. ///
  501. /// General arithmetic functions:
  502. ///
  503. /// - date_time + date_time_period_set = date_time: move time point forward by specific periods like date_time +
  504. /// month;
  505. /// - date_time - date_time_period_set = date_time: move time point backward by specific periods like date_time -
  506. /// month;
  507. /// - date_time << date_time_period_set = date_time: roll time point forward by specific periods with rolling to
  508. /// begin if overflows: like "2010-01-31" << 2* day == "2010-01-02" instead of "2010-02-02"
  509. /// - date_time >> date_time_period_set = date_time: roll time point backward by specific periods with rolling to
  510. /// end if overflows: like "2010-01-02" >> 2* day == "2010-01-31" instead of "2009-12-30"
  511. /// - date_time / period_type = int - current period value: like "2010-12-21" / month == 12. "2010-12-21" / year =
  512. /// 2010
  513. /// - (date_time - date_time) / period_type = int: distance between dates in period_type. Like ("2010-12-01" -
  514. /// "2008-12-01") / month = 24.
  515. ///
  516. /// You can also assign specific periods using assignment operator like:
  517. /// some_time = year * 1995 that sets the year to 1995.
  518. class BOOST_LOCALE_DECL date_time {
  519. public:
  520. /// Default constructor, uses default calendar initialized date_time object to current time.
  521. ///
  522. /// \throws std::bad_cast: Global locale does not have \ref calendar_facet facet installed
  523. date_time();
  524. /// Copy a date_time
  525. date_time(const date_time& other);
  526. // Move construct a date_time
  527. date_time(date_time&&) noexcept = default;
  528. /// copy date_time and change some fields according to the \a set
  529. date_time(const date_time& other, const date_time_period_set& set);
  530. /// assign the date_time
  531. date_time& operator=(const date_time& other);
  532. // Move assign a date_time
  533. date_time& operator=(date_time&&) noexcept = default;
  534. /// Create a date_time object using POSIX time \a time and default calendar
  535. ///
  536. /// \throws std::bad_cast: Global locale does not have \ref calendar_facet facet installed
  537. date_time(double time);
  538. /// Create a date_time object using POSIX time \a time and calendar \a cal
  539. date_time(double time, const calendar& cal);
  540. /// Create a date_time object using calendar \a cal and initializes it to current time.
  541. date_time(const calendar& cal);
  542. /// Create a date_time object using default calendar and define values given in \a set
  543. ///
  544. /// \throws std::bad_cast: Global locale does not have \ref calendar_facet facet installed
  545. date_time(const date_time_period_set& set);
  546. /// Create a date_time object using calendar \a cal and define values given in \a set
  547. date_time(const date_time_period_set& set, const calendar& cal);
  548. /// assign values to various periods in set \a f
  549. date_time& operator=(const date_time_period_set& f);
  550. /// set specific period \a f value to \a v
  551. void set(period::period_type f, int v);
  552. /// get specific period \a f value
  553. int get(period::period_type f) const;
  554. /// syntactic sugar for get(f)
  555. int operator/(period::period_type f) const { return get(f); }
  556. /// add single period f to the current date_time
  557. date_time operator+(period::period_type f) const { return *this + date_time_period(f); }
  558. /// subtract single period f from the current date_time
  559. date_time operator-(period::period_type f) const { return *this - date_time_period(f); }
  560. /// add single period f to the current date_time
  561. date_time& operator+=(period::period_type f) { return *this += date_time_period(f); }
  562. /// subtract single period f from the current date_time
  563. date_time& operator-=(period::period_type f) { return *this -= date_time_period(f); }
  564. /// roll forward a date by single period f.
  565. date_time operator<<(period::period_type f) const { return *this << date_time_period(f); }
  566. /// roll backward a date by single period f.
  567. date_time operator>>(period::period_type f) const { return *this >> date_time_period(f); }
  568. /// roll forward a date by single period f.
  569. date_time& operator<<=(period::period_type f) { return *this <<= date_time_period(f); }
  570. /// roll backward a date by single period f.
  571. date_time& operator>>=(period::period_type f) { return *this >>= date_time_period(f); }
  572. /// add date_time_period to the current date_time
  573. date_time operator+(const date_time_period& v) const;
  574. /// subtract date_time_period from the current date_time
  575. date_time operator-(const date_time_period& v) const;
  576. /// add date_time_period to the current date_time
  577. date_time& operator+=(const date_time_period& v);
  578. /// subtract date_time_period from the current date_time
  579. date_time& operator-=(const date_time_period& v);
  580. /// roll current date_time forward by date_time_period v
  581. date_time operator<<(const date_time_period& v) const;
  582. /// roll current date_time backward by date_time_period v
  583. date_time operator>>(const date_time_period& v) const;
  584. /// roll current date_time forward by date_time_period v
  585. date_time& operator<<=(const date_time_period& v);
  586. /// roll current date_time backward by date_time_period v
  587. date_time& operator>>=(const date_time_period& v);
  588. /// add date_time_period_set v to the current date_time
  589. date_time operator+(const date_time_period_set& v) const;
  590. /// subtract date_time_period_set v from the current date_time
  591. date_time operator-(const date_time_period_set& v) const;
  592. /// add date_time_period_set v to the current date_time
  593. date_time& operator+=(const date_time_period_set& v);
  594. /// subtract date_time_period_set v from the current date_time
  595. date_time& operator-=(const date_time_period_set& v);
  596. /// roll current date_time forward by date_time_period_set v
  597. date_time operator<<(const date_time_period_set& v) const;
  598. /// roll current date_time backward by date_time_period_set v
  599. date_time operator>>(const date_time_period_set& v) const;
  600. /// roll current date_time forward by date_time_period_set v
  601. date_time& operator<<=(const date_time_period_set& v);
  602. /// roll current date_time backward by date_time_period_set v
  603. date_time& operator>>=(const date_time_period_set& v);
  604. /// Get POSIX time
  605. ///
  606. /// The POSIX time is number of seconds since January 1st, 1970 00:00 UTC, ignoring leap seconds.
  607. double time() const;
  608. /// Set POSIX time
  609. ///
  610. /// The POSIX time is number of seconds since January 1st, 1970 00:00 UTC, ignoring leap seconds.
  611. /// This time can be fetched from Operating system clock using C function time, gettimeofday and others.
  612. void time(double v);
  613. /// Get the name of the associated timezone
  614. std::string timezone() const;
  615. /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
  616. bool operator==(const date_time& other) const;
  617. /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
  618. bool operator!=(const date_time& other) const;
  619. /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
  620. bool operator<(const date_time& other) const;
  621. /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
  622. bool operator>(const date_time& other) const;
  623. /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
  624. bool operator<=(const date_time& other) const;
  625. /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
  626. bool operator>=(const date_time& other) const;
  627. /// swaps two dates - efficient, does not throw
  628. void swap(date_time& other) noexcept;
  629. /// calculate the distance from this date_time to \a other in terms of periods \a f
  630. int difference(const date_time& other, period::period_type f) const;
  631. /// Get minimal possible value for *this time point for a period \a f.
  632. int minimum(period::period_type f) const;
  633. /// Get minimal possible value for *this time point for a period \a f. For example
  634. /// in February maximum(day) may be 28 or 29, in January maximum(day)==31
  635. int maximum(period::period_type f) const;
  636. /// Check if *this time point is in daylight saving time
  637. bool is_in_daylight_saving_time() const;
  638. private:
  639. hold_ptr<abstract_calendar> impl_;
  640. };
  641. inline void swap(date_time& left, date_time& right) noexcept
  642. {
  643. left.swap(right);
  644. }
  645. /// Writes date_time \a t to output stream \a out.
  646. ///
  647. /// This function uses locale, calendar and time zone of the target stream \a in.
  648. ///
  649. /// For example:
  650. /// \code
  651. /// date_time now(time(nullptr),hebrew_calendar)
  652. /// std::cout << "Year: " << period::year(now) << " Full Date:" << now;
  653. /// \endcode
  654. ///
  655. /// The output may be Year:5770 Full Date:Jan 1, 2010
  656. template<typename CharType>
  657. std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const date_time& t)
  658. {
  659. const double time_point = t.time();
  660. ios_info& info = ios_info::get(out);
  661. const uint64_t display_flags = info.display_flags();
  662. if(as::detail::is_datetime_display_flags(display_flags)) {
  663. out << time_point;
  664. } else {
  665. info.display_flags(flags::datetime);
  666. out << time_point;
  667. info.display_flags(display_flags);
  668. }
  669. return out;
  670. }
  671. /// Reads date_time \a t from output stream \a in
  672. ///
  673. /// This function uses locale, calendar and time zone of the source stream \a in.
  674. template<typename CharType>
  675. std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, date_time& t)
  676. {
  677. double v;
  678. const uint64_t display_flags = ios_info::get(in).display_flags();
  679. if(as::detail::is_datetime_display_flags(display_flags)) {
  680. in >> v;
  681. } else {
  682. ios_info::get(in).display_flags(flags::datetime);
  683. in >> v;
  684. ios_info::get(in).display_flags(display_flags);
  685. }
  686. if(!in.fail())
  687. t.time(v);
  688. return in;
  689. }
  690. #ifdef BOOST_MSVC
  691. # pragma warning(push)
  692. # pragma warning(disable : 4512) // assignment operator could not be generated
  693. #endif
  694. /// \brief This class represents a period: a pair of two date_time objects.
  695. ///
  696. /// It is generally used as syntactic sugar to calculate difference between two dates.
  697. ///
  698. /// Note: it stores references to the original objects, so it is not recommended to be used
  699. /// outside of the equation you calculate the difference in.
  700. class date_time_duration {
  701. public:
  702. /// Create an object were \a first represents earlier point on time line and \a second is later
  703. /// point.
  704. date_time_duration(const date_time& first, const date_time& second) : s_(first), e_(second) {}
  705. /// find a difference in terms of period_type \a f
  706. int get(period::period_type f) const { return start().difference(end(), f); }
  707. /// Syntactic sugar for get(f)
  708. int operator/(period::period_type f) const { return start().difference(end(), f); }
  709. /// Get starting point
  710. const date_time& start() const { return s_; }
  711. /// Get ending point
  712. const date_time& end() const { return e_; }
  713. private:
  714. const date_time& s_;
  715. const date_time& e_;
  716. };
  717. #ifdef BOOST_MSVC
  718. # pragma warning(pop)
  719. #endif
  720. /// Calculates the difference between two dates, the left operand is a later point on time line.
  721. /// Returns date_time_duration object.
  722. inline date_time_duration operator-(const date_time& later, const date_time& earlier)
  723. {
  724. return date_time_duration(earlier, later);
  725. }
  726. namespace period {
  727. /// Extract from date_time numerical value of Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1]
  728. inline int era(const date_time& dt)
  729. {
  730. return dt.get(era());
  731. }
  732. /// Extract from date_time numerical value of Year, it is calendar specific, for example 2011 in Gregorian
  733. /// calendar.
  734. inline int year(const date_time& dt)
  735. {
  736. return dt.get(year());
  737. }
  738. /// Extract from date_time numerical value of Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2
  739. /// BC == -1.
  740. inline int extended_year(const date_time& dt)
  741. {
  742. return dt.get(extended_year());
  743. }
  744. /// Extract from date_time numerical value of The month of year, calendar specific, in Gregorian [0..11]
  745. inline int month(const date_time& dt)
  746. {
  747. return dt.get(month());
  748. }
  749. /// Extract from date_time numerical value of The day of month, calendar specific, in Gregorian [1..31]
  750. inline int day(const date_time& dt)
  751. {
  752. return dt.get(day());
  753. }
  754. /// Extract from date_time numerical value of The number of day in year, starting from 1, in Gregorian [1..366]
  755. inline int day_of_year(const date_time& dt)
  756. {
  757. return dt.get(day_of_year());
  758. }
  759. /// Extract from date_time numerical value of Day of week, Sunday=1, Monday=2,..., Saturday=7.
  760. ///
  761. /// Note that updating this value respects local day of week, so for example,
  762. /// If first day of week is Monday and the current day is Tuesday then setting
  763. /// the value to Sunday (1) would forward the date by 5 days forward and not backward
  764. /// by two days as it could be expected if the numbers were taken as is.
  765. inline int day_of_week(const date_time& dt)
  766. {
  767. return dt.get(day_of_week());
  768. }
  769. /// Extract from date_time numerical value of Original number of the day of the week in month. For example 1st
  770. /// Sunday,
  771. /// 2nd Sunday, etc. in Gregorian [1..5]
  772. inline int day_of_week_in_month(const date_time& dt)
  773. {
  774. return dt.get(day_of_week_in_month());
  775. }
  776. /// Extract from date_time numerical value of Local day of week, for example in France Monday is 1, in US
  777. /// Sunday is 1, [1..7]
  778. inline int day_of_week_local(const date_time& dt)
  779. {
  780. return dt.get(day_of_week_local());
  781. }
  782. /// Extract from date_time numerical value of 24 clock hour [0..23]
  783. inline int hour(const date_time& dt)
  784. {
  785. return dt.get(hour());
  786. }
  787. /// Extract from date_time numerical value of 12 clock hour [0..11]
  788. inline int hour_12(const date_time& dt)
  789. {
  790. return dt.get(hour_12());
  791. }
  792. /// Extract from date_time numerical value of am or pm marker [0..1]
  793. inline int am_pm(const date_time& dt)
  794. {
  795. return dt.get(am_pm());
  796. }
  797. /// Extract from date_time numerical value of minute [0..59]
  798. inline int minute(const date_time& dt)
  799. {
  800. return dt.get(minute());
  801. }
  802. /// Extract from date_time numerical value of second [0..59]
  803. inline int second(const date_time& dt)
  804. {
  805. return dt.get(second());
  806. }
  807. /// Extract from date_time numerical value of The week number in the year
  808. inline int week_of_year(const date_time& dt)
  809. {
  810. return dt.get(week_of_year());
  811. }
  812. /// Extract from date_time numerical value of The week number within current month
  813. inline int week_of_month(const date_time& dt)
  814. {
  815. return dt.get(week_of_month());
  816. }
  817. /// Extract from date_time numerical value of First day of week, constant, for example Sunday in US = 1, Monday
  818. /// in France = 2
  819. inline int first_day_of_week(const date_time& dt)
  820. {
  821. return dt.get(first_day_of_week());
  822. }
  823. /// Extract from date_time_duration numerical value of duration in Era i.e. AC, BC in Gregorian and Julian
  824. /// calendar, range [0,1]
  825. inline int era(const date_time_duration& dt)
  826. {
  827. return dt.get(era());
  828. }
  829. /// Extract from date_time_duration numerical value of duration in years
  830. inline int year(const date_time_duration& dt)
  831. {
  832. return dt.get(year());
  833. }
  834. /// Extract from date_time_duration numerical value of duration in extended years (for Gregorian/Julian
  835. /// calendars, where 1 BC == 0, 2 BC == -1).
  836. inline int extended_year(const date_time_duration& dt)
  837. {
  838. return dt.get(extended_year());
  839. }
  840. /// Extract from date_time_duration numerical value of duration in months
  841. inline int month(const date_time_duration& dt)
  842. {
  843. return dt.get(month());
  844. }
  845. /// Extract from date_time_duration numerical value of duration in days of month
  846. inline int day(const date_time_duration& dt)
  847. {
  848. return dt.get(day());
  849. }
  850. /// Extract from date_time_duration numerical value of duration in days of year
  851. inline int day_of_year(const date_time_duration& dt)
  852. {
  853. return dt.get(day_of_year());
  854. }
  855. /// Extract from date_time_duration numerical value of duration in days of week
  856. inline int day_of_week(const date_time_duration& dt)
  857. {
  858. return dt.get(day_of_week());
  859. }
  860. /// Extract from date_time_duration numerical value of duration in original number of the day of the week in
  861. /// month
  862. inline int day_of_week_in_month(const date_time_duration& dt)
  863. {
  864. return dt.get(day_of_week_in_month());
  865. }
  866. /// Extract from date_time_duration numerical value of duration in local day of week
  867. inline int day_of_week_local(const date_time_duration& dt)
  868. {
  869. return dt.get(day_of_week_local());
  870. }
  871. /// Extract from date_time_duration numerical value of duration in hours
  872. inline int hour(const date_time_duration& dt)
  873. {
  874. return dt.get(hour());
  875. }
  876. /// Extract from date_time_duration numerical value of duration in 12 clock hours
  877. inline int hour_12(const date_time_duration& dt)
  878. {
  879. return dt.get(hour_12());
  880. }
  881. /// Extract from date_time_duration numerical value of duration in am or pm markers
  882. inline int am_pm(const date_time_duration& dt)
  883. {
  884. return dt.get(am_pm());
  885. }
  886. /// Extract from date_time_duration numerical value of duration in minutes
  887. inline int minute(const date_time_duration& dt)
  888. {
  889. return dt.get(minute());
  890. }
  891. /// Extract from date_time_duration numerical value of duration in seconds
  892. inline int second(const date_time_duration& dt)
  893. {
  894. return dt.get(second());
  895. }
  896. /// Extract from date_time_duration numerical value of duration in the week number in the year
  897. inline int week_of_year(const date_time_duration& dt)
  898. {
  899. return dt.get(week_of_year());
  900. }
  901. /// Extract from date_time_duration numerical value of duration in The week number within current month
  902. inline int week_of_month(const date_time_duration& dt)
  903. {
  904. return dt.get(week_of_month());
  905. }
  906. /// Extract from date_time_duration numerical value of duration in the first day of week
  907. inline int first_day_of_week(const date_time_duration& dt)
  908. {
  909. return dt.get(first_day_of_week());
  910. }
  911. } // namespace period
  912. /// @}
  913. }} // namespace boost::locale
  914. #ifdef BOOST_MSVC
  915. # pragma warning(pop)
  916. #endif
  917. ///
  918. /// \example calendar.cpp
  919. ///
  920. /// Example of using date_time functions for generating calendar for current year.
  921. ///
  922. #endif