How to translate EPOCH values to human readable format
From Wiki-UX.info
Contents
Abstract[edit]
The following ANSI C program can be used to translate decimal or hexacimal EPOCH values to human readable format on the command shell.
Code[edit]
// This C program is provided as an as-is fix it comes with no support
// Wiki-UX.info makes no representations as to its fitness for purpose.
// It is up to whoever uses this program to ensure that whatever
// functionality it provides is what they require.
// This program translates EPOCH value, either hexdecimal or decimal
// to human readable date format
#include <stdio.h>
#include <time.h>
int errors(int errorcode);
int main(int argc, char** argv)
{
time_t now;
int i;
struct tm *ts;
char buf[80];
if(argc != 2)
return(errors(1));
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'd': fscanf(stdin, "%d", &now);
break;
case 'x': fscanf(stdin, "%x", &now);
printf("decimal = %d\n", now);
break;
default: return(errors(2));
}
}
}
/* Format and print the 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;
}
int errors(int errorcode) {
switch (errorcode) {
case 1:
fprintf(stderr, "usage: epocdate -d | -x\n");
break;
case 2:
fprintf(stderr, "Invalid argument!\nusage: epocdate -d | -x\n");
break;
}
return errorcode;
}
Usage[edit]
# ./epocdate
usage: epocdate -d | -x
Example[edit]
# echo "0x4a428a84" | ./epocdate -x
Wed 2009-06-24 14:20:20 CST
# echo "1245874820" | ./epocdate -d
Wed 2009-06-24 14:20:20 CST