示例#1
0
/*!
  Returns a QDateTime representation of this value, in local time.
  If this QJSValue is not a date, or the value of the date is NaN
  (Not-a-Number), an invalid QDateTime is returned.

  \sa isDate()
*/
QDateTime QJSValue::toDateTime() const
{
    QV4::DateObject *date = d->value.asDateObject();
    if (!date)
        return QDateTime();
    return date->toQDateTime();
}
示例#2
0
QV4::ReturnedValue QQmlDateExtension::method_toLocaleDateString(QV4::CallContext *ctx)
{
    if (ctx->argc() > 2)
        return QV4::DatePrototype::method_toLocaleDateString(ctx);

    QV4::Scope scope(ctx);

    QV4::DateObject *dateObj = ctx->thisObject().asDateObject();
    if (!dateObj)
        return QV4::DatePrototype::method_toLocaleDateString(ctx);

    QDateTime dt = dateObj->toQDateTime();
    QDate date = dt.date();

    if (ctx->argc() == 0) {
        // Use QLocale for standard toLocaleString() function
        QLocale locale;
        return ctx->d()->engine->newString(locale.toString(date))->asReturnedValue();
    }

    if (!isLocaleObject(ctx->args()[0]))
        return QV4::DatePrototype::method_toLocaleDateString(ctx); // Use the default Date toLocaleDateString()

    GET_LOCALE_DATA_RESOURCE(ctx->args()[0]);

    QLocale::FormatType enumFormat = QLocale::LongFormat;
    QString formattedDate;
    if (ctx->argc() == 2) {
        if (ctx->args()[1].isString()) {
            QString format = ctx->args()[1].stringValue()->toQString();
            formattedDate = r->d()->locale.toString(date, format);
        } else if (ctx->args()[1].isNumber()) {
            quint32 intFormat = ctx->args()[1].toNumber();
            QLocale::FormatType format = QLocale::FormatType(intFormat);
            formattedDate = r->d()->locale.toString(date, format);
        } else {
            V4THROW_ERROR("Locale: Date.loLocaleDateString(): Invalid date format");
        }
    } else {
         formattedDate = r->d()->locale.toString(date, enumFormat);
    }

    return ctx->d()->engine->newString(formattedDate)->asReturnedValue();
}