DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
oks
src
time.cpp
Go to the documentation of this file.
1
// DUNE DAQ modification notice:
2
// This file has been modified from the original ATLAS oks source for the DUNE DAQ project.
3
// Fork baseline commit: oks-08-03-04 (2022-04-14).
4
// Renamed since fork: no.
5
6
#define _OksBuildDll_
7
8
#include "
oks/defs.hpp
"
9
#include "
oks_utils.hpp
"
10
11
#include <stdlib.h>
12
#include <time.h>
13
14
#include <iomanip>
15
#include <sstream>
16
17
namespace
dunedaq
{
18
namespace
oks
{
19
20
//
21
// The method converts string 's' to time
22
// It is expected that the 's' is in the
23
// following format: dd/mm/[yy]yy hh:mm[:ss]
24
//
25
26
void
27
Date::set
(
const
char
* s)
28
{
29
p_tm
.tm_sec =
p_tm
.tm_min =
p_tm
.tm_hour =
p_tm
.tm_mday =
p_tm
.tm_mon =
p_tm
.tm_year = 0;
30
31
if
(s == 0)
return
;
32
33
// skip leading spaces
34
35
while
(*s ==
' '
) ++s;
36
37
38
const
char
* s2 = s;
// original time used in error reports
39
char
* nds = 0;
// non-digit symbol returned by strtol
40
41
42
// find the day
43
44
p_tm
.tm_mday = strtol(s, &nds, 10);
45
46
if
(*nds !=
'/'
) {
47
Oks::error_msg
(
"oks::Time::Time()"
) <<
"failed to find the day in \'"
<< s2 <<
"\'\n"
;
48
return
;
49
}
50
else
51
s = nds + 1;
52
53
if
(
p_tm
.tm_mday > 31 ||
p_tm
.tm_mday == 0) {
54
Oks::error_msg
(
"oks::Time::Time()"
) <<
"bad day "
<<
p_tm
.tm_mday <<
" in \'"
<< s2 <<
"\'\n"
;
55
return
;
56
}
57
58
59
// find the month
60
61
p_tm
.tm_mon = strtol(s, &nds, 10);
62
63
if
(*nds !=
'/'
) {
64
Oks::error_msg
(
"oks::Time::Time()"
) <<
"failed to find the month in \'"
<< s2 <<
"\'\n"
;
65
return
;
66
}
67
else
68
s = nds + 1;
69
70
if
(
p_tm
.tm_mon > 12 ||
p_tm
.tm_mon == 0) {
71
Oks::error_msg
(
"oks::Time::Time()"
) <<
"bad month "
<<
p_tm
.tm_mon <<
" in \'"
<< s2 <<
"\'\n"
;
72
return
;
73
}
74
75
p_tm
.tm_mon--;
76
77
78
// find the year
79
80
p_tm
.tm_year = strtol(s, &nds, 10);
81
82
if
(
p_tm
.tm_year >= 0) {
83
if
(
p_tm
.tm_year < 70)
p_tm
.tm_year += 2000;
84
else
if
(
p_tm
.tm_year < 100)
p_tm
.tm_year += 1900;
85
}
86
87
// check if there is no data anymore
88
89
if
(*nds == 0)
return
;
90
91
s = nds;
92
93
94
// skip spaces
95
96
while
(*s ==
' '
) ++s;
97
if
(*s == 0)
return
;
98
99
100
// find the hour
101
102
p_tm
.tm_hour = strtol(s, &nds, 10);
103
104
if
(*nds !=
':'
) {
105
Oks::error_msg
(
"oks::Time::Time()"
) <<
"failed to find the hour in \'"
<< s2 <<
"\'\n"
;
106
return
;
107
}
108
else
109
s = nds + 1;
110
111
if
(
p_tm
.tm_hour > 23) {
112
Oks::error_msg
(
"oks::Time::Time()"
) <<
"bad hour "
<<
p_tm
.tm_hour <<
" in \'"
<< s2 <<
"\'\n"
;
113
return
;
114
}
115
116
117
// find the minute
118
119
p_tm
.tm_min = strtol(s, &nds, 10);
120
121
if
(
p_tm
.tm_min > 59) {
122
Oks::error_msg
(
"oks::Time::Time()"
) <<
"bad minute "
<<
p_tm
.tm_min <<
" in \'"
<< s2 <<
"\'\n"
;
123
return
;
124
}
125
126
if
(*nds !=
':'
)
return
;
127
128
129
// find the second
130
131
p_tm
.tm_sec = strtol(nds + 1, &nds, 10);
132
133
if
(
p_tm
.tm_sec > 59) {
134
Oks::error_msg
(
"oks::Time::Time()"
) <<
"bad second "
<<
p_tm
.tm_sec <<
" in \'"
<< s2 <<
"\'\n"
;
135
return
;
136
}
137
}
138
139
140
std::string
141
Date::str
()
const
142
{
143
std::ostringstream s;
144
145
s <<
p_tm
.tm_mday <<
'/'
<< (
p_tm
.tm_mon + 1) <<
'/'
<< std::setfill(
'0'
) << std::setw(2)
146
<< ((
p_tm
.tm_year < 1970 ||
p_tm
.tm_year >= 2070) ?
p_tm
.tm_year : (
p_tm
.tm_year < 2000 ? (
p_tm
.tm_year - 1900) : (
p_tm
.tm_year - 2000)));
147
148
return
s.str();
149
}
150
151
std::string
152
Time::str
()
const
153
{
154
std::string ds =
Date::str
();
155
156
std::ostringstream s;
157
158
s << ds << std::setfill(
'0'
)
159
<<
' '
<< std::setw(2) <<
p_tm
.tm_hour
160
<<
':'
<< std::setw(2) <<
p_tm
.tm_min
161
<<
':'
<< std::setw(2) <<
p_tm
.tm_sec;
162
163
return
s.str();
164
}
165
166
167
std::ostream&
operator<<
(std::ostream& s,
const
Date
& d)
168
{
169
std::string str = d.str();
170
s << str;
171
return
s;
172
}
173
174
175
std::ostream&
operator<<
(std::ostream& s,
const
Time
& t)
176
{
177
std::string str = t.
str
();
178
s << str;
179
return
s;
180
}
181
183
184
std::string
Oks::Tokenizer::s_empty
;
185
186
Oks::Tokenizer::Tokenizer
(
const
std::string& s,
const
char
* d) :
187
p_string
(s),
188
p_delimeters
(d),
189
p_idx
(
p_string
.find_first_not_of(
p_delimeters
))
190
{
191
}
192
193
const
std::string
194
Oks::Tokenizer::next
()
195
{
196
if
(
p_idx
== std::string::npos)
return
s_empty
;
197
std::string::size_type end_idx =
p_string
.find_first_of(
p_delimeters
,
p_idx
);
198
if
(end_idx == std::string::npos) end_idx=
p_string
.length();
199
std::string token =
p_string
.substr(
p_idx
, end_idx -
p_idx
);
200
p_idx
=
p_string
.find_first_not_of(
p_delimeters
, end_idx);
201
return
token;
202
}
203
204
bool
205
Oks::Tokenizer::next
(std::string& token)
206
{
207
if
(
p_idx
== std::string::npos) {
208
token.clear();
209
return
false
;
210
}
211
212
std::string::size_type end_idx =
p_string
.find_first_of(
p_delimeters
,
p_idx
);
213
if
(end_idx == std::string::npos) end_idx=
p_string
.length();
214
token.assign(
p_string
,
p_idx
, end_idx -
p_idx
);
215
p_idx
=
p_string
.find_first_not_of(
p_delimeters
, end_idx);
216
return
true
;
217
}
218
219
}
// namespace oks
220
}
// namespace dunedaq
dunedaq::oks::Date
Definition
oks_utils.hpp:74
dunedaq::oks::Date::set
void set(const char *)
Definition
time.cpp:27
dunedaq::oks::Date::p_tm
struct tm p_tm
Definition
oks_utils.hpp:93
dunedaq::oks::Date::str
virtual std::string str() const
Definition
time.cpp:141
dunedaq::oks::Oks::Tokenizer::p_idx
std::string::size_type p_idx
Definition
defs.hpp:76
dunedaq::oks::Oks::Tokenizer::next
const std::string next()
Definition
time.cpp:194
dunedaq::oks::Oks::Tokenizer::p_delimeters
const char * p_delimeters
Definition
defs.hpp:75
dunedaq::oks::Oks::Tokenizer::Tokenizer
Tokenizer(const std::string &s, const char *separator)
Definition
time.cpp:186
dunedaq::oks::Oks::Tokenizer::s_empty
static std::string s_empty
Definition
defs.hpp:78
dunedaq::oks::Oks::Tokenizer::p_string
std::string p_string
Definition
defs.hpp:74
dunedaq::oks::Oks::error_msg
static std::ostream & error_msg(const char *)
Definition
kernel.cpp:561
dunedaq::oks::Time
Definition
oks_utils.hpp:100
dunedaq::oks::Time::str
virtual std::string str() const
Definition
time.cpp:152
defs.hpp
dunedaq::oks::operator<<
std::ostream & operator<<(std::ostream &s, const oks::exception &ex)
Definition
exceptions.hpp:44
dunedaq
Including Qt Headers.
Definition
module.cpp:16
oks
Definition
ral.hpp:35
oks_utils.hpp
Generated on
for DUNE-DAQ by
1.17.0