summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-01-11 23:27:05 -0500
committerLinus Groh <mail@linusgroh.de>2022-01-12 15:43:12 +0100
commit8987deb9840dbb8bc7311e964918dfba56857275 (patch)
tree6712228bfdb04f0156019c8c0966a61f7e4d5c34
parentf6786881aabdffd77c82bebdd783ed022c89222b (diff)
downloadserenity-8987deb9840dbb8bc7311e964918dfba56857275.zip
LibJS: Partially implement the LocalTZA AO
Intl.DateTimeFormat will invoke this AO with isUTC=true, so this only implements that branch in LocalTZA.
-rw-r--r--Userland/Libraries/LibJS/Runtime/Date.cpp24
-rw-r--r--Userland/Libraries/LibJS/Runtime/Date.h1
2 files changed, 25 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Date.cpp b/Userland/Libraries/LibJS/Runtime/Date.cpp
index 4eb86742d8..c7dc26131a 100644
--- a/Userland/Libraries/LibJS/Runtime/Date.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Date.cpp
@@ -10,6 +10,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/GlobalObject.h>
+#include <LibTimeZone/TimeZone.h>
#include <time.h>
namespace JS {
@@ -315,6 +316,29 @@ u8 week_day(double t)
return static_cast<u8>(modulo(day(t) + 4, 7));
}
+// 21.4.1.7 LocalTZA ( t, isUTC ), https://tc39.es/ecma262/#sec-local-time-zone-adjustment
+double local_tza(double time, bool is_utc, Optional<StringView> time_zone_override)
+{
+ // The time_zone_override parameter is non-standard, but allows callers to override the system
+ // time zone with a specific value without setting environment variables.
+ auto time_zone = time_zone_override.value_or(TimeZone::current_time_zone());
+
+ // When isUTC is true, LocalTZA( tUTC, true ) should return the offset of the local time zone from
+ // UTC measured in milliseconds at time represented by time value tUTC. When the result is added to
+ // tUTC, it should yield the corresponding Number tlocal.
+ if (is_utc) {
+ auto offset = TimeZone::get_time_zone_offset(time_zone, AK::Time::from_milliseconds(time));
+ return offset.value_or(0) * 1000;
+ }
+
+ // When isUTC is false, LocalTZA( tlocal, false ) should return the offset of the local time zone from
+ // UTC measured in milliseconds at local time represented by Number tlocal. When the result is subtracted
+ // from tlocal, it should yield the corresponding time value tUTC.
+
+ // FIXME: Implement isUTC=false when any caller needs it.
+ TODO();
+}
+
// 21.4.1.11 MakeTime ( hour, min, sec, ms ), https://tc39.es/ecma262/#sec-maketime
Value make_time(GlobalObject& global_object, Value hour, Value min, Value sec, Value ms)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Date.h b/Userland/Libraries/LibJS/Runtime/Date.h
index 33a6502ae9..26a38b083b 100644
--- a/Userland/Libraries/LibJS/Runtime/Date.h
+++ b/Userland/Libraries/LibJS/Runtime/Date.h
@@ -104,6 +104,7 @@ u8 min_from_time(double);
u8 sec_from_time(double);
u16 ms_from_time(double);
u8 week_day(double);
+double local_tza(double time, bool is_utc, Optional<StringView> time_zone_override = {});
double day(double);
Value make_time(GlobalObject& global_object, Value hour, Value min, Value sec, Value ms);
Value make_day(GlobalObject& global_object, Value year, Value month, Value date);