Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:11

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