[Interest] Working with QDateTime's timezone information
Jan Kundrát
jkt at flaska.net
Tue Jun 12 16:26:08 CEST 2012
Hi, my application receives some dates in textual formats from the
Internet; some of them are in RFC2822's format "Mon, 11 Jun 2012
13:35:56 -0700" (and other variants due to backward compatibility),
others in RFC3501's "date-time" format which is "07-Mar-2007 15:03:32
+0100".
I'd like to save them into a QDataStream for further retrieval and after
loading them back, I'd love to show them both converted to the user's
local time and also in the original format, including the original time
zone information. I haven't found a way of doing this in Qt and would
like to ask for help about that.
Right now my code makes the parsing by hand ([1] for the RFC2822 dates
and [2] for the IMAP's format) because I have to work with the timezone
from information the string and QDateTime's public functions don't seem
to offer a way of doing that when working with custom date formats which
I need. After the timestamp is parsed, I add/subtract the correct number
of seconds from the resulting QDateTime and afterwards I tell Qt that my
resulting QDateTime is in the UTC format. After that, I can easily show
it converted to the user's local time.
What I'm missing are two bits:
- A public API for telling Qt "hey, set the date to exactly this value
and remember the timezone, which is *this*". I could probably hack
around this one by constructing a string from the already parsed
QDateTime, adding my TZ information to the end of that and calling
QDateTime::fromString(Qt::ISODate), but that looks like a crude hack.
Essentially the Qt::ISODate format specifier looks like the only
publicly available function which accepts timezone specification.
- A formatting specifier used for producing the TZ information in a text
form. My code is an e-mail reader and I don't rely on locale's way of
showing data (I want to show just "10:30" for today's mails, for
example, see [3]). It seems that there's no publicly available
format-string specifier which could produce the TZ info. Again, I can
probably hack around that by doing stuff similar to how I produce
"current time" in RFC2822 [4] and manual operations on the formatting
string (ie. introduce a special application-specific modifier "Z" for
timezone information, check the format string for its occurrence, split
the formatting specifier into two parts, before the specifier and after
that, call QDateTime's toString() on them and add the determined TZ info
back manually, but that looks like an awful amount of work.
So, the questions:
- Is there a publicly available way of doing what I want to do, ie. am I
missing something?
- Does Qt5 change anything in this matter?
- Shall I bother with this fancy formatting using an approach I've
outlined above, including that string -> QDateTime -> string ->
QDateTime conversion?
- Would I hit any obstacle in Qt 4.7+ if I decied to use QDateTime's
*internal* functions setUtcOffset() and utcOffset()? I see that I
shouldn't be using them, but in this case it seems to me that they
publish information which is only hardly available in other ways, so
it's *so* tempting to just have a look at them.
I'm looking forward to your opinion.
With kind regards,
Jan
[1]
https://projects.flaska.net/projects/trojita/repository/revisions/d365ab2/entry/src/Imap/Parser/LowLevelParser.cpp#L319
[2]
https://projects.flaska.net/projects/trojita/repository/revisions/d365ab2/entry/src/Imap/Parser/Response.cpp#L696
[3]
https://projects.flaska.net/projects/trojita/repository/revisions/d365ab2/entry/src/Imap/Model/PrettyMsgListModel.cpp#L174
[4]
https://projects.flaska.net/projects/trojita/repository/revisions/d365ab2/entry/src/Imap/Model/Utils.cpp#L474
--
Trojita, a fast e-mail client -- http://trojita.flaska.net/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120612/c428e7f6/attachment.sig>
More information about the Interest
mailing list