Created
August 16, 2016 04:25
-
-
Save gilson27/21fa18496d3ff09d3a3b0db96818fc4b to your computer and use it in GitHub Desktop.
A simple C function to print time
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 <string.h> | |
| #include <stdio.h> | |
| #include <time.h> | |
| #include <sys/time.h> | |
| #define LENGTH 256 | |
| void print_time(char *buf) | |
| { | |
| time_t t; | |
| struct timeval tv; | |
| gettimeofday(&tv, NULL); | |
| time(&t); | |
| sprintf(buf, 256 ,"%f", tv.tv_sec + (tv.tv_usec * 0.000001)); | |
| return buf; | |
| } | |
| int main() { | |
| char *buf; | |
| buf = (char*)malloc(LENGTH); | |
| if(buf==NULL) { | |
| fprintf(stderr, "Unable to alocate %d bytes", LENGTH) | |
| return -1; | |
| } | |
| memset(buf,'\0', LENGTH); | |
| print_time(buf); | |
| fprintf(stdout, "time=%s", buf); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment