How to get a suitable timestamp in c for logs?

I am creating a client-server application. I want to make some notes.

The server is in C. Now I am printing messages to the terminal. So I will probably just copy this into sprintf and add a timestamp. How can I make this timestamp? It should probably include date, hours, minutes, seconds.

+4
source share
2 answers
#include <time.h> void timestamp() { time_t ltime; /* calendar time */ ltime=time(NULL); /* get current cal time */ printf("%s",asctime( localtime(&ltime) ) ); } 

It just prints on my PC

 Wed Mar 07 12:27:29 2012 

Check out the full range of time related features. http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html

+13
source

Please find the version of Pavan's safe answer below.

 time_t ltime; struct tm result; char stime[32]; ltime = time(NULL); localtime_r(&ltime, &result); asctime_r(&result, stime); 

See this for more details.

+5
source

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


All Articles