Qt-interest Archive, July 2007
Re: QtScript: Bug in Date.parse, or mistake at my side?
Message 1 in thread
Seneca wrote:
> According to ECMA-262 the Date.parse should work in the following
> example:
>
> var x = new Date();
> print(x.valueOf()); // the result we expect in the next two statements
> print(Date.parse(x.toString()));
> print(Date.parse(x.toUTCString()));
>
> However what I get is an error message along with a large negative
> number:
> QDateTime::fromString: Paremeter out of range
> -210892374000000
>
> I also did a wild guess and added a QDateTime format string as second
> parameter, however also without luck.
>
> Any thoughts?
>
Hi Seneca,
Is this on Windows? Could you provide an example of the string you get
from new Date().toString()?
Regards,
Kent
--
[ signature omitted ]
Message 2 in thread
> Is this on Windows? Could you provide an example of the string you get
> >from new Date().toString()?
Hi Kent,
This is on Windows XP (German) with Qt 4.3.0.
What I get is:
Mi 4. Jul 11:55:01 2007
--
[ signature omitted ]
Message 3 in thread
Strange enough the time printen was offset 3 hours from our actual time
(actual time was 14:55:01), and we are in fact +1 hour from greenwich (utc).
> This is on Windows XP (German) with Qt 4.3.0.
> What I get is:
>
> Mi 4. Jul 11:55:01 2007
--
[ signature omitted ]
Message 4 in thread
Seneca wrote:
>> Is this on Windows? Could you provide an example of the string you get
>> >from new Date().toString()?
>>
>
>
> Hi Kent,
>
> This is on Windows XP (German) with Qt 4.3.0.
> What I get is:
>
> Mi 4. Jul 11:55:01 2007
>
Hi Seneca,
There's a bug in QDateTime::fromString() on Windows that causes this to
be incorrectly parsed. We're working on fixing it for 4.3.1. What you
can do in the meantime is use custom conversion functions that take
format strings, for example like this:
#include <QtScript>
QScriptValue dateFromString(QScriptContext *context, QScriptEngine *engine)
{
QString str = context->argument(0).toString();
QString format = context->argument(1).toString();
return engine->newDate(QDateTime::fromString(str, format));
}
QScriptValue dateToString(QScriptContext *context, QScriptEngine *engine)
{
QDateTime date = context->argument(0).toDateTime();
QString format = context->argument(1).toString();
return QScriptValue(engine, date.toString(format));
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QScriptEngine engine;
engine.globalObject().setProperty("dateFromString",
engine.newFunction(dateFromString));
engine.globalObject().setProperty("dateToString",
engine.newFunction(dateToString));
engine.evaluate("var format = 'ddd MMM d hh:mm:ss yyyy';"
"var x = new Date();"
"x.setMilliseconds(0);"
"var s = dateToString(x, format);"
"print(x.valueOf(), '-', s);"
"var y = dateFromString(s, format);"
"print(y.valueOf(), '-', dateToString(y, format));");
return 0;
}
Thanks for your report.
Regards,
Kent
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]