DatePrototype.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  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/Function.h>
  27. #include <AK/String.h>
  28. #include <LibCore/DateTime.h>
  29. #include <LibJS/Interpreter.h>
  30. #include <LibJS/Runtime/Date.h>
  31. #include <LibJS/Runtime/DatePrototype.h>
  32. #include <LibJS/Runtime/Error.h>
  33. #include <LibJS/Runtime/Value.h>
  34. namespace JS {
  35. static Date* this_date_from_interpreter(Interpreter& interpreter)
  36. {
  37. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  38. if (!this_object)
  39. return nullptr;
  40. if (!this_object->is_date()) {
  41. interpreter.throw_exception<Error>("TypeError", "object must be of type Date");
  42. return nullptr;
  43. }
  44. return static_cast<Date*>(this_object);
  45. }
  46. DatePrototype::DatePrototype()
  47. {
  48. put_native_function("getDate", get_date);
  49. put_native_function("getDay", get_day);
  50. put_native_function("getFullYear", get_full_year);
  51. put_native_function("getHours", get_hours);
  52. put_native_function("getMilliseconds", get_milliseconds);
  53. put_native_function("getMinutes", get_minutes);
  54. put_native_function("getMonth", get_month);
  55. put_native_function("getSeconds", get_seconds);
  56. put_native_function("getTime", get_time);
  57. put_native_function("toString", to_string);
  58. }
  59. DatePrototype::~DatePrototype()
  60. {
  61. }
  62. Value DatePrototype::get_date(Interpreter& interpreter)
  63. {
  64. auto* this_object = this_date_from_interpreter(interpreter);
  65. if (!this_object)
  66. return {};
  67. auto date = this_object->datetime().day();
  68. return Value(static_cast<double>(date));
  69. }
  70. Value DatePrototype::get_day(Interpreter& interpreter)
  71. {
  72. auto* this_object = this_date_from_interpreter(interpreter);
  73. if (!this_object)
  74. return {};
  75. auto day = this_object->datetime().weekday();
  76. return Value(static_cast<double>(day));
  77. }
  78. Value DatePrototype::get_full_year(Interpreter& interpreter)
  79. {
  80. auto* this_object = this_date_from_interpreter(interpreter);
  81. if (!this_object)
  82. return {};
  83. auto full_year = this_object->datetime().year();
  84. return Value(static_cast<double>(full_year));
  85. }
  86. Value DatePrototype::get_hours(Interpreter& interpreter)
  87. {
  88. auto* this_object = this_date_from_interpreter(interpreter);
  89. if (!this_object)
  90. return {};
  91. auto hours = this_object->datetime().hour();
  92. return Value(static_cast<double>(hours));
  93. }
  94. Value DatePrototype::get_milliseconds(Interpreter& interpreter)
  95. {
  96. auto* this_object = this_date_from_interpreter(interpreter);
  97. if (!this_object)
  98. return {};
  99. auto milliseconds = this_object->milliseconds();
  100. return Value(static_cast<double>(milliseconds));
  101. }
  102. Value DatePrototype::get_minutes(Interpreter& interpreter)
  103. {
  104. auto* this_object = this_date_from_interpreter(interpreter);
  105. if (!this_object)
  106. return {};
  107. auto minutes = this_object->datetime().minute();
  108. return Value(static_cast<double>(minutes));
  109. }
  110. Value DatePrototype::get_month(Interpreter& interpreter)
  111. {
  112. auto* this_object = this_date_from_interpreter(interpreter);
  113. if (!this_object)
  114. return {};
  115. auto months = this_object->datetime().month() - 1;
  116. return Value(static_cast<double>(months));
  117. }
  118. Value DatePrototype::get_seconds(Interpreter& interpreter)
  119. {
  120. auto* this_object = this_date_from_interpreter(interpreter);
  121. if (!this_object)
  122. return {};
  123. auto seconds = this_object->datetime().second();
  124. return Value(static_cast<double>(seconds));
  125. }
  126. Value DatePrototype::get_time(Interpreter& interpreter)
  127. {
  128. auto* this_object = this_date_from_interpreter(interpreter);
  129. if (!this_object)
  130. return {};
  131. auto seconds = this_object->datetime().timestamp();
  132. auto milliseconds = this_object->milliseconds();
  133. return Value(static_cast<double>(seconds * 1000 + milliseconds));
  134. }
  135. Value DatePrototype::to_string(Interpreter& interpreter)
  136. {
  137. auto* this_object = this_date_from_interpreter(interpreter);
  138. if (!this_object)
  139. return {};
  140. // FIXME: Deal with timezones once SerenityOS has a working tzset(3)
  141. auto string = this_object->datetime().to_string("%a %b %d %Y %T GMT+0000 (UTC)");
  142. return js_string(interpreter.heap(), move(string));
  143. }
  144. }