00001 import os,sys,time,calendar
00002 from datetime import datetime,timedelta
00003
00004 class lumiTime(object):
00005 def __init__(self):
00006 self.coraltimefm='MM/DD/YY HH24:MI:SS.FF6'
00007 self.pydatetimefm='%m/%d/%y %H:%M:%S.%f'
00008 self.nbx=3564
00009 self.bunchspace_us=0.025
00010 self.bunchspace_s=25e-09
00011
00012 def LSDuration(self,norbits):
00013 return timedelta(microseconds=(self.nbx*norbits*self.bunchspace_us))
00014
00015 def OrbitDuration(self):
00016 return timedelta(microseconds=(self.nbx*self.bunchspace_us))
00017
00018 def OrbitToTimeStr(self,begStrTime,orbitnumber,begorbit=0):
00019 '''
00020 given a orbit number, return its corresponding time. Assuming begin time has orbit=0
00021 '''
00022 return self.DatetimeToStr(self.StrToDatetime(begStrTime)+(orbitnumber-begorbit)*self.OrbitDuration())
00023 def OrbitToTime(self,begStrTime,orbitnumber,begorbit=0):
00024 '''
00025 given a orbit number, return its corresponding time. Default run begin time counting from orbit=0
00026 '''
00027 return self.StrToDatetime(begStrTime)+(orbitnumber-begorbit)*self.OrbitDuration()
00028 def OrbitToLocalTimestamp(self,begStrTime,orbitnumber,begorbit=0):
00029 '''
00030 given a orbit number, return its corresponding unixtimestamp. Default run begin time counting from orbit=0
00031 '''
00032 os.environ['TZ']='CET'
00033 time.tzset()
00034 orbittime=self.OrbitToTime(begStrTime,orbitnumber,begorbit)
00035 return time.mktime(orbittime.timetuple())+orbittime.microsecond/1e6
00036
00037 def OrbitToUTCTimestamp(self,begStrTime,orbitnumber,begorbit=0):
00038 '''
00039 given a orbit number, return its corresponding unixtimestamp. Default run begin time counting from orbit=0
00040 '''
00041 os.environ['TZ']='UTC'
00042 time.tzset()
00043 orbittime=self.OrbitToTime(begStrTime,orbitnumber,begorbit)
00044 return time.mktime(orbittime.timetuple())+(orbittime.microsecond/1e6)
00045 def StrToDatetime(self,strTime,customfm=''):
00046 '''convert string timestamp to python datetime
00047 '''
00048 result=''
00049 try:
00050 if not customfm:
00051 result=datetime.strptime(strTime,self.pydatetimefm)
00052 else:
00053 result=datetime.strptime(strTime,customfm)
00054 except Exception,er:
00055 print str(er)
00056 return result
00057 def DatetimeToStr(self,timeValue,customfm=''):
00058 '''convert python datetime to string timestamp
00059 '''
00060 result=''
00061 try:
00062 if not customfm:
00063 result=timeValue.strftime(self.pydatetimefm)
00064 else:
00065 result=timeValue.strftime(customfm)
00066 except Exception,er:
00067 print str(er)
00068 return result
00069 if __name__=='__main__':
00070 begTimeStr='03/30/10 10:10:01.339198'
00071 c=lumiTime()
00072 print 'orbit 0 : ',c.OrbitToTime(begTimeStr,0,0)
00073 print 'orbit 1 : ',c.OrbitToTime(begTimeStr,1,0)
00074 print 'orbit 262144 : ',c.OrbitToTime(begTimeStr,262144,0)
00075 print 'orbit 0 : ',c.OrbitToUTCTimestamp(begTimeStr,0,0);
00076 print 'orbit 0 : ',c.OrbitToLocalTimestamp(begTimeStr,0,0);