8 #include "src/base/timezone-cache.h" 9 #include "src/globals.h" 10 #include "src/objects/smi.h" 17 static const int kMsPerMin = 60 * 1000;
18 static const int kSecPerDay = 24 * 60 * 60;
19 static const int64_t kMsPerDay = kSecPerDay * 1000;
20 static const int64_t kMsPerMonth = kMsPerDay * 30;
23 static const int kMaxEpochTimeInSec = kMaxInt;
24 static const int64_t kMaxEpochTimeInMs =
25 static_cast<int64_t>(kMaxInt) * 1000;
28 static const int64_t kMaxTimeInMs =
29 static_cast<int64_t>(864000000) * 10000000;
33 static const int64_t kMaxTimeBeforeUTCInMs = kMaxTimeInMs + kMsPerMonth;
36 static const int kInvalidLocalOffsetInMs = kMaxInt;
39 static const int kInvalidStamp = -1;
50 void ResetDateCache();
54 static int DaysFromTime(
int64_t time_ms) {
55 if (time_ms < 0) time_ms -= (kMsPerDay - 1);
56 return static_cast<int>(time_ms / kMsPerDay);
62 static int TimeInDay(
int64_t time_ms,
int days) {
63 return static_cast<int>(time_ms - days * kMsPerDay);
67 static double TimeClip(
double time);
71 int Weekday(
int days) {
72 int result = (days + 4) % 7;
73 return result >= 0 ? result : result + 7;
77 bool IsLeap(
int year) {
78 return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
82 int LocalOffsetInMs(
int64_t time,
bool is_utc) {
83 return GetLocalOffsetFromOS(time, is_utc);
87 const char* LocalTimezone(
int64_t time_ms) {
88 if (time_ms < 0 || time_ms > kMaxEpochTimeInMs) {
89 time_ms = EquivalentTime(time_ms);
91 bool is_dst = DaylightSavingsOffsetInMs(time_ms) != 0;
92 const char** name = is_dst ? &dst_tz_name_ : &tz_name_;
93 if (*name ==
nullptr) {
94 *name = tz_cache_->LocalTimezone(static_cast<double>(time_ms));
100 int TimezoneOffset(
int64_t time_ms) {
101 int64_t local_ms = ToLocal(time_ms);
102 return static_cast<int>((time_ms - local_ms) / kMsPerMin);
108 return time_ms + LocalOffsetInMs(time_ms,
true);
114 return time_ms - LocalOffsetInMs(time_ms,
false);
128 int days = DaysFromTime(time_ms);
129 int time_within_day_ms =
static_cast<int>(time_ms - days * kMsPerDay);
130 int year, month, day;
131 YearMonthDayFromDays(days, &year, &month, &day);
132 int new_days = DaysFromYearMonth(EquivalentYear(year), month) + day - 1;
133 return static_cast<int64_t>(new_days) * kMsPerDay + time_within_day_ms;
140 int EquivalentYear(
int year) {
141 int week_day = Weekday(DaysFromYearMonth(year, 0));
142 int recent_year = (IsLeap(year) ? 1956 : 1967) + (week_day * 12) % 28;
145 return 2008 + (recent_year + 3 * 28 - 2008) % 28;
150 void YearMonthDayFromDays(
int days,
int* year,
int* month,
int* day);
154 int DaysFromYearMonth(
int year,
int month);
157 void BreakDownTime(
int64_t time_ms,
int* year,
int* month,
int* day,
158 int* weekday,
int* hour,
int* min,
int* sec,
int* ms);
164 Smi stamp() {
return stamp_; }
165 void* stamp_address() {
return &stamp_; }
168 virtual int GetDaylightSavingsOffsetFromOS(
int64_t time_sec) {
169 double time_ms =
static_cast<double>(time_sec * 1000);
170 return static_cast<int>(tz_cache_->DaylightSavingsOffset(time_ms));
173 virtual int GetLocalOffsetFromOS(
int64_t time_ms,
bool is_utc);
181 static const int kDefaultDSTDeltaInSec = 19 * kSecPerDay;
184 static const int kDSTSize = 32;
197 int DaylightSavingsOffsetInMs(
int64_t time_ms);
204 void ProbeDST(
int time_sec);
208 DST* LeastRecentlyUsedDST(DST* skip);
212 inline void ExtendTheAfterSegment(
int time_sec,
int offset_ms);
215 inline void ClearSegment(DST* segment);
217 bool InvalidSegment(DST* segment) {
218 return segment->start_sec > segment->end_sec;
225 int dst_usage_counter_;
229 int local_offset_ms_;
239 const char* tz_name_;
240 const char* dst_tz_name_;