DateTime.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <AK/Time.h>
  8. #include <LibCore/DateTime.h>
  9. #include <sys/time.h>
  10. #include <time.h>
  11. namespace Core {
  12. DateTime DateTime::now()
  13. {
  14. return from_timestamp(time(nullptr));
  15. }
  16. DateTime DateTime::create(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second)
  17. {
  18. DateTime dt;
  19. dt.set_time(year, month, day, hour, minute, second);
  20. return dt;
  21. }
  22. DateTime DateTime::from_timestamp(time_t timestamp)
  23. {
  24. struct tm tm;
  25. localtime_r(&timestamp, &tm);
  26. DateTime dt;
  27. dt.m_year = tm.tm_year + 1900;
  28. dt.m_month = tm.tm_mon + 1;
  29. dt.m_day = tm.tm_mday;
  30. dt.m_hour = tm.tm_hour;
  31. dt.m_minute = tm.tm_min;
  32. dt.m_second = tm.tm_sec;
  33. dt.m_timestamp = timestamp;
  34. return dt;
  35. }
  36. unsigned DateTime::weekday() const
  37. {
  38. return ::day_of_week(m_year, m_month, m_day);
  39. }
  40. unsigned DateTime::days_in_month() const
  41. {
  42. return ::days_in_month(m_year, m_month);
  43. }
  44. unsigned DateTime::day_of_year() const
  45. {
  46. return ::day_of_year(m_year, m_month, m_day);
  47. }
  48. bool DateTime::is_leap_year() const
  49. {
  50. return ::is_leap_year(m_year);
  51. }
  52. void DateTime::set_time(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second)
  53. {
  54. struct tm tm = {};
  55. tm.tm_sec = (int)second;
  56. tm.tm_min = (int)minute;
  57. tm.tm_hour = (int)hour;
  58. tm.tm_mday = (int)day;
  59. tm.tm_mon = (int)month - 1;
  60. tm.tm_year = (int)year - 1900;
  61. tm.tm_isdst = -1;
  62. // mktime() doesn't read tm.tm_wday and tm.tm_yday, no need to fill them in.
  63. m_timestamp = mktime(&tm);
  64. // mktime() normalizes the components to the right ranges (Jan 32 -> Feb 1 etc), so read fields back out from tm.
  65. m_year = tm.tm_year + 1900;
  66. m_month = tm.tm_mon + 1;
  67. m_day = tm.tm_mday;
  68. m_hour = tm.tm_hour;
  69. m_minute = tm.tm_min;
  70. m_second = tm.tm_sec;
  71. }
  72. String DateTime::to_string(const String& format) const
  73. {
  74. const char wday_short_names[7][4] = {
  75. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  76. };
  77. const char wday_long_names[7][10] = {
  78. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  79. };
  80. const char mon_short_names[12][4] = {
  81. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  82. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  83. };
  84. const char mon_long_names[12][10] = {
  85. "January", "February", "March", "April", "May", "June",
  86. "July", "August", "September", "October", "November", "December"
  87. };
  88. struct tm tm;
  89. localtime_r(&m_timestamp, &tm);
  90. StringBuilder builder;
  91. const int format_len = format.length();
  92. for (int i = 0; i < format_len; ++i) {
  93. if (format[i] != '%') {
  94. builder.append(format[i]);
  95. } else {
  96. if (++i == format_len)
  97. return String();
  98. switch (format[i]) {
  99. case 'a':
  100. builder.append(wday_short_names[tm.tm_wday]);
  101. break;
  102. case 'A':
  103. builder.append(wday_long_names[tm.tm_wday]);
  104. break;
  105. case 'b':
  106. builder.append(mon_short_names[tm.tm_mon]);
  107. break;
  108. case 'B':
  109. builder.append(mon_long_names[tm.tm_mon]);
  110. break;
  111. case 'C':
  112. builder.appendff("{:02}", (tm.tm_year + 1900) / 100);
  113. break;
  114. case 'd':
  115. builder.appendff("{:02}", tm.tm_mday);
  116. break;
  117. case 'D':
  118. builder.appendff("{:02}/{:02}/{:02}", tm.tm_mon + 1, tm.tm_mday, (tm.tm_year + 1900) % 100);
  119. break;
  120. case 'e':
  121. builder.appendff("{:2}", tm.tm_mday);
  122. break;
  123. case 'h':
  124. builder.append(mon_short_names[tm.tm_mon]);
  125. break;
  126. case 'H':
  127. builder.appendff("{:02}", tm.tm_hour);
  128. break;
  129. case 'I':
  130. builder.appendff("{:02}", tm.tm_hour % 12);
  131. break;
  132. case 'j':
  133. builder.appendff("{:03}", tm.tm_yday + 1);
  134. break;
  135. case 'm':
  136. builder.appendff("{:02}", tm.tm_mon + 1);
  137. break;
  138. case 'M':
  139. builder.appendff("{:02}", tm.tm_min);
  140. break;
  141. case 'n':
  142. builder.append('\n');
  143. break;
  144. case 'p':
  145. builder.append(tm.tm_hour < 12 ? "a.m." : "p.m.");
  146. break;
  147. case 'r':
  148. builder.appendff("{:02}:{:02}:{:02} {}", tm.tm_hour % 12, tm.tm_min, tm.tm_sec, tm.tm_hour < 12 ? "a.m." : "p.m.");
  149. break;
  150. case 'R':
  151. builder.appendff("{:02}:{:02}", tm.tm_hour, tm.tm_min);
  152. break;
  153. case 'S':
  154. builder.appendff("{:02}", tm.tm_sec);
  155. break;
  156. case 't':
  157. builder.append('\t');
  158. break;
  159. case 'T':
  160. builder.appendff("{:02}:{:02}:{:02}", tm.tm_hour, tm.tm_min, tm.tm_sec);
  161. break;
  162. case 'u':
  163. builder.appendff("{}", tm.tm_wday ? tm.tm_wday : 7);
  164. break;
  165. case 'U': {
  166. const int wday_of_year_beginning = (tm.tm_wday + 6 * tm.tm_yday) % 7;
  167. const int week_number = (tm.tm_yday + wday_of_year_beginning) / 7;
  168. builder.appendff("{:02}", week_number);
  169. break;
  170. }
  171. case 'V': {
  172. const int wday_of_year_beginning = (tm.tm_wday + 6 + 6 * tm.tm_yday) % 7;
  173. int week_number = (tm.tm_yday + wday_of_year_beginning) / 7 + 1;
  174. if (wday_of_year_beginning > 3) {
  175. if (tm.tm_yday >= 7 - wday_of_year_beginning)
  176. --week_number;
  177. else {
  178. const int days_of_last_year = days_in_year(tm.tm_year + 1900 - 1);
  179. const int wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7;
  180. week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1;
  181. if (wday_of_last_year_beginning > 3)
  182. --week_number;
  183. }
  184. }
  185. builder.appendff("{:02}", week_number);
  186. break;
  187. }
  188. case 'w':
  189. builder.appendff("{}", tm.tm_wday);
  190. break;
  191. case 'W': {
  192. const int wday_of_year_beginning = (tm.tm_wday + 6 + 6 * tm.tm_yday) % 7;
  193. const int week_number = (tm.tm_yday + wday_of_year_beginning) / 7;
  194. builder.appendff("{:02}", week_number);
  195. break;
  196. }
  197. case 'y':
  198. builder.appendff("{:02}", (tm.tm_year + 1900) % 100);
  199. break;
  200. case 'Y':
  201. builder.appendff("{}", tm.tm_year + 1900);
  202. break;
  203. case '%':
  204. builder.append('%');
  205. break;
  206. default:
  207. return String();
  208. }
  209. }
  210. }
  211. return builder.build();
  212. }
  213. }