File indexing completed on 2024-04-06 11:59:50
0001
0002
0003 from __future__ import print_function
0004 import sys
0005 import time
0006 import calendar
0007
0008 """ Converts between a 64bit timestamp and a human readable string
0009 usage: ./convertTime.py [-l] time1 [time2 ...]
0010 - "-l" to use local time
0011 - "time" is either a 64bit timestamp or a string formatted "DD/MM/YYYY HH:MM:SS"
0012 """
0013
0014
0015 def pack(high,low):
0016 """pack high,low 32bit unsigned int to one unsigned 64bit long long
0017 Note:the print value of result number may appear signed, if the sign bit is used.
0018 """
0019 h=high<<32
0020 return (h|low)
0021
0022 def secondsFromString(t, localTime = True):
0023 """convert from a string in the format output from timeStamptoDate to a 32bit seconds from the epoch.
0024 If the time is UTC, the boolean value localTime must be set to False.
0025 The format accepted is \"DD/MM/YYYY HH:MM:SS\". The year must be the full number.
0026 """
0027
0028 timeStruct = time.strptime(t, "%d/%m/%Y %H:%M:%S")
0029 if localTime:
0030
0031 return int(time.mktime(timeStruct))
0032 else:
0033
0034 return calendar.timegm(timeStruct)
0035
0036 def packFromString(s, localTime = True):
0037 """pack from a string in the format output from timeStamptoDate to a 64bit timestamp.
0038 If the time is UTC, the boolean value localTime must be set to False.
0039 The format accepted is \"DD/MM/YYYY HH:MM:SS\" . The year must be the full number.
0040 """
0041 return pack(secondsFromString(s, localTime), 0)
0042
0043 def unpack(i):
0044 """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
0045 """
0046 high=i>>32
0047 low=i&0xFFFFFFFF
0048 return(high,low)
0049
0050 def addZeros(time):
0051 """Adds a zero to the start of a single digit number"""
0052 timeString = str(time)
0053 if len(timeString) < 2:
0054 return ("0"+timeString)
0055 return timeString
0056
0057 def getMonth(s):
0058 months = { 'Jan':1, 'Feb':2, 'Mar':3, 'Apr': 4, 'May': 5, 'Jun': 6,
0059 'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12 }
0060 return months[s]
0061
0062 def timeStamptoDate(i, localTime = True):
0063 """convert 64bit timestamp to local date in string format.
0064 If the time is UTC, the boolean value localTime must be set to False.
0065 The format accepted is \"DD/MM/YYYY HH:MM:SS\" . The year must be the full number.
0066 """
0067
0068 try:
0069 if localTime:
0070
0071 date=time.ctime(unpack(i)[0])
0072 else:
0073
0074 date=time.asctime(time.gmtime(unpack(i)[0]))
0075
0076 date = date.split()
0077 date[1] = getMonth(date[1])
0078 date = addZeros(date[2]) +'/'+ addZeros(date[1]) +'/'+ date[4] +' '+ date[3]
0079 except:
0080
0081 print("Could not unpack time stamp %s, unpacked to %s!"%(i,unpack(i)[0]))
0082 date=-1
0083 return date
0084
0085 def printUsage():
0086 print('usage: ./convertTime.py time localTime')
0087 print(' - "time" is either a 64bit timestamp or a string formatted "DD/MM/YYYY HH:MM:SS"')
0088 print(' - "useUTC" is a bool that defaults to True (set to False for local time)')
0089
0090
0091
0092 def main(time, localTime=True):
0093
0094 if time.isdigit():
0095 time = long(time)
0096 return timeStamptoDate(time, localTime)
0097
0098
0099 else:
0100 return packFromString(time, localTime)
0101
0102
0103
0104 if __name__ == "__main__":
0105 args = sys.argv[:]
0106 if len(args) < 2 :
0107 printUsage()
0108 sys.exit(1)
0109 args = args[1:]
0110 if args[0]=='-h' or args[0]=='--help':
0111 printUsage()
0112 sys.exit(0)
0113 args = args[1:]
0114
0115
0116 useUTC = True
0117 if args[0]=='-l' or args[0]=='--localtime':
0118 useUTC = False
0119 args=args[1:]
0120
0121 for time0 in args:
0122 time1 = main(time0, not useUTC)
0123 print(time0, '->', time1)
0124
0125
0126
0127