import time
import os
import sys
import string

# Find next non-whitespace character
def find_nonwhite(buf, index):
  while(index < len(buf)):
    wi = 0;
    while(wi < len(string.whitespace)):
      if(string.whitespace[wi] == buf[index]):
        break
      else:
        wi = wi + 1

    if(wi >= len(string.whitespace)):
      return index
    else:
      index = index + 1


# Slice out date and clean it up a bit
def slice(buf, frag):
  arr_start = find_nonwhite(buf, buf.find(frag)+len(frag))
  arr_end   = buf.find("\n", arr_start);
  arr = buf[arr_start:arr_end]
  arr=arr.replace("PST ","")  
  arr=arr.replace("PDT ","")  
  return arr

# read file into memory
file = sys.argv[1]
filename = file[(sys.argv[1].rfind("/")+1):]   
s = os.stat(file)
buf = os.read(os.open(file, os.O_RDONLY), s[6])

# String time to int
def timetoint(arr):
  try:
    arrt = time.strptime(arr, "%a %b %d %H:%M:%S %Y")
  except:
    arrt = 0

  return arrt
#  s = str(arrt[0]) + str(arrt[1]) + str(arrt[2]) + str(arrt[3]) + str(arrt[4]) + str(arrt[5]);
#  return s

# find and convert dates
arr = timetoint(slice(buf, "\n>Arrival-Date:"))
if(arr == 0):
  sys.exit(1)

chg = timetoint(slice(buf, "\nState-Changed-When:"))

if(chg == 0):
  print filename + "\tNot changed"
else:
  print filename + "\t" + time.strftime("%d.%m.%Y",arr) + "\t" + time.strftime("%d.%m.%Y",chg)
