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