DateConstructor.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <LibCore/DateTime.h>
  27. #include <LibJS/Interpreter.h>
  28. #include <LibJS/Runtime/Date.h>
  29. #include <LibJS/Runtime/DateConstructor.h>
  30. #include <LibJS/Runtime/GlobalObject.h>
  31. #include <sys/time.h>
  32. #include <time.h>
  33. namespace JS {
  34. DateConstructor::DateConstructor(GlobalObject& global_object)
  35. : NativeFunction("Date", *global_object.function_prototype())
  36. {
  37. }
  38. void DateConstructor::initialize(GlobalObject& global_object)
  39. {
  40. NativeFunction::initialize(global_object);
  41. define_property("prototype", global_object.date_prototype(), 0);
  42. define_property("length", Value(7), Attribute::Configurable);
  43. define_native_function("now", now, 0, Attribute::Writable | Attribute::Configurable);
  44. define_native_function("UTC", utc, 1, Attribute::Writable | Attribute::Configurable);
  45. }
  46. DateConstructor::~DateConstructor()
  47. {
  48. }
  49. Value DateConstructor::call(Interpreter& interpreter)
  50. {
  51. auto date = construct(interpreter, *this);
  52. if (!date.is_object())
  53. return {};
  54. return js_string(interpreter, static_cast<Date&>(date.as_object()).string());
  55. }
  56. Value DateConstructor::construct(Interpreter& interpreter, Function&)
  57. {
  58. if (interpreter.argument_count() == 0) {
  59. struct timeval tv;
  60. gettimeofday(&tv, nullptr);
  61. auto datetime = Core::DateTime::now();
  62. auto milliseconds = static_cast<u16>(tv.tv_usec / 1000);
  63. return Date::create(global_object(), datetime, milliseconds);
  64. }
  65. if (interpreter.argument_count() == 1 && interpreter.argument(0).is_string()) {
  66. // FIXME: Parse simplified ISO8601-like string, like Date.parse() will do.
  67. struct timeval tv;
  68. gettimeofday(&tv, nullptr);
  69. auto datetime = Core::DateTime::now();
  70. auto milliseconds = static_cast<u16>(tv.tv_usec / 1000);
  71. return Date::create(global_object(), datetime, milliseconds);
  72. }
  73. if (interpreter.argument_count() == 1) {
  74. // A timestamp since the epoch, in UTC.
  75. // FIXME: Date() probably should use a double as internal representation, so that NaN arguments and larger offsets are handled correctly.
  76. // FIXME: DateTime::from_timestamp() seems to not support negative offsets.
  77. double value = interpreter.argument(0).to_double(interpreter);
  78. auto datetime = Core::DateTime::from_timestamp(static_cast<time_t>(value / 1000));
  79. auto milliseconds = static_cast<u16>(fmod(value, 1000));
  80. return Date::create(global_object(), datetime, milliseconds);
  81. }
  82. // A date/time in components, in local time.
  83. // FIXME: This doesn't construct an "Invalid Date" object if one of the parameters is NaN.
  84. // FIXME: This doesn't range-check args and convert months > 12 to year increments etc.
  85. auto arg_or = [&interpreter](size_t i, i32 fallback) { return interpreter.argument_count() > i ? interpreter.argument(i).to_i32(interpreter) : fallback; };
  86. int year = interpreter.argument(0).to_i32(interpreter);
  87. int month_index = interpreter.argument(1).to_i32(interpreter);
  88. int day = arg_or(2, 1);
  89. int hours = arg_or(3, 0);
  90. int minutes = arg_or(4, 0);
  91. int seconds = arg_or(5, 0);
  92. int milliseconds = arg_or(6, 0);
  93. if (year >= 0 && year <= 99)
  94. year += 1900;
  95. int month = month_index + 1;
  96. auto datetime = Core::DateTime::create(year, month, day, hours, minutes, seconds);
  97. return Date::create(global_object(), datetime, milliseconds);
  98. }
  99. JS_DEFINE_NATIVE_FUNCTION(DateConstructor::now)
  100. {
  101. struct timeval tv;
  102. gettimeofday(&tv, nullptr);
  103. return Value(tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0);
  104. }
  105. JS_DEFINE_NATIVE_FUNCTION(DateConstructor::utc)
  106. {
  107. auto arg_or = [&interpreter](size_t i, i32 fallback) { return interpreter.argument_count() > i ? interpreter.argument(i).to_i32(interpreter) : fallback; };
  108. int year = interpreter.argument(0).to_i32(interpreter);
  109. if (year >= 0 && year <= 99)
  110. year += 1900;
  111. struct tm tm = {};
  112. tm.tm_year = year - 1900;
  113. tm.tm_mon = arg_or(1, 0); // 0-based in both tm and JavaScript
  114. tm.tm_mday = arg_or(2, 1);
  115. tm.tm_hour = arg_or(3, 0);
  116. tm.tm_min = arg_or(4, 0);
  117. tm.tm_sec = arg_or(5, 0);
  118. // timegm() doesn't read tm.tm_wday and tm.tm_yday, no need to fill them in.
  119. int milliseconds = arg_or(6, 0);
  120. return Value(1000.0 * timegm(&tm) + milliseconds);
  121. }
  122. }