Skip to content

Instantly share code, notes, and snippets.

@gilson27
Created August 16, 2016 04:25
Show Gist options
  • Select an option

  • Save gilson27/21fa18496d3ff09d3a3b0db96818fc4b to your computer and use it in GitHub Desktop.

Select an option

Save gilson27/21fa18496d3ff09d3a3b0db96818fc4b to your computer and use it in GitHub Desktop.
A simple C function to print time
#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