tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DateObject.h (5295B)


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