Skip to content

Instantly share code, notes, and snippets.

@sbp
Created April 18, 2013 17:48
Show Gist options
  • Save sbp/5414770 to your computer and use it in GitHub Desktop.
Save sbp/5414770 to your computer and use it in GitHub Desktop.
Print a unixtime
#include <math.h>
#include <stdio.h>
#include <time.h>
int main() {
double time;
struct tm *gm_t;
size_t size;
char s[128];
time = 1366303735.958;
int unixtime = (int)floor(time);
// http://stackoverflow.com/questions/5589383
// Extract fractional part of double *efficiently* in C
double fraction = time - ((long)time); // or int64_t
gm_t = gmtime((time_t *)&unixtime);
size = strftime(s, sizeof(s), "%a, %d %b %Y %T.%%s %z\n", gm_t);
char *fr[8];
s[size] = 0;
sprintf(fr, "%f", fraction);
// printf("%s", (char *)&fr + 2);
printf(s, (char *)&fr + 2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment