posix_time_zone.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #ifndef _DATE_TIME_POSIX_TIME_ZONE__
  2. #define _DATE_TIME_POSIX_TIME_ZONE__
  3. /* Copyright (c) 2003-2005 CrystalClear Software, Inc.
  4. * Subject to the Boost Software License, Version 1.0. (See accompanying
  5. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. * Author: Jeff Garland, Bart Garst
  7. * $Date: 2008-11-26 10:39:19 -0500 (Wed, 26 Nov 2008) $
  8. */
  9. #include <string>
  10. #include <sstream>
  11. #include <stdexcept>
  12. #include <boost/tokenizer.hpp>
  13. #include <boost/throw_exception.hpp>
  14. #include <boost/date_time/gregorian/gregorian.hpp>
  15. #include <boost/date_time/time_zone_names.hpp>
  16. #include <boost/date_time/time_zone_base.hpp>
  17. #include <boost/date_time/local_time/dst_transition_day_rules.hpp>
  18. #include <boost/date_time/posix_time/posix_time.hpp>
  19. #include <boost/date_time/string_convert.hpp>
  20. #include <boost/date_time/time_parsing.hpp>
  21. namespace boost{
  22. namespace local_time{
  23. //! simple exception for UTC and Daylight savings start/end offsets
  24. struct bad_offset : public std::out_of_range
  25. {
  26. bad_offset(std::string const& msg = std::string()) :
  27. std::out_of_range(std::string("Offset out of range: " + msg)) {}
  28. };
  29. //! simple exception for UTC daylight savings adjustment
  30. struct bad_adjustment : public std::out_of_range
  31. {
  32. bad_adjustment(std::string const& msg = std::string()) :
  33. std::out_of_range(std::string("Adjustment out of range: " + msg)) {}
  34. };
  35. typedef boost::date_time::dst_adjustment_offsets<boost::posix_time::time_duration> dst_adjustment_offsets;
  36. //! A time zone class constructed from a POSIX time zone string
  37. /*! A POSIX time zone string takes the form of:<br>
  38. * "std offset dst [offset],start[/time],end[/time]" (w/no spaces)
  39. * 'std' specifies the abbrev of the time zone.<br>
  40. * 'offset' is the offset from UTC.<br>
  41. * 'dst' specifies the abbrev of the time zone during daylight savings time.<br>
  42. * The second offset is how many hours changed during DST. Default=1<br>
  43. * 'start' and'end' are the dates when DST goes into (and out of) effect.<br>
  44. * 'offset' takes the form of: [+|-]hh[:mm[:ss]] {h=0-23, m/s=0-59}<br>
  45. * 'time' and 'offset' take the same form. Time defaults=02:00:00<br>
  46. * 'start' and 'end' can be one of three forms:<br>
  47. * Mm.w.d {month=1-12, week=1-5 (5 is always last), day=0-6}<br>
  48. * Jn {n=1-365 Feb29 is never counted}<br>
  49. * n {n=0-365 Feb29 is counted in leap years}<br>
  50. * Example "PST-5PDT01:00:00,M4.1.0/02:00:00,M10.1.0/02:00:00"
  51. * <br>
  52. * Exceptions will be thrown under these conditions:<br>
  53. * An invalid date spec (see date class)<br>
  54. * A boost::local_time::bad_offset exception will be thrown for:<br>
  55. * A DST start or end offset that is negative or more than 24 hours<br>
  56. * A UTC zone that is greater than +12 or less than -12 hours<br>
  57. * A boost::local_time::bad_adjustment exception will be thrown for:<br>
  58. * A DST adjustment that is 24 hours or more (positive or negative)<br>
  59. */
  60. template<class CharT>
  61. class posix_time_zone_base : public date_time::time_zone_base<posix_time::ptime,CharT> {
  62. public:
  63. typedef boost::posix_time::time_duration time_duration_type;
  64. typedef date_time::time_zone_names_base<CharT> time_zone_names;
  65. typedef date_time::time_zone_base<posix_time::ptime,CharT> base_type;
  66. typedef typename base_type::string_type string_type;
  67. typedef CharT char_type;
  68. typedef typename base_type::stringstream_type stringstream_type;
  69. typedef boost::char_separator<char_type, std::char_traits<char_type> > char_separator_type;
  70. typedef boost::tokenizer<char_separator_type,
  71. typename string_type::const_iterator,
  72. string_type> tokenizer_type;
  73. typedef typename boost::tokenizer<char_separator_type,
  74. typename string_type::const_iterator,
  75. string_type>::iterator tokenizer_iterator_type;
  76. //! Construct from a POSIX time zone string
  77. posix_time_zone_base(const string_type& s) :
  78. //zone_names_("std_name","std_abbrev","no-dst","no-dst"),
  79. zone_names_(),
  80. has_dst_(false),
  81. base_utc_offset_(posix_time::hours(0)),
  82. dst_offsets_(posix_time::hours(0),posix_time::hours(0),posix_time::hours(0)),
  83. dst_calc_rules_()
  84. {
  85. #ifdef __HP_aCC
  86. // Work around bug in aC++ compiler: see QXCR1000880488 in the
  87. // HP bug tracking system
  88. const char_type sep_chars[2] = {',',0};
  89. #else
  90. const char_type sep_chars[2] = {','};
  91. #endif
  92. char_separator_type sep(sep_chars);
  93. tokenizer_type tokens(s, sep);
  94. tokenizer_iterator_type it = tokens.begin();
  95. calc_zone(*it++);
  96. if(has_dst_){
  97. string_type tmp_str = *it++;
  98. calc_rules(tmp_str, *it);
  99. }
  100. }
  101. virtual ~posix_time_zone_base() {};
  102. //!String for the zone when not in daylight savings (eg: EST)
  103. virtual string_type std_zone_abbrev()const
  104. {
  105. return zone_names_.std_zone_abbrev();
  106. }
  107. //!String for the timezone when in daylight savings (eg: EDT)
  108. /*! For those time zones that have no DST, an empty string is used */
  109. virtual string_type dst_zone_abbrev() const
  110. {
  111. return zone_names_.dst_zone_abbrev();
  112. }
  113. //!String for the zone when not in daylight savings (eg: Eastern Standard Time)
  114. /*! The full STD name is not extracted from the posix time zone string.
  115. * Therefore, the STD abbreviation is used in it's place */
  116. virtual string_type std_zone_name()const
  117. {
  118. return zone_names_.std_zone_name();
  119. }
  120. //!String for the timezone when in daylight savings (eg: Eastern Daylight Time)
  121. /*! The full DST name is not extracted from the posix time zone string.
  122. * Therefore, the STD abbreviation is used in it's place. For time zones
  123. * that have no DST, an empty string is used */
  124. virtual string_type dst_zone_name()const
  125. {
  126. return zone_names_.dst_zone_name();
  127. }
  128. //! True if zone uses daylight savings adjustments otherwise false
  129. virtual bool has_dst()const
  130. {
  131. return has_dst_;
  132. }
  133. //! Local time that DST starts -- NADT if has_dst is false
  134. virtual posix_time::ptime dst_local_start_time(gregorian::greg_year y)const
  135. {
  136. gregorian::date d(gregorian::not_a_date_time);
  137. if(has_dst_)
  138. {
  139. d = dst_calc_rules_->start_day(y);
  140. }
  141. return posix_time::ptime(d, dst_offsets_.dst_start_offset_);
  142. }
  143. //! Local time that DST ends -- NADT if has_dst is false
  144. virtual posix_time::ptime dst_local_end_time(gregorian::greg_year y)const
  145. {
  146. gregorian::date d(gregorian::not_a_date_time);
  147. if(has_dst_)
  148. {
  149. d = dst_calc_rules_->end_day(y);
  150. }
  151. return posix_time::ptime(d, dst_offsets_.dst_end_offset_);
  152. }
  153. //! Base offset from UTC for zone (eg: -07:30:00)
  154. virtual time_duration_type base_utc_offset()const
  155. {
  156. return base_utc_offset_;
  157. }
  158. //! Adjustment forward or back made while DST is in effect
  159. virtual time_duration_type dst_offset()const
  160. {
  161. return dst_offsets_.dst_adjust_;
  162. }
  163. //! Returns a POSIX time_zone string for this object
  164. virtual string_type to_posix_string() const
  165. {
  166. // std offset dst [offset],start[/time],end[/time] - w/o spaces
  167. stringstream_type ss;
  168. ss.fill('0');
  169. boost::shared_ptr<dst_calc_rule> no_rules;
  170. // std
  171. ss << std_zone_abbrev();
  172. // offset
  173. if(base_utc_offset().is_negative()) {
  174. // inverting the sign guarantees we get two digits
  175. ss << '-' << std::setw(2) << base_utc_offset().invert_sign().hours();
  176. }
  177. else {
  178. ss << '+' << std::setw(2) << base_utc_offset().hours();
  179. }
  180. if(base_utc_offset().minutes() != 0 || base_utc_offset().seconds() != 0) {
  181. ss << ':' << std::setw(2) << base_utc_offset().minutes();
  182. if(base_utc_offset().seconds() != 0) {
  183. ss << ':' << std::setw(2) << base_utc_offset().seconds();
  184. }
  185. }
  186. if(dst_calc_rules_ != no_rules) {
  187. // dst
  188. ss << dst_zone_abbrev();
  189. // dst offset
  190. if(dst_offset().is_negative()) {
  191. // inverting the sign guarantees we get two digits
  192. ss << '-' << std::setw(2) << dst_offset().invert_sign().hours();
  193. }
  194. else {
  195. ss << '+' << std::setw(2) << dst_offset().hours();
  196. }
  197. if(dst_offset().minutes() != 0 || dst_offset().seconds() != 0) {
  198. ss << ':' << std::setw(2) << dst_offset().minutes();
  199. if(dst_offset().seconds() != 0) {
  200. ss << ':' << std::setw(2) << dst_offset().seconds();
  201. }
  202. }
  203. // start/time
  204. ss << ',' << date_time::convert_string_type<char, char_type>(dst_calc_rules_->start_rule_as_string()) << '/'
  205. << std::setw(2) << dst_offsets_.dst_start_offset_.hours() << ':'
  206. << std::setw(2) << dst_offsets_.dst_start_offset_.minutes();
  207. if(dst_offsets_.dst_start_offset_.seconds() != 0) {
  208. ss << ':' << std::setw(2) << dst_offsets_.dst_start_offset_.seconds();
  209. }
  210. // end/time
  211. ss << ',' << date_time::convert_string_type<char, char_type>(dst_calc_rules_->end_rule_as_string()) << '/'
  212. << std::setw(2) << dst_offsets_.dst_end_offset_.hours() << ':'
  213. << std::setw(2) << dst_offsets_.dst_end_offset_.minutes();
  214. if(dst_offsets_.dst_end_offset_.seconds() != 0) {
  215. ss << ':' << std::setw(2) << dst_offsets_.dst_end_offset_.seconds();
  216. }
  217. }
  218. return ss.str();
  219. }
  220. private:
  221. time_zone_names zone_names_;
  222. bool has_dst_;
  223. time_duration_type base_utc_offset_;
  224. dst_adjustment_offsets dst_offsets_;
  225. boost::shared_ptr<dst_calc_rule> dst_calc_rules_;
  226. /*! Extract time zone abbreviations for STD & DST as well
  227. * as the offsets for the time shift that occurs and how
  228. * much of a shift. At this time full time zone names are
  229. * NOT extracted so the abbreviations are used in their place */
  230. void calc_zone(const string_type& obj){
  231. const char_type empty_string[2] = {'\0'};
  232. stringstream_type ss(empty_string);
  233. typename string_type::const_pointer sit = obj.c_str(), obj_end = sit + obj.size();
  234. string_type l_std_zone_abbrev, l_dst_zone_abbrev;
  235. // get 'std' name/abbrev
  236. while(std::isalpha(*sit)){
  237. ss << *sit++;
  238. }
  239. l_std_zone_abbrev = ss.str();
  240. ss.str(empty_string);
  241. // get UTC offset
  242. if(sit != obj_end){
  243. // get duration
  244. while(sit != obj_end && !std::isalpha(*sit)){
  245. ss << *sit++;
  246. }
  247. base_utc_offset_ = date_time::str_from_delimited_time_duration<time_duration_type,char_type>(ss.str());
  248. ss.str(empty_string);
  249. // base offset must be within range of -12 hours to +12 hours
  250. if(base_utc_offset_ < time_duration_type(-12,0,0) ||
  251. base_utc_offset_ > time_duration_type(12,0,0))
  252. {
  253. boost::throw_exception(bad_offset(posix_time::to_simple_string(base_utc_offset_)));
  254. }
  255. }
  256. // get DST data if given
  257. if(sit != obj_end){
  258. has_dst_ = true;
  259. // get 'dst' name/abbrev
  260. while(sit != obj_end && std::isalpha(*sit)){
  261. ss << *sit++;
  262. }
  263. l_dst_zone_abbrev = ss.str();
  264. ss.str(empty_string);
  265. // get DST offset if given
  266. if(sit != obj_end){
  267. // get duration
  268. while(sit != obj_end && !std::isalpha(*sit)){
  269. ss << *sit++;
  270. }
  271. dst_offsets_.dst_adjust_ = date_time::str_from_delimited_time_duration<time_duration_type,char_type>(ss.str());
  272. ss.str(empty_string);
  273. }
  274. else{ // default DST offset
  275. dst_offsets_.dst_adjust_ = posix_time::hours(1);
  276. }
  277. // adjustment must be within +|- 1 day
  278. if(dst_offsets_.dst_adjust_ <= time_duration_type(-24,0,0) ||
  279. dst_offsets_.dst_adjust_ >= time_duration_type(24,0,0))
  280. {
  281. boost::throw_exception(bad_adjustment(posix_time::to_simple_string(dst_offsets_.dst_adjust_)));
  282. }
  283. }
  284. // full names not extracted so abbrevs used in their place
  285. zone_names_ = time_zone_names(l_std_zone_abbrev, l_std_zone_abbrev, l_dst_zone_abbrev, l_dst_zone_abbrev);
  286. }
  287. void calc_rules(const string_type& start, const string_type& end){
  288. #ifdef __HP_aCC
  289. // Work around bug in aC++ compiler: see QXCR1000880488 in the
  290. // HP bug tracking system
  291. const char_type sep_chars[2] = {'/',0};
  292. #else
  293. const char_type sep_chars[2] = {'/'};
  294. #endif
  295. char_separator_type sep(sep_chars);
  296. tokenizer_type st_tok(start, sep);
  297. tokenizer_type et_tok(end, sep);
  298. tokenizer_iterator_type sit = st_tok.begin();
  299. tokenizer_iterator_type eit = et_tok.begin();
  300. // generate date spec
  301. char_type x = string_type(*sit).at(0);
  302. if(x == 'M'){
  303. M_func(*sit, *eit);
  304. }
  305. else if(x == 'J'){
  306. julian_no_leap(*sit, *eit);
  307. }
  308. else{
  309. julian_day(*sit, *eit);
  310. }
  311. ++sit;
  312. ++eit;
  313. // generate durations
  314. // starting offset
  315. if(sit != st_tok.end()){
  316. dst_offsets_.dst_start_offset_ = date_time::str_from_delimited_time_duration<time_duration_type,char_type>(*sit);
  317. }
  318. else{
  319. // default
  320. dst_offsets_.dst_start_offset_ = posix_time::hours(2);
  321. }
  322. // start/end offsets must fall on given date
  323. if(dst_offsets_.dst_start_offset_ < time_duration_type(0,0,0) ||
  324. dst_offsets_.dst_start_offset_ >= time_duration_type(24,0,0))
  325. {
  326. boost::throw_exception(bad_offset(posix_time::to_simple_string(dst_offsets_.dst_start_offset_)));
  327. }
  328. // ending offset
  329. if(eit != et_tok.end()){
  330. dst_offsets_.dst_end_offset_ = date_time::str_from_delimited_time_duration<time_duration_type,char_type>(*eit);
  331. }
  332. else{
  333. // default
  334. dst_offsets_.dst_end_offset_ = posix_time::hours(2);
  335. }
  336. // start/end offsets must fall on given date
  337. if(dst_offsets_.dst_end_offset_ < time_duration_type(0,0,0) ||
  338. dst_offsets_.dst_end_offset_ >= time_duration_type(24,0,0))
  339. {
  340. boost::throw_exception(bad_offset(posix_time::to_simple_string(dst_offsets_.dst_end_offset_)));
  341. }
  342. }
  343. /* Parses out a start/end date spec from a posix time zone string.
  344. * Date specs come in three possible formats, this function handles
  345. * the 'M' spec. Ex "M2.2.4" => 2nd month, 2nd week, 4th day .
  346. */
  347. void M_func(const string_type& s, const string_type& e){
  348. typedef gregorian::nth_kday_of_month nkday;
  349. unsigned short sm=0,sw=0,sd=0,em=0,ew=0,ed=0; // start/end month,week,day
  350. #ifdef __HP_aCC
  351. // Work around bug in aC++ compiler: see QXCR1000880488 in the
  352. // HP bug tracking system
  353. const char_type sep_chars[3] = {'M','.',0};
  354. #else
  355. const char_type sep_chars[3] = {'M','.'};
  356. #endif
  357. char_separator_type sep(sep_chars);
  358. tokenizer_type stok(s, sep), etok(e, sep);
  359. tokenizer_iterator_type it = stok.begin();
  360. sm = lexical_cast<unsigned short>(*it++);
  361. sw = lexical_cast<unsigned short>(*it++);
  362. sd = lexical_cast<unsigned short>(*it);
  363. it = etok.begin();
  364. em = lexical_cast<unsigned short>(*it++);
  365. ew = lexical_cast<unsigned short>(*it++);
  366. ed = lexical_cast<unsigned short>(*it);
  367. dst_calc_rules_ = shared_ptr<dst_calc_rule>(
  368. new nth_kday_dst_rule(
  369. nth_last_dst_rule::start_rule(
  370. static_cast<nkday::week_num>(sw),sd,sm),
  371. nth_last_dst_rule::start_rule(
  372. static_cast<nkday::week_num>(ew),ed,em)
  373. )
  374. );
  375. }
  376. //! Julian day. Feb29 is never counted, even in leap years
  377. // expects range of 1-365
  378. void julian_no_leap(const string_type& s, const string_type& e){
  379. typedef gregorian::gregorian_calendar calendar;
  380. const unsigned short year = 2001; // Non-leap year
  381. unsigned short sm=1;
  382. int sd=0;
  383. sd = lexical_cast<int>(s.substr(1)); // skip 'J'
  384. while(sd >= calendar::end_of_month_day(year,sm)){
  385. sd -= calendar::end_of_month_day(year,sm++);
  386. }
  387. unsigned short em=1;
  388. int ed=0;
  389. ed = lexical_cast<int>(e.substr(1)); // skip 'J'
  390. while(ed > calendar::end_of_month_day(year,em)){
  391. ed -= calendar::end_of_month_day(year,em++);
  392. }
  393. dst_calc_rules_ = shared_ptr<dst_calc_rule>(
  394. new partial_date_dst_rule(
  395. partial_date_dst_rule::start_rule(
  396. sd, static_cast<date_time::months_of_year>(sm)),
  397. partial_date_dst_rule::end_rule(
  398. ed, static_cast<date_time::months_of_year>(em))
  399. )
  400. );
  401. }
  402. //! Julian day. Feb29 is always counted, but exception thrown in non-leap years
  403. // expects range of 0-365
  404. void julian_day(const string_type& s, const string_type& e){
  405. int sd=0, ed=0;
  406. sd = lexical_cast<int>(s);
  407. ed = lexical_cast<int>(e);
  408. dst_calc_rules_ = shared_ptr<dst_calc_rule>(
  409. new partial_date_dst_rule(
  410. partial_date_dst_rule::start_rule(++sd),// args are 0-365
  411. partial_date_dst_rule::end_rule(++ed) // pd expects 1-366
  412. )
  413. );
  414. }
  415. //! helper function used when throwing exceptions
  416. static std::string td_as_string(const time_duration_type& td)
  417. {
  418. std::string s;
  419. #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
  420. s = posix_time::to_simple_string(td);
  421. #else
  422. std::stringstream ss;
  423. ss << td;
  424. s = ss.str();
  425. #endif
  426. return s;
  427. }
  428. };
  429. typedef posix_time_zone_base<char> posix_time_zone;
  430. } } // namespace boost::local_time
  431. #endif // _DATE_TIME_POSIX_TIME_ZONE__