Qt QDateTime toString("h:m:s ap") ap/a/AP/a missing - qt

I've noted the "ap/a/AP/a" is missing while converting a date to string. For "h:m:s ap", i.e., I get "11:5:42 ". The same happens for each of the "ap/a/AP/a" forms.
What I'm missing?
void DecoderBr1::recordOnFile(QDateTime dateTime, QByteArray ba)
{
QString filename(dateTime.toString("yyyy MMMM dd#HH.mm.ss zzz ap"));
filename.append(".log");
Recorder recorder;
recorder.recordFile(filename, ba);
}

It depends on your locale. Not every locale support AM/PM format.
For example, my default locale is "it_IT" and does not print "AM/PM". Setting another locale (e.g. "en_EN") instead works as expected.
QDateTime t = QDateTime::fromString("2015-07-16T19:20:30+01:00", Qt::ISODate);
QString st = t.toString("yyyy MMMM dd#HH.mm.ss zzz ap");
QString locale_st_HH = QLocale("en_EN").toString(t, "yyyy MMMM dd#HH.mm.ss zzz ap");
QString locale_st_hh = QLocale("en_EN").toString(t, "yyyy MMMM dd#hh.mm.ss zzz ap");
qDebug() << st;
// With italian locale does not print AM/PM
// "2015 luglio 16#19.20.30 000 "
qDebug() << locale_st_HH;
// With en_EN locale it works
//"2015 July 16#19.20.30 000 pm"
qDebug() << locale_st_hh;
// With en_EN locale it works
// With hh it prints 07 pm instead of 19 pm // Credits to #t3ft3l--i
//"2015 July 16#07.20.30 000 pm"

Not all locale support this format of QDateTime output.
For result you are need create variable QLocale with parameters locale(language, country), which support it. For example:
QLocale eng(QLocale::English, QLocale::UnitedStates);
Then you can use QLocale::toString() method with chosen locale like this:
qDebug() << eng.toString(datetime, "yyyy MMMM dd#HH.mm.ss zzz ap");
Works for me at your example. And this way don't substitute your native locale.

Related

Converting ISO::DateTime to number format

I want to change the date time format to numbers only so that it can be incremented.
Current Implementation is saved as QString 2019-03-13T09:01:22+01:0
Expected result: 201903120858031
qt is a really powerful framework, you are just coding a solution in a wrong way.
Imagine the pain in the back just validating dates, leap years etc etc
every date and dateTime has the methods to do calendar math operations.
see this example as ref:
//
QDateTime dateOrigin = QDateTime::currentDateTime();
QDateTime ReturnDate = QDateTime::currentDateTime();
QString isoDate{dateOrigin.toString(Qt::ISODate)};
qDebug() << "before: " << isoDate;
ReturnDate = dateOrigin.addDays(1);
ReturnDate = dateOrigin.addMonths(1);
ReturnDate = dateOrigin.addYears(1);
qDebug() << "after: " << ReturnDate.toString(Qt::ISODate);
//
update:
lets suppose that 201903120858031 is a date time formated as yyyyMMddHHmmss, then you can convert that to a long
//201903120858031
//yyyyMMddHHmmss
qDebug() << "after: " << ReturnDate.toString("yyyyMMddHHmmss");
qDebug() << "after as long number: " << ReturnDate.toString("yyyyMMddHHmmss").toLong();
producing the output:
before: "2019-02-28T12:43:33"
after: "20200228124333"
after as number: 20200228124333

Read QDateTime from QDataStream giving null

I have the following region of data being read into a QDataStream object :
DE 07 05 19 0E 28 1A
This should translate to the date/time: 25-05-2014 15:40:26
I am trying to use the following to read this into a QDateTime variable:
QFile fileIn(iFile);
if (!fileIn.open(QIODevice::ReadOnly)) return;
QDataStream data(&fileIn);
data.setByteOrder(QDataStream::LittleEndian);
data.setVersion(QDataStream::Qt_5_0);
data.skipRawData(32);
.
.
QDateTime time;
data >> time;
qDebug () << time.date();
Instead I get null/blank in time.
Output is:
QDate("")
The first thing here is how on earth this data is serialised. I've had a look and can't work it out: each byte doesn't nicely convert to each part of date/time, the bytes you are trying to reverse engineer aren't the epoch value of that date converted into hex. You can't expect QDateTime to magically know your raw data format. Report back with data format and I'll try and help.

Display month and day in localized format

I need to get correct month and day format for various locales.
I have QLable and QDate, label must display month and day. I try format date by QLocale.
//Yes, I got system locale and set is as default
QLocale::toString(date, "MMMM d");
But result is incorrect.
For example, "MMMM d" in German locale equal to:
"d. MMMM"
for French is:
"d MMMM"
How to transform "MMMM d" format to locale settings in Qt 4.8?
Thanks!
P.S. In javascript I use following code
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
var options = {month: 'long', day: 'numeric' };
console.log(date.toLocaleDateString('de-DE', options));
One way to do it would be to parse QLocale::LongFormat:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QList<QLocale> locales;
locales << QLocale(QLocale::English);
locales << QLocale(QLocale::German);
locales << QLocale(QLocale::French);
locales << QLocale(QLocale::Russian);
locales << QLocale(QLocale::Chinese);
locales << QLocale(QLocale::Korean);
foreach(QLocale locale, locales) {
QString format = locale.dateFormat(QLocale::LongFormat);
QRegExp rx("([^d]d(?!d)[^,;]?\\s?|M+.?){2}");
rx.indexIn(format);
QString localed = rx.cap(0).trimmed();
qDebug() << locale.bcp47Name() << "\t" << localed << "\t" << locale.toString(QDateTime::currentDateTime(), localed);
}
return app.exec();
}
Outputs:
"en-US" "MMMM d" "March 17"
"de-DE" "d. MMMM" "17. März"
"fr-FR" "d MMMM" "17 mars"
"ru-RU" "d MMMM" "17 марта"
"zh-CN" "M月d日" "3月17日"
"ko-KR" "M월 d일" "3월 17일"
So, I used ICU library. DateTimePatternGenerator solve my problem

QT convert string to UTF-8

I'm having some troubles converting a string Bob Rosén to Bob Rosén
Tried:
QString str = v.toObject().value("name").toString(); // Contains Bob Rosén
qDebug() << str.toUtf8(); // Outputs Bob Rosén
Any ideas?
Following works for me. A more elegant solution may exist.
QString str = "Bob Rosén";
qDebug() << QString::fromUtf8(str.toLatin1().constData());

How to convert from timestamp to date in Qt?

I have a Unix timestamp and I need to convert it to human readable date + time. How can it be done in Qt?
int unixTime = 1234567890;
QDateTime timestamp;
timestamp.setTime_t(unixTime);
qDebug() << timestamp.toString(Qt::SystemLocaleShortDate);
That should get you going. Like Matthew said, see QDateTime.setTime_t, as well as QDateTime.toString. The toString has an enumeration with several different options, as well as an overload where you can pass a string allowing however much customization you like.
QDateTime.setTime_t
You can use the static function: "fromTime_t", like:
qDebug() << QDateTime::fromTime_t(your_time_stamp).toString("dd/MM/yyyy hh:mm:ss");
One good example
qint64 qiTimestamp=QDateTime::currentMSecsSinceEpoch();
QDateTime dt;
dt.setTime_t(qiTimestamp/1000);
ui->lineEdit_DateTime->setText(dt.toString("yyyy-MM-dd hh:mm:ss"));
Note that QDateTime::setTime_t() has been deprecated, use QDateTime::setSecsSinceEpoch(). Same for fromTime_t(): use QDateTime::fromSecsSinceEpoch().
As mentioned, instead of using the obsolete _t members here is a working example (Qt 5).
qint64 llTS=1591132400;
QString szFull=QDateTime::fromSecsSinceEpoch(llTS).toString("dddd d MMMM yyyy hh:mm:ss");
szFull: Tuesday 2 June 2020 23:13:20
Source: QDateTime (Qt 5)
Date formatting: QDate
Time formatting: QTime

Resources