How to get timestamp in c

I want to get a timestamp for my input to c. I wrote a function to get a timestamp. But when I return the variable I m, I get a different value.

My code is:

#include <stdio.h> #include <stdlib.h> #include <time.h> char* get_timestamp(){ time_t rawtime; struct tm * timeinfo; char buffer[16]; time (&rawtime); timeinfo = localtime (&rawtime); strftime (buffer,16,"%G%m%d%H%M%S",timeinfo); puts(buffer); return buffer; } int main() { puts(get_timestamp()); return 0; } 

output:

 20130315204815 Ir?0315204815 

Can someone help with this ... Thanks.

+4
source share
5 answers

You return a pointer to the stack variable, and therefore it is not valid for use after the function returns:

  char buffer[16]; 

Will be allocated on the stack in function. When you return, the stack is cleared and buffer no longer valid. With minimal modifications, this is probably the best function signature:

 void get_timestamp( char *buffer, size_t buffLen ) 

Assuming you correctly allocated a place for buffer before calling get_timestamp .

+5
source

buffer[16] is a local array that stops at the end of the char* get_timestamp() function. Then you return a pointer to an array that does not exist.

+6
source

As another said, you are using data that is on the stack, and ceased to exist after you leave the function that declared it. I see two simple possibilities to solve this problem:

Option 1: allocate a buffer variable in the calling function and pass the pointer to get_timestamp

 void get_timestamp( char *buffer, size_t buffersize ) { .... strftime (buffer,buffersize,"%G%m%d%H%M%S",timeinfo); } int main() { char buffer[16]; puts(get_timestamp(buffer,16)); return 0; } 

Note. / Editing: I threw off a very true point about passing buffer size to this proposed solution.

Option 2: if you cannot or do not want to change the signature of your function, you can use a static variable, but do not forget that static variables can cause problems in multi-threaded programs.

 static char buffer[16]; char* get_timestamp(){ ... } int main() { puts(get_timestamp()); return 0; } 

Of course, you could use malloc , but in this situation this seems redundant and more error prone than the two fixes you described.

+2
source

The string you are returning is an automatic variable. When you exit a function, access to this variable is not possible. According to the specifications, this behavior is undefined. Use malloc to highlight the line and everything will be fine. Just remember to release it later.

+1
source

Answering this question, I wanted the function to be a simple, friendly thread, not return char * (which is often tedious for control), thread safe and can stand on its feet. I have an aversion to functions that return char * or pointers that need to be controlled.

The function below does not call malloc.

The function takes no parameters and returns a timestamp. I think it works well.

 struct Timestamp { time_t seconds; long milliseconds; char timestring[32]; }; struct Timestamp getTimestamp() { char timebuffer[32] = {0}; struct timeval tv = {0}; struct tm *tmval = NULL; struct tm gmtval = {0}; struct timespec curtime = {0}; struct Timestamp timestamp; int i = 0; // Get current time clock_gettime(CLOCK_REALTIME, &curtime); // Set the fields timestamp.seconds = curtime.tv_sec; timestamp.milliseconds = round(curtime.tv_nsec/1.0e6); if((tmval = gmtime_r(&timestamp.seconds, &gmtval)) != NULL) { // Build the first part of the time strftime(timebuffer, sizeof timebuffer, "%Y-%m-%d %H:%M:%S", &gmtval); // Add the milliseconds part and build the time string snprintf(timestamp.timestring, sizeof timestamp.timestring, "%s.%03ld", timebuffer, timestamp.milliseconds); } return timestamp; } int main() { char timebuffer[64] = {0}; int i = 0; struct timespec sleeptime = {0, 5000000L}; struct Timestamp timestamp; for (i=0; i < 20; i++) { timestamp = getTimestamp(); printf("Time is: %s \n", timestamp.timestring); nanosleep(&sleeptime, NULL); } return 0; } 
+1
source

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


All Articles