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