Last active
October 3, 2019 06:14
-
-
Save gustavorv86/7569bd1790515bc070a389bb08f230c8 to your computer and use it in GitHub Desktop.
Function that returns the current date in string format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/time.h> | |
#include <time.h> | |
/** | |
* Function that returns the current date in string format. | |
* | |
* Output format example: "2018-1-4 10:17:12.508977" | |
* | |
* @param buffer String pointer. | |
* @param size String size. | |
*/ | |
void get_localtime(char * buffer, int size) { | |
struct timeval s_tv; | |
struct tm s_tm; | |
gettimeofday(&s_tv, NULL); | |
localtime_r(&s_tv.tv_sec, &s_tm); | |
snprintf(buffer, size, "%d-%d-%d %d:%d:%d.%li", | |
s_tm.tm_year + 1900, | |
s_tm.tm_mon + 1, | |
s_tm.tm_mday, | |
s_tm.tm_hour, | |
s_tm.tm_min, | |
s_tm.tm_sec, | |
s_tv.tv_usec | |
); | |
} | |
int main() { | |
char buffer[32]; | |
get_localtime(buffer, sizeof(buffer)); | |
printf("%s \n", buffer); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment