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/. */
7 #ifndef vm_DateObject_h_
8 #define vm_DateObject_h_
12 #include "vm/DateTime.h"
13 #include "vm/NativeObject.h"
17 class DateObject
: public NativeObject
{
18 // Time in milliseconds since the (Unix) epoch.
19 static const uint32_t UTC_TIME_SLOT
= 0;
21 // Raw time zone offset in seconds, i.e. without daylight saving adjustment,
22 // of the current system zone.
24 // This value is exclusively used to verify the cached slots are still valid.
26 // It is NOT the return value of Date.prototype.getTimezoneOffset()!
27 static const uint32_t UTC_TIME_ZONE_OFFSET_SLOT
= 1;
30 * Cached slots holding local properties of the date.
31 * These are undefined until the first actual lookup occurs
32 * and are reset to undefined whenever the date's time is modified.
34 static const uint32_t COMPONENTS_START_SLOT
= 2;
36 static const uint32_t LOCAL_TIME_SLOT
= COMPONENTS_START_SLOT
+ 0;
37 static const uint32_t LOCAL_YEAR_SLOT
= COMPONENTS_START_SLOT
+ 1;
38 static const uint32_t LOCAL_MONTH_SLOT
= COMPONENTS_START_SLOT
+ 2;
39 static const uint32_t LOCAL_DATE_SLOT
= COMPONENTS_START_SLOT
+ 3;
40 static const uint32_t LOCAL_DAY_SLOT
= COMPONENTS_START_SLOT
+ 4;
43 * Unlike the above slots that hold LocalTZA-adjusted component values,
44 * LOCAL_SECONDS_INTO_YEAR_SLOT holds a composite value that can be used
45 * to compute LocalTZA-adjusted hours, minutes, and seconds values.
46 * Specifically, LOCAL_SECONDS_INTO_YEAR_SLOT holds the number of
47 * LocalTZA-adjusted seconds into the year. Unix timestamps ignore leap
48 * seconds, so recovering hours/minutes/seconds requires only trivial
49 * division/modulus operations.
51 static const uint32_t LOCAL_SECONDS_INTO_YEAR_SLOT
=
52 COMPONENTS_START_SLOT
+ 5;
54 static const uint32_t RESERVED_SLOTS
= LOCAL_SECONDS_INTO_YEAR_SLOT
+ 1;
57 static const JSClass class_
;
58 static const JSClass protoClass_
;
60 js::DateTimeInfo::ForceUTC
forceUTC() const;
62 JS::ClippedTime
clippedTime() const {
63 double t
= getFixedSlot(UTC_TIME_SLOT
).toDouble();
64 JS::ClippedTime clipped
= JS::TimeClip(t
);
65 MOZ_ASSERT(mozilla::NumbersAreIdentical(clipped
.toDouble(), t
));
69 const js::Value
& UTCTime() const { return getFixedSlot(UTC_TIME_SLOT
); }
70 const js::Value
& localTime() const {
71 return getReservedSlot(LOCAL_TIME_SLOT
);
74 // Set UTC time to a given time and invalidate cached local time.
75 void setUTCTime(JS::ClippedTime t
);
76 void setUTCTime(JS::ClippedTime t
, MutableHandleValue vp
);
78 // Cache the local time, year, month, and so forth of the object.
79 // If UTC time is not finite (e.g., NaN), the local time
80 // slots will be set to the UTC time without conversion.
81 void fillLocalTimeSlots();
83 const js::Value
& localYear() const {
84 return getReservedSlot(LOCAL_YEAR_SLOT
);
86 const js::Value
& localMonth() const {
87 return getReservedSlot(LOCAL_MONTH_SLOT
);
89 const js::Value
& localDate() const {
90 return getReservedSlot(LOCAL_DATE_SLOT
);
92 const js::Value
& localDay() const { return getReservedSlot(LOCAL_DAY_SLOT
); }
94 const js::Value
& localSecondsIntoYear() const {
95 return getReservedSlot(LOCAL_SECONDS_INTO_YEAR_SLOT
);
101 #endif // vm_DateObject_h_