Convert from epoch to human readable date
How to convert epoch / UNIX timestamps to normal readable date/time using C.
Example C routine using STRFTIME. STRFTIME converts information from a time structure to a string form, and writes the string into the memory area pointed to by "string".
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now;
struct tm ts;
char buf[80];
// Get current time
time(&now);
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
ts = *localtime(&now);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
printf("%s\n", buf);
return 0;
}
For more information on strftime click here.Convert from human readable date to epoch
struct tm t;
time_t t_of_day;
t.tm_year = 2011-1900;
t.tm_mon = 7; // Month, 0 - jan
t.tm_mday = 8; // Day of the month
t.tm_hour = 16;
t.tm_min = 11;
t.tm_sec = 42;
t.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown
t_of_day = mktime(&t);
printf("seconds since the Epoch: %ld\n", (long) t_of_day)
Thanks to random_dude.
Comments and questions
blog comments powered by DisqusPages
Home
Tools
Epoch converter
Batch converter
Epoch clock
Epoch list
Time zone converter
LDAP converter
Unix hex timestamp
Mac timestamp
Bin/Oct/Hex converter
Programming
Routines by language
Epoch in C
Epoch in MySQL
Epoch in PERL
Epoch in PHP
Epoch in VBScript/ASP/JavaScript
Tools
Epoch converter
Batch converter
Epoch clock
Epoch list
Time zone converter
LDAP converter
Unix hex timestamp
Mac timestamp
Bin/Oct/Hex converter
Programming
Routines by language
Epoch in C
Epoch in MySQL
Epoch in PERL
Epoch in PHP
Epoch in VBScript/ASP/JavaScript
Date and Time
Calculate difference
between two dates
Week numbers
Weeks by year
Day numbers
Days by year
More
Comments & questions
Este sitio en Español
Related cookbooks
Unicode Tools
Character Set Tools
Follow us
Calculate difference
between two dates
Week numbers
Weeks by year
Day numbers
Days by year
More
Comments & questions
Este sitio en Español
Related cookbooks
Unicode Tools
Character Set Tools
Follow us
