ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

292 lines
9.1 KiB

  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. #define BOOSTER_SOURCE
  9. #include <booster/locale/date_time_facet.h>
  10. #include <booster/locale/date_time.h>
  11. #include <booster/locale/formatting.h>
  12. #include "all_generator.h"
  13. #include <booster/thread.h>
  14. #include <unicode/calendar.h>
  15. #include <unicode/gregocal.h>
  16. #include <unicode/utypes.h>
  17. #include <booster/auto_ptr_inc.h>
  18. #include <math.h>
  19. #include "cdata.h"
  20. #include "uconv.h"
  21. #include "time_zone.h"
  22. #include <iostream>
  23. namespace booster {
  24. namespace locale {
  25. namespace impl_icu {
  26. static void check_and_throw_dt(UErrorCode &e)
  27. {
  28. if(U_FAILURE(e)) {
  29. date_time_error(u_errorName(e));
  30. }
  31. }
  32. using period::marks::period_mark;
  33. static UCalendarDateFields to_icu(period::marks::period_mark f)
  34. {
  35. using namespace period::marks;
  36. switch(f) {
  37. case era: return UCAL_ERA;
  38. case year: return UCAL_YEAR;
  39. case extended_year: return UCAL_EXTENDED_YEAR;
  40. case month: return UCAL_MONTH;
  41. case day: return UCAL_DATE;
  42. case day_of_year: return UCAL_DAY_OF_YEAR;
  43. case day_of_week: return UCAL_DAY_OF_WEEK;
  44. case day_of_week_in_month: return UCAL_DAY_OF_WEEK_IN_MONTH;
  45. case day_of_week_local: return UCAL_DOW_LOCAL;
  46. case hour: return UCAL_HOUR_OF_DAY;
  47. case hour_12: return UCAL_HOUR;
  48. case am_pm: return UCAL_AM_PM;
  49. case minute: return UCAL_MINUTE;
  50. case second: return UCAL_SECOND;
  51. case week_of_year: return UCAL_WEEK_OF_YEAR;
  52. case week_of_month: return UCAL_WEEK_OF_MONTH;
  53. default:
  54. throw booster::invalid_argument("Invalid date_time period type");
  55. }
  56. }
  57. class calendar_impl : public abstract_calendar {
  58. public:
  59. calendar_impl(cdata const &dat)
  60. {
  61. UErrorCode err=U_ZERO_ERROR;
  62. calendar_.reset(icu::Calendar::createInstance(dat.locale,err));
  63. check_and_throw_dt(err);
  64. #if U_ICU_VERSION_MAJOR_NUM*100 + U_ICU_VERSION_MINOR_NUM < 402
  65. // workaround old/invalid data, it should be 4 in general
  66. calendar_->setMinimalDaysInFirstWeek(4);
  67. #endif
  68. encoding_ = dat.encoding;
  69. }
  70. calendar_impl(calendar_impl const &other)
  71. {
  72. calendar_.reset(other.calendar_->clone());
  73. encoding_ = other.encoding_;
  74. }
  75. calendar_impl *clone() const
  76. {
  77. return new calendar_impl(*this);
  78. }
  79. void set_value(period::marks::period_mark p,int value)
  80. {
  81. calendar_->set(to_icu(p),int32_t(value));
  82. }
  83. int get_value(period::marks::period_mark p,value_type type) const
  84. {
  85. UErrorCode err=U_ZERO_ERROR;
  86. int v=0;
  87. if(p==period::marks::first_day_of_week) {
  88. guard l(lock_);
  89. v=calendar_->getFirstDayOfWeek(err);
  90. }
  91. else {
  92. UCalendarDateFields uper=to_icu(p);
  93. guard l(lock_);
  94. switch(type) {
  95. case absolute_minimum:
  96. v=calendar_->getMinimum(uper);
  97. break;
  98. case actual_minimum:
  99. v=calendar_->getActualMinimum(uper,err);
  100. break;
  101. case greatest_minimum:
  102. v=calendar_->getGreatestMinimum(uper);
  103. break;
  104. case current:
  105. v=calendar_->get(uper,err);
  106. break;
  107. case least_maximum:
  108. v=calendar_->getLeastMaximum(uper);
  109. break;
  110. case actual_maximum:
  111. v=calendar_->getActualMaximum(uper,err);
  112. break;
  113. case absolute_maximum:
  114. v=calendar_->getMaximum(uper);
  115. break;
  116. }
  117. }
  118. check_and_throw_dt(err);
  119. return v;
  120. }
  121. virtual void set_time(posix_time const &p)
  122. {
  123. double utime = p.seconds * 1000.0 + p.nanoseconds / 1000000.0;
  124. UErrorCode code=U_ZERO_ERROR;
  125. calendar_->setTime(utime,code);
  126. check_and_throw_dt(code);
  127. }
  128. virtual void normalize()
  129. {
  130. // Can't call complete() explicitly (protected)
  131. // calling get wich calls complete
  132. UErrorCode code=U_ZERO_ERROR;
  133. calendar_->get(UCAL_YEAR,code);
  134. check_and_throw_dt(code);
  135. }
  136. virtual posix_time get_time() const
  137. {
  138. UErrorCode code=U_ZERO_ERROR;
  139. double rtime = 0;
  140. {
  141. guard l(lock_);
  142. rtime = calendar_->getTime(code);
  143. }
  144. check_and_throw_dt(code);
  145. rtime/=1000.0;
  146. double secs = floor(rtime);
  147. posix_time res;
  148. res.seconds = static_cast<int64_t>(secs);
  149. res.nanoseconds = static_cast<uint32_t>((rtime - secs) / 1e9);
  150. if(res.nanoseconds > 999999999)
  151. res.nanoseconds = 999999999;
  152. return res;
  153. }
  154. virtual void set_option(calendar_option_type opt,int /*v*/)
  155. {
  156. switch(opt) {
  157. case is_gregorian:
  158. throw date_time_error("is_gregorian is not settable options for calendar");
  159. case is_dst:
  160. throw date_time_error("is_dst is not settable options for calendar");
  161. default:
  162. ;
  163. }
  164. }
  165. virtual int get_option(calendar_option_type opt) const
  166. {
  167. switch(opt) {
  168. case is_gregorian:
  169. return dynamic_cast<icu::GregorianCalendar const *>(calendar_.get())!=0;
  170. case is_dst:
  171. {
  172. guard l(lock_);
  173. UErrorCode err = U_ZERO_ERROR;
  174. bool res = ( calendar_->inDaylightTime(err) != 0 );
  175. check_and_throw_dt(err);
  176. return res;
  177. }
  178. default:
  179. return 0;
  180. }
  181. }
  182. virtual void adjust_value(period::marks::period_mark p,update_type u,int difference)
  183. {
  184. UErrorCode err=U_ZERO_ERROR;
  185. switch(u) {
  186. case move:
  187. calendar_->add(to_icu(p),difference,err);
  188. break;
  189. case roll:
  190. calendar_->roll(to_icu(p),difference,err);
  191. break;
  192. }
  193. check_and_throw_dt(err);
  194. }
  195. virtual int difference(abstract_calendar const *other_ptr,period::marks::period_mark p) const
  196. {
  197. UErrorCode err=U_ZERO_ERROR;
  198. double other_time = 0;
  199. //
  200. // fieldDifference has side effect of moving calendar (WTF?)
  201. // So we clone it for performing this operation
  202. //
  203. std::auto_ptr<icu::Calendar> self(calendar_->clone());
  204. calendar_impl const *other_cal=dynamic_cast<calendar_impl const *>(other_ptr);
  205. if(other_cal){
  206. guard l(other_cal->lock_);
  207. other_time = other_cal->calendar_->getTime(err);
  208. check_and_throw_dt(err);
  209. }
  210. else {
  211. posix_time p = other_ptr->get_time();
  212. other_time = p.seconds * 1000.0 + p.nanoseconds / 1000000.0;
  213. }
  214. int diff = self->fieldDifference(other_time,to_icu(p),err);
  215. check_and_throw_dt(err);
  216. return diff;
  217. }
  218. virtual void set_timezone(std::string const &tz)
  219. {
  220. calendar_->adoptTimeZone(get_time_zone(tz));
  221. }
  222. virtual std::string get_timezone() const
  223. {
  224. icu::UnicodeString tz;
  225. calendar_->getTimeZone().getID(tz);
  226. icu_std_converter<char> cvt(encoding_);
  227. return cvt.std(tz);
  228. }
  229. virtual bool same(abstract_calendar const *other) const
  230. {
  231. calendar_impl const *oc=dynamic_cast<calendar_impl const *>(other);
  232. if(!oc)
  233. return false;
  234. return calendar_->isEquivalentTo(*oc->calendar_)!=0;
  235. }
  236. private:
  237. typedef booster::unique_lock<booster::mutex> guard;
  238. mutable booster::mutex lock_;
  239. std::string encoding_;
  240. hold_ptr<icu::Calendar> calendar_;
  241. };
  242. class icu_calendar_facet : public calendar_facet {
  243. public:
  244. icu_calendar_facet(cdata const &d,size_t refs = 0) :
  245. calendar_facet(refs),
  246. data_(d)
  247. {
  248. }
  249. virtual abstract_calendar *create_calendar() const
  250. {
  251. return new calendar_impl(data_);
  252. }
  253. private:
  254. cdata data_;
  255. };
  256. std::locale create_calendar(std::locale const &in,cdata const &d)
  257. {
  258. return std::locale(in,new icu_calendar_facet(d));
  259. }
  260. } // impl_icu
  261. } // locale
  262. } // boost
  263. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4