diff options
Diffstat (limited to 'src/qml/jsruntime/qv4dateobject.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4dateobject.cpp | 61 |
1 files changed, 40 insertions, 21 deletions
diff --git a/src/qml/jsruntime/qv4dateobject.cpp b/src/qml/jsruntime/qv4dateobject.cpp index ed64493d9a..2cb020e495 100644 --- a/src/qml/jsruntime/qv4dateobject.cpp +++ b/src/qml/jsruntime/qv4dateobject.cpp @@ -315,14 +315,14 @@ static inline double ParseString(const QString &s, double localTZA) First, try the format defined in ECMA 262's "Date Time String Format"; only if that fails, fall back to QDateTime for parsing - The defined string format is YYYY-MM-DDTHH:mm:ss.sssZ; the time (T and all - after it) may be omitted; in each part, the second and later components - are optional; and there's an extended syntax for negative and large - positive years: +/-YYYYYY; the leading sign, even when +, isn't optional. - If month or day is omitted, it is 01; if minute or second is omitted, it's - 00; if milliseconds are omitted, they're 000. - - When the time zone offset is absent, date-only forms are interpreted as + The defined string format is yyyy-MM-ddTHH:mm:ss.zzzt; the time (T and all + after it) may be omitted. In each part, the second and later components + are optional. There's an extended syntax for negative and large positive + years: ±yyyyyy; the leading sign, even when +, isn't optional. If month + (MM) or day (dd) is omitted, it is 01; if minute (mm) or second (ss) is + omitted, it's 00; if milliseconds (zzz) are omitted, they're 000. + + When the time zone offset (t) is absent, date-only forms are interpreted as indicating a UTC time and date-time forms are interpreted in local time. */ @@ -719,11 +719,33 @@ QString DateObject::dateTimeToString(const QDateTime &dateTime, ExecutionEngine return ToString(TimeClip(dateTime.toMSecsSinceEpoch()), engine->localTZA); } +double DateObject::dateTimeToNumber(const QDateTime &dateTime) +{ + if (!dateTime.isValid()) + return qQNaN(); + return TimeClip(dateTime.toMSecsSinceEpoch()); +} + QDateTime DateObject::stringToDateTime(const QString &string, ExecutionEngine *engine) { return ToDateTime(ParseString(string, engine->localTZA), QTimeZone::LocalTime); } +QDateTime DateObject::timestampToDateTime(double timestamp, QTimeZone zone) +{ + return ToDateTime(timestamp, zone); +} + +double DateObject::componentsToTimestamp( + double year, double month, double day, double hours, + double mins, double secs, double ms, ExecutionEngine *v4) +{ + if (year >= 0 && year <= 99) + year += 1900; + const double t = MakeDate(MakeDay(year, month, day), MakeTime(hours, mins, secs, ms)); + return UTC(t, v4->localTZA); +} + QDate DateObject::dateTimeToDate(const QDateTime &dateTime) { // If the Date object was parse()d from a string with no time part @@ -744,9 +766,9 @@ QDate DateObject::dateTimeToDate(const QDateTime &dateTime) DEFINE_OBJECT_VTABLE(DateCtor); -void Heap::DateCtor::init(QV4::ExecutionContext *scope) +void Heap::DateCtor::init(QV4::ExecutionEngine *engine) { - Heap::FunctionObject::init(scope, QStringLiteral("Date")); + Heap::FunctionObject::init(engine, QStringLiteral("Date")); } ReturnedValue DateCtor::virtualCallAsConstructor(const FunctionObject *that, const Value *argv, int argc, const Value *newTarget) @@ -773,17 +795,14 @@ ReturnedValue DateCtor::virtualCallAsConstructor(const FunctionObject *that, con } else { // d.argc > 1 - double year = argv[0].toNumber(); - double month = argv[1].toNumber(); - double day = argc >= 3 ? argv[2].toNumber() : 1; - double hours = argc >= 4 ? argv[3].toNumber() : 0; - double mins = argc >= 5 ? argv[4].toNumber() : 0; - double secs = argc >= 6 ? argv[5].toNumber() : 0; - double ms = argc >= 7 ? argv[6].toNumber() : 0; - if (year >= 0 && year <= 99) - year += 1900; - t = MakeDate(MakeDay(year, month, day), MakeTime(hours, mins, secs, ms)); - t = UTC(t, v4->localTZA); + const double year = argv[0].toNumber(); + const double month = argv[1].toNumber(); + const double day = argc >= 3 ? argv[2].toNumber() : 1; + const double hours = argc >= 4 ? argv[3].toNumber() : 0; + const double mins = argc >= 5 ? argv[4].toNumber() : 0; + const double secs = argc >= 6 ? argv[5].toNumber() : 0; + const double ms = argc >= 7 ? argv[6].toNumber() : 0; + t = DateObject::componentsToTimestamp(year, month, day, hours, mins, secs, ms, v4); } ReturnedValue o = Encode(v4->newDateObject(t)); |