CMS 3D CMS Logo

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