PDA

View Full Version : Qt 5.7 QTimeZone::abbreviation()



ad5xj
30th June 2016, 19:50
I would like to display the local time zone as an abbreviation similar to CST, CDT, etc. The QTimeZone class returns "Central Daylight Time" for the abbreviation function. This does not seem to be abbreviated at all. I would have expected the common "CDT" abbreviation instead.

Here is the code:


QDateTime curtim = QDateTime::currentDateTime();
ui->lblCurTime->setText(curtim.toString("hh:mm:ss"));
mwd->TZ = curtim.timeZone();
QString abbr = mwd->TZ = curtim.timeZone().abbreviation();
ui->lblTZ->setText(abbr);


The output looks like 12:30:30 Central Daylight Time

Environment:
OS: Windows 10 32-bit
Qt: v5.7.1 MinGW 5.3.0 32-bit

Note: Same problem exists on Linux Mint 17.3 32-bit

What am I doing wrong?

anda_skoa
30th June 2016, 20:27
How does that even comile?

QTimeZone::abbreviation() has a required argument?

Anyway, have you tried QTimeZone::displayName() with QTimeZone::ShortName?

Cheers,
_

ad5xj
1st July 2016, 21:41
Well maybe it is a bug. Not sure. I will modify my code and try your suggestion.

jefftee
1st July 2016, 22:11
What am I doing wrong?
This works for me:



QDateTime curtim = QDateTime::currentDateTime();
QString abbr = curtim.timeZoneAbbreviation();
qDebug() << "Current time: " << curtim.toString("hh:mm:ss t");
qDebug() << "TZ=" << abbr;


Output:



Current time: "15:00:01 CDT"
TZ= "CDT"

ad5xj
1st July 2016, 22:22
Thanks jefftee that works great!