📄 qscriptecmadate.cpp
字号:
#else SYSTEMTIME st; GetSystemTime(&st); FILETIME ft; SystemTimeToFileTime(&st, &ft); LARGE_INTEGER li; li.LowPart = ft.dwLowDateTime; li.HighPart = ft.dwHighDateTime; return double(li.QuadPart - Q_INT64_C(116444736000000000)) / 10000.0;#endif}static inline qsreal TimeClip(qsreal t){ if (! qIsFinite(t) || fabs(t) > 8.64e15) return qSNaN(); return QScriptEnginePrivate::toInteger(t);}static inline qsreal ParseString(const QString &s){ QDateTime dt = QDateTime::fromString(s); if (!dt.isValid()) return qSNaN(); int year = dt.date().year(); int month = dt.date().month() - 1; int day = dt.date().day(); int hours = dt.time().hour(); int mins = dt.time().minute(); int secs = dt.time().second(); int ms = dt.time().msec(); double t = MakeDate(MakeDay(year, month, day), MakeTime(hours, mins, secs, ms)); return t = TimeClip(UTC(t));}static inline QDateTime ToDateTime(qsreal t){ if (qIsNaN(t)) return QDateTime(); int year = int(YearFromTime(t)); int month = int(MonthFromTime(t) + 1); int day = int(DateFromTime(t)); int hours = HourFromTime(t); int mins = MinFromTime(t); int secs = SecFromTime(t); int ms = msFromTime(t); return QDateTime(QDate(year, month, day), QTime(hours, mins, secs, ms));}static inline qsreal FromDateTime(const QDateTime &dt){ QDate date = dt.date(); QTime taim = dt.time(); int year = date.year(); int month = date.month() - 1; int day = date.day(); int hours = taim.hour(); int mins = taim.minute(); int secs = taim.second(); int ms = taim.msec(); return MakeDate(MakeDay(year, month, day), MakeTime(hours, mins, secs, ms));}static inline QString ToString(qsreal t){ t = LocalTime(t); return ToDateTime(t).toString();}static inline QString ToUTCString(qsreal t){ return ToDateTime(t).toString();}static inline QString ToDateString(qsreal t){ t = LocalTime(t); return ToDateTime(t).date().toString();}static inline QString ToTimeString(qsreal t){ t = LocalTime(t); return ToDateTime(t).time().toString();}static inline QString ToLocaleString(qsreal t){ t = LocalTime(t); return ToDateTime(t).toString(Qt::LocaleDate);}static inline QString ToLocaleDateString(qsreal t){ t = LocalTime(t); return ToDateTime(t).date().toString(Qt::LocaleDate);}static inline QString ToLocaleTimeString(qsreal t){ t = LocalTime(t); return ToDateTime(t).time().toString(Qt::LocaleDate);}static qsreal getLocalTZA(){#ifndef Q_WS_WIN struct tm* t; time_t curr; time(&curr); t = localtime(&curr); time_t locl = mktime(t); t = gmtime(&curr); time_t globl = mktime(t); return double(locl - globl) * 1000.0;#else TIME_ZONE_INFORMATION tzInfo; GetTimeZoneInformation(&tzInfo); return tzInfo.Bias * 60.0 * 1000.0;#endif}namespace QScript { namespace Ecma {Date::Date(QScriptEnginePrivate *eng): Core(eng){ LocalTZA = getLocalTZA(); m_classInfo = eng->registerClass(QLatin1String("Date")); publicPrototype.invalidate(); newDate(&publicPrototype, qSNaN()); const QScriptValue::PropertyFlags flags = QScriptValue::SkipInEnumeration; eng->newConstructor(&ctor, this, publicPrototype); ctor.setProperty(QLatin1String("parse"), eng->createFunction(method_parse, 1, m_classInfo), flags); ctor.setProperty(QLatin1String("UTC"), eng->createFunction(method_UTC, 1, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("toString"), eng->createFunction(method_toString, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("toDateString"), eng->createFunction(method_toDateString, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("toTimeString"), eng->createFunction(method_toTimeString, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("toLocaleString"), eng->createFunction(method_toLocaleString, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("toLocaleDateString"), eng->createFunction(method_toLocaleDateString, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("toLocaleTimeString"), eng->createFunction(method_toLocaleTimeString, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("valueOf"), eng->createFunction(method_valueOf, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getTime"), eng->createFunction(method_getTime, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getYear"), eng->createFunction(method_getYear, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getFullYear"), eng->createFunction(method_getFullYear, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getUTCFullYear"), eng->createFunction(method_getUTCFullYear, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getMonth"), eng->createFunction(method_getMonth, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getUTCMonth"), eng->createFunction(method_getUTCMonth, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getDate"), eng->createFunction(method_getDate, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getUTCDate"), eng->createFunction(method_getUTCDate, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getDay"), eng->createFunction(method_getDay, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getUTCDay"), eng->createFunction(method_getUTCDay, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getHours"), eng->createFunction(method_getHours, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getUTCHours"), eng->createFunction(method_getUTCHours, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getMinutes"), eng->createFunction(method_getMinutes, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getUTCMinutes"), eng->createFunction(method_getUTCMinutes, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getSeconds"), eng->createFunction(method_getSeconds, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getUTCSeconds"), eng->createFunction(method_getUTCSeconds, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getMilliseconds"), eng->createFunction(method_getMilliseconds, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getUTCMilliseconds"), eng->createFunction(method_getUTCMilliseconds, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("getTimezoneOffset"), eng->createFunction(method_getTimezoneOffset, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setTime"), eng->createFunction(method_setTime, 1, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setMilliseconds"), eng->createFunction(method_setMilliseconds, 1, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setUTCMilliseconds"), eng->createFunction(method_setUTCMilliseconds, 1, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setSeconds"), eng->createFunction(method_setSeconds, 2, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setUTCSeconds"), eng->createFunction(method_setUTCSeconds, 2, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setMinutes"), eng->createFunction(method_setMinutes, 3, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setUTCMinutes"), eng->createFunction(method_setUTCMinutes, 3, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setHours"), eng->createFunction(method_setHours, 4, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setUTCHours"), eng->createFunction(method_setUTCHours, 4, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setDate"), eng->createFunction(method_setDate, 1, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setUTCDate"), eng->createFunction(method_setUTCDate, 1, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setMonth"), eng->createFunction(method_setMonth, 2, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setUTCMonth"), eng->createFunction(method_setUTCMonth, 2, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setYear"), eng->createFunction(method_setYear, 1, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setFullYear"), eng->createFunction(method_setFullYear, 3, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("setUTCFullYear"), eng->createFunction(method_setUTCFullYear, 3, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("toUTCString"), eng->createFunction(method_toUTCString, 0, m_classInfo), flags); publicPrototype.setProperty(QLatin1String("toGMTString"), eng->createFunction(method_toUTCString, 0, m_classInfo), flags);}Date::~Date(){}void Date::execute(QScriptContextPrivate *context){ if (!context->isCalledAsConstructor()) { double t = currentTime(); context->setReturnValue(QScriptValueImpl(engine(), ToString(t))); return; } // called as constructor qsreal t; if (context->argumentCount() == 0) t = currentTime(); else if (context->argumentCount() == 1) { QScriptValue arg = context->argument(0).toPrimitive(); if (arg.isString()) t = ParseString(arg.toString()); else t = TimeClip(arg.toNumber()); } else { // context->argumentCount() > 1 qsreal year = context->argument(0).toNumber(); qsreal month = context->argument(1).toNumber(); qsreal day = context->argumentCount() >= 3 ? context->argument(2).toNumber() : 1; qsreal hours = context->argumentCount() >= 4 ? context->argument(3).toNumber() : 0; qsreal mins = context->argumentCount() >= 5 ? context->argument(4).toNumber() : 0; qsreal secs = context->argumentCount() >= 6 ? context->argument(5).toNumber() : 0; qsreal ms = context->argumentCount() >= 7 ? context->argument(6).toNumber() : 0; if (year >= 0 && year <= 99) year += 1900; t = MakeDate(MakeDay(year, month, day), MakeTime(hours, mins, secs, ms)); t = TimeClip(UTC(t)); } QScriptValueImpl &obj = context->m_thisObject; obj.setClassInfo(classInfo()); obj.setInternalValue(QScriptValueImpl(engine(), t)); obj.setPrototype(publicPrototype); context->setReturnValue(obj);}void Date::newDate(QScriptValueImpl *result, qsreal t){ engine()->newObject(result, publicPrototype, classInfo()); result->setInternalValue(QScriptValueImpl(engine(), t));}void Date::newDate(QScriptValueImpl *result, const QDateTime &dt){ newDate(result, FromDateTime(dt));}void Date::newDate(QScriptValueImpl *result, const QDate &d){ newDate(result, QDateTime(d));}QDateTime Date::toDateTime(const QScriptValueImpl &date) const{ Q_ASSERT(date.classInfo() == m_classInfo); qsreal t = date.internalValue().toNumber(); return ToDateTime(t);}QScriptValueImpl Date::method_parse(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *){ return QScriptValueImpl(eng, ParseString(context->argument(0).toString()));}QScriptValueImpl Date::method_UTC(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -