From baf5eed1ef5a71cdcf4ee56cb3e9c9eaa45ea60b Mon Sep 17 00:00:00 2001 From: Daniel Seither Date: Tue, 4 Feb 2014 12:36:27 +0100 Subject: QDateTime: Fix sign handling in the timezone offset parser Previously, parsing negative timezone offsets with minutes != 00 produced wrong results. Examples (in -> out) -00:15 -> +00:15 -01:15 -> -00:45 Change-Id: I6fa30810a08bdf2996365661720b2e362e8aeb93 Reviewed-by: Thiago Macieira Reviewed-by: John Layt --- src/corelib/tools/qdatetime.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/corelib/tools/qdatetime.cpp') diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index e38a5f569a..280d516452 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -265,17 +265,24 @@ static int fromOffsetString(const QString &offsetString, bool *valid) if (size < 2 || size > 6) return 0; + // sign will be +1 for a positive and -1 for a negative offset + int sign; + // First char must be + or - - const QChar sign = offsetString.at(0); - if (sign != QLatin1Char('+') && sign != QLatin1Char('-')) + const QChar signChar = offsetString.at(0); + if (signChar == QLatin1Char('+')) + sign = 1; + else if (signChar == QLatin1Char('-')) + sign = -1; + else return 0; // Split the hour and minute parts - QStringList parts = offsetString.split(QLatin1Char(':')); + QStringList parts = offsetString.mid(1).split(QLatin1Char(':')); if (parts.count() == 1) { // [+-]HHMM format - parts.append(parts.at(0).mid(3)); - parts[0] = parts.at(0).left(3); + parts.append(parts.at(0).mid(2)); + parts[0] = parts.at(0).left(2); } bool ok = false; @@ -288,7 +295,7 @@ static int fromOffsetString(const QString &offsetString, bool *valid) return 0; *valid = true; - return ((hour * 60) + minute) * 60; + return sign * ((hour * 60) + minute) * 60; } /***************************************************************************** -- cgit v1.2.3 From ac109e1316750175100cafc7f486b0c8ff03f716 Mon Sep 17 00:00:00 2001 From: Daniel Seither Date: Tue, 4 Feb 2014 12:39:17 +0100 Subject: QDateTime: Fix sign handling in the timezone offset writer Previously, this produced wrong results, for example -3:30 became -3:-30. Change-Id: I10efdfb48e5542b917c86b29cf8a99bfc26f7fe0 Reviewed-by: John Layt Reviewed-by: Thiago Macieira --- src/corelib/tools/qdatetime.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/corelib/tools/qdatetime.cpp') diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index 280d516452..801876629c 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -253,7 +253,7 @@ static QString toOffsetString(Qt::DateFormat format, int offset) return result.arg(offset >= 0 ? QLatin1Char('+') : QLatin1Char('-')) .arg(qAbs(offset) / SECS_PER_HOUR, 2, 10, QLatin1Char('0')) - .arg((offset / 60) % 60, 2, 10, QLatin1Char('0')); + .arg((qAbs(offset) / 60) % 60, 2, 10, QLatin1Char('0')); } // Parse offset in [+-]HH[:]MM format -- cgit v1.2.3