DateTime.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibCore/DateTime.h>
  28. #include <sys/time.h>
  29. #include <time.h>
  30. namespace Core {
  31. DateTime DateTime::now()
  32. {
  33. return from_timestamp(time(nullptr));
  34. }
  35. DateTime DateTime::create(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second)
  36. {
  37. DateTime dt;
  38. dt.m_year = year;
  39. dt.m_month = month;
  40. dt.m_day = day;
  41. dt.m_hour = hour;
  42. dt.m_minute = minute;
  43. dt.m_second = second;
  44. struct tm tm = {};
  45. tm.tm_sec = (int)second;
  46. tm.tm_min = (int)minute;
  47. tm.tm_hour = (int)hour;
  48. tm.tm_mday = (int)day;
  49. tm.tm_mon = (int)month - 1;
  50. tm.tm_year = (int)year - 1900;
  51. tm.tm_wday = (int)dt.weekday();
  52. tm.tm_yday = (int)dt.day_of_year();
  53. dt.m_timestamp = mktime(&tm);
  54. return dt;
  55. }
  56. DateTime DateTime::from_timestamp(time_t timestamp)
  57. {
  58. struct tm tm;
  59. localtime_r(&timestamp, &tm);
  60. DateTime dt;
  61. dt.m_year = tm.tm_year + 1900;
  62. dt.m_month = tm.tm_mon + 1;
  63. dt.m_day = tm.tm_mday;
  64. dt.m_hour = tm.tm_hour;
  65. dt.m_minute = tm.tm_min;
  66. dt.m_second = tm.tm_sec;
  67. dt.m_timestamp = timestamp;
  68. return dt;
  69. }
  70. unsigned DateTime::weekday() const
  71. {
  72. int target_year = m_year;
  73. static const int seek_table[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
  74. if (m_month < 3)
  75. --target_year;
  76. return (target_year + target_year / 4 - target_year / 100 + target_year / 400 + seek_table[m_month - 1] + m_day) % 7;
  77. }
  78. unsigned DateTime::days_in_month() const
  79. {
  80. bool is_long_month = (m_month == 1 || m_month == 3 || m_month == 5 || m_month == 7 || m_month == 8 || m_month == 10 || m_month == 12);
  81. if (m_month == 2)
  82. return is_leap_year() ? 29 : 28;
  83. return is_long_month ? 31 : 30;
  84. }
  85. unsigned DateTime::day_of_year() const
  86. {
  87. static const int seek_table[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  88. int day_of_year = seek_table[m_month - 1] + m_day;
  89. if (is_leap_year() && m_month > 3)
  90. day_of_year++;
  91. return day_of_year - 1;
  92. }
  93. bool DateTime::is_leap_year() const
  94. {
  95. return ((m_year % 400 == 0) || (m_year % 4 == 0 && m_year % 100 != 0));
  96. }
  97. void DateTime::set_time(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second)
  98. {
  99. m_year = year;
  100. m_month = month;
  101. m_day = day;
  102. m_hour = hour;
  103. m_minute = minute;
  104. m_second = second;
  105. struct tm tm = {};
  106. tm.tm_sec = (int)second;
  107. tm.tm_min = (int)minute;
  108. tm.tm_hour = (int)hour;
  109. tm.tm_mday = (int)day;
  110. tm.tm_mon = (int)month - 1;
  111. tm.tm_year = (int)year - 1900;
  112. tm.tm_wday = (int)weekday();
  113. tm.tm_yday = (int)day_of_year();
  114. m_timestamp = mktime(&tm);
  115. }
  116. String DateTime::to_string(const String& format) const
  117. {
  118. const char wday_short_names[7][4] = {
  119. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  120. };
  121. const char wday_long_names[7][10] = {
  122. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  123. };
  124. const char mon_short_names[12][4] = {
  125. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  126. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  127. };
  128. const char mon_long_names[12][10] = {
  129. "January", "February", "March", "April", "May", "June",
  130. "July", "Auguest", "September", "October", "November", "December"
  131. };
  132. struct tm tm;
  133. localtime_r(&m_timestamp, &tm);
  134. StringBuilder builder;
  135. const int format_len = format.length();
  136. for (int i = 0; i < format_len; ++i) {
  137. if (format[i] != '%') {
  138. builder.append(format[i]);
  139. } else {
  140. if (++i == format_len)
  141. return String();
  142. switch (format[i]) {
  143. case 'a':
  144. builder.append(wday_short_names[tm.tm_wday]);
  145. break;
  146. case 'A':
  147. builder.append(wday_long_names[tm.tm_wday]);
  148. break;
  149. case 'b':
  150. builder.append(mon_short_names[tm.tm_mon]);
  151. break;
  152. case 'B':
  153. builder.append(mon_long_names[tm.tm_mon]);
  154. break;
  155. case 'C':
  156. builder.appendf("%02d", (tm.tm_year + 1900) / 100);
  157. break;
  158. case 'd':
  159. builder.appendf("%02d", tm.tm_mday);
  160. break;
  161. case 'D':
  162. builder.appendf("%02d/%02d/%02d", tm.tm_mon + 1, tm.tm_mday, (tm.tm_year + 1900) % 100);
  163. break;
  164. case 'e':
  165. builder.appendf("%2d", tm.tm_mday);
  166. break;
  167. case 'h':
  168. builder.append(mon_short_names[tm.tm_mon]);
  169. break;
  170. case 'H':
  171. builder.appendf("%02d", tm.tm_hour);
  172. break;
  173. case 'I':
  174. builder.appendf("%02d", tm.tm_hour % 12);
  175. break;
  176. case 'j':
  177. builder.appendf("%03d", tm.tm_yday + 1);
  178. break;
  179. case 'm':
  180. builder.appendf("%02d", tm.tm_mon + 1);
  181. break;
  182. case 'M':
  183. builder.appendf("%02d", tm.tm_min);
  184. break;
  185. case 'n':
  186. builder.append('\n');
  187. break;
  188. case 'p':
  189. builder.append(tm.tm_hour < 12 ? "a.m." : "p.m.");
  190. break;
  191. case 'r':
  192. builder.appendf("%02d:%02d:%02d %s", tm.tm_hour % 12, tm.tm_min, tm.tm_sec, tm.tm_hour < 12 ? "a.m." : "p.m.");
  193. break;
  194. case 'R':
  195. builder.appendf("%02d:%02d", tm.tm_hour, tm.tm_min);
  196. break;
  197. case 'S':
  198. builder.appendf("%02d", tm.tm_sec);
  199. break;
  200. case 't':
  201. builder.append('\t');
  202. break;
  203. case 'T':
  204. builder.appendf("%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec);
  205. break;
  206. case 'u':
  207. builder.appendf("%d", tm.tm_wday ? tm.tm_wday : 7);
  208. break;
  209. case 'U': {
  210. const int wday_of_year_beginning = (tm.tm_wday + 6 * tm.tm_yday) % 7;
  211. const int week_number = (tm.tm_yday + wday_of_year_beginning) / 7;
  212. builder.appendf("%02d", week_number);
  213. break;
  214. }
  215. case 'V': {
  216. const int wday_of_year_beginning = (tm.tm_wday + 6 + 6 * tm.tm_yday) % 7;
  217. int week_number = (tm.tm_yday + wday_of_year_beginning) / 7 + 1;
  218. if (wday_of_year_beginning > 3) {
  219. if (tm.tm_yday >= 7 - wday_of_year_beginning)
  220. --week_number;
  221. else {
  222. const bool last_year_is_leap = ((tm.tm_year + 1900 - 1) % 4 == 0 && (tm.tm_year + 1900 - 1) % 100 != 0) || (tm.tm_year + 1900 - 1) % 400 == 0;
  223. const int days_of_last_year = 365 + last_year_is_leap;
  224. const int wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7;
  225. week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1;
  226. if (wday_of_last_year_beginning > 3)
  227. --week_number;
  228. }
  229. }
  230. builder.appendf("%02d", week_number);
  231. break;
  232. }
  233. case 'w':
  234. builder.appendf("%d", tm.tm_wday);
  235. break;
  236. case 'W': {
  237. const int wday_of_year_beginning = (tm.tm_wday + 6 + 6 * tm.tm_yday) % 7;
  238. const int week_number = (tm.tm_yday + wday_of_year_beginning) / 7;
  239. builder.appendf("%02d", week_number);
  240. break;
  241. }
  242. case 'y':
  243. builder.appendf("%02d", (tm.tm_year + 1900) % 100);
  244. break;
  245. case 'Y':
  246. builder.appendf("%d", tm.tm_year + 1900);
  247. break;
  248. case '%':
  249. builder.append('%');
  250. break;
  251. default:
  252. return String();
  253. }
  254. }
  255. }
  256. return builder.build();
  257. }
  258. bool DateTime::is_before(const String& other) const
  259. {
  260. auto now_string = String::format("%04d%02d%02d%02d%02d%02dZ", year(), month(), weekday(), hour(), minute(), second());
  261. return __builtin_strcasecmp(now_string.characters(), other.characters()) < 0;
  262. }
  263. const LogStream& operator<<(const LogStream& stream, const DateTime& value)
  264. {
  265. return stream << value.to_string();
  266. }
  267. }