Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

localtime wtih strftime does not seem to work properly #9472

Open
justinmcgrath opened this issue Aug 24, 2021 · 8 comments
Open

localtime wtih strftime does not seem to work properly #9472

justinmcgrath opened this issue Aug 24, 2021 · 8 comments

Comments

@justinmcgrath
Copy link

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
Copy link
Member

lazka commented Aug 26, 2021

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
Copy link
Author

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

@lazka lazka transferred this issue from msys2/MSYS2-packages Aug 26, 2021
@justinmcgrath
Copy link
Author

justinmcgrath commented Aug 26, 2021

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
Copy link
Member

lazka commented Aug 26, 2021

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

@lazka
Copy link
Member

lazka commented Aug 26, 2021

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
Copy link
Author

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
Copy link
Member

lazka commented Aug 26, 2021

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
Copy link
Author

justinmcgrath commented Aug 26, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants