From Wiki-UX.info

Wiki-UX / How to translate EPOCH values to human readable format
Jump to: navigation, search

How to translate EPOCH values to human readable format

Contents

Abstract

The following ANSI C program can be used to translate decimal or hexacimal EPOCH values to human readable format on the command shell.

Code

// 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

# ./epocdate
usage: epocdate -d | -x

Example

# echo "0x4a428a84" | ./epocdate -x
Wed 2009-06-24 14:20:20 CST
 
# echo "1245874820" | ./epocdate -d
Wed 2009-06-24 14:20:20 CST

Reference

Authors

This page was last modified on 22 July 2010, at 03:49. This page has been accessed 851 times.