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