C ++ 11 get current date and time as a string

What is the modern way to get date and time as strings in C ++ 11?

I know about std::put_time , but the link says that I will use it only in streams.

There is std::chrono::system_clock that provides to_time_t returning time as time_t and no date, right?

I could use a string stream like bames53: Date and time output in C ++ using std :: chrono , but this seems like a workaround.

+5
source share
5 answers

Using Howard Hinnantโ€™s free open source datetime library , you can get the current UTC time as std::string in one line of code:

 std::string s = date::format("%F %T", std::chrono::system_clock::now()); 

I just ran this and the line contains:

 2017-03-30 17:05:13.400455 

That's right, it even gives you complete accuracy. If you do not like this format, all strftime formatting flags are available. If you want to get your local time, there is also a clock library , although this is not only a title.

 std::string s = date::format("%F %T %Z", date::make_zoned(date::current_zone(), std::chrono::system_clock::now())); 

Output:

 2017-03-30 13:05:13.400455 EDT 
+5
source

Firstly, std::time_t really captures both the date and time, since it usually represents seconds since January 1, 1970.

There is not much support for handling dates in C ++ 11. You still have to depend on the promotion if you do not want to do this, mainly manually. Here's how to do it manually.

You can use it in thread safe mode and together with any std::chrono::*clock , for example std::system_clock , for example:

 std::string get_date_string(std::chrono::time_point t) { auto as_time_t = std::chrono::system_clock::to_time_t(t); struct tm tm; if (::gmtime_r(&as_time_t, &tm)) if (std::strftime(some_buffer, sizeof(some_buffer), "%F", &tm)) return std::string{some_buffer}; throw std::runtime_error("Failed to get current date as string"); } 

In another place you can specify:

 get_date_string(std::system_clock::now()); 

Relatively good, this solution is that at the API level you still use modern portable C ++ concepts such as std::chrono::time_point and, of course, std::string .

+3
source

You can use the code snippet below as it will serve your purpose. Here, use the header file time.h for the required localtime () function, and then using the strftime () function with the required parameters, you get the output and it returns it as a string.

 #include <iostream> #include <string> #include <time.h> std::string current_date(); std::string current_time(); int main(){ std::cout<<"Current date => "<<current_date()<<"\n"; std::cout<<"Current time => "<<current_time()<<"\n"; } std::string current_date(){ time_t now = time(NULL); struct tm tstruct; char buf[40]; tstruct = *localtime(&now); //format: day DD-MM-YYYY strftime(buf, sizeof(buf), "%A %d/%m/%Y", &tstruct); return buf; } std::string current_time(){ time_t now = time(NULL); struct tm tstruct; char buf[40]; tstruct = *localtime(&now); //format: HH:mm:ss strftime(buf, sizeof(buf), "%X", &tstruct); return buf; } 
+1
source

such a timestamp;

 #include <iostream> #include <chrono> #include <ctime> int main() { std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now(); std::time_t start_time = std::chrono::system_clock::to_time_t(now); char timedisplay[100]; struct tm buf; errno_t err = localtime_s(&buf, &start_time); if (std::strftime(timedisplay, sizeof(timedisplay), "%H:%M:%S", &buf)) { std::cout << timedisplay << '\n'; } } 

Date in a similar way.

0
source

Simpler:

 string CurrentDate() { std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); char buf[100] = {0}; std::strftime(buf, sizeof(buf), "%Y-%m-%d", std::localtime(&now)); return buf; } 

Adjust the format in the same way as the time.

Note that I suspect this will not work for multi-threaded code, since std::localtime() returns a pointer to the internal structure.

0
source

Source: https://habr.com/ru/post/1241308/


All Articles