/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* JavaScript date/time computation and creation functions. */
// ES6 20.3.1.15. // // Clip a double to JavaScript's date range (or to an invalid date) using the // ECMAScript TimeClip algorithm. inline ClippedTime TimeClip(double time) { // Steps 1-2. // // NB: Not written as `Abs(time) > MaxTimeMagnitude` to handle NaN cheaply. // NaN compared to any other number always returns false.
constexpr double MaxTimeMagnitude = 8.64e15; if (!(mozilla::Abs(time) <= MaxTimeMagnitude)) { return ClippedTime(mozilla::UnspecifiedNaN<double>());
}
// Step 3. return ClippedTime(ToInteger(time));
}
// ES6 20.3.1.15. // // Clip an int64_t to JavaScript's date range (or to an invalid date) using the // ECMAScript TimeClip algorithm. inline ClippedTime TimeClip(int64_t time) { // Steps 1-2.
constexpr int64_t MaxTimeMagnitude = 8.64e15; if (mozilla::Abs(time) > MaxTimeMagnitude) { return ClippedTime(mozilla::UnspecifiedNaN<double>());
}
// Produce a double Value from the given time. Because times may be NaN, // prefer using this to manual canonicalization. inline Value TimeValue(ClippedTime time) { return CanonicalizedDoubleValue(time.toDouble());
}
// Create a new Date object whose [[DateValue]] internal slot contains the // clipped |time|. (Users who must represent times outside that range must use // another representation.) extern JS_PUBLIC_API JSObject* NewDateObject(JSContext* cx, ClippedTime time);
/** *CreateanewDateobjectforayear/month/day-of-month/hour/minute/second. * *Thecreateddateisinitializedwiththetimevalue * *TimeClip(UTC(MakeDate(MakeDay(year,mon,mday), *MakeTime(hour,min,sec,0.0)))) * *whereeachfunction/operationisasspecifiedinECMAScript. * *Assertthatmon<12tohelpcatchoff-by-oneusererrors,whicharecommon *duetothe0-basedmonthnumberingcopiedintoJSfromJava(java.util.Date *in1995).
*/ extern JS_PUBLIC_API JSObject* NewDateObject(JSContext* cx, int year, int mon, int mday, int hour, int min, int sec);
// Year is a year, month is 0-11, day is 1-based. The return value is a number // of milliseconds since the epoch. // // Consistent with the MakeDate algorithm defined in ECMAScript, this value is // *not* clipped! Use JS::TimeClip if you need a clipped date.
JS_PUBLIC_API double MakeDate(double year, unsigned month, unsigned day);
// Year is a year, month is 0-11, day is 1-based, and time is in milliseconds. // The return value is a number of milliseconds since the epoch. // // Consistent with the MakeDate algorithm defined in ECMAScript, this value is // *not* clipped! Use JS::TimeClip if you need a clipped date.
JS_PUBLIC_API double MakeDate(double year, unsigned month, unsigned day, double time);
// Takes an integer number of milliseconds since the epoch and returns the // year. Can return NaN, and will do so if NaN is passed in.
JS_PUBLIC_API double YearFromTime(double time);
// Takes an integer number of milliseconds since the epoch and returns the // month (0-11). Can return NaN, and will do so if NaN is passed in.
JS_PUBLIC_API double MonthFromTime(double time);
// Takes an integer number of milliseconds since the epoch and returns the // day (1-based). Can return NaN, and will do so if NaN is passed in.
JS_PUBLIC_API double DayFromTime(double time);
// Takes an integer year and returns the number of days from epoch to the given // year. // NOTE: The calculation performed by this function is literally that given in // the ECMAScript specification. Nonfinite years, years containing fractional // components, and years outside ECMAScript's date range are not handled with // any particular intelligence. Garbage in, garbage out.
JS_PUBLIC_API double DayFromYear(double year);
// Takes an integer number of milliseconds since the epoch and an integer year, // returns the number of days in that year. If |time| is nonfinite, returns NaN. // Otherwise |time| *must* correspond to a time within the valid year |year|. // This should usually be ensured by computing |year| as // |JS::DayFromYear(time)|.
JS_PUBLIC_API double DayWithinYear(double time, double year);
// The callback will be a wrapper function that accepts a double (the time // to clamp and jitter) and a JS::RTPCallerTypeToken (a wrapper for // mozilla::RTPCallerType) that can be used to decide the proper clamping // behavior to use. Inside the JS Engine, other parameters that may be needed // are all constant, so they are handled inside the wrapper function
using ReduceMicrosecondTimePrecisionCallback = double (*)(double, JS::RTPCallerTypeToken, JSContext*);
// Set a callback into the toolkit/components/resistfingerprinting function that // will centralize time resolution and jitter into one place. // Defining such a callback requires all Realms that are created afterwards // to have a set JS::RTPCallerTypeToken, via RealmBehaviors or // JS::SetRealmReduceTimerPrecisionCallerType.
JS_PUBLIC_API void SetReduceMicrosecondTimePrecisionCallback(
ReduceMicrosecondTimePrecisionCallback callback);
// Get the previously set ReduceMicrosecondTimePrecisionCallback callback or // nullptr.
JS_PUBLIC_API ReduceMicrosecondTimePrecisionCallback
GetReduceMicrosecondTimePrecisionCallback();
// Returns whether a given string follows the Date Time String Format.
JS_PUBLIC_API bool IsISOStyleDate(JSContext* cx, const JS::Latin1Chars& str);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.