2022-04-25 02:10:31 +00:00
|
|
|
// Copyright 2022 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2012-03-09 12:07:29 +00:00
|
|
|
|
2019-05-15 12:52:58 +00:00
|
|
|
#include "src/date/date.h"
|
2022-04-25 02:10:31 +00:00
|
|
|
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/isolate.h"
|
2019-05-22 12:44:24 +00:00
|
|
|
#include "src/handles/global-handles.h"
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/init/v8.h"
|
2022-04-25 02:10:31 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
2012-03-09 12:07:29 +00:00
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
namespace v8 {
|
2022-04-25 02:10:31 +00:00
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
namespace internal {
|
2012-03-09 12:07:29 +00:00
|
|
|
|
2022-04-25 02:10:31 +00:00
|
|
|
class DateTest : public TestWithContext {
|
|
|
|
public:
|
|
|
|
void CheckDST(int64_t time) {
|
|
|
|
DateCache* date_cache = i_isolate()->date_cache();
|
|
|
|
int64_t actual = date_cache->ToLocal(time);
|
|
|
|
int64_t expected = time + date_cache->GetLocalOffsetFromOS(time, true);
|
|
|
|
CHECK_EQ(actual, expected);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DateCacheMock : public DateCache {
|
2012-03-09 12:07:29 +00:00
|
|
|
public:
|
|
|
|
struct Rule {
|
|
|
|
int year, start_month, start_day, end_month, end_day, offset_sec;
|
|
|
|
};
|
|
|
|
|
|
|
|
DateCacheMock(int local_offset, Rule* rules, int rules_count)
|
|
|
|
: local_offset_(local_offset), rules_(rules), rules_count_(rules_count) {}
|
|
|
|
|
|
|
|
protected:
|
2018-09-14 15:34:02 +00:00
|
|
|
int GetDaylightSavingsOffsetFromOS(int64_t time_sec) override {
|
2012-03-09 12:07:29 +00:00
|
|
|
int days = DaysFromTime(time_sec * 1000);
|
|
|
|
int time_in_day_sec = TimeInDay(time_sec * 1000, days) / 1000;
|
|
|
|
int year, month, day;
|
|
|
|
YearMonthDayFromDays(days, &year, &month, &day);
|
|
|
|
Rule* rule = FindRuleFor(year, month, day, time_in_day_sec);
|
2017-10-13 16:33:03 +00:00
|
|
|
return rule == nullptr ? 0 : rule->offset_sec * 1000;
|
2012-03-09 12:07:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 15:34:02 +00:00
|
|
|
int GetLocalOffsetFromOS(int64_t time_sec, bool is_utc) override {
|
Reland "Implement a new spec for timezone offset calculation"
This is a reland of dbdede0101270b4ea332e52544215fe3c4a9bd71
after a webkit layout test (geolocation-api/timestamp.html) was
fixed by
https://chromium-review.googlesource.com/c/chromium/src/+/994343 .
Original change's description:
> Implement a new spec for timezone offset calculation
>
> https://github.com/tc39/ecma262/pull/778 was recently merged
> to Ecma 262.
>
> It changes the way to convert between "local time" and UTC in such
> a way that it'd work for all timezones whether or not there has
> been any change in the timezone offset of the standard time. For
> instance, Europe/Moscow and some parts of US state of Indiana have
> changed the standard (non-DST) timezone offset a few times. The
> previous spec assumes that the the standard timezone offset is
> constant, but the new spec take into account the offset change
> history.
>
> In addition, it specifies a new way to calculate the timezone
> offset during a timezone transition (either in and
> out of DST or timezone offset shift).
>
> During a negative transition (e.g. fall backward / getting
> out of DST), repeated times are to be interpreted as if the
> offset before the transition is in effect.
>
> During a positive transition (e.g. spring forward / getting
> into DST), skipped times are to be treated similarly. That
> is, they are to be interpreted as if the offset before the
> transition is in effect.
>
> With icu-timezone-data, v8 is compliant to the new spec for the
> past and the future as well as now whether or not the standard
> timezone offset of a given timezone has changed over time
> (e.g. Europe/Moscow, Pacific/Apia). With icu-timezone-data,
> Australia/Lord_Howe (30 minute DST change) also works per spec.
>
> Without icu-timezone-data, it works only for timezones of which
> the standard timezone offset is the same as the current offset
> (e.g. most North American timezones other than parts of Indiana)
> and of which the DST shift is an hour. For instance, it doesn't work
> for Europe/Moscow in 2010 when the standard timezone offset was
> +4h because the current (2018) standard timezone offset is +3h. Neither
> does it for Lord Howe in Australia with the DST shift of 0.5 hr.
>
> This CL used to require one of the two ICU CLs below, but not
> any more.
>
> https://chromium-review.googlesource.com/c/chromium/deps/icu/+/572652
> https://chromium-review.googlesource.com/851265 (a proposed CL to the
> upstream ICU).
>
> Bug: v8:3547,chromium:417640,v8:5714
> Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
> Change-Id: Ib162295da5bee31b2390bd0918157014aebd3e33
> Reviewed-on: https://chromium-review.googlesource.com/572148
> Commit-Queue: Jungshik Shin <jshin@chromium.org>
> Reviewed-by: Daniel Ehrenberg <littledan@chromium.org>
> Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#52332}
Bug: v8:3547, chromium:417640, v8:5714
Change-Id: I47536c111143f75e3cfeecf5d9761c43a98a10f5
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng;master.tryserver.blink:linux_trusty_blink_rel
Reviewed-on: https://chromium-review.googlesource.com/995971
Commit-Queue: Jungshik Shin <jshin@chromium.org>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52372}
2018-04-03 17:18:38 +00:00
|
|
|
return local_offset_ + GetDaylightSavingsOffsetFromOS(time_sec);
|
2012-03-09 12:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Rule* FindRuleFor(int year, int month, int day, int time_in_day_sec) {
|
2017-10-13 16:33:03 +00:00
|
|
|
Rule* result = nullptr;
|
2012-03-09 12:07:29 +00:00
|
|
|
for (int i = 0; i < rules_count_; i++)
|
|
|
|
if (Match(&rules_[i], year, month, day, time_in_day_sec)) {
|
|
|
|
result = &rules_[i];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Match(Rule* rule, int year, int month, int day, int time_in_day_sec) {
|
|
|
|
if (rule->year != 0 && rule->year != year) return false;
|
|
|
|
if (rule->start_month > month) return false;
|
|
|
|
if (rule->end_month < month) return false;
|
|
|
|
int start_day = ComputeRuleDay(year, rule->start_month, rule->start_day);
|
|
|
|
if (rule->start_month == month && start_day > day) return false;
|
|
|
|
if (rule->start_month == month && start_day == day &&
|
|
|
|
2 * 3600 > time_in_day_sec)
|
|
|
|
return false;
|
|
|
|
int end_day = ComputeRuleDay(year, rule->end_month, rule->end_day);
|
|
|
|
if (rule->end_month == month && end_day < day) return false;
|
|
|
|
if (rule->end_month == month && end_day == day &&
|
|
|
|
2 * 3600 <= time_in_day_sec)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ComputeRuleDay(int year, int month, int day) {
|
|
|
|
if (day != 0) return day;
|
|
|
|
int days = DaysFromYearMonth(year, month);
|
|
|
|
// Find the first Sunday of the month.
|
|
|
|
while (Weekday(days + day) != 6) day++;
|
|
|
|
return day + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int local_offset_;
|
|
|
|
Rule* rules_;
|
|
|
|
int rules_count_;
|
|
|
|
};
|
|
|
|
|
2022-04-25 02:10:31 +00:00
|
|
|
static int64_t TimeFromYearMonthDay(DateCache* date_cache, int year, int month,
|
2012-03-09 12:07:29 +00:00
|
|
|
int day) {
|
|
|
|
int64_t result = date_cache->DaysFromYearMonth(year, month);
|
|
|
|
return (result + day - 1) * DateCache::kMsPerDay;
|
|
|
|
}
|
|
|
|
|
2022-04-25 02:10:31 +00:00
|
|
|
TEST_F(DateTest, DaylightSavingsTime) {
|
|
|
|
v8::HandleScope scope(isolate());
|
2012-03-09 12:07:29 +00:00
|
|
|
DateCacheMock::Rule rules[] = {
|
2022-04-25 02:10:31 +00:00
|
|
|
{0, 2, 0, 10, 0, 3600}, // DST from March to November in any year.
|
|
|
|
{2010, 2, 0, 7, 20, 3600}, // DST from March to August 20 in 2010.
|
|
|
|
{2010, 7, 20, 8, 10,
|
|
|
|
0}, // No DST from August 20 to September 10 in 2010.
|
|
|
|
{2010, 8, 10, 10, 0, 3600}, // DST from September 10 to November in 2010.
|
2012-03-09 12:07:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int local_offset_ms = -36000000; // -10 hours.
|
|
|
|
|
|
|
|
DateCacheMock* date_cache =
|
2022-04-25 02:10:31 +00:00
|
|
|
new DateCacheMock(local_offset_ms, rules, arraysize(rules));
|
2012-03-09 12:07:29 +00:00
|
|
|
|
2022-04-25 02:10:31 +00:00
|
|
|
reinterpret_cast<Isolate*>(isolate())->set_date_cache(date_cache);
|
2012-03-09 12:07:29 +00:00
|
|
|
|
|
|
|
int64_t start_of_2010 = TimeFromYearMonthDay(date_cache, 2010, 0, 1);
|
|
|
|
int64_t start_of_2011 = TimeFromYearMonthDay(date_cache, 2011, 0, 1);
|
|
|
|
int64_t august_20 = TimeFromYearMonthDay(date_cache, 2010, 7, 20);
|
|
|
|
int64_t september_10 = TimeFromYearMonthDay(date_cache, 2010, 8, 10);
|
|
|
|
CheckDST((august_20 + september_10) / 2);
|
|
|
|
CheckDST(september_10);
|
|
|
|
CheckDST(september_10 + 2 * 3600);
|
|
|
|
CheckDST(september_10 + 2 * 3600 - 1000);
|
|
|
|
CheckDST(august_20 + 2 * 3600);
|
|
|
|
CheckDST(august_20 + 2 * 3600 - 1000);
|
|
|
|
CheckDST(august_20);
|
|
|
|
// Check each day of 2010.
|
2022-04-25 02:10:31 +00:00
|
|
|
for (int64_t time = start_of_2011 + 2 * 3600; time >= start_of_2010;
|
2012-03-09 12:07:29 +00:00
|
|
|
time -= DateCache::kMsPerDay) {
|
|
|
|
CheckDST(time);
|
|
|
|
CheckDST(time - 1000);
|
|
|
|
CheckDST(time + 1000);
|
|
|
|
}
|
|
|
|
// Check one day from 2010 to 2100.
|
|
|
|
for (int year = 2100; year >= 2010; year--) {
|
|
|
|
CheckDST(TimeFromYearMonthDay(date_cache, year, 5, 5));
|
|
|
|
}
|
|
|
|
CheckDST((august_20 + september_10) / 2);
|
|
|
|
CheckDST(september_10);
|
|
|
|
CheckDST(september_10 + 2 * 3600);
|
|
|
|
CheckDST(september_10 + 2 * 3600 - 1000);
|
|
|
|
CheckDST(august_20 + 2 * 3600);
|
|
|
|
CheckDST(august_20 + 2 * 3600 - 1000);
|
|
|
|
CheckDST(august_20);
|
|
|
|
}
|
2016-05-20 17:47:21 +00:00
|
|
|
|
2016-06-22 18:06:25 +00:00
|
|
|
namespace {
|
|
|
|
int legacy_parse_count = 0;
|
|
|
|
void DateParseLegacyCounterCallback(v8::Isolate* isolate,
|
|
|
|
v8::Isolate::UseCounterFeature feature) {
|
|
|
|
if (feature == v8::Isolate::kLegacyDateParser) legacy_parse_count++;
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2022-04-25 02:10:31 +00:00
|
|
|
TEST_F(DateTest, DateParseLegacyUseCounter) {
|
|
|
|
v8::HandleScope scope(isolate());
|
|
|
|
isolate()->SetUseCounterCallback(DateParseLegacyCounterCallback);
|
2016-06-22 18:06:25 +00:00
|
|
|
CHECK_EQ(0, legacy_parse_count);
|
2022-04-25 02:10:31 +00:00
|
|
|
RunJS("Date.parse('2015-02-31')");
|
2016-06-22 18:06:25 +00:00
|
|
|
CHECK_EQ(0, legacy_parse_count);
|
2022-04-25 02:10:31 +00:00
|
|
|
RunJS("Date.parse('2015-02-31T11:22:33.444Z01:23')");
|
2016-06-22 18:06:25 +00:00
|
|
|
CHECK_EQ(0, legacy_parse_count);
|
2022-04-25 02:10:31 +00:00
|
|
|
RunJS("Date.parse('2015-02-31T11:22:33.444')");
|
2016-06-22 18:06:25 +00:00
|
|
|
CHECK_EQ(0, legacy_parse_count);
|
2022-04-25 02:10:31 +00:00
|
|
|
RunJS("Date.parse('2000 01 01')");
|
2016-06-22 18:06:25 +00:00
|
|
|
CHECK_EQ(1, legacy_parse_count);
|
2022-04-25 02:10:31 +00:00
|
|
|
RunJS("Date.parse('2015-02-31T11:22:33.444 ')");
|
2016-06-22 18:06:25 +00:00
|
|
|
CHECK_EQ(1, legacy_parse_count);
|
|
|
|
}
|
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|