Date.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <LibCore/DateTime.h>
  8. #include <LibJS/Heap/Heap.h>
  9. #include <LibJS/Runtime/Date.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <sys/time.h>
  12. #include <time.h>
  13. namespace JS {
  14. Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, i16 milliseconds, bool is_invalid)
  15. {
  16. return global_object.heap().allocate<Date>(global_object, datetime, milliseconds, is_invalid, *global_object.date_prototype());
  17. }
  18. Date* Date::now(GlobalObject& global_object)
  19. {
  20. struct timeval tv;
  21. gettimeofday(&tv, nullptr);
  22. auto datetime = Core::DateTime::now();
  23. auto milliseconds = static_cast<i16>(tv.tv_usec / 1000);
  24. return create(global_object, datetime, milliseconds);
  25. }
  26. Date::Date(Core::DateTime datetime, i16 milliseconds, bool is_invalid, Object& prototype)
  27. : Object(prototype)
  28. , m_datetime(datetime)
  29. , m_milliseconds(milliseconds)
  30. , m_is_invalid(is_invalid)
  31. {
  32. }
  33. Date::~Date()
  34. {
  35. }
  36. tm Date::to_utc_tm() const
  37. {
  38. time_t timestamp = m_datetime.timestamp();
  39. struct tm tm;
  40. gmtime_r(&timestamp, &tm);
  41. return tm;
  42. }
  43. int Date::utc_date() const
  44. {
  45. return to_utc_tm().tm_mday;
  46. }
  47. int Date::utc_day() const
  48. {
  49. return to_utc_tm().tm_wday;
  50. }
  51. int Date::utc_full_year() const
  52. {
  53. return to_utc_tm().tm_year + 1900;
  54. }
  55. int Date::utc_hours() const
  56. {
  57. return to_utc_tm().tm_hour;
  58. }
  59. int Date::utc_minutes() const
  60. {
  61. return to_utc_tm().tm_min;
  62. }
  63. int Date::utc_month() const
  64. {
  65. return to_utc_tm().tm_mon;
  66. }
  67. int Date::utc_seconds() const
  68. {
  69. return to_utc_tm().tm_sec;
  70. }
  71. String Date::gmt_date_string() const
  72. {
  73. // Mon, 18 Dec 1995 17:28:35 GMT
  74. // FIXME: Note that we're totally cheating with the timezone part here..
  75. return datetime().to_string("%a, %e %b %Y %T GMT");
  76. }
  77. String Date::iso_date_string() const
  78. {
  79. auto tm = to_utc_tm();
  80. int year = tm.tm_year + 1900;
  81. int month = tm.tm_mon + 1;
  82. StringBuilder builder;
  83. if (year < 0)
  84. builder.appendff("-{:06}", -year);
  85. else if (year > 9999)
  86. builder.appendff("+{:06}", year);
  87. else
  88. builder.appendff("{:04}", year);
  89. builder.append('-');
  90. builder.appendff("{:02}", month);
  91. builder.append('-');
  92. builder.appendff("{:02}", tm.tm_mday);
  93. builder.append('T');
  94. builder.appendff("{:02}", tm.tm_hour);
  95. builder.append(':');
  96. builder.appendff("{:02}", tm.tm_min);
  97. builder.append(':');
  98. builder.appendff("{:02}", tm.tm_sec);
  99. builder.append('.');
  100. builder.appendff("{:03}", m_milliseconds);
  101. builder.append('Z');
  102. return builder.build();
  103. }
  104. }