-
Notifications
You must be signed in to change notification settings - Fork 54
/
DateTime.cpp
818 lines (691 loc) · 26.9 KB
/
DateTime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "vm/DateTime.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/TextUtils.h"
#include "mozilla/Unused.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <time.h>
#if !defined(XP_WIN)
#include <limits.h>
#include <unistd.h>
#endif /* !defined(XP_WIN) */
#include "jsutil.h"
#include "js/Date.h"
#include "threading/ExclusiveData.h"
#if ENABLE_INTL_API && !MOZ_SYSTEM_ICU
#include "unicode/basictz.h"
#include "unicode/locid.h"
#endif /* ENABLE_INTL_API && !MOZ_SYSTEM_ICU */
#if ENABLE_INTL_API && (!MOZ_SYSTEM_ICU || defined(ICU_TZ_HAS_RECREATE_DEFAULT))
#include "unicode/timezone.h"
#include "unicode/unistr.h"
#endif /* ENABLE_INTL_API && (!MOZ_SYSTEM_ICU || defined(ICU_TZ_HAS_RECREATE_DEFAULT)) */
#include "util/Text.h"
#include "vm/MutexIDs.h"
static bool
ComputeLocalTime(time_t local, struct tm* ptm)
{
#if defined(_WIN32)
return localtime_s(ptm, &local) == 0;
#elif defined(HAVE_LOCALTIME_R)
return localtime_r(&local, ptm);
#else
struct tm* otm = localtime(&local);
if (!otm) {
return false;
}
*ptm = *otm;
return true;
#endif
}
static bool
ComputeUTCTime(time_t t, struct tm* ptm)
{
#if defined(_WIN32)
return gmtime_s(ptm, &t) == 0;
#elif defined(HAVE_GMTIME_R)
return gmtime_r(&t, ptm);
#else
struct tm* otm = gmtime(&t);
if (!otm) {
return false;
}
*ptm = *otm;
return true;
#endif
}
/*
* Compute the offset in seconds from the current UTC time to the current local
* standard time (i.e. not including any offset due to DST).
*
* Examples:
*
* Suppose we are in California, USA on January 1, 2013 at 04:00 PST (UTC-8, no
* DST in effect), corresponding to 12:00 UTC. This function would then return
* -8 * SecondsPerHour, or -28800.
*
* Or suppose we are in Berlin, Germany on July 1, 2013 at 17:00 CEST (UTC+2,
* DST in effect), corresponding to 15:00 UTC. This function would then return
* +1 * SecondsPerHour, or +3600.
*/
static int32_t
UTCToLocalStandardOffsetSeconds()
{
using js::SecondsPerDay;
using js::SecondsPerHour;
using js::SecondsPerMinute;
// Get the current time.
time_t currentMaybeWithDST = time(nullptr);
if (currentMaybeWithDST == time_t(-1)) {
return 0;
}
// Break down the current time into its (locally-valued, maybe with DST)
// components.
struct tm local;
if (!ComputeLocalTime(currentMaybeWithDST, &local)) {
return 0;
}
// Compute a |time_t| corresponding to |local| interpreted without DST.
time_t currentNoDST;
if (local.tm_isdst == 0) {
// If |local| wasn't DST, we can use the same time.
currentNoDST = currentMaybeWithDST;
} else {
// If |local| respected DST, we need a time broken down into components
// ignoring DST. Turn off DST in the broken-down time. Create a fresh
// copy of |local|, because mktime() will reset tm_isdst = 1 and will
// adjust tm_hour and tm_hour accordingly.
struct tm localNoDST = local;
localNoDST.tm_isdst = 0;
// Compute a |time_t t| corresponding to the broken-down time with DST
// off. This has boundary-condition issues (for about the duration of
// a DST offset) near the time a location moves to a different time
// zone. But 1) errors will be transient; 2) locations rarely change
// time zone; and 3) in the absence of an API that provides the time
// zone offset directly, this may be the best we can do.
currentNoDST = mktime(&localNoDST);
if (currentNoDST == time_t(-1)) {
return 0;
}
}
// Break down the time corresponding to the no-DST |local| into UTC-based
// components.
struct tm utc;
if (!ComputeUTCTime(currentNoDST, &utc)) {
return 0;
}
// Finally, compare the seconds-based components of the local non-DST
// representation and the UTC representation to determine the actual
// difference.
int utc_secs = utc.tm_hour * SecondsPerHour + utc.tm_min * SecondsPerMinute;
int local_secs = local.tm_hour * SecondsPerHour + local.tm_min * SecondsPerMinute;
// Same-day? Just subtract the seconds counts.
if (utc.tm_mday == local.tm_mday) {
return local_secs - utc_secs;
}
// If we have more UTC seconds, move local seconds into the UTC seconds'
// frame of reference and then subtract.
if (utc_secs > local_secs) {
return (SecondsPerDay + local_secs) - utc_secs;
}
// Otherwise we have more local seconds, so move the UTC seconds into the
// local seconds' frame of reference and then subtract.
return local_secs - (utc_secs + SecondsPerDay);
}
bool
js::DateTimeInfo::internalUpdateTimeZoneAdjustment(ResetTimeZoneMode mode)
{
/*
* The difference between local standard time and UTC will never change for
* a given time zone.
*/
utcToLocalStandardOffsetSeconds_ = UTCToLocalStandardOffsetSeconds();
int32_t newTZA = utcToLocalStandardOffsetSeconds_ * msPerSecond;
if (mode == ResetTimeZoneMode::DontResetIfOffsetUnchanged && newTZA == localTZA_) {
return false;
}
localTZA_ = newTZA;
dstRange_.reset();
#if ENABLE_INTL_API && !MOZ_SYSTEM_ICU
utcRange_.reset();
localRange_.reset();
{
// Tell the analysis the |pFree| function pointer called by uprv_free
// cannot GC.
JS::AutoSuppressGCAnalysis nogc;
timeZone_ = nullptr;
}
standardName_ = nullptr;
daylightSavingsName_ = nullptr;
#endif /* ENABLE_INTL_API && !MOZ_SYSTEM_ICU */
return true;
}
js::DateTimeInfo::DateTimeInfo()
{
internalUpdateTimeZoneAdjustment(ResetTimeZoneMode::ResetEvenIfOffsetUnchaged);
}
js::DateTimeInfo::~DateTimeInfo() = default;
int64_t
js::DateTimeInfo::toClampedSeconds(int64_t milliseconds)
{
int64_t seconds = milliseconds / msPerSecond;
if (seconds > MaxTimeT) {
seconds = MaxTimeT;
} else if (seconds < MinTimeT) {
/* Go ahead a day to make localtime work (does not work with 0). */
seconds = SecondsPerDay;
}
return seconds;
}
int32_t
js::DateTimeInfo::computeDSTOffsetMilliseconds(int64_t utcSeconds)
{
MOZ_ASSERT(utcSeconds >= MinTimeT);
MOZ_ASSERT(utcSeconds <= MaxTimeT);
#if ENABLE_INTL_API && !MOZ_SYSTEM_ICU
UDate date = UDate(utcSeconds * msPerSecond);
constexpr bool dateIsLocalTime = false;
int32_t rawOffset, dstOffset;
UErrorCode status = U_ZERO_ERROR;
timeZone()->getOffset(date, dateIsLocalTime, rawOffset, dstOffset, status);
if (U_FAILURE(status)) {
return 0;
}
return dstOffset;
#else
struct tm tm;
if (!ComputeLocalTime(static_cast<time_t>(utcSeconds), &tm)) {
return 0;
}
// NB: The offset isn't computed correctly when the standard local offset
// at |utcSeconds| is different from |utcToLocalStandardOffsetSeconds|.
int32_t dayoff = int32_t((utcSeconds + utcToLocalStandardOffsetSeconds_) % SecondsPerDay);
int32_t tmoff = tm.tm_sec + (tm.tm_min * SecondsPerMinute) + (tm.tm_hour * SecondsPerHour);
int32_t diff = tmoff - dayoff;
if (diff < 0) {
diff += SecondsPerDay;
} else if (uint32_t(diff) >= SecondsPerDay) {
diff -= SecondsPerDay;
}
return diff * msPerSecond;
#endif /* ENABLE_INTL_API && !MOZ_SYSTEM_ICU */
}
int32_t
js::DateTimeInfo::internalGetDSTOffsetMilliseconds(int64_t utcMilliseconds)
{
int64_t utcSeconds = toClampedSeconds(utcMilliseconds);
return getOrComputeValue(dstRange_, utcSeconds, &DateTimeInfo::computeDSTOffsetMilliseconds);
}
int32_t
js::DateTimeInfo::getOrComputeValue(RangeCache& range, int64_t seconds, ComputeFn compute)
{
range.sanityCheck();
auto checkSanity = mozilla::MakeScopeExit([&range]() {
range.sanityCheck();
});
// NB: Be aware of the initial range values when making changes to this
// code: the first call to this method, with those initial range
// values, must result in a cache miss.
MOZ_ASSERT(seconds != INT64_MIN);
if (range.startSeconds <= seconds && seconds <= range.endSeconds) {
return range.offsetMilliseconds;
}
if (range.oldStartSeconds <= seconds && seconds <= range.oldEndSeconds) {
return range.oldOffsetMilliseconds;
}
range.oldOffsetMilliseconds = range.offsetMilliseconds;
range.oldStartSeconds = range.startSeconds;
range.oldEndSeconds = range.endSeconds;
if (range.startSeconds <= seconds) {
int64_t newEndSeconds = Min(range.endSeconds + RangeExpansionAmount, MaxTimeT);
if (newEndSeconds >= seconds) {
int32_t endOffsetMilliseconds = (this->*compute)(newEndSeconds);
if (endOffsetMilliseconds == range.offsetMilliseconds) {
range.endSeconds = newEndSeconds;
return range.offsetMilliseconds;
}
range.offsetMilliseconds = (this->*compute)(seconds);
if (range.offsetMilliseconds == endOffsetMilliseconds) {
range.startSeconds = seconds;
range.endSeconds = newEndSeconds;
} else {
range.endSeconds = seconds;
}
return range.offsetMilliseconds;
}
range.offsetMilliseconds = (this->*compute)(seconds);
range.startSeconds = range.endSeconds = seconds;
return range.offsetMilliseconds;
}
int64_t newStartSeconds = Max<int64_t>(range.startSeconds - RangeExpansionAmount, MinTimeT);
if (newStartSeconds <= seconds) {
int32_t startOffsetMilliseconds = (this->*compute)(newStartSeconds);
if (startOffsetMilliseconds == range.offsetMilliseconds) {
range.startSeconds = newStartSeconds;
return range.offsetMilliseconds;
}
range.offsetMilliseconds = (this->*compute)(seconds);
if (range.offsetMilliseconds == startOffsetMilliseconds) {
range.startSeconds = newStartSeconds;
range.endSeconds = seconds;
} else {
range.startSeconds = seconds;
}
return range.offsetMilliseconds;
}
range.startSeconds = range.endSeconds = seconds;
range.offsetMilliseconds = (this->*compute)(seconds);
return range.offsetMilliseconds;
}
void
js::DateTimeInfo::RangeCache::reset()
{
// The initial range values are carefully chosen to result in a cache miss
// on first use given the range of possible values. Be careful to keep
// these values and the caching algorithm in sync!
offsetMilliseconds = 0;
startSeconds = endSeconds = INT64_MIN;
oldOffsetMilliseconds = 0;
oldStartSeconds = oldEndSeconds = INT64_MIN;
sanityCheck();
}
void
js::DateTimeInfo::RangeCache::sanityCheck()
{
auto assertRange = [](int64_t start, int64_t end) {
MOZ_ASSERT(start <= end);
MOZ_ASSERT_IF(start == INT64_MIN, end == INT64_MIN);
MOZ_ASSERT_IF(end == INT64_MIN, start == INT64_MIN);
MOZ_ASSERT_IF(start != INT64_MIN,
start >= MinTimeT && end >= MinTimeT);
MOZ_ASSERT_IF(start != INT64_MIN,
start <= MaxTimeT && end <= MaxTimeT);
};
assertRange(startSeconds, endSeconds);
assertRange(oldStartSeconds, oldEndSeconds);
}
#if ENABLE_INTL_API && !MOZ_SYSTEM_ICU
int32_t
js::DateTimeInfo::computeUTCOffsetMilliseconds(int64_t localSeconds)
{
MOZ_ASSERT(localSeconds >= MinTimeT);
MOZ_ASSERT(localSeconds <= MaxTimeT);
UDate date = UDate(localSeconds * msPerSecond);
// ES2019 draft rev 0ceb728a1adbffe42b26972a6541fd7f398b1557
//
// 20.3.1.7 LocalTZA
//
// If |localSeconds| represents either a skipped (at a positive time zone
// transition) or repeated (at a negative time zone transition) locale
// time, it must be interpreted as a time value before the transition.
constexpr int32_t skippedTime = icu::BasicTimeZone::kFormer;
constexpr int32_t repeatedTime = icu::BasicTimeZone::kFormer;
int32_t rawOffset, dstOffset;
UErrorCode status = U_ZERO_ERROR;
// All ICU TimeZone classes derive from BasicTimeZone, so we can safely
// perform the static_cast.
// Once <https://unicode-org.atlassian.net/browse/ICU-13705> is fixed we
// can remove this extra cast.
auto* basicTz = static_cast<icu::BasicTimeZone*>(timeZone());
basicTz->getOffsetFromLocal(date, skippedTime, repeatedTime, rawOffset, dstOffset, status);
if (U_FAILURE(status)) {
return 0;
}
return rawOffset + dstOffset;
}
int32_t
js::DateTimeInfo::computeLocalOffsetMilliseconds(int64_t utcSeconds)
{
MOZ_ASSERT(utcSeconds >= MinTimeT);
MOZ_ASSERT(utcSeconds <= MaxTimeT);
UDate date = UDate(utcSeconds * msPerSecond);
constexpr bool dateIsLocalTime = false;
int32_t rawOffset, dstOffset;
UErrorCode status = U_ZERO_ERROR;
timeZone()->getOffset(date, dateIsLocalTime, rawOffset, dstOffset, status);
if (U_FAILURE(status)) {
return 0;
}
return rawOffset + dstOffset;
}
int32_t
js::DateTimeInfo::internalGetOffsetMilliseconds(int64_t milliseconds, TimeZoneOffset offset)
{
int64_t seconds = toClampedSeconds(milliseconds);
return offset == TimeZoneOffset::UTC
? getOrComputeValue(localRange_, seconds, &DateTimeInfo::computeLocalOffsetMilliseconds)
: getOrComputeValue(utcRange_, seconds, &DateTimeInfo::computeUTCOffsetMilliseconds);
}
bool
js::DateTimeInfo::internalTimeZoneDisplayName(char16_t* buf, size_t buflen,
int64_t utcMilliseconds, const char* locale)
{
MOZ_ASSERT(buf != nullptr);
MOZ_ASSERT(buflen > 0);
MOZ_ASSERT(locale != nullptr);
// Clear any previously cached names when the default locale changed.
if (!locale_ || std::strcmp(locale_.get(), locale) != 0) {
locale_ = DuplicateString(locale);
if (!locale_) {
return false;
}
standardName_.reset();
daylightSavingsName_.reset();
}
bool daylightSavings = internalGetDSTOffsetMilliseconds(utcMilliseconds) != 0;
JS::UniqueTwoByteChars& cachedName = daylightSavings ? daylightSavingsName_ : standardName_;
if (!cachedName) {
// Retrieve the display name for the given locale.
icu::UnicodeString displayName;
timeZone()->getDisplayName(daylightSavings, icu::TimeZone::LONG, icu::Locale(locale),
displayName);
size_t capacity = displayName.length() + 1; // Null-terminate.
JS::UniqueTwoByteChars displayNameChars(js_pod_malloc<char16_t>(capacity));
if (!displayNameChars) {
return false;
}
// Copy the display name. This operation always succeeds because the
// destination buffer is large enough to hold the complete string.
UErrorCode status = U_ZERO_ERROR;
displayName.extract(displayNameChars.get(), capacity, status);
MOZ_ASSERT(U_SUCCESS(status));
MOZ_ASSERT(displayNameChars[capacity - 1] == '\0');
cachedName = std::move(displayNameChars);
}
// Return an empty string if the display name doesn't fit into the buffer.
size_t length = js_strlen(cachedName.get());
if (length < buflen) {
std::copy(cachedName.get(), cachedName.get() + length, buf);
} else {
length = 0;
}
buf[length] = '\0';
return true;
}
icu::TimeZone*
js::DateTimeInfo::timeZone()
{
if (!timeZone_) {
// The current default might be stale, because JS::ResetTimeZone()
// doesn't immediately update ICU's default time zone. So perform an
// update if needed.
js::ResyncICUDefaultTimeZone();
timeZone_.reset(icu::TimeZone::createDefault());
MOZ_ASSERT(timeZone_);
}
return timeZone_.get();
}
#endif /* ENABLE_INTL_API && !MOZ_SYSTEM_ICU */
/* static */ js::ExclusiveData<js::DateTimeInfo>*
js::DateTimeInfo::instance;
/* static */ js::ExclusiveData<js::IcuTimeZoneStatus>*
js::IcuTimeZoneState;
bool
js::InitDateTimeState()
{
MOZ_ASSERT(!DateTimeInfo::instance,
"we should be initializing only once");
DateTimeInfo::instance = js_new<ExclusiveData<DateTimeInfo>>(mutexid::DateTimeInfoMutex);
if (!DateTimeInfo::instance) {
return false;
}
MOZ_ASSERT(!IcuTimeZoneState,
"we should be initializing only once");
// Set the ICU time zone status into the invalid state, so we compute the
// actual defaults on first access. We don't yet want to initialize ICU's
// time zone classes, because that may cause I/O operations slowing down
// the JS engine initialization, which we're currently in the middle of.
IcuTimeZoneState = js_new<ExclusiveData<IcuTimeZoneStatus>>(mutexid::IcuTimeZoneStateMutex,
IcuTimeZoneStatus::NeedsUpdate);
if (!IcuTimeZoneState) {
js_delete(DateTimeInfo::instance);
DateTimeInfo::instance = nullptr;
return false;
}
return true;
}
/* static */ void
js::FinishDateTimeState()
{
js_delete(IcuTimeZoneState);
IcuTimeZoneState = nullptr;
js_delete(DateTimeInfo::instance);
DateTimeInfo::instance = nullptr;
}
void
js::ResetTimeZoneInternal(ResetTimeZoneMode mode)
{
bool needsUpdate = js::DateTimeInfo::updateTimeZoneAdjustment(mode);
#if ENABLE_INTL_API && defined(ICU_TZ_HAS_RECREATE_DEFAULT)
if (needsUpdate) {
auto guard = js::IcuTimeZoneState->lock();
guard.get() = js::IcuTimeZoneStatus::NeedsUpdate;
}
#else
mozilla::Unused << needsUpdate;
#endif
}
JS_PUBLIC_API(void)
JS::ResetTimeZone()
{
js::ResetTimeZoneInternal(js::ResetTimeZoneMode::ResetEvenIfOffsetUnchaged);
}
#if defined(XP_WIN)
static bool
IsOlsonCompatibleWindowsTimeZoneId(const char* tz)
{
// ICU ignores the TZ environment variable on Windows and instead directly
// invokes Win API functions to retrieve the current time zone. But since
// we're still using the POSIX-derived localtime_s() function on Windows
// and localtime_s() does return a time zone adjusted value based on the
// TZ environment variable, we need to manually adjust the default ICU
// time zone if TZ is set.
//
// Windows supports the following format for TZ: tzn[+|-]hh[:mm[:ss]][dzn]
// where "tzn" is the time zone name for standard time, the time zone
// offset is positive for time zones west of GMT, and "dzn" is the
// optional time zone name when daylight savings are observed. Daylight
// savings are always based on the U.S. daylight saving rules, that means
// for example it's not possible to use "TZ=CET-1CEST" to select the IANA
// time zone "CET".
//
// When comparing this restricted format for TZ to all IANA time zone
// names, the following time zones are in the intersection of what's
// supported by Windows and is also a valid IANA time zone identifier.
//
// Even though the time zone offset is marked as mandatory on MSDN, it
// appears it defaults to zero when omitted. This in turn means we can
// also allow the time zone identifiers "UCT", "UTC", and "GMT".
static const char* const allowedIds[] = {
// From tzdata's "northamerica" file:
"EST5EDT",
"CST6CDT",
"MST7MDT",
"PST8PDT",
// From tzdata's "backward" file:
"GMT+0",
"GMT-0",
"GMT0",
"UCT",
"UTC",
// From tzdata's "etcetera" file:
"GMT",
};
for (const auto& allowedId : allowedIds) {
if (std::strcmp(allowedId, tz) == 0) {
return true;
}
}
return false;
}
#elif ENABLE_INTL_API && defined(ICU_TZ_HAS_RECREATE_DEFAULT)
static inline const char*
TZContainsAbsolutePath(const char* tzVar)
{
// A TZ environment variable may be an absolute path. The path
// format of TZ may begin with a colon. (ICU handles relative paths.)
if (tzVar[0] == ':' && tzVar[1] == '/') {
return tzVar + 1;
}
if (tzVar[0] == '/') {
return tzVar;
}
return nullptr;
}
/**
* Given a presumptive path |tz| to a zoneinfo time zone file
* (e.g. /etc/localtime), attempt to compute the time zone encoded by that
* path by repeatedly resolving symlinks until a path containing "/zoneinfo/"
* followed by time zone looking components is found. If a symlink is broken,
* symlink-following recurs too deeply, non time zone looking components are
* encountered, or some other error is encountered, return the empty string.
*
* If a non-empty string is returned, it's only guaranteed to have certain
* syntactic validity. It might not actually *be* a time zone name.
*/
static icu::UnicodeString
ReadTimeZoneLink(const char* tz)
{
// The resolved link name can have different paths depending on the OS.
// Follow ICU and only search for "/zoneinfo/"; see $ICU/common/putil.cpp.
static constexpr char ZoneInfoPath[] = "/zoneinfo/";
constexpr size_t ZoneInfoPathLength = mozilla::ArrayLength(ZoneInfoPath) - 1; // exclude NUL
// Stop following symlinks after a fixed depth, because some common time
// zones are stored in files whose name doesn't match an Olson time zone
// name. For example on Ubuntu, "/usr/share/zoneinfo/America/New_York" is a
// symlink to "/usr/share/zoneinfo/posixrules" and "posixrules" is not an
// Olson time zone name.
// Four hops should be a reasonable limit for most use cases.
constexpr uint32_t FollowDepthLimit = 4;
#ifdef PATH_MAX
constexpr size_t PathMax = PATH_MAX;
#else
constexpr size_t PathMax = 4096;
#endif
static_assert(PathMax > 0, "PathMax should be larger than zero");
char linkName[PathMax];
constexpr size_t linkNameLen = mozilla::ArrayLength(linkName) - 1; // -1 to null-terminate.
// Return if the TZ value is too large.
if (std::strlen(tz) > linkNameLen) {
return icu::UnicodeString();
}
std::strcpy(linkName, tz);
char linkTarget[PathMax];
constexpr size_t linkTargetLen = mozilla::ArrayLength(linkTarget) - 1; // -1 to null-terminate.
uint32_t depth = 0;
// Search until we find "/zoneinfo/" in the link name.
const char* timeZoneWithZoneInfo;
while (!(timeZoneWithZoneInfo = std::strstr(linkName, ZoneInfoPath))) {
// Return if the symlink nesting is too deep.
if (++depth > FollowDepthLimit) {
return icu::UnicodeString();
}
// Return on error or if the result was truncated.
ssize_t slen = readlink(linkName, linkTarget, linkTargetLen);
if (slen < 0 || size_t(slen) >= linkTargetLen) {
return icu::UnicodeString();
}
// Ensure linkTarget is null-terminated. (readlink may not necessarily
// null-terminate the string.)
size_t len = size_t(slen);
linkTarget[len] = '\0';
// If the target is absolute, continue with that.
if (linkTarget[0] == '/') {
std::strcpy(linkName, linkTarget);
continue;
}
// If the target is relative, it must be resolved against either the
// directory the link was in, or against the current working directory.
char* separator = std::strrchr(linkName, '/');
// If the link name is just something like "foo", resolve linkTarget
// against the current working directory.
if (!separator) {
std::strcpy(linkName, linkTarget);
continue;
}
// Remove everything after the final path separator in linkName.
separator[1] = '\0';
// Return if the concatenated path name is too large.
if (std::strlen(linkName) + len > linkNameLen) {
return icu::UnicodeString();
}
// Keep it simple and just concatenate the path names.
std::strcat(linkName, linkTarget);
}
const char* timeZone = timeZoneWithZoneInfo + ZoneInfoPathLength;
size_t timeZoneLen = std::strlen(timeZone);
// Reject the result if it doesn't match the time zone id pattern or
// legacy time zone names.
// See <https://github.com/eggert/tz/blob/master/theory.html>.
for (size_t i = 0; i < timeZoneLen; i++) {
char c = timeZone[i];
// According to theory.html, '.' is allowed in time zone ids, but the
// accompanying zic.c file doesn't allow it. Assume the source file is
// correct and disallow '.' here, too.
if (mozilla::IsAsciiAlphanumeric(c) || c == '_' || c == '-' || c == '+') {
continue;
}
// Reject leading, trailing, or consecutive '/' characters.
if (c == '/' && i > 0 && i + 1 < timeZoneLen && timeZone[i + 1] != '/') {
continue;
}
return icu::UnicodeString();
}
return icu::UnicodeString(timeZone, timeZoneLen, US_INV);
}
#endif /* ENABLE_INTL_API && defined(ICU_TZ_HAS_RECREATE_DEFAULT) */
void
js::ResyncICUDefaultTimeZone()
{
#if ENABLE_INTL_API && defined(ICU_TZ_HAS_RECREATE_DEFAULT)
auto guard = IcuTimeZoneState->lock();
if (guard.get() == IcuTimeZoneStatus::NeedsUpdate) {
bool recreate = true;
if (const char* tz = std::getenv("TZ")) {
icu::UnicodeString tzid;
#if defined(XP_WIN)
// If TZ is set and its value is valid under Windows' and IANA's
// time zone identifier rules, update the ICU default time zone to
// use this value.
if (IsOlsonCompatibleWindowsTimeZoneId(tz)) {
tzid.setTo(icu::UnicodeString(tz, -1, US_INV));
} else {
// If |tz| isn't a supported time zone identifier, use the
// default Windows time zone for ICU.
// TODO: Handle invalid time zone identifiers (bug 342068).
}
#else
// The TZ environment variable allows both absolute and
// relative paths, optionally beginning with a colon (':').
// (Relative paths, without the colon, are just Olson time
// zone names.) We need to handle absolute paths ourselves,
// including handling that they might be symlinks.
// <https://unicode-org.atlassian.net/browse/ICU-13694>
if (const char* tzlink = TZContainsAbsolutePath(tz)) {
tzid.setTo(ReadTimeZoneLink(tzlink));
}
#endif /* defined(XP_WIN) */
if (!tzid.isEmpty()) {
mozilla::UniquePtr<icu::TimeZone> newTimeZone(icu::TimeZone::createTimeZone(tzid));
MOZ_ASSERT(newTimeZone);
if (*newTimeZone != icu::TimeZone::getUnknown()) {
// adoptDefault() takes ownership of the time zone.
icu::TimeZone::adoptDefault(newTimeZone.release());
recreate = false;
}
}
}
if (recreate) {
icu::TimeZone::recreateDefault();
}
guard.get() = IcuTimeZoneStatus::Valid;
}
#endif
}