#include <stdio.h>
#include <time.h>
#include <string.h>

/* Scale time back a bit, for shorter Message-ID's. */
#define INN_OFFSET 673416000L

static char ALPHABET[] = "0123456789abcdefghijklmnopqrstuv";

/*
**  Return a Radix-32 string as a number, or ~0 on error.
*/
unsigned long
Decode32(
  char *p)
{
  unsigned long l;
  char *cp;
  
  for (l = 0; *p; p++) {
    if ((cp = strchr(ALPHABET, *p)) == NULL)
      return ~0;
    l = (l << 5) + cp - ALPHABET;
  }
  return l;
}

int
main(
  int argc,
  char *argv[])
{
  char tid[32];
  char *pid;
  char *aux;
  char sep;
  time_t ptime;

  /* get message-id */
  if (argc > 1) {
    sscanf(argv[1], "<%31s", tid);
  } else {
    scanf("<%31s", tid);
  }
  /* decode and print time */
  if (NULL == (pid = strchr(tid, '$'))) {
    pid = strchr(tid, '@');
  }
  sep = *pid;
  *pid = '\0';
  ptime = Decode32(tid)+(INN_OFFSET);
  printf("Posting time (GMT)  : %s", asctime(gmtime(&ptime)));
  printf("Posting time (local): %s", asctime(localtime(&ptime)));
  /* decode and print process id (if present) */
  pid++;
  if (sep == '$') {
    if (NULL == (aux = strchr(pid, '$'))) {
      aux = strchr(pid, '@');
    }
    *aux = '\0';
    printf("Process Id          : %d\n", Decode32(pid));
  }

  return 0;
}


