ATLAS Offline Software
TimeDivider.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <algorithm>
6 #include <time.h>
7 
8 #include "TimeDivider.h"
9 
10 TimeDivider::TimeDivider(unsigned int intervals, unsigned int duration, unit u )
11  : m_intervals(intervals),
12  m_duration(duration),
13  m_unit(u),
14  m_current_interval(0) {
15  unsigned int temp_iv;
16  time_t temp_time = time(0);
17  m_beginning = temp_time;
18  isPassed(temp_time, temp_iv, temp_iv);
19 }
20 
21 bool TimeDivider::isPassed(time_t time, unsigned int& newinterval, unsigned int& oldinterval) {
22 
23  // convert to tm structure for seconds & minutes
24  struct tm t;
25  gmtime_r(&time, &t);
26  unsigned count = 0;
27  if (m_unit == seconds) {
28  count = t.tm_sec;
29  } else if ( m_unit == minutes ) {
30  count = t.tm_min;
31  }
32 
33  // calculate next interval
34  unsigned int iv = (count % (m_intervals*m_duration))/m_duration;
35 
36  // is new interval differnt than recently returned? If so signal it.
37  if ( m_current_interval != iv ) {
38  oldinterval = m_current_interval;
39  newinterval = m_current_interval = iv;
40  m_beginning = time;
41  return true;
42  }
43  newinterval = oldinterval = m_current_interval;
44  return false;
45 }
46 
47 unsigned int TimeDivider::forcePassed(time_t time, unsigned int& interval ) {
49  return std::max((time - m_beginning), (long int)1); // never return 0
50 }
51 
52 
53 /* this can be used to test TimeDivider
54  int main() {
55  TimeDivider ts(6, 10, TimeDivider::seconds);
56  TimeDivider sts(10, 5, TimeDivider::seconds);
57  TimeDivider tmin(3, 1, TimeDivider::minutes);
58 
59  for ( int i = 0; i < 900; ++i ) {
60 
61  usleep(500000);
62  time_t t = time(0);
63  tm gmt = *gmtime(&t);
64  unsigned interval;
65  bool switchNow;
66  switchNow = ts.isPassed(t, interval);
67  cout << "second divider " << switchNow << " to " << interval << " " << ctime(&t);
68 
69  //switchNow = sts.isPassed(t, interval);
70  // cout << "second (short) divider " << switchNow << " to " << interval << " " << ctime(&t);
71 
72  // switchNow = tmin.isPassed(t, interval);
73  // cout << "minutes divider " << switchNow << " to " << interval << " " << gmt.tm_min << ":" << gmt.tm_sec << endl;
74 
75  }
76  }
77 */
TimeDivider.h
max
#define max(a, b)
Definition: cfImp.cxx:41
TimeDivider::m_intervals
unsigned int m_intervals
Definition: TimeDivider.h:50
TimeDivider::TimeDivider
TimeDivider(unsigned int intervals, unsigned int duration, unit u)
construct the TimeDivider
Definition: TimeDivider.cxx:10
TimeDivider::isPassed
bool isPassed(time_t time, unsigned int &newinterval, unsigned int &oldinterval)
method to know if one shoudl switch to new interval
Definition: TimeDivider.cxx:21
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
TimeDivider::m_duration
unsigned int m_duration
Definition: TimeDivider.h:51
TimeDivider::m_beginning
time_t m_beginning
Definition: TimeDivider.h:54
TimeDivider::forcePassed
unsigned int forcePassed(time_t time, unsigned int &oldinterval)
method
Definition: TimeDivider.cxx:47
getLatestRuns.interval
interval
Definition: getLatestRuns.py:24
PixelAthHitMonAlgCfg.duration
duration
Definition: PixelAthHitMonAlgCfg.py:152
TimeDivider::m_current_interval
unsigned int m_current_interval
Definition: TimeDivider.h:53
TimeDivider::seconds
@ seconds
Definition: TimeDivider.h:22
TimeDivider::unit
unit
Definition: TimeDivider.h:22
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
TimeDivider::minutes
@ minutes
Definition: TimeDivider.h:22
TimeDivider::m_unit
unit m_unit
Definition: TimeDivider.h:52