DateTime.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.set_time(year, month, day, hour, minute, second);
  39. return dt;
  40. }
  41. DateTime DateTime::from_timestamp(time_t timestamp)
  42. {
  43. struct tm tm;
  44. localtime_r(&timestamp, &tm);
  45. DateTime dt;
  46. dt.m_year = tm.tm_year + 1900;
  47. dt.m_month = tm.tm_mon + 1;
  48. dt.m_day = tm.tm_mday;
  49. dt.m_hour = tm.tm_hour;
  50. dt.m_minute = tm.tm_min;
  51. dt.m_second = tm.tm_sec;
  52. dt.m_timestamp = timestamp;
  53. return dt;
  54. }
  55. unsigned DateTime::weekday() const
  56. {
  57. int target_year = m_year;
  58. static const int seek_table[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
  59. if (m_month < 3)
  60. --target_year;
  61. return (target_year + target_year / 4 - target_year / 100 + target_year / 400 + seek_table[m_month - 1] + m_day) % 7;
  62. }
  63. unsigned DateTime::days_in_month() const
  64. {
  65. 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);
  66. if (m_month == 2)
  67. return is_leap_year() ? 29 : 28;
  68. return is_long_month ? 31 : 30;
  69. }
  70. unsigned DateTime::day_of_year() const
  71. {
  72. static const int seek_table[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  73. int day_of_year = seek_table[m_month - 1] + m_day;
  74. if (is_leap_year() && m_month > 3)
  75. day_of_year++;
  76. return day_of_year - 1;
  77. }
  78. bool DateTime::is_leap_year() const
  79. {
  80. return ((m_year % 400 == 0) || (m_year % 4 == 0 && m_year % 100 != 0));
  81. }
  82. void DateTime::set_time(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second)
  83. {
  84. m_year = year;
  85. m_month = month;
  86. m_day = day;
  87. m_hour = hour;
  88. m_minute = minute;
  89. m_second = second;
  90. struct tm tm = {};
  91. tm.tm_sec = (int)second;
  92. tm.tm_min = (int)minute;
  93. tm.tm_hour = (int)hour;
  94. tm.tm_mday = (int)day;
  95. tm.tm_mon = (int)month - 1;
  96. tm.tm_year = (int)year - 1900;
  97. tm.tm_wday = (int)weekday();
  98. tm.tm_yday = (int)day_of_year();
  99. m_timestamp = mktime(&tm);
  100. }
  101. String DateTime::to_string(const String& format) const
  102. {
  103. const char wday_short_names[7][4] = {
  104. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  105. };
  106. const char wday_long_names[7][10] = {
  107. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  108. };
  109. const char mon_short_names[12][4] = {
  110. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  111. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  112. };
  113. const char mon_long_names[12][10] = {
  114. "January", "February", "March", "April", "May", "June",
  115. "July", "August", "September", "October", "November", "December"
  116. };
  117. struct tm tm;
  118. localtime_r(&m_timestamp, &tm);
  119. StringBuilder builder;
  120. const int format_len = format.length();
  121. for (int i = 0; i < format_len; ++i) {
  122. if (format[i] != '%') {
  123. builder.append(format[i]);
  124. } else {
  125. if (++i == format_len)
  126. return String();
  127. switch (format[i]) {
  128. case 'a':
  129. builder.append(wday_short_names[tm.tm_wday]);
  130. break;
  131. case 'A':
  132. builder.append(wday_long_names[tm.tm_wday]);
  133. break;
  134. case 'b':
  135. builder.append(mon_short_names[tm.tm_mon]);
  136. break;
  137. case 'B':
  138. builder.append(mon_long_names[tm.tm_mon]);
  139. break;
  140. case 'C':
  141. builder.appendf("%02d", (tm.tm_year + 1900) / 100);
  142. break;
  143. case 'd':
  144. builder.appendf("%02d", tm.tm_mday);
  145. break;
  146. case 'D':
  147. builder.appendf("%02d/%02d/%02d", tm.tm_mon + 1, tm.tm_mday, (tm.tm_year + 1900) % 100);
  148. break;
  149. case 'e':
  150. builder.appendf("%2d", tm.tm_mday);
  151. break;
  152. case 'h':
  153. builder.append(mon_short_names[tm.tm_mon]);
  154. break;
  155. case 'H':
  156. builder.appendf("%02d", tm.tm_hour);
  157. break;
  158. case 'I':
  159. builder.appendf("%02d", tm.tm_hour % 12);
  160. break;
  161. case 'j':
  162. builder.appendf("%03d", tm.tm_yday + 1);
  163. break;
  164. case 'm':
  165. builder.appendf("%02d", tm.tm_mon + 1);
  166. break;
  167. case 'M':
  168. builder.appendf("%02d", tm.tm_min);
  169. break;
  170. case 'n':
  171. builder.append('\n');
  172. break;
  173. case 'p':
  174. builder.append(tm.tm_hour < 12 ? "a.m." : "p.m.");
  175. break;
  176. case 'r':
  177. builder.appendf("%02d:%02d:%02d %s", tm.tm_hour % 12, tm.tm_min, tm.tm_sec, tm.tm_hour < 12 ? "a.m." : "p.m.");
  178. break;
  179. case 'R':
  180. builder.appendf("%02d:%02d", tm.tm_hour, tm.tm_min);
  181. break;
  182. case 'S':
  183. builder.appendf("%02d", tm.tm_sec);
  184. break;
  185. case 't':
  186. builder.append('\t');
  187. break;
  188. case 'T':
  189. builder.appendf("%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec);
  190. break;
  191. case 'u':
  192. builder.appendf("%d", tm.tm_wday ? tm.tm_wday : 7);
  193. break;
  194. case 'U': {
  195. const int wday_of_year_beginning = (tm.tm_wday + 6 * tm.tm_yday) % 7;
  196. const int week_number = (tm.tm_yday + wday_of_year_beginning) / 7;
  197. builder.appendf("%02d", week_number);
  198. break;
  199. }
  200. case 'V': {
  201. const int wday_of_year_beginning = (tm.tm_wday + 6 + 6 * tm.tm_yday) % 7;
  202. int week_number = (tm.tm_yday + wday_of_year_beginning) / 7 + 1;
  203. if (wday_of_year_beginning > 3) {
  204. if (tm.tm_yday >= 7 - wday_of_year_beginning)
  205. --week_number;
  206. else {
  207. 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;
  208. const int days_of_last_year = 365 + last_year_is_leap;
  209. const int wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7;
  210. week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1;
  211. if (wday_of_last_year_beginning > 3)
  212. --week_number;
  213. }
  214. }
  215. builder.appendf("%02d", week_number);
  216. break;
  217. }
  218. case 'w':
  219. builder.appendf("%d", tm.tm_wday);
  220. break;
  221. case 'W': {
  222. const int wday_of_year_beginning = (tm.tm_wday + 6 + 6 * tm.tm_yday) % 7;
  223. const int week_number = (tm.tm_yday + wday_of_year_beginning) / 7;
  224. builder.appendf("%02d", week_number);
  225. break;
  226. }
  227. case 'y':
  228. builder.appendf("%02d", (tm.tm_year + 1900) % 100);
  229. break;
  230. case 'Y':
  231. builder.appendf("%d", tm.tm_year + 1900);
  232. break;
  233. case '%':
  234. builder.append('%');
  235. break;
  236. default:
  237. return String();
  238. }
  239. }
  240. }
  241. return builder.build();
  242. }
  243. bool DateTime::is_before(const String& other) const
  244. {
  245. auto now_string = String::format("%04d%02d%02d%02d%02d%02dZ", year(), month(), weekday(), hour(), minute(), second());
  246. return __builtin_strcasecmp(now_string.characters(), other.characters()) < 0;
  247. }
  248. const LogStream& operator<<(const LogStream& stream, const DateTime& value)
  249. {
  250. return stream << value.to_string();
  251. }
  252. }