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

Use of ctime needs to be phased out and updated to strftime #99

Open
rezalas opened this issue Mar 9, 2021 · 1 comment
Open

Use of ctime needs to be phased out and updated to strftime #99

rezalas opened this issue Mar 9, 2021 · 1 comment

Comments

@rezalas
Copy link
Owner

rezalas commented Mar 9, 2021

With ctime not being thread safe the suggested replacement is strftime for safety. Doing so requires a refactor of all time_t as well, since they can't be used with strftime and are also not thread safe.

see:
ctime c++ ref
strftime c++ ref

@sean-gilliam
Copy link
Collaborator

Here's a small snippet that outputs the same values for both ctime and strftime

Input:

#include <iostream>
#include <ctime>

int main()
{
    auto result = std::time(nullptr);
    std::cout << std::ctime(&result) << std::endl;

    char buffer [80];
    auto t = std::time(nullptr);
    auto lt = std::localtime(&t);

    //TODO: check the return value to see if bytes are filled
    std::strftime(buffer, 80, "%a %b %2d %T %Y%n", lt);
    std::cout <<  buffer << std::endl;
  
    return 0;
}

Output:

> clang++-7 -pthread -std=c++17 -o main main.cpp
> ./main
Thu Oct 27 22:18:34 2022

Thu Oct 27 22:18:34 2022

> 

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

No branches or pull requests

2 participants