CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/RecoLuminosity/LumiDB/python/lumiTime.py

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