CosMik
CosMik

Reputation: 51

Can I use another way to get the time from C++?

string GetTime()
{
    time_t Timev;
    struct tm * TimeInformation;
    time(&(time_t)Timev);
    (tm*)TimeInformation = localtime(&(time_t)Timev);
    char Timec[100];
    strftime((char*)Timec, 100, "[%X]:", (tm*)TimeInformation);
    string GetTimex((char*)Timec);
    return GetTimex;
};

Why do I get the warning

warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

Is there any other way to get time in format [Hour:Minutes:Second], and is it possible to shorten the code like int abc; abc=123 to int abc{123}?

Upvotes: 3

Views: 1161

Answers (4)

Howard Hinnant
Howard Hinnant

Reputation: 219578

If you are willing to install Howard Hinnant's free, open-source tz library, then GetTime() can be simplified down to:

#include "tz.h"
#include <string>

std::string
GetTime()
{
    using namespace std::chrono;
    using namespace date;
    return format("[%X]:", make_zoned(current_zone(), system_clock::now()));
}

This just output for me:

[10:42:32]:

Here are the installation directions (including for Windows).

Upvotes: 4

Toby Speight
Toby Speight

Reputation: 30992

The return value of std::localtime() (when it suceeds) is a

pointer to a static internal std::tm object

Further,

The structure may be shared between std::gmtime, std::localtime, and std::ctime, and may be overwritten on each invocation.

(from cppreference)

This means that if you're using any of these functions in other threads, you may have a data race. If you're sure that no other threads are using any of those functions (perhaps because your program is single-threaded), then you may safely ignore the warning.

It appears that your compiler is recommending you use its implementation-defined alternative - whether you do is up to you, but you might want to consider isolating any platform-dependency you then introduce.

Upvotes: 1

Jonas
Jonas

Reputation: 7017

You could use this example:

#include <iostream>
#include <iomanip> // std::put_time
#include <ctime>   // std::localtime, std::time, std::time_t, std::gmtime

int main()
{
    std::time_t t = std::time(nullptr);
    std::cout << "UTC:   " << std::put_time(std::gmtime(&t), "%X") << '\n';
    std::cout << "local: " << std::put_time(std::localtime(&t), "%X") << '\n';
}

Possible output:

UTC:   14:18:02
local: 14:18:02

And your GetTime would then be:

std::string GetTime()
{
    std::time_t t = std::time(nullptr);
    std::stringstream ss;
    ss << std::put_time(std::localtime(&t), "[%X]");
    return ss.str();
}

Upvotes: 2

Simone Cifani
Simone Cifani

Reputation: 784

localtime has an internal storage that is static, which means it is not threadsafe. Most systems do have threadsafe alternatives, but they are not part of the standard library. For example, Windows has localtime_s and Linux/Posix has localtime_r. The std library functions std::strftime and std::put_time could be a safer alternative, like explained in this article

Upvotes: 1

Related Questions