diff options
author | Liang Qi <liang.qi@nokia.com> | 2010-07-05 13:35:51 (GMT) |
---|---|---|
committer | Liang Qi <liang.qi@nokia.com> | 2010-07-05 13:35:51 (GMT) |
commit | 752d46c90ee0fc5f06f01feedd8e0659178b15d4 (patch) | |
tree | df627cda0259bc80cc4aad0341a8ac0bf6148a0e /src/corelib/tools | |
parent | 0352f68257f67fc4d252e16b7d2f63a22bc60d4f (diff) | |
download | Qt-752d46c90ee0fc5f06f01feedd8e0659178b15d4.zip Qt-752d46c90ee0fc5f06f01feedd8e0659178b15d4.tar.gz Qt-752d46c90ee0fc5f06f01feedd8e0659178b15d4.tar.bz2 |
Support time zone designator in QDateTime::fromString() based on ISO 8601-2004 standard.
Task-number: QTBUG-11623
Reviewed-by: Denis Dzyubenko
Reviewed-by: David Boddie
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qdatetime.cpp | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index 347de0c..c6ab4e4 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -3044,12 +3044,37 @@ QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f) if (tmp.size() == 10) return QDateTime(date); + tmp = tmp.mid(11); + // Recognize UTC specifications if (tmp.endsWith(QLatin1Char('Z'))) { ts = Qt::UTC; tmp.chop(1); } - return QDateTime(date, QTime::fromString(tmp.mid(11), Qt::ISODate), ts); + + // Recognize timezone specifications + QRegExp rx(QLatin1String("[+-]")); + if (tmp.contains(rx)) { + int idx = tmp.indexOf(rx); + QString tmp2 = tmp.mid(idx); + tmp = tmp.left(idx); + bool ok = true; + int ntzhour = 1; + int ntzminute = 3; + if ( tmp2.indexOf(QLatin1Char(':')) == 3 ) + ntzminute = 4; + const int tzhour(tmp2.mid(ntzhour, 2).toInt(&ok)); + const int tzminute(tmp2.mid(ntzminute, 2).toInt(&ok)); + QTime tzt(tzhour, tzminute); + int utcOffset = (tzt.hour() * 60 + tzt.minute()) * 60; + if ( utcOffset != 0 ) { + ts = Qt::OffsetFromUTC; + QDateTime dt(date, QTime::fromString(tmp, Qt::ISODate), ts); + dt.setUtcOffset( utcOffset * (tmp2.startsWith(QLatin1Char('-')) ? -1 : 1) ); + return dt; + } + } + return QDateTime(date, QTime::fromString(tmp, Qt::ISODate), ts); } case Qt::SystemLocaleDate: case Qt::SystemLocaleShortDate: |