DateTime.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * @file artis/utils/DateTime.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2019 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 {
  33. namespace utils {
  34. enum DateTimeUnitOptions {
  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. enum DateFormat {
  42. DATE_FORMAT_EXTENDED, //"%Y-%m-%d %H:%M:%S"
  43. DATE_FORMAT_YMD, //"%Y-%m-%d"
  44. DATE_FORMAT_HMS //"%H:%M:%S"
  45. };
  46. class DateTime {
  47. public:
  48. /**
  49. * @brief Write the current date and time conform to RFC 822.
  50. * @code
  51. * std::cout << "currentDate(): `"
  52. * << vle::utils::DateTime::currentDate()
  53. * << "'\n";
  54. *
  55. * // Display: currentDate(): `2011-Jun-09 12:13:21'
  56. * @endcode
  57. * @return string representation of date.
  58. */
  59. static std::string currentDate();
  60. /* * * * */
  61. /**
  62. * @brief Get the year in the simulation time.
  63. * @code
  64. * vle::utils::DateTime::year(2451545) == 2000u;
  65. * @endcode
  66. * @param time The simulation time.
  67. * @return An unsigned int.
  68. */
  69. static unsigned int year(double time);
  70. /**
  71. * @brief Get the month in the simulation time.
  72. * @code
  73. * vle::utils::DateTime::month(2451545) == 1u;
  74. * @endcode
  75. * @param time The simulation time.
  76. * @return An unsigned int.
  77. */
  78. static unsigned int month(double time);
  79. /**
  80. * @brief Get the day of the month in the simulation time.
  81. * @code
  82. * vle::utils::DateTime::dayOfMonth((2451545)) == 1u;
  83. * @endcode
  84. * @param time The simulation time.
  85. * @return An unsigned int.
  86. */
  87. static unsigned int dayOfMonth(double time);
  88. /**
  89. * @brief Get the day in the week of the simulation time.
  90. * @code
  91. * vle::utils::DateTime::dayOfWeek((2451545)) == 6u;
  92. * @endcode
  93. * @param time The simulation time.
  94. * @return An unsigned int.
  95. */
  96. static unsigned int dayOfWeek(double time);
  97. /**
  98. * @brief Get the day in the year of the simulation time.
  99. * @code
  100. * vle::utils::DateTime::dayOfYear((2451545)) == 1u;
  101. * @endcode
  102. * @param time The simulation time.
  103. * @return An unsigned int.
  104. */
  105. static unsigned int dayOfYear(double time);
  106. /**
  107. * @brief Get the week in the year of the simulation time.
  108. * @code
  109. * vle::utils::DateTime::dayOfYear((2451545)) == 1u;
  110. * @endcode
  111. * @param time The simulation time.
  112. * @return An unsigned int.
  113. */
  114. static unsigned int weekOfYear(double time);
  115. /**
  116. * @brief Check if the simulation time is a leap year.
  117. * @param time The simulation time.
  118. * @return true if time is a leap year, false otherwise.
  119. */
  120. static bool isLeapYear(double time);
  121. /**
  122. * @brief Get the number of day in the year for the simulaton time.
  123. * @code
  124. * vle::utils::Datime::aYear(2451545) == 366;
  125. * @endcode
  126. * @param time The simulation time.
  127. * @return number of day.
  128. */
  129. static double aYear(double time);
  130. /**
  131. * @brief Get the number of day in the month for the simulation time.
  132. * @code
  133. * vle::utils::Datime::aMonth(2451545) == 31;
  134. * vle::utils::Datime::aMonth(2451576) == 29;
  135. * @endcode
  136. * @param time The simulation time.
  137. * @return number of day.
  138. */
  139. static double aMonth(double time);
  140. /**
  141. * @brief Get the number of day in a week.
  142. * @return Return 7.
  143. */
  144. static inline double aWeek() { return 7; }
  145. /**
  146. * @brief Get the number of day in a day.
  147. * @return Return 1.
  148. */
  149. static inline double aDay() { return 1; }
  150. /**
  151. * @brief Get number of days in n-years from the simulation time.
  152. * @code
  153. * vle::utils::DateTime::years((2451545), 1), 366);
  154. * vle::utils::DateTime::years((2451545), 2), 731);
  155. * @endcode
  156. * @param time The simulation time.
  157. * @param n The number of years.
  158. * @return The number of days in n-years.
  159. */
  160. static double years(double time, unsigned int n);
  161. /**
  162. * @brief Get number of days in n-months from the simulation time.
  163. * @code
  164. * vle::utils::DateTime::months((2451545), 2) = 60;
  165. * @endcode
  166. * @param time The simulation time.
  167. * @param n The number of weeks.
  168. * @return The number of days in n-months.
  169. */
  170. static double months(double time, unsigned int n);
  171. /**
  172. * @brief Get number of days in n-weeks.
  173. * @param n Number of weeks.
  174. * @return n * 7.
  175. */
  176. static inline double weeks(unsigned int n) { return (int) (7 * n); }
  177. /**
  178. * @brief Get number of days in n-days.
  179. * @param n Number of days.
  180. * @return n.
  181. */
  182. static inline double days(unsigned int n) { return (int) n; }
  183. /**
  184. * @brief Convert std::string unit ("day", "week", "month", "year") into
  185. * the DateTime::Unit type.
  186. * @param unit The std::string unit to convert.
  187. * @return The convertion of Day if error.
  188. */
  189. static DateTimeUnitOptions convertUnit(const std::string& unit);
  190. /**
  191. * @brief A easy function to call days(), weeks(), months() or years()
  192. * using a DateTime::Unit type.
  193. * @param time The simulation date (useless for Day, Week).
  194. * @param duration The number of DateTime::Unit.
  195. * @param unit The unit.
  196. * @return A number of day.
  197. */
  198. static double duration(double time,
  199. double duration,
  200. DateTimeUnitOptions unit);
  201. /* * * * */
  202. /**
  203. * @brief Convert an julian day number into a string.
  204. * @code
  205. * vle::utils::DateTime::toJulianDayNumber(2452192) = "2001-10-9";
  206. * @endcode
  207. * @param date The date to convert.
  208. * @return A string representation of the julian day.
  209. */
  210. static std::string toJulianDayNumber(int date);
  211. /**
  212. * @brief Convert a string into a julian day number;
  213. * @code
  214. * vle::utils::DateTime::toJulianDayNumber("2001-10-9") = 2452192;
  215. * @endcode
  216. * @param date The date to convert.
  217. * @return A julian day number.
  218. */
  219. static long toJulianDayNumber(const std::string& date);
  220. /**
  221. * @brief Convert a julian date into a string.
  222. * @code
  223. * vle::utils::DateTime::toJulianDay(2454115.05486)) = "2001-10-9 hh:mm:ss";
  224. * @endcode
  225. * @param date The date to convert.
  226. * @return A string representation of the julian day.
  227. */
  228. static std::string toJulianDay(double date);
  229. /**
  230. * @brief Convert a string into a julian day.
  231. * @code
  232. * vle::utils::DateTime::toJulianDay("2001-10-9 hh:mm:ss") = 2454115.05486;
  233. * @endcode
  234. * @param date The date to convert.
  235. * @return A string representation of the julian day.
  236. */
  237. static double toJulianDay(const std::string& date);
  238. /**
  239. * @brief Convert a julian date into a string in a given format.
  240. * @code
  241. * artis::utils::DateTime::toJulianDay(2454115.05486, DATE_FORMAT_YMD)) = "2001-10-9";
  242. * @endcode
  243. * @param date The date to convert.
  244. * @param format The string format from enum artis::utils::DateFormat.
  245. * @return A string representation of the julian day.
  246. */
  247. static std::string toJulianDayFmt(double date, artis::utils::DateFormat format);
  248. /* * * * */
  249. /**
  250. * @brief Check if the date is a valid year in gregorian calendard.
  251. *
  252. * @param date The date to check.
  253. *
  254. * @return True if date is a valid year, false otherwise.
  255. */
  256. static bool isValidYear(double date);
  257. /**
  258. * @brief Explode the specified date attribute to year, month, day in the
  259. * month, hours, minutes and seconds.
  260. *
  261. * @param date The date to convert.
  262. * @param year Output parameter to represent year.
  263. * @param month Output parameter to represent month.
  264. * @param day Output parameter to represent day in a month (1..31).
  265. * @param hours Output parameter to represent hours in date.
  266. * @param minutes Output parameter to represent minutes in date.
  267. * @param seconds Output parameter to represent seconds in date.
  268. *
  269. * @throw utils::ArgError error to convert the date.
  270. *
  271. * @return The remainder of the conversion.
  272. */
  273. static double toTime(double date,
  274. int& year,
  275. int& month,
  276. int& day,
  277. int& hours,
  278. int& minutes,
  279. int& seconds);
  280. /**
  281. * @brief Explode current date to year, month, day in the
  282. * month, hours, minutes and seconds.
  283. *
  284. * @param year Output parameter to represent year.
  285. * @param month Output parameter to represent month.
  286. * @param day Output parameter to represent day in a month (1..31).
  287. *
  288. */
  289. static void currentDate(int& year,
  290. int& month,
  291. int& day);
  292. static void format_date(const std::string& str, std::string& date)
  293. {
  294. std::vector<std::string> list;
  295. boost::split(list, str, boost::is_any_of("-/"));
  296. date = (boost::format("%1%/%2%/%3%") % list[2] % list[1] %
  297. list[0]).str();
  298. }
  299. };
  300. }
  301. }
  302. #endif