DatePrototype.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/GlobalObject.h>
  34. #include <LibJS/Runtime/Value.h>
  35. namespace JS {
  36. static Date* this_date_from_interpreter(Interpreter& interpreter)
  37. {
  38. auto* this_object = interpreter.this_value(interpreter.global_object()).to_object(interpreter);
  39. if (!this_object)
  40. return nullptr;
  41. if (!this_object->is_date()) {
  42. interpreter.throw_exception<TypeError>(ErrorType::NotA, "Date");
  43. return nullptr;
  44. }
  45. return static_cast<Date*>(this_object);
  46. }
  47. DatePrototype::DatePrototype()
  48. : Object(interpreter().global_object().object_prototype())
  49. {
  50. u8 attr = Attribute::Writable | Attribute::Configurable;
  51. define_native_function("getDate", get_date, 0, attr);
  52. define_native_function("getDay", get_day, 0, attr);
  53. define_native_function("getFullYear", get_full_year, 0, attr);
  54. define_native_function("getHours", get_hours, 0, attr);
  55. define_native_function("getMilliseconds", get_milliseconds, 0, attr);
  56. define_native_function("getMinutes", get_minutes, 0, attr);
  57. define_native_function("getMonth", get_month, 0, attr);
  58. define_native_function("getSeconds", get_seconds, 0, attr);
  59. define_native_function("getTime", get_time, 0, attr);
  60. define_native_function("toDateString", to_date_string, 0, attr);
  61. define_native_function("toTimeString", to_time_string, 0, attr);
  62. define_native_function("toString", to_string, 0, attr);
  63. }
  64. DatePrototype::~DatePrototype()
  65. {
  66. }
  67. Value DatePrototype::get_date(Interpreter& interpreter)
  68. {
  69. auto* this_object = this_date_from_interpreter(interpreter);
  70. if (!this_object)
  71. return {};
  72. auto date = this_object->datetime().day();
  73. return Value(static_cast<double>(date));
  74. }
  75. Value DatePrototype::get_day(Interpreter& interpreter)
  76. {
  77. auto* this_object = this_date_from_interpreter(interpreter);
  78. if (!this_object)
  79. return {};
  80. auto day = this_object->datetime().weekday();
  81. return Value(static_cast<double>(day));
  82. }
  83. Value DatePrototype::get_full_year(Interpreter& interpreter)
  84. {
  85. auto* this_object = this_date_from_interpreter(interpreter);
  86. if (!this_object)
  87. return {};
  88. auto full_year = this_object->datetime().year();
  89. return Value(static_cast<double>(full_year));
  90. }
  91. Value DatePrototype::get_hours(Interpreter& interpreter)
  92. {
  93. auto* this_object = this_date_from_interpreter(interpreter);
  94. if (!this_object)
  95. return {};
  96. auto hours = this_object->datetime().hour();
  97. return Value(static_cast<double>(hours));
  98. }
  99. Value DatePrototype::get_milliseconds(Interpreter& interpreter)
  100. {
  101. auto* this_object = this_date_from_interpreter(interpreter);
  102. if (!this_object)
  103. return {};
  104. auto milliseconds = this_object->milliseconds();
  105. return Value(static_cast<double>(milliseconds));
  106. }
  107. Value DatePrototype::get_minutes(Interpreter& interpreter)
  108. {
  109. auto* this_object = this_date_from_interpreter(interpreter);
  110. if (!this_object)
  111. return {};
  112. auto minutes = this_object->datetime().minute();
  113. return Value(static_cast<double>(minutes));
  114. }
  115. Value DatePrototype::get_month(Interpreter& interpreter)
  116. {
  117. auto* this_object = this_date_from_interpreter(interpreter);
  118. if (!this_object)
  119. return {};
  120. auto months = this_object->datetime().month() - 1;
  121. return Value(static_cast<double>(months));
  122. }
  123. Value DatePrototype::get_seconds(Interpreter& interpreter)
  124. {
  125. auto* this_object = this_date_from_interpreter(interpreter);
  126. if (!this_object)
  127. return {};
  128. auto seconds = this_object->datetime().second();
  129. return Value(static_cast<double>(seconds));
  130. }
  131. Value DatePrototype::get_time(Interpreter& interpreter)
  132. {
  133. auto* this_object = this_date_from_interpreter(interpreter);
  134. if (!this_object)
  135. return {};
  136. auto seconds = this_object->datetime().timestamp();
  137. auto milliseconds = this_object->milliseconds();
  138. return Value(static_cast<double>(seconds * 1000 + milliseconds));
  139. }
  140. Value DatePrototype::to_date_string(Interpreter& interpreter)
  141. {
  142. auto* this_object = this_date_from_interpreter(interpreter);
  143. if (!this_object)
  144. return {};
  145. auto string = this_object->date_string();
  146. return js_string(interpreter, move(string));
  147. }
  148. Value DatePrototype::to_time_string(Interpreter& interpreter)
  149. {
  150. auto* this_object = this_date_from_interpreter(interpreter);
  151. if (!this_object)
  152. return {};
  153. auto string = this_object->time_string();
  154. return js_string(interpreter, move(string));
  155. }
  156. Value DatePrototype::to_string(Interpreter& interpreter)
  157. {
  158. auto* this_object = this_date_from_interpreter(interpreter);
  159. if (!this_object)
  160. return {};
  161. auto string = this_object->string();
  162. return js_string(interpreter, move(string));
  163. }
  164. }