date_time.hpp 46 KB

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