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
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.orbits_per_ls=262144 # a lumisection is 2**18 orbits
10  self.bunchspace_us=0.02495 #in microseconds
11  self.bunchspace_s=24.95e-09 #in seconds
12 
13  #def timestampTodatetimeUTC(self,ts):
14  ##return datetime.fromtimestamp(ts,tz=pytz.utc)
15  #return datetime.utcfromtimestamp(ts)
16 
17  def LSDuration(self):
18  return self.OrbitDuration(self.orbits_per_ls)
19 
20  def OrbitDuration(self, norbits = 1):
21  return timedelta(microseconds=(self.nbx*norbits*self.bunchspace_us))
22 
23  def OrbitToTimeStr(self,begStrTime,orbitnumber,begorbit=0,customfm=''):
24  '''
25  given a orbit number, return its corresponding time. Assuming begin time has orbit=0
26  '''
27  return self.DatetimeToStr(self.StrToDatetime(begStrTime)+self.OrbitDuration(orbitnumber-begorbit),customfm=customfm)
28 
29  def OrbitToTime(self,begStrTime,orbitnumber,begorbit=0,customfm=''):
30  '''
31  given a orbit number, return its corresponding time. Default run begin time counting from orbit=0
32  '''
33  return self.StrToDatetime(begStrTime,customfm=customfm)+self.OrbitDuration(orbitnumber-begorbit)
34 
35  def OrbitToLocalTimestamp(self,begStrTime,orbitnumber,begorbit=0,customfm=''):
36  '''
37  given a orbit number, return its corresponding unixtimestamp. Default run begin time counting from orbit=0
38  '''
39  os.environ['TZ']='CET'
40  time.tzset()
41  orbittime=self.OrbitToTime(begStrTime,orbitnumber,begorbit=begorbit,customfm=customfm)
42  return time.mktime(orbittime.timetuple())+orbittime.microsecond/1e6
43 
44  def OrbitToUTCTimestamp(self,begStrTime,orbitnumber,begorbit=0,customfm=''):
45  '''
46  given a orbit number, return its corresponding unixtimestamp. Default run begin time counting from orbit=0
47  '''
48  os.environ['TZ']='UTC'
49  time.tzset()
50  orbittime=self.OrbitToTime(begStrTime,orbitnumber,begorbit=begorbit,customfm=customfm)
51  return time.mktime(orbittime.timetuple())+(orbittime.microsecond/1e6)
52  def StrToDatetime(self,strTime,customfm=''):
53  '''convert string timestamp to python datetime
54  '''
55  result=''
56  try:
57  if not customfm:
58  result=datetime.strptime(strTime,self.pydatetimefm)
59  else:
60  result=datetime.strptime(strTime,customfm)
61  except Exception as er:
62  print str(er)
63  return result
64  def DatetimeToStr(self,timeValue,customfm=''):
65  '''convert python datetime to string timestamp
66  '''
67  result=''
68  try:
69  if not customfm:
70  result=timeValue.strftime(self.pydatetimefm)
71  else:
72  result=timeValue.strftime(customfm)
73  except Exception as er:
74  print str(er)
75  return result
76 if __name__=='__main__':
77  begTimeStr='03/30/10 10:10:01'
79  print 'orbit 0 : ',c.OrbitToTime(begTimeStr,0,0)
80  print 'orbit 1 : ',c.OrbitToTime(begTimeStr,1,0)
81  print 'orbit 262144 : ',c.OrbitToTime(begTimeStr,262144,0)
82  print 'orbit 0 : ',c.OrbitToUTCTimestamp(begTimeStr,0,0);
83  print 'orbit 0 : ',c.OrbitToLocalTimestamp(begTimeStr,0,0);
def LSDuration
return datetime.fromtimestamp(ts,tz=pytz.utc) return datetime.utcfromtimestamp(ts) ...
Definition: lumiTime.py:17
def OrbitToUTCTimestamp
Definition: lumiTime.py:44
def OrbitToLocalTimestamp
Definition: lumiTime.py:35