diff options
Diffstat (limited to 'src/qml/jsruntime/qv4dateobject.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4dateobject.cpp | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/src/qml/jsruntime/qv4dateobject.cpp b/src/qml/jsruntime/qv4dateobject.cpp index 5397ad43c5..4f3138a452 100644 --- a/src/qml/jsruntime/qv4dateobject.cpp +++ b/src/qml/jsruntime/qv4dateobject.cpp @@ -466,7 +466,7 @@ static inline double ParseString(const QString &s) if (format < Minute || format >= TimezoneHour) error = true; format = TimezoneHour; - } else if (*ch == 'Z' || *ch == 0) { + } else if (*ch == 'Z' || ch->unicode() == 0) { format = Done; } current = 0; @@ -565,7 +565,7 @@ static inline QString ToString(double t) { if (std::isnan(t)) return QStringLiteral("Invalid Date"); - QString str = ToDateTime(t, Qt::LocalTime).toString() + QStringLiteral(" GMT"); + QString str = ToDateTime(t, Qt::LocalTime).toString() + QLatin1String(" GMT"); double tzoffset = LocalTZA + DaylightSavingTA(t); if (tzoffset) { int hours = static_cast<int>(::fabs(tzoffset) / 1000 / 60 / 60); @@ -634,11 +634,35 @@ static double getLocalTZA() DEFINE_OBJECT_VTABLE(DateObject); -Heap::DateObject::DateObject(const QDateTime &date) +void Heap::DateObject::init(const QDateTime &date) { + Object::init(); this->date = date.isValid() ? date.toMSecsSinceEpoch() : qt_qnan(); } +void Heap::DateObject::init(const QTime &time) +{ + Object::init(); + if (!time.isValid()) { + date = qt_qnan(); + return; + } + + /* All programmers know that stuff starts at 0. Whatever that may mean in this context (and + * local timezone), it's before the epoch, so there is defenitely no DST problem. Specifically: + * you can't start with a date before the epoch, add some[*] hours, and end up with a date + * after. That's a problem for timezones where new year happens during DST, like + * Australia/Hobart, because we have to ignore DST before the epoch (but honor it after the + * epoch). + * + * [*] Well, when "some" is in the range 0-24. If you add something like 1M then this might + * still happen. + */ + static const double d = MakeDay(0, 0, 0); + double t = MakeTime(time.hour(), time.minute(), time.second(), time.msec()); + date = TimeClip(UTC(MakeDate(d, t))); +} + QDateTime DateObject::toQDateTime() const { return ToDateTime(date(), Qt::LocalTime); @@ -646,14 +670,13 @@ QDateTime DateObject::toQDateTime() const DEFINE_OBJECT_VTABLE(DateCtor); -Heap::DateCtor::DateCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("Date")) +void Heap::DateCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("Date")); } -ReturnedValue DateCtor::construct(const Managed *m, CallData *callData) +void DateCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const DateCtor *>(m)->engine()); double t = 0; if (callData->argc == 0) @@ -687,13 +710,13 @@ ReturnedValue DateCtor::construct(const Managed *m, CallData *callData) t = TimeClip(UTC(t)); } - return Encode(scope.engine->newDateObject(Primitive::fromDouble(t))); + scope.result = Encode(scope.engine->newDateObject(Primitive::fromDouble(t))); } -ReturnedValue DateCtor::call(const Managed *m, CallData *) +void DateCtor::call(const Managed *m, Scope &scope, CallData *) { double t = currentTime(); - return static_cast<const DateCtor *>(m)->engine()->newString(ToString(t))->asReturnedValue(); + scope.result = static_cast<const DateCtor *>(m)->engine()->newString(ToString(t)); } void DatePrototype::init(ExecutionEngine *engine, Object *ctor) @@ -1311,7 +1334,8 @@ ReturnedValue DatePrototype::method_toJSON(CallContext *ctx) ScopedCallData callData(scope); callData->thisObject = ctx->thisObject(); - return toIso->call(callData); + toIso->call(scope, callData); + return scope.result.asReturnedValue(); } void DatePrototype::timezoneUpdated() |