DateTime.cpp 8.7 KB

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