CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
lumiTime.py
Go to the documentation of this file.
1 import os,sys,time,calendar,pytz
2 from datetime import datetime,timedelta
3 
4 class lumiTime(object):
5  def __init__(self):
6  self.coraltimefm='MM/DD/YY HH24:MI:SS'
7  self.pydatetimefm='%m/%d/%y %H:%M:%S'
8  self.nbx=3564
9  self.bunchspace_us=0.02495 #in microseconds
10  self.bunchspace_s=24.95e-09 #in seconds
11 
12  def timestampTodatetimeUTC(self,ts):
13  return datetime.fromtimestamp(ts,tz=pytz.utc)
14 
15  def LSDuration(self,norbits):
16  return timedelta(microseconds=(self.nbx*norbits*self.bunchspace_us))
17 
18  def OrbitDuration(self):
19  return timedelta(microseconds=(self.nbx*self.bunchspace_us))
20 
21  def OrbitToTimeStr(self,begStrTime,orbitnumber,begorbit=0,customfm=''):
22  '''
23  given a orbit number, return its corresponding time. Assuming begin time has orbit=0
24  '''
25  return self.DatetimeToStr(self.StrToDatetime(begStrTime)+(orbitnumber-begorbit)*self.OrbitDuration(),customfm=customfm)
26  def OrbitToTime(self,begStrTime,orbitnumber,begorbit=0,customfm=''):
27  '''
28  given a orbit number, return its corresponding time. Default run begin time counting from orbit=0
29  '''
30  return self.StrToDatetime(begStrTime,customfm=customfm)+(orbitnumber-begorbit)*self.OrbitDuration()
31  def OrbitToLocalTimestamp(self,begStrTime,orbitnumber,begorbit=0,customfm=''):
32  '''
33  given a orbit number, return its corresponding unixtimestamp. Default run begin time counting from orbit=0
34  '''
35  os.environ['TZ']='CET'
36  time.tzset()
37  orbittime=self.OrbitToTime(begStrTime,orbitnumber,begorbit=begorbit,customfm=customfm)
38  return time.mktime(orbittime.timetuple())+orbittime.microsecond/1e6
39 
40  def OrbitToUTCTimestamp(self,begStrTime,orbitnumber,begorbit=0,customfm=''):
41  '''
42  given a orbit number, return its corresponding unixtimestamp. Default run begin time counting from orbit=0
43  '''
44  os.environ['TZ']='UTC'
45  time.tzset()
46  orbittime=self.OrbitToTime(begStrTime,orbitnumber,begorbit=begorbit,customfm=customfm)
47  return time.mktime(orbittime.timetuple())+(orbittime.microsecond/1e6)
48  def StrToDatetime(self,strTime,customfm=''):
49  '''convert string timestamp to python datetime
50  '''
51  result=''
52  try:
53  if not customfm:
54  result=datetime.strptime(strTime,self.pydatetimefm)
55  else:
56  result=datetime.strptime(strTime,customfm)
57  except Exception,er:
58  print str(er)
59  return result
60  def DatetimeToStr(self,timeValue,customfm=''):
61  '''convert python datetime to string timestamp
62  '''
63  result=''
64  try:
65  if not customfm:
66  result=timeValue.strftime(self.pydatetimefm)
67  else:
68  result=timeValue.strftime(customfm)
69  except Exception,er:
70  print str(er)
71  return result
72 if __name__=='__main__':
73  begTimeStr='03/30/10 10:10:01'
75  print 'orbit 0 : ',c.OrbitToTime(begTimeStr,0,0)
76  print 'orbit 1 : ',c.OrbitToTime(begTimeStr,1,0)
77  print 'orbit 262144 : ',c.OrbitToTime(begTimeStr,262144,0)
78  print 'orbit 0 : ',c.OrbitToUTCTimestamp(begTimeStr,0,0);
79  print 'orbit 0 : ',c.OrbitToLocalTimestamp(begTimeStr,0,0);
def timestampTodatetimeUTC
Definition: lumiTime.py:12
def OrbitToUTCTimestamp
Definition: lumiTime.py:40
list object
Definition: dbtoconf.py:77
def OrbitToLocalTimestamp
Definition: lumiTime.py:31