DateTime.cpp 9.4 KB

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