DateTime.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * @file artis/utils/DateTime.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2017 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef ARTIS_UTILS_DATE_TIME_HPP
  22. #define ARTIS_UTILS_DATE_TIME_HPP
  23. #include <ostream>
  24. #include <string>
  25. #include <boost/algorithm/string.hpp>
  26. #include <boost/date_time.hpp>
  27. #include <boost/date_time/posix_time/posix_time.hpp>
  28. #include <boost/lexical_cast.hpp>
  29. #include <boost/numeric/conversion/cast.hpp>
  30. #include <boost/version.hpp>
  31. #include <artis/utils/Exception.hpp>
  32. namespace artis { namespace utils {
  33. enum DateTimeUnitOptions
  34. {
  35. DATE_TIME_UNIT_NONE,
  36. DATE_TIME_UNIT_DAY,
  37. DATE_TIME_UNIT_WEEK,
  38. DATE_TIME_UNIT_MONTH,
  39. DATE_TIME_UNIT_YEAR
  40. };
  41. class DateTime
  42. {
  43. public:
  44. /**
  45. * @brief Write the current date and time conform to RFC 822.
  46. * @code
  47. * std::cout << "currentDate(): `"
  48. * << vle::utils::DateTime::currentDate()
  49. * << "'\n";
  50. *
  51. * // Display: currentDate(): `2011-Jun-09 12:13:21'
  52. * @endcode
  53. * @return string representation of date.
  54. */
  55. static std::string currentDate();
  56. /* * * * */
  57. /**
  58. * @brief Get the year in the simulation time.
  59. * @code
  60. * vle::utils::DateTime::year(2451545) == 2000u;
  61. * @endcode
  62. * @param time The simulation time.
  63. * @return An unsigned int.
  64. */
  65. static unsigned int year(double time);
  66. /**
  67. * @brief Get the month in the simulation time.
  68. * @code
  69. * vle::utils::DateTime::month(2451545) == 1u;
  70. * @endcode
  71. * @param time The simulation time.
  72. * @return An unsigned int.
  73. */
  74. static unsigned int month(double time);
  75. /**
  76. * @brief Get the day of the month in the simulation time.
  77. * @code
  78. * vle::utils::DateTime::dayOfMonth((2451545)) == 1u;
  79. * @endcode
  80. * @param time The simulation time.
  81. * @return An unsigned int.
  82. */
  83. static unsigned int dayOfMonth(double time);
  84. /**
  85. * @brief Get the day in the week of the simulation time.
  86. * @code
  87. * vle::utils::DateTime::dayOfWeek((2451545)) == 6u;
  88. * @endcode
  89. * @param time The simulation time.
  90. * @return An unsigned int.
  91. */
  92. static unsigned int dayOfWeek(double time);
  93. /**
  94. * @brief Get the day in the year of the simulation time.
  95. * @code
  96. * vle::utils::DateTime::dayOfYear((2451545)) == 1u;
  97. * @endcode
  98. * @param time The simulation time.
  99. * @return An unsigned int.
  100. */
  101. static unsigned int dayOfYear(double time);
  102. /**
  103. * @brief Get the week in the year of the simulation time.
  104. * @code
  105. * vle::utils::DateTime::dayOfYear((2451545)) == 1u;
  106. * @endcode
  107. * @param time The simulation time.
  108. * @return An unsigned int.
  109. */
  110. static unsigned int weekOfYear(double time);
  111. /**
  112. * @brief Check if the simulation time is a leap year.
  113. * @param time The simulation time.
  114. * @return true if time is a leap year, false otherwise.
  115. */
  116. static bool isLeapYear(double time);
  117. /**
  118. * @brief Get the number of day in the year for the simulaton time.
  119. * @code
  120. * vle::utils::Datime::aYear(2451545) == 366;
  121. * @endcode
  122. * @param time The simulation time.
  123. * @return number of day.
  124. */
  125. static double aYear(double time);
  126. /**
  127. * @brief Get the number of day in the month for the simulation time.
  128. * @code
  129. * vle::utils::Datime::aMonth(2451545) == 31;
  130. * vle::utils::Datime::aMonth(2451576) == 29;
  131. * @endcode
  132. * @param time The simulation time.
  133. * @return number of day.
  134. */
  135. static double aMonth(double time);
  136. /**
  137. * @brief Get the number of day in a week.
  138. * @return Return 7.
  139. */
  140. static inline double aWeek() { return 7; }
  141. /**
  142. * @brief Get the number of day in a day.
  143. * @return Return 1.
  144. */
  145. static inline double aDay() { return 1; }
  146. /**
  147. * @brief Get number of days in n-years from the simulation time.
  148. * @code
  149. * vle::utils::DateTime::years((2451545), 1), 366);
  150. * vle::utils::DateTime::years((2451545), 2), 731);
  151. * @endcode
  152. * @param time The simulation time.
  153. * @param n The number of years.
  154. * @return The number of days in n-years.
  155. */
  156. static double years(double time, unsigned int n);
  157. /**
  158. * @brief Get number of days in n-months from the simulation time.
  159. * @code
  160. * vle::utils::DateTime::months((2451545), 2) = 60;
  161. * @endcode
  162. * @param time The simulation time.
  163. * @param n The number of weeks.
  164. * @return The number of days in n-months.
  165. */
  166. static double months(double time, unsigned int n);
  167. /**
  168. * @brief Get number of days in n-weeks.
  169. * @param n Number of weeks.
  170. * @return n * 7.
  171. */
  172. static inline double weeks(unsigned int n) { return (int)(7 * n); }
  173. /**
  174. * @brief Get number of days in n-days.
  175. * @param n Number of days.
  176. * @return n.
  177. */
  178. static inline double days(unsigned int n) { return (int)n; }
  179. /**
  180. * @brief Convert std::string unit ("day", "week", "month", "year") into
  181. * the DateTime::Unit type.
  182. * @param unit The std::string unit to convert.
  183. * @return The convertion of Day if error.
  184. */
  185. static DateTimeUnitOptions convertUnit(const std::string& unit);
  186. /**
  187. * @brief A easy function to call days(), weeks(), months() or years()
  188. * using a DateTime::Unit type.
  189. * @param time The simulation date (useless for Day, Week).
  190. * @param duration The number of DateTime::Unit.
  191. * @param unit The unit.
  192. * @return A number of day.
  193. */
  194. static double duration(double time,
  195. double duration,
  196. DateTimeUnitOptions unit);
  197. /* * * * */
  198. /**
  199. * @brief Convert an julian day number into a string.
  200. * @code
  201. * vle::utils::DateTime::toJulianDayNumber(2452192) = "2001-10-9";
  202. * @endcode
  203. * @param date The date to convert.
  204. * @return A string representation of the julian day.
  205. */
  206. static std::string toJulianDayNumber(unsigned long date);
  207. /**
  208. * @brief Convert a string into a julian day number;
  209. * @code
  210. * vle::utils::DateTime::toJulianDayNumber("2001-10-9") = 2452192;
  211. * @endcode
  212. * @param date The date to convert.
  213. * @return A julian day number.
  214. */
  215. static long toJulianDayNumber(const std::string& date);
  216. /**
  217. * @brief Convert a julian date into a string.
  218. * @code
  219. * vle::utils::DateTime::toJulianDay(2454115.05486)) = "2001-10-9 hh:mm:ss";
  220. * @endcode
  221. * @param date The date to convert.
  222. * @return A string representation of the julian day.
  223. */
  224. static std::string toJulianDay(double date);
  225. /**
  226. * @brief Convert a string into a julian day.
  227. * @code
  228. * vle::utils::DateTime::toJulianDay("2001-10-9 hh:mm:ss") = 2454115.05486;
  229. * @endcode
  230. * @param date The date to convert.
  231. * @return A string representation of the julian day.
  232. */
  233. static double toJulianDay(const std::string& date);
  234. /* * * * */
  235. /**
  236. * @brief Check if the date is a valid year in gregorian calendard.
  237. *
  238. * @param date The date to check.
  239. *
  240. * @return True if date is a valid year, false otherwise.
  241. */
  242. static bool isValidYear(double date);
  243. /**
  244. * @brief Explode the specified date attribute to year, month, day in the
  245. * month, hours, minutes and seconds.
  246. *
  247. * @param date The date to convert.
  248. * @param year Output parameter to represent year.
  249. * @param month Output parameter to represent month.
  250. * @param day Output parameter to represent day in a month (1..31).
  251. * @param hours Output parameter to represent hours in date.
  252. * @param minutes Output parameter to represent minutes in date.
  253. * @param seconds Output parameter to represent seconds in date.
  254. *
  255. * @throw utils::ArgError error to convert the date.
  256. *
  257. * @return The remainder of the conversion.
  258. */
  259. static double toTime(double date,
  260. long& year,
  261. long& month,
  262. long& day,
  263. long& hours,
  264. long& minutes,
  265. long& seconds);
  266. /**
  267. * @brief Explode current date to year, month, day in the
  268. * month, hours, minutes and seconds.
  269. *
  270. * @param year Output parameter to represent year.
  271. * @param month Output parameter to represent month.
  272. * @param day Output parameter to represent day in a month (1..31).
  273. *
  274. */
  275. static void currentDate(long& year,
  276. long& month,
  277. long& day);
  278. static void format_date(const std::string& str, std::string& date)
  279. {
  280. std::vector < std::string > list;
  281. boost::split(list, str, boost::is_any_of("-/"));
  282. date = (boost::format("%1%/%2%/%3%") % list[2] % list[1] %
  283. list[0]).str();
  284. }
  285. };
  286. } }
  287. #endif