Date.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(Core::DateTime datetime, i16 milliseconds, bool is_invalid, Object& prototype)
  19. : Object(prototype)
  20. , m_datetime(datetime)
  21. , m_milliseconds(milliseconds)
  22. , m_is_invalid(is_invalid)
  23. {
  24. }
  25. Date::~Date()
  26. {
  27. }
  28. tm Date::to_utc_tm() const
  29. {
  30. time_t timestamp = m_datetime.timestamp();
  31. struct tm tm;
  32. gmtime_r(&timestamp, &tm);
  33. return tm;
  34. }
  35. int Date::utc_date() const
  36. {
  37. return to_utc_tm().tm_mday;
  38. }
  39. int Date::utc_day() const
  40. {
  41. return to_utc_tm().tm_wday;
  42. }
  43. int Date::utc_full_year() const
  44. {
  45. return to_utc_tm().tm_year + 1900;
  46. }
  47. int Date::utc_hours() const
  48. {
  49. return to_utc_tm().tm_hour;
  50. }
  51. int Date::utc_minutes() const
  52. {
  53. return to_utc_tm().tm_min;
  54. }
  55. int Date::utc_month() const
  56. {
  57. return to_utc_tm().tm_mon;
  58. }
  59. int Date::utc_seconds() const
  60. {
  61. return to_utc_tm().tm_sec;
  62. }
  63. String Date::gmt_date_string() const
  64. {
  65. // Mon, 18 Dec 1995 17:28:35 GMT
  66. // FIXME: Note that we're totally cheating with the timezone part here..
  67. return datetime().to_string("%a, %e %b %Y %T GMT");
  68. }
  69. String Date::iso_date_string() const
  70. {
  71. auto tm = to_utc_tm();
  72. int year = tm.tm_year + 1900;
  73. int month = tm.tm_mon + 1;
  74. StringBuilder builder;
  75. if (year < 0)
  76. builder.appendff("-{:06}", -year);
  77. else if (year > 9999)
  78. builder.appendff("+{:06}", year);
  79. else
  80. builder.appendff("{:04}", year);
  81. builder.append('-');
  82. builder.appendff("{:02}", month);
  83. builder.append('-');
  84. builder.appendff("{:02}", tm.tm_mday);
  85. builder.append('T');
  86. builder.appendff("{:02}", tm.tm_hour);
  87. builder.append(':');
  88. builder.appendff("{:02}", tm.tm_min);
  89. builder.append(':');
  90. builder.appendff("{:02}", tm.tm_sec);
  91. builder.append('.');
  92. builder.appendff("{:03}", m_milliseconds);
  93. builder.append('Z');
  94. return builder.build();
  95. }
  96. }