Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
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 "jsobj.h"
11 :
12 : #include "js/Date.h"
13 : #include "js/Value.h"
14 :
15 : namespace js {
16 :
17 : class DateObject : public NativeObject
18 : {
19 : static const uint32_t UTC_TIME_SLOT = 0;
20 : static const uint32_t TZA_SLOT = 1;
21 :
22 : /*
23 : * Cached slots holding local properties of the date.
24 : * These are undefined until the first actual lookup occurs
25 : * and are reset to undefined whenever the date's time is modified.
26 : */
27 : static const uint32_t COMPONENTS_START_SLOT = 2;
28 :
29 : static const uint32_t LOCAL_TIME_SLOT = COMPONENTS_START_SLOT + 0;
30 : static const uint32_t LOCAL_YEAR_SLOT = COMPONENTS_START_SLOT + 1;
31 : static const uint32_t LOCAL_MONTH_SLOT = COMPONENTS_START_SLOT + 2;
32 : static const uint32_t LOCAL_DATE_SLOT = COMPONENTS_START_SLOT + 3;
33 : static const uint32_t LOCAL_DAY_SLOT = COMPONENTS_START_SLOT + 4;
34 :
35 : /*
36 : * Unlike the above slots that hold LocalTZA-adjusted component values,
37 : * LOCAL_SECONDS_INTO_YEAR_SLOT holds a composite value that can be used
38 : * to compute LocalTZA-adjusted hours, minutes, and seconds values.
39 : * Specifically, LOCAL_SECONDS_INTO_YEAR_SLOT holds the number of
40 : * LocalTZA-adjusted seconds into the year. Unix timestamps ignore leap
41 : * seconds, so recovering hours/minutes/seconds requires only trivial
42 : * division/modulus operations.
43 : */
44 : static const uint32_t LOCAL_SECONDS_INTO_YEAR_SLOT = COMPONENTS_START_SLOT + 5;
45 :
46 : static const uint32_t RESERVED_SLOTS = LOCAL_SECONDS_INTO_YEAR_SLOT + 1;
47 :
48 : public:
49 : static const Class class_;
50 : static const Class protoClass_;
51 :
52 0 : JS::ClippedTime clippedTime() const {
53 0 : double t = getFixedSlot(UTC_TIME_SLOT).toDouble();
54 0 : JS::ClippedTime clipped = JS::TimeClip(t);
55 0 : MOZ_ASSERT(mozilla::NumbersAreIdentical(clipped.toDouble(), t));
56 0 : return clipped;
57 : }
58 :
59 65 : const js::Value& UTCTime() const {
60 65 : return getFixedSlot(UTC_TIME_SLOT);
61 : }
62 :
63 : // Set UTC time to a given time and invalidate cached local time.
64 : void setUTCTime(JS::ClippedTime t);
65 : void setUTCTime(JS::ClippedTime t, MutableHandleValue vp);
66 :
67 : inline double cachedLocalTime();
68 :
69 : // Cache the local time, year, month, and so forth of the object.
70 : // If UTC time is not finite (e.g., NaN), the local time
71 : // slots will be set to the UTC time without conversion.
72 : void fillLocalTimeSlots();
73 :
74 : static MOZ_ALWAYS_INLINE bool getTime_impl(JSContext* cx, const CallArgs& args);
75 : static MOZ_ALWAYS_INLINE bool getYear_impl(JSContext* cx, const CallArgs& args);
76 : static MOZ_ALWAYS_INLINE bool getFullYear_impl(JSContext* cx, const CallArgs& args);
77 : static MOZ_ALWAYS_INLINE bool getUTCFullYear_impl(JSContext* cx, const CallArgs& args);
78 : static MOZ_ALWAYS_INLINE bool getMonth_impl(JSContext* cx, const CallArgs& args);
79 : static MOZ_ALWAYS_INLINE bool getUTCMonth_impl(JSContext* cx, const CallArgs& args);
80 : static MOZ_ALWAYS_INLINE bool getDate_impl(JSContext* cx, const CallArgs& args);
81 : static MOZ_ALWAYS_INLINE bool getUTCDate_impl(JSContext* cx, const CallArgs& args);
82 : static MOZ_ALWAYS_INLINE bool getDay_impl(JSContext* cx, const CallArgs& args);
83 : static MOZ_ALWAYS_INLINE bool getUTCDay_impl(JSContext* cx, const CallArgs& args);
84 : static MOZ_ALWAYS_INLINE bool getHours_impl(JSContext* cx, const CallArgs& args);
85 : static MOZ_ALWAYS_INLINE bool getUTCHours_impl(JSContext* cx, const CallArgs& args);
86 : static MOZ_ALWAYS_INLINE bool getMinutes_impl(JSContext* cx, const CallArgs& args);
87 : static MOZ_ALWAYS_INLINE bool getUTCMinutes_impl(JSContext* cx, const CallArgs& args);
88 : static MOZ_ALWAYS_INLINE bool getUTCSeconds_impl(JSContext* cx, const CallArgs& args);
89 : static MOZ_ALWAYS_INLINE bool getUTCMilliseconds_impl(JSContext* cx, const CallArgs& args);
90 : static MOZ_ALWAYS_INLINE bool getTimezoneOffset_impl(JSContext* cx, const CallArgs& args);
91 : };
92 :
93 : } // namespace js
94 :
95 : #endif // vm_DateObject_h_
|