PDA

View Full Version : Qt ISO 8601 time support



kangaba
6th September 2015, 22:20
Hi,
Can any Qt5 time related class deal with time formatted as "PT23H12M48S"?
I tried QTime and QDateTime and none works.

Example:
QString str = "PT23H12M48S";
auto t = QDateTime::fromString(str);
qDebug() << "time is:" << t.toString("HH:mm::ss"); // prints empty string

ChrisW67
6th September 2015, 22:42
That encoding is not the time but a duration (or part of a time interval), i.e. 23 hours, 12 minutes and 48 seconds not 11:12:48 PM. Nothing I am aware of handles that directly in Qt. You could disassemble it with a QRegularExpression and build a number of seconds fairly easily, but there are quite a few variations you need to be careful of in the standard. You also need to be careful if you are adding durations to absolute times around daylight savings changes.

kangaba
6th September 2015, 23:04
Thanks for the intelligent answer. Right, that's a duration. For some reason the ODF standard (LibreOffice) stores time values in this format. Alright, I'll parse it back and forth myself.

ChrisW67
7th September 2015, 12:25
ISO 8601 also defines a format for absolute time and date, i.e. YYYY-MM-DDThh:mm:ss, and that is handled by Qt's QDateTime.

AFAICT the ODF uses both XML dateTime and duration (inspired by ISO 8601) in different contexts.