Debian Codegraph @

js/src/vm/DateObject.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef vm_DateObject_h_
8 #define vm_DateObject_h_
9
10 #include "js/Date.h"
11 #include "js/Value.h"
12 #include "vm/DateTime.h"
13 #include "vm/NativeObject.h"
14
15 namespace js {
16
17 class DateObject : public NativeObject {
18 // Time in milliseconds since the (Unix) epoch.
19 //
20 // The stored value is guaranteed to be a Double.
21 static const uint32_t UTC_TIME_SLOT = 0;
22
23 // Raw time zone offset in seconds, i.e. without daylight saving adjustment,
24 // of the current system zone.
25 //
26 // This value is exclusively used to verify the cached slots are still valid.
27 //
28 // It is NOT the return value of Date.prototype.getTimezoneOffset()!
29 //
30 // The stored value is either an Int32 or Undefined.
31 static const uint32_t UTC_TIME_ZONE_OFFSET_SLOT = 1;
32
33 /*
34 * Cached slots holding local properties of the date.
35 * These are undefined until the first actual lookup occurs
36 * and are reset to undefined whenever the date's time is modified.
37 *
38 * - LOCAL_TIME_SLOT is either a Double or Undefined.
39 * - The remaining slots store either Int32, NaN, or Undefined values.
40 */
41 static const uint32_t COMPONENTS_START_SLOT = 2;
42
43 static const uint32_t LOCAL_TIME_SLOT = COMPONENTS_START_SLOT + 0;
44 static const uint32_t LOCAL_YEAR_SLOT = COMPONENTS_START_SLOT + 1;
45 static const uint32_t LOCAL_MONTH_SLOT = COMPONENTS_START_SLOT + 2;
46 static const uint32_t LOCAL_DATE_SLOT = COMPONENTS_START_SLOT + 3;
47 static const uint32_t LOCAL_DAY_SLOT = COMPONENTS_START_SLOT + 4;
48
49 /*
50 * Unlike the above slots that hold LocalTZA-adjusted component values,
51 * LOCAL_SECONDS_INTO_YEAR_SLOT holds a composite value that can be used
52 * to compute LocalTZA-adjusted hours, minutes, and seconds values.
53 * Specifically, LOCAL_SECONDS_INTO_YEAR_SLOT holds the number of
54 * LocalTZA-adjusted seconds into the year. Unix timestamps ignore leap
55 * seconds, so recovering hours/minutes/seconds requires only trivial
56 * division/modulus operations.
57 */
58 static const uint32_t LOCAL_SECONDS_INTO_YEAR_SLOT =
59 COMPONENTS_START_SLOT + 5;
60
61 static const uint32_t RESERVED_SLOTS = LOCAL_SECONDS_INTO_YEAR_SLOT + 1;
62
63 public:
64 static const JSClass class_;
65 static const JSClass protoClass_;
66
67 js::DateTimeInfo::ForceUTC forceUTC() const;
68
69 JS::ClippedTime clippedTime() const {
70 double t = getFixedSlot(UTC_TIME_SLOT).toDouble();
71 JS::ClippedTime clipped = JS::TimeClip(t);
72 MOZ_ASSERT(mozilla::NumbersAreIdentical(clipped.toDouble(), t));
73 return clipped;
74 }
75
76 /**
77 * Return the time in milliseconds since the epoch. The value is guaranteed to
78 * be a Double.
79 */
80 const js::Value& UTCTime() const { return getFixedSlot(UTC_TIME_SLOT); }
81
82 /**
83 * Return the cached local time. The value is either a Double or Undefined.
84 */
85 const js::Value& localTime() const {
86 return getReservedSlot(LOCAL_TIME_SLOT);
87 }
88
89 // Set UTC time to a given time and invalidate cached local time.
90 void setUTCTime(JS::ClippedTime t);
91 void setUTCTime(JS::ClippedTime t, MutableHandleValue vp);
92
93 // Cache the local time, year, month, and so forth of the object.
94 // If UTC time is not finite (e.g., NaN), the local time
95 // slots will be set to the UTC time without conversion.
96 void fillLocalTimeSlots();
97
98 /**
99 * Return the cached local year. The value is either an Int32, NaN, or
100 * Undefined.
101 */
102 const js::Value& localYear() const {
103 return getReservedSlot(LOCAL_YEAR_SLOT);
104 }
105
106 /**
107 * Return the cached local month. The value is either an Int32, NaN, or
108 * Undefined.
109 */
110 const js::Value& localMonth() const {
111 return getReservedSlot(LOCAL_MONTH_SLOT);
112 }
113
114 /**
115 * Return the cached local day of month. The value is either an Int32, NaN,
116 * or Undefined.
117 */
118 const js::Value& localDate() const {
119 return getReservedSlot(LOCAL_DATE_SLOT);
120 }
121
122 /**
123 * Return the cached local day of week. The value is either an Int32, NaN,
124 * or Undefined.
125 */
126 const js::Value& localDay() const { return getReservedSlot(LOCAL_DAY_SLOT); }
127
128 /**
129 * Return the cached local seconds of year. The value is either an Int32, NaN,
130 * or Undefined.
131 */
132 const js::Value& localSecondsIntoYear() const {
133 return getReservedSlot(LOCAL_SECONDS_INTO_YEAR_SLOT);
134 }
135
136 static constexpr size_t offsetOfUTCTimeSlot() {
137 return getFixedSlotOffset(UTC_TIME_SLOT);
138 }
139 static constexpr size_t offsetOfUTCTimeZoneOffsetSlot() {
140 return getFixedSlotOffset(UTC_TIME_ZONE_OFFSET_SLOT);
141 }
142 static constexpr size_t offsetOfLocalTimeSlot() {
143 return getFixedSlotOffset(LOCAL_TIME_SLOT);
144 }
145 static constexpr size_t offsetOfLocalYearSlot() {
146 return getFixedSlotOffset(LOCAL_YEAR_SLOT);
147 }
148 static constexpr size_t offsetOfLocalMonthSlot() {
149 return getFixedSlotOffset(LOCAL_MONTH_SLOT);
150 }
151 static constexpr size_t offsetOfLocalDateSlot() {
152 return getFixedSlotOffset(LOCAL_DATE_SLOT);
153 }
154 static constexpr size_t offsetOfLocalDaySlot() {
155 return getFixedSlotOffset(LOCAL_DAY_SLOT);
156 }
157 static constexpr size_t offsetOfLocalSecondsIntoYearSlot() {
158 return getFixedSlotOffset(LOCAL_SECONDS_INTO_YEAR_SLOT);
159 }
160 };
161
162 } // namespace js
163
164 #endif // vm_DateObject_h_