my own kdatetime patches: toString with InvalidOffset; operator==

Jon Severinsson jon at severinsson.net
Fri Oct 19 21:44:43 UTC 2012


> --- a/kdecore/date/kdatetime.cpp
> +++ b/kdecore/date/kdatetime.cpp
> @@ -1773,6 +1773,8 @@ QString KDateTime::toString(TimeFormat format) const
>              offset = d->timeZoneOffset();   // calculate offset and cache UTC value
>          else
>              offset = tz.isValid() ? tz.offsetAtZoneTime(d->dt()) : d->specUtcOffset;
> +        if (offset == KTimeZone::InvalidOffset)
> +            return result + QLatin1Char(tzsign) + QLatin1String("invalidtz");
>          if (offset < 0)
>          {
>              offset = -offset;
> 
> Without this fix, an invalid timezone led to a string representation like
> this: KDateTime(2012-10-19T00:00:00--596523:-14)
> Nasty overflow in there, leading to '-' as a char followed by a negative
> number ;) (This is due to InvalidOffset being equal to 0x80000000)
> Now it prints:
>   KDateTime(2012-10-19T00:00:00+invalidtz)
> which is at least readable. It's invalid, of course, but it's readable :)

Looks good to me.

> Should it say something else?

KDateTime(2012-10-19T00:00:00+EINVAL) perhaps :P

> Sounds like this toString() fix should even go into 4.9?

Yes, sounds like :)

> The other issue (the one that made me debug all this) is that
> bool KDateTime::operator==(const KDateTime &other)
> doesn't handle the case where one is invalid and other is valid.
> It ends up calling toUtc() on both, and that can be equal (to 0).
> 
> How about this fix?
> 
> --- a/kdecore/date/kdatetime.cpp
> +++ b/kdecore/date/kdatetime.cpp
> @@ -1357,6 +1357,8 @@ bool KDateTime::operator==(const KDateTime &other)
> const return d->secondOccurrence() == other.d->secondOccurrence() && 
> d->dt() == other.d->dt();
>      }
> +    if (d->specType == Invalid || other.d->specType == Invalid)
> +        return false;
>      // Don't waste time converting to UTC if the dates aren't close
> enough. if (qAbs(d->date().daysTo(other.d->date())) > 2)
>          return false;
> 
> [at this point we know already they are not equal]

Looks good to me, but you should probably check if operator<() and/or compare() needs
a similar fix (so they considering all invalid KDateTime smaller than all valid KDateTime).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-frameworks-devel/attachments/20121019/fac7ae77/attachment.sig>


More information about the Kde-frameworks-devel mailing list