ATLAS Offline Software
Loading...
Searching...
No Matches
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
10TimeDivider::TimeDivider(unsigned int intervals, unsigned int duration, unit u )
11 : m_intervals(intervals),
12 m_duration(duration),
13 m_unit(u),
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
21bool 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
47unsigned int TimeDivider::forcePassed(time_t time, unsigned int& interval ) {
48 interval = m_current_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*/
unsigned int forcePassed(time_t time, unsigned int &oldinterval)
method
time_t m_beginning
Definition TimeDivider.h:54
unsigned int m_intervals
Definition TimeDivider.h:50
unsigned int m_current_interval
Definition TimeDivider.h:53
TimeDivider(unsigned int intervals, unsigned int duration, unit u)
construct the TimeDivider
bool isPassed(time_t time, unsigned int &newinterval, unsigned int &oldinterval)
method to know if one shoudl switch to new interval
unsigned int m_duration
Definition TimeDivider.h:51
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146