msys2 / MINGW-packages

Package scripts for MinGW-w64 targets to build under MSYS2.
https://packages.msys2.org
BSD 3-Clause "New" or "Revised" License
2.25k stars 1.21k forks source link

localtime wtih strftime does not seem to work properly #9472

Open justinmcgrath opened 3 years ago

justinmcgrath commented 3 years ago

There's a very good chance I just don't understand what is going on, but this does not work as I'd expect (not my code).

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

using namespace std::chrono;
using std::string;

string current_iso8601_datetime()
{
    system_clock::time_point now = system_clock::now();
    time_t timet = system_clock::to_time_t(now);
    std::tm tm{};
    string format = string("%I:%M:%S%z");
    localtime_s(&tm, &timet);
    string result = string(255, 0);
    std::cout << result << std::endl;
    size_t length = std::strftime(&result[0], result.size(), format.c_str(), &tm);
    result.resize(length);
    return result;
}

int main()
{
    std::cout << current_iso8601_datetime() << std::endl;
}

Using MSYS2 (installed with msys2-x86_64-20210725.exe) and g++ version 10.3.0, this outputs something like "11:55:59Central Daylight Time", but it should be "11:55:59-0500".

"%z" is supposed to return the offset from UTC. "%Z" is supposed to return the time zone name. Both are outputting the name. On a similar vein, "%F" and "%T" do not work at all.

Compiling on Ubuntu 20.04 with whatever that latest g++ is works as expected.

Links to strftime descriptions for MS and the standard: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strftime-wcsftime-strftime-l-wcsftime-l?view=msvc-160 https://www.cplusplus.com/reference/ctime/strftime/

lazka commented 3 years ago

Unrelated to the problem, do you maybe want to use the mingw gcc instead? pacman -S mingw-w64-x86_64-toolchain -> open a mingw64 shell, use g++ there.

justinmcgrath commented 3 years ago

That is what I've done, so maybe I should I report this to mingw instead?

justinmcgrath commented 3 years ago

Yeah, I think I've confused something things. If I use an MSYS terminal, and change localtime_s(&tm, &timet); to localtime_r(&timet, &tm); in order to match the different libraries, then everything works correctly. The problem appears to be in mingw.

lazka commented 3 years ago

I see. I get under mingw64: 08:53:00W. Europe Daylight Time and under ucrt64: 08:53:23+0200

lazka commented 3 years ago

So it looks to me that msvcrt is lacking those features. This looks related: https://sourceforge.net/p/mingw-w64/bugs/793/

If you depend on your mingw builds behaving like with MSVC I'd suggest to use the ucrt64 (or clang) environment instead. See https://www.msys2.org/docs/environments/ for details

justinmcgrath commented 3 years ago

Ah, I haven't used msys in a while and didn't realize it worked this way. How do I install the ucrt64 environment? I can't seem to figure it out.

lazka commented 3 years ago

It's there by default, C:/msys64/ucrt64.exe. its packages/groups start with mingw-w64-ucrt-x86_64-, for example pacman -S mingw-w64-ucrt-x86_64-toolchain

justinmcgrath commented 3 years ago

Ah, thank you.

On Thu, Aug 26, 2021 at 4:22 PM Christoph Reiter @.***> wrote:

It's there by default, C:/msys64/ucrt64.exe. its packages/groups start with mingw-w64-ucrt-x86_64-, for example pacman -S mingw-w64-ucrt-x86_64-toolchain

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/msys2/MINGW-packages/issues/9472#issuecomment-906752398, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACB3DN3WDGFG66HUCYTKM4TT62WCLANCNFSM5C37AYBQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.