DevHeads.net

KF5: Parsing times with timezone abbreviations

Hi,

what is the recommended way to parse times with short timezone
abbreviations, such as "16:00 CET", in Qt 5 / KF5 land? (I am asking because
this is the format the BBC is using for weather observation times in their
RSS files.)

In kdelibs 4, this should do what we want:
dateTime = KDateTime::fromString(timeString, "%H:%M %Z").toLocalZone().dateTime();
but KDateTime is deprecated in KF5.

I looked at:
* QDateTime, but neither of the 2 QDateTime::fromString overloads in Qt 5.2
support the needed format, at least according to the documentation. The
one that takes a string does not support time zones (or so the
documentation says), the one that takes an enum entry does not support the
required format either.
* QTimeZone, but I cannot find a straightforward (documented) way to get a
QTimeZone from only the short abbreviation. I guess one could go through
all the availableTimeZoneIds and checking if the abbreviation matches, but
that won't necessarily give the expected (most probable) result
(especially if the abbreviation is not unique), will it?

Kevin Kofler

Comments

Re: KF5: Parsing times with timezone abbreviations

By Thiago Macieira at 03/30/2014 - 21:08

Em seg 24 mar 2014, às 04:33:34, Kevin Kofler escreveu:
Time zone abbreviations are useless, since they are not unique. Simply strip
them out of your string before passing to QDateTime.

Re: KF5: Parsing times with timezone abbreviations

By Kevin Kofler at 03/31/2014 - 06:53

Thiago Macieira wrote:
Nice theory, but there is no other way to know what time this actually is.
Unless you can offer a mapping from latitude and longitude to timezone, or a
way to automatically figure it out from "place name, country" (which is
especially fun for those countries that span multiple time zones, because
the place name can be a small town somewhere).

I do see the problem, e.g. I get "EST" as the timezone for Sydney,
Australia, which is obviously not the same as the US "EST". I suppose
KDateTime will do the wrong thing for that. :-(

Maybe we need a (timezone abbreviation, country) → timezone map (and an API
where I can just feed in the time including the abbreviation and the country
name and get a correct QDateTime; heck, for most countries, the abbreviation
could be ignored entirely, it only matters for huge countries such as the
USA or Russia)?

Kevin Kofler

Re: KF5: Parsing times with timezone abbreviations

By David Jarvie at 03/31/2014 - 09:10

On Mon, March 31, 2014 11:53 am, Kevin Kofler wrote:
They can't just be ignored for small countries, since they may contain a
daylight savings time indication.

Re: KF5: Parsing times with timezone abbreviations

By Kevin Kofler at 03/31/2014 - 14:35

David Jarvie wrote:
Except for one "evil" hour every year, a given local time with the date
included is either necessarily DST or necessarily non-DST.

That said, sure, if we are about to look at the abbreviation anyway, we may
just as well always do it.

Now for that BBC weather RSS issue, I figured out that it apparently always
reports the pubDate (which is unfortunately not the real date&time the
conditions are from, but the date&time the RSS was requested at) in the
local time of the place, given in a format like +1100. (That's the offset
for Sydney, at which I looked because of that Australian vs. US timezone
ambiguity.) So we could probably work with that, and hope that the reported
observation time is really always given in the same timezone as that pubDate
(and that this does not change in the future, e.g. to a pubDate in UTC).

But what do we do with other sources using this (admittedly broken) type of
timezone specification? There's even RFC-822 that specifies those 3-letter
specifications as one of the allowed formats, but only for "UT"="GMT"=UTC
and for the continental US time zones ([PMCE][SD]T). As a data point of what
other implementations do with that, the BSD strptime actually tries to
support RFC-822 completely if you pass "%z" (but does not support non-US
three-letter abbreviations), the glibc one didn't bother at all last I
checked (its "%z" supports only the +1100 type (ISO 8601) format).

Kevin Kofler

Re: KF5: Parsing times with timezone abbreviations

By Kevin Kofler at 03/30/2014 - 20:27

Hi,

about a week ago, I wrote:
One week later, I still didn't get a useful answer to this question. (I also
tried pinging #kde-devel on IRC and John Layt through the contact form on
his blog, neither has helped getting an answer either.)

The real-world code where this is needed:
<a href="https://bugs.kde.org/show_bug.cgi?id=330773#c20" title="https://bugs.kde.org/show_bug.cgi?id=330773#c20">https://bugs.kde.org/show_bug.cgi?id=330773#c20</a>
(This patch is against kde-workspace 4.11.7 and so just uses KDateTime, but
as I said, that is deprecated in KF5, so we will need something different
for the KF5 port.)

Do I have to conclude that there is no supported way to perform this very
basic task in KF5? Do we have to start requiring some third-party time-
handling library in the weather plasmoid?

Kevin Kofler

Re: KF5: Parsing times with timezone abbreviations

By Luca Beltrame at 03/24/2014 - 05:39

Can you try looking at KFormat? The KF5 porting notes explicitly mention
that.

Re: KF5: Parsing times with timezone abbreviations

By Kevin Kofler at 03/25/2014 - 15:19

Luca Beltrame wrote:
KFormat does not do any parsing.

Kevin Kofler