DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
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
17namespace dunedaq {
18namespace 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
26void
27Date::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)
32 return;
33
34 // skip leading spaces
35
36 while (*s == ' ')
37 ++s;
38
39 const char* s2 = s; // original time used in error reports
40 char* nds = 0; // non-digit symbol returned by strtol
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 } else
50 s = nds + 1;
51
52 if (p_tm.tm_mday > 31 || p_tm.tm_mday == 0) {
53 Oks::error_msg("oks::Time::Time()") << "bad day " << p_tm.tm_mday << " in \'" << s2 << "\'\n";
54 return;
55 }
56
57 // find the month
58
59 p_tm.tm_mon = strtol(s, &nds, 10);
60
61 if (*nds != '/') {
62 Oks::error_msg("oks::Time::Time()") << "failed to find the month in \'" << s2 << "\'\n";
63 return;
64 } else
65 s = nds + 1;
66
67 if (p_tm.tm_mon > 12 || p_tm.tm_mon == 0) {
68 Oks::error_msg("oks::Time::Time()") << "bad month " << p_tm.tm_mon << " in \'" << s2 << "\'\n";
69 return;
70 }
71
72 p_tm.tm_mon--;
73
74 // find the year
75
76 p_tm.tm_year = strtol(s, &nds, 10);
77
78 if (p_tm.tm_year >= 0) {
79 if (p_tm.tm_year < 70)
80 p_tm.tm_year += 2000;
81 else if (p_tm.tm_year < 100)
82 p_tm.tm_year += 1900;
83 }
84
85 // check if there is no data anymore
86
87 if (*nds == 0)
88 return;
89
90 s = nds;
91
92 // skip spaces
93
94 while (*s == ' ')
95 ++s;
96 if (*s == 0)
97 return;
98
99 // find the hour
100
101 p_tm.tm_hour = strtol(s, &nds, 10);
102
103 if (*nds != ':') {
104 Oks::error_msg("oks::Time::Time()") << "failed to find the hour in \'" << s2 << "\'\n";
105 return;
106 } else
107 s = nds + 1;
108
109 if (p_tm.tm_hour > 23) {
110 Oks::error_msg("oks::Time::Time()") << "bad hour " << p_tm.tm_hour << " in \'" << s2 << "\'\n";
111 return;
112 }
113
114 // find the minute
115
116 p_tm.tm_min = strtol(s, &nds, 10);
117
118 if (p_tm.tm_min > 59) {
119 Oks::error_msg("oks::Time::Time()") << "bad minute " << p_tm.tm_min << " in \'" << s2 << "\'\n";
120 return;
121 }
122
123 if (*nds != ':')
124 return;
125
126 // find the second
127
128 p_tm.tm_sec = strtol(nds + 1, &nds, 10);
129
130 if (p_tm.tm_sec > 59) {
131 Oks::error_msg("oks::Time::Time()") << "bad second " << p_tm.tm_sec << " in \'" << s2 << "\'\n";
132 return;
133 }
134}
135
136std::string
138{
139 std::ostringstream s;
140
141 s << p_tm.tm_mday << '/' << (p_tm.tm_mon + 1) << '/' << std::setfill('0') << std::setw(2)
142 << ((p_tm.tm_year < 1970 || p_tm.tm_year >= 2070)
143 ? p_tm.tm_year
144 : (p_tm.tm_year < 2000 ? (p_tm.tm_year - 1900) : (p_tm.tm_year - 2000)));
145
146 return s.str();
147}
148
149std::string
151{
152 std::string ds = Date::str();
153
154 std::ostringstream s;
155
156 s << ds << std::setfill('0') << ' ' << std::setw(2) << p_tm.tm_hour << ':' << std::setw(2) << p_tm.tm_min << ':'
157 << std::setw(2) << p_tm.tm_sec;
158
159 return s.str();
160}
161
162std::ostream&
163operator<<(std::ostream& s, const Date& d)
164{
165 std::string str = d.str();
166 s << str;
167 return s;
168}
169
170std::ostream&
171operator<<(std::ostream& s, const Time& t)
172{
173 std::string str = t.str();
174 s << str;
175 return s;
176}
177
179
180std::string Oks::Tokenizer::s_empty;
181
182Oks::Tokenizer::Tokenizer(const std::string& s, const char* d)
183 : p_string(s)
184 , p_delimeters(d)
185 , p_idx(p_string.find_first_not_of(p_delimeters))
186{
187}
188
189const std::string
191{
192 if (p_idx == std::string::npos)
193 return s_empty;
194 std::string::size_type end_idx = p_string.find_first_of(p_delimeters, p_idx);
195 if (end_idx == std::string::npos)
196 end_idx = p_string.length();
197 std::string token = p_string.substr(p_idx, end_idx - p_idx);
198 p_idx = p_string.find_first_not_of(p_delimeters, end_idx);
199 return token;
200}
201
202bool
203Oks::Tokenizer::next(std::string& token)
204{
205 if (p_idx == std::string::npos) {
206 token.clear();
207 return false;
208 }
209
210 std::string::size_type end_idx = p_string.find_first_of(p_delimeters, p_idx);
211 if (end_idx == std::string::npos)
212 end_idx = p_string.length();
213 token.assign(p_string, p_idx, end_idx - p_idx);
214 p_idx = p_string.find_first_not_of(p_delimeters, end_idx);
215 return true;
216}
217
218} // namespace oks
219} // namespace dunedaq
void set(const char *)
Definition time.cpp:27
virtual std::string str() const
Definition time.cpp:137
std::string::size_type p_idx
Definition defs.hpp:69
const std::string next()
Definition time.cpp:190
const char * p_delimeters
Definition defs.hpp:68
Tokenizer(const std::string &s, const char *separator)
Definition time.cpp:182
static std::string s_empty
Definition defs.hpp:71
static std::ostream & error_msg(const char *)
Definition kernel.cpp:511
virtual std::string str() const
Definition time.cpp:150
std::ostream & operator<<(std::ostream &s, const oks::exception &ex)
The DUNE-DAQ namespace.
Definition ral.hpp:59