#encoding=utf-8
'''
Created on Nov 18, 2015

@author: lowitty
'''
import sys

#get "YYYY-mm-dd HH:MM:SS" format time from "07:df:0a:07:0e:14:01:00:2b:08:00"
def getLongTimeFromSNMPTimeStamp(s, isFull = False):
    try:
        s = s.strip().split(':')
        y = str(int(s[0]) * 256 + int(s[1], 16))
        im = int(s[2], 16)
        m = ('0' + str(im)) if(im < 10) else str(im)
        id = int(s[3], 16)
        d = ('0' + str(id)) if(id < 10) else str(id)
        iH = int(s[4], 16)
        H = '0' + str(iH) if (iH < 10) else str(iH)
        iM = int(s[5], 16)
        M = '0' + str(iM) if (iM < 10) else str(iM)
        iS = int(s[6], 16)
        S = '0' + str(iS) if (iS < 10) else str(iS)
        iDs = int(s[7], 16)
        Ds = '0' + str(iDs) if (iDs < 10) else str(iDs)
        
        Z = str(chr(int(s[8], 16)))
        iZH = int(s[9], 16)
        iZM = int(s[10], 16)
        ZH = '0' + str(iZH) if (iZH < 10) else str(iZH)
        ZM = '0' + str(iZM) if (iZM < 10) else str(iZM)
        if(isFull):
            return y + '-' + m + '-' + d + ' ' + H + ':' + M + ':' + S + '.' + Ds + Z + ZH + ZM
        else:
            return y + '-' + m + '-' + d + ' ' + H + ':' + M + ':' + S
    except Exception as e:
        return 'Parse failed, ERROR MSG: ' + str(e)

if __name__ == '__main__':
    lenOfPara = len(sys.argv)
    if(lenOfPara < 2):
        print 'This tool is used to cast time from SNMP time stamp. Example: python castsnmptimestamp.py 07:df:0a:07:0e:14:01:00:2b:08:00'
    elif(lenOfPara == 2):
        if(str(sys.argv[1]).strip().upper() == 'F' or str(sys.argv[1]).strip().upper() == 'FULL'):
            print 'Please feed at least one SNMP time stamp. Example: python castsnmptimestamp.py full 07:df:0a:07:0e:14:01:00:2b:08:00'
        else:
            if(len(str(sys.argv[1]).strip().split(':')) == 11):
                print "1:\t" + getLongTimeFromSNMPTimeStamp(str(sys.argv[1]))
            else:
                print "1:\tTime format is wrong! Time: " + str(sys.argv[1]).strip()
    else:
        if(str(sys.argv[1]).strip().upper() == 'F' or str(sys.argv[1]).strip().upper() == 'FULL'):
            for i in range(2, len(sys.argv)):
                print str(i - 1) + ":\t" + getLongTimeFromSNMPTimeStamp(str(sys.argv[i]), True)
        else:
            for i in range(1, len(sys.argv)):
                print str(i) + ":\t" + getLongTimeFromSNMPTimeStamp(str(sys.argv[i]))