Search code examples
c++timezonewindowutcstrftime

std::strftime displays timezone UTC as UTC+1 using gmtime in windows


I am trying to understand why in windows (in linux works) std::strftime displays the timezone UTC as UTC+1.

If I run the next code

#include <ctime>
#include <chrono>
#include <string>
#include <iostream>

int main()
{
    std::time_t t = std::time(nullptr);
    
    char utc_str[80];
    std::strftime(utc_str, 80, "%H:%M:%S %z", std::gmtime(&t));
    std::string utcStd = { utc_str };
    std::cout << "UTC: " << utcStd << std::endl;

    char local_str[80];
    std::strftime(local_str, 80, "%H:%M:%S %z", std::localtime(&t));
    std::string localStd = { local_str };
    std::cout << "Local: " << std::string(localStd) << std::endl;

    return 0;
 }

I get the next output:

    UTC: 07:26:18 +0100
    Local: 09:26:18 +0200

Where the UTC and localtime times are correct but the UTC timezone is incorrect.

Any ideas?


Solution

  • Microsoft has been always famous for its implementations of C library functions to work somewhat differently than specified in standards. Other platform vendor doing that is Apple. As it has lasted for decades it must be is deliberate strategy.

    There are bugs reported about that time zone in Visual Studio implementation of strftime. Perhaps what you observe is other "bug", other manifestation of same "bug" or new manifestation of some "fix".

    Usage of some open source library can be less headache than what these library vendors provide. Typical suggestion is boost but I have observed it to be inefficient and to misbehave. The one by Howard Hinnant is quite decent.