DateTime.hpp 10 KB

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