Search code examples
c++qthttp-headerstimezone

How to generate a time-stamp for HTTP-Header If-Modified-Since from QDateTime?


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.


Solution

  • I think this will do:

    QString modificationDate = fileinfo.lastModified().toUTC().toString("ddd, dd MMM yyyy hh:mm:ss") + " GMT";