date.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include "date.hpp"
  2. #include "calendar.hpp"
  3. #include "calendar_type.hpp"
  4. #include "calendar_util.hpp"
  5. #include <boost/date_time/gregorian/gregorian.hpp>
  6. #include <boost/date_time/posix_time/posix_time.hpp>
  7. using namespace boost::posix_time;
  8. using namespace boost::gregorian;
  9. namespace xios
  10. {
  11. /// ////////////////////// Définitions ////////////////////// ///
  12. CDate::CDate(void)
  13. : relCalendar(NULL)
  14. , year(0), month(1), day(1)
  15. , hour(0), minute(0), second(0)
  16. {}
  17. CDate::CDate(const CCalendar& calendar)
  18. : relCalendar(&calendar)
  19. , year(0), month(1), day(1)
  20. , hour(0), minute(0), second(0)
  21. {}
  22. CDate::CDate(const CCalendar& calendar,
  23. int yr, int mth, int d,
  24. int hr, int min, int sec)
  25. : relCalendar(&calendar)
  26. , year(yr), month(mth), day(d)
  27. , hour(hr), minute(min), second(sec)
  28. {
  29. if (!this->checkDate())
  30. {
  31. DEBUG(<< "La date initialisée a été modifiée "
  32. << "car elle était incorrecte par rapport au calendrier souhaité.");
  33. }
  34. }
  35. CDate::CDate(const CDate& date)
  36. : relCalendar(date.relCalendar)
  37. , year(date.year), month(date.month), day(date.day)
  38. , hour(date.hour), minute(date.minute), second(date.second)
  39. {
  40. // Delay the verification until we get a calendar we can compare the date to
  41. if (relCalendar && !checkDate())
  42. {
  43. DEBUG(<< "La date initialisée a été modifiée "
  44. << "car elle était incorrecte par rapport au calendrier souhaité.");
  45. }
  46. }
  47. CDate::~CDate(void)
  48. { /* Ne rien faire de plus */ }
  49. ///---------------------------------------------------------------
  50. CDate& CDate::operator=(const CDate& date)
  51. {
  52. relCalendar = date.relCalendar;
  53. year = date.year; month = date.month; day = date.day;
  54. hour = date.hour; minute = date.minute; second = date.second;
  55. return (*this);
  56. }
  57. bool CDate::operator==(const CDate& date)
  58. {
  59. return (&(*relCalendar) == &(*date.relCalendar) &&
  60. year == date.year && month == date.month && day == date.day &&
  61. hour == date.hour && minute == date.minute && second == date.second);
  62. }
  63. StdOStream& operator<<(StdOStream& out, const CDate& date)
  64. {
  65. std::streamsize s;
  66. char c;
  67. int width=4;
  68. double maxSize=10000;
  69. while (date.year>=maxSize)
  70. {
  71. maxSize*=10;
  72. width++;
  73. }
  74. s = out.width(width); c = out.fill('0'); out << date.year << '-';
  75. s = out.width(2); c = out.fill('0'); out << date.month << '-';
  76. s = out.width(2); c = out.fill('0'); out << date.day << ' ';
  77. s = out.width(2); c = out.fill('0'); out << date.hour << ':';
  78. s = out.width(2); c = out.fill('0'); out << date.minute << ':';
  79. s = out.width(2); c = out.fill('0'); out << date.second;
  80. return out;
  81. }
  82. StdIStream& operator>>(StdIStream& in, CDate& date)
  83. {
  84. if (date.relCalendar)
  85. date.relCalendar->parseDate(in, date);
  86. else
  87. CCalendar::parseDateDefault(in, date);
  88. return in;
  89. }
  90. CDate::operator Time(void) const // Non vérifiée, pas optimisée ...
  91. {
  92. // This will check that a calendar was correctly associated to the date
  93. const CCalendar& c = getRelCalendar();
  94. // Todo : Tester si la date courante est supérieure à la date initiale.
  95. Time t = getSecondOfYear() - c.getTimeOrigin().getSecondOfYear();
  96. if (c.hasLeapYear())
  97. {
  98. for (CDate d(c.getTimeOrigin()); d.getYear() < getYear(); d.setYear(d.getYear() + 1))
  99. t += c.getYearTotalLength(d);
  100. }
  101. else
  102. t += Time(getYear() - c.getTimeOrigin().getYear()) * c.getYearTotalLength(*this);
  103. return t;
  104. }
  105. //----------------------------------------------------------------
  106. bool CDate::checkDate(void)
  107. {
  108. // This will also check that a calendar was correctly associated to the date
  109. return getRelCalendar().checkDate(*this);
  110. }
  111. //----------------------------------------------------------------
  112. int CDate::getYear (void) const { return (this->year ); }
  113. int CDate::getMonth (void) const { return (this->month ); }
  114. int CDate::getDay (void) const { return (this->day ); }
  115. int CDate::getHour (void) const { return (this->hour ); }
  116. int CDate::getMinute(void) const { return (this->minute); }
  117. int CDate::getSecond(void) const { return (this->second); }
  118. //----------------------------------------------------------------
  119. const CCalendar& CDate::getRelCalendar(void) const
  120. {
  121. if (!this->relCalendar)
  122. ERROR("const CCalendar& CDate::getRelCalendar(void) const",
  123. "Invalid state: The date is not associated with any calendar.");
  124. return (*this->relCalendar);
  125. }
  126. bool CDate::hasRelCalendar(void) const
  127. { return (this->relCalendar != NULL); }
  128. //----------------------------------------------------------------
  129. /*!
  130. Get the number of seconds since the beginning of the year.
  131. \return the number of seconds since the beginning of the year.
  132. */
  133. int CDate::getSecondOfYear() const
  134. {
  135. CDate yearStart(*this);
  136. const CCalendar& c = getRelCalendar();
  137. int nbDay = 0;
  138. for (yearStart.setMonth(1); yearStart.getMonth() < getMonth(); yearStart.setMonth(yearStart.getMonth() + 1))
  139. nbDay += c.getMonthLength(yearStart);
  140. // We need to use getDayLengthInSeconds instead of getDayLength since we might
  141. // have a non-integral number of hours per day for user defined calendars
  142. return ((nbDay + getDay() - 1) * c.getDayLengthInSeconds()
  143. + (getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond());
  144. }
  145. /*!
  146. Get the number of days (expressed as a real number) since the beginning of the year.
  147. \return the number of days (expressed as a real number) since the beginning of the year.
  148. */
  149. double CDate::getDayOfYear() const
  150. {
  151. return double(getSecondOfYear()) / getRelCalendar().getDayLengthInSeconds();
  152. }
  153. /*!
  154. Get the fraction of the current year as a real number between 0 and 1.
  155. \return the fraction of the current year.
  156. */
  157. double CDate::getFractionOfYear() const
  158. {
  159. return double(getSecondOfYear()) / getRelCalendar().getYearTotalLength(*this);
  160. }
  161. /*!
  162. Get the number of seconds since the beginning of the day.
  163. \return the number of seconds since the beginning of the day.
  164. */
  165. int CDate::getSecondOfDay() const
  166. {
  167. const CCalendar& c = getRelCalendar();
  168. return ((getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond());
  169. }
  170. /*!
  171. Get the fraction of the current day as a real number between 0 and 1.
  172. \return the fraction of the current day.
  173. */
  174. double CDate::getFractionOfDay() const
  175. {
  176. return double(getSecondOfDay()) / getRelCalendar().getDayLengthInSeconds();
  177. }
  178. //----------------------------------------------------------------
  179. void CDate::setYear (int newyear) { this->year = newyear; }
  180. void CDate::setMonth (int newmonth) { this->month = newmonth; }
  181. void CDate::setDay (int newday) { this->day = newday; }
  182. void CDate::setHour (int newhour) { this->hour = newhour; }
  183. void CDate::setMinute(int newminute) { this->minute = newminute; }
  184. void CDate::setSecond(int newsecond) { this->second = newsecond; }
  185. void CDate::setDate(int yr, int mth, int d, int hr, int min, int sec)
  186. {
  187. this->year = yr;
  188. this->month = mth;
  189. this->day = d;
  190. this->hour = hr;
  191. this->minute = min;
  192. this->second = sec;
  193. }
  194. //----------------------------------------------------------------
  195. void CDate::addMonth(int value)
  196. {// Value doit être égale à 1 ou -1.
  197. const CCalendar& c = getRelCalendar();
  198. int nbMonthsPerYear = c.getYearLength();
  199. this->month += value;
  200. if (this->month == nbMonthsPerYear + 1) { year++; this->month = 1; }
  201. if (this->month == 0) { year--; this->month = nbMonthsPerYear; }
  202. }
  203. //----------------------------------------------------------------
  204. bool CDate::setRelCalendar(const CCalendar& relCalendar)
  205. {
  206. this->relCalendar = &relCalendar;
  207. return this->checkDate();
  208. }
  209. //----------------------------------------------------------------
  210. CDate CDate::FromString(const StdString& str, const CCalendar& calendar)
  211. {
  212. CDate dt(calendar);
  213. StdIStringStream iss(str);
  214. iss >> dt;
  215. return dt;
  216. }
  217. //----------------------------------------------------------------
  218. StdString CDate::getStryyyymmdd(void) const
  219. {
  220. std::streamsize s;
  221. char c;
  222. ostringstream oss;
  223. s = oss.width(4); c = oss.fill('0'); oss << year;
  224. s = oss.width(2); c = oss.fill('0'); oss << month;
  225. s = oss.width(2); c = oss.fill('0'); oss << day;
  226. return oss.str();
  227. }
  228. string CDate::getStr(const string& str) const
  229. {
  230. ostringstream oss;
  231. int level;
  232. level=0;
  233. for(string::const_iterator it=str.begin();it!=str.end();++it)
  234. {
  235. if (level==0)
  236. {
  237. if (*it=='%') level++;
  238. else oss<<*it;
  239. }
  240. else if (level==1)
  241. {
  242. switch (*it)
  243. {
  244. case 'y' :
  245. oss.width(4); oss.fill('0'); oss << year;
  246. level=0;
  247. break;
  248. case 'm' : // month or minute
  249. level++;
  250. break;
  251. case 'd' :
  252. oss.width(2); oss.fill('0'); oss << day;
  253. level=0;
  254. break;
  255. case 'h' :
  256. oss.width(2); oss.fill('0'); oss << hour;
  257. level=0;
  258. break;
  259. case 's' :
  260. oss.width(2); oss.fill('0'); oss << second;
  261. level=0;
  262. break;
  263. case 'S' : // seconds since time origin
  264. oss.width(0); oss << Time(*this);
  265. level=0;
  266. break;
  267. case 'D' : // days since time origin
  268. oss.width(0); oss << Time(*this) / getRelCalendar().getDayLengthInSeconds();
  269. level=0;
  270. break;
  271. default :
  272. oss<<'%'<<*it;
  273. level=0;
  274. }
  275. }
  276. else if (level==2)
  277. {
  278. switch (*it)
  279. {
  280. case 'o' : // month
  281. oss.width(2); oss.fill('0'); oss << month;
  282. level=0;
  283. break;
  284. case 'i' : //minute
  285. oss.width(2); oss.fill('0'); oss << minute;
  286. level=0;
  287. break;
  288. default :
  289. oss<<"%m"<<*it;
  290. level=0;
  291. }
  292. }
  293. }
  294. return oss.str();
  295. }
  296. StdString CDate::toString(void) const
  297. {
  298. StdOStringStream oss;
  299. oss << *this;
  300. return oss.str();
  301. }
  302. ///---------------------------------------------------------------
  303. } // namespace xios