I want to set an If-Modified-Since-header on a request and take the time from the timestamp on the file. So I extracted the timestamp into a QDateTime. I开发者_如何学运维 could generate something looking similar to the date-format HTTP uses, but my server and my client use different time-zones. Is there a way to get the timezone-string from Qt or another way to generate the string for the header. Here my code so far:
QLocale locale(QLocale::English, QLocale::UnitedStates);
QString modificationDate = locale.toString(fileinfo.lastModified(), "ddd, dd MMM yyyy hh:mm:ss 'GMT'");
I have to set the locale, because the system-locale is different and the server doesn't understand the format generated that way. It would be helpful, if I could get the timezone from Qt, so that I could add that to the String instead of the constant 'GMT'. But I didn't found a way to get the timezone Qt is using.
I think this will do:
QString modificationDate = fileinfo.lastModified().toUTC().toString("ddd, dd MMM yyyy hh:mm:ss") + "GMT";
Here is an slightly more elegant solution, which is used by Qt internally:
QByteArray QNetworkHeadersPrivate::toHttpDate(const QDateTime &dt)
{
return QLocale::c().toString(dt, QLatin1String("ddd, dd MMM yyyy hh:mm:ss 'GMT'"))
.toLatin1();
}
精彩评论