AFLOW
 
Loading...
Searching...
No Matches
aurostd_time.cpp
Go to the documentation of this file.
1
2#include "aurostd_time.h"
3
4#include <cmath>
5#include <cstdlib>
6#include <ctime>
7#include <iostream>
8#include <string>
9#include <vector>
10
11#include <sys/time.h>
12#include <time.h>
13
14#include "aurostd.h"
15
16#include "aflow_xhost.h" // for XHOST.DEBUG
17
18using std::cerr;
19using std::endl;
20using std::string;
21using std::vector;
22
23// ***************************************************************************
24// TIME evolution stuff
25namespace aurostd {
26 int get_day() {
27 const time_t t = time(nullptr);
28 struct tm* ptr_now = localtime(&t);
29 return get_day(*ptr_now);
30 } // CO20200624
31 int get_day(const tm& tstruct) {
32 return tstruct.tm_mday;
33 } // CO20200624
34 int get_month() {
35 const time_t t = time(nullptr);
36 struct tm* ptr_now = localtime(&t);
37 return get_month(*ptr_now);
38 } // CO20200624
39 int get_month(const tm& tstruct) {
40 return tstruct.tm_mon + 1;
41 } // CO20200624
42 int get_year() {
43 const time_t t = time(nullptr);
44 struct tm* ptr_now = localtime(&t);
45 return get_year(*ptr_now);
46 }
47 int get_year(const tm& tstruct) {
48 return tstruct.tm_year + 1900;
49 } // CO20200624
50 void get_offset_utc(int& offset_hours, int& offset_mins) {
51 const time_t t = time(nullptr);
52 struct tm* ptr_now = localtime(&t);
53 return get_offset_utc(*ptr_now, offset_hours, offset_mins);
54 } // CO20210601: https://codereview.stackexchange.com/questions/175353/getting-current-timezone
55 void get_offset_utc(const tm& _tstruct_inp, int& offset_hours, int& offset_mins) { // CO20210601
56 // https://codereview.stackexchange.com/questions/175353/getting-current-timezone
57 const bool LDEBUG = (false || XHOST.DEBUG);
58 char buffer[30];
59 tm tstruct_inp = _tstruct_inp; // mktime modifies tstruct, make copy
60 const time_t t_inp = std::mktime(&tstruct_inp);
61 if (LDEBUG) {
62 cerr << __AFLOW_FUNC__ << " ///////////////////////////////////////////////" << endl;
63 cerr << __AFLOW_FUNC__ << " LOOKING AT: tstruct_inp" << endl;
64 cerr << __AFLOW_FUNC__ << " tstruct_inp.tm_sec=" << tstruct_inp.tm_sec << endl;
65 cerr << __AFLOW_FUNC__ << " tstruct_inp.tm_min=" << tstruct_inp.tm_min << endl;
66 cerr << __AFLOW_FUNC__ << " tstruct_inp.tm_hour=" << tstruct_inp.tm_hour << endl;
67 cerr << __AFLOW_FUNC__ << " tstruct_inp.tm_mday=" << tstruct_inp.tm_mday << endl;
68 cerr << __AFLOW_FUNC__ << " tstruct_inp.tm_mon=" << tstruct_inp.tm_mon << endl;
69 cerr << __AFLOW_FUNC__ << " tstruct_inp.tm_year=" << tstruct_inp.tm_year << endl;
70 cerr << __AFLOW_FUNC__ << " tstruct_inp.tm_wday=" << tstruct_inp.tm_wday << endl;
71 cerr << __AFLOW_FUNC__ << " tstruct_inp.tm_yday=" << tstruct_inp.tm_yday << endl;
72 cerr << __AFLOW_FUNC__ << " tstruct_inp.tm_isdst=" << tstruct_inp.tm_isdst << endl;
73 cerr << __AFLOW_FUNC__ << " mktime(tstruct_inp)=" << t_inp << endl;
74 strftime(buffer, 30, "%F %T %Z", &tstruct_inp);
75 cerr << __AFLOW_FUNC__ << " tstruct_inp=" << buffer << endl; //%Y:%m:%d %H:%M:%S
76 cerr << __AFLOW_FUNC__ << " ///////////////////////////////////////////////" << endl;
77 }
78 //
79 time_t t_local = t_inp;
80 const bool fix_utc_2_now = false; // this is good for debugging different time zones
81 if (fix_utc_2_now) {
82 t_local = time(nullptr);
83 } // struct tm *tstruct_now=localtime(&t_now);
84 struct tm* ptr_tstruct_gmt = std::gmtime(&t_local); // get gmt wrt to local (now vs. input)
85 if (LDEBUG) {
86 cerr << __AFLOW_FUNC__ << " ///////////////////////////////////////////////" << endl;
87 cerr << __AFLOW_FUNC__ << " LOOKING AT: ptr_tstruct_gmt (BEFORE DST CHANGE)" << endl;
88 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_sec=" << ptr_tstruct_gmt->tm_sec << endl;
89 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_min=" << ptr_tstruct_gmt->tm_min << endl;
90 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_hour=" << ptr_tstruct_gmt->tm_hour << endl;
91 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_mday=" << ptr_tstruct_gmt->tm_mday << endl;
92 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_mon=" << ptr_tstruct_gmt->tm_mon << endl;
93 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_year=" << ptr_tstruct_gmt->tm_year << endl;
94 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_wday=" << ptr_tstruct_gmt->tm_wday << endl;
95 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_yday=" << ptr_tstruct_gmt->tm_yday << endl;
96 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_isdst=" << ptr_tstruct_gmt->tm_isdst << endl;
97 tm tstruct_tmp = *ptr_tstruct_gmt; // mktime modifies tstruct
98 cerr << __AFLOW_FUNC__ << " mktime(ptr_tstruct_gmt)=" << std::mktime(&tstruct_tmp) << endl;
99 strftime(buffer, 30, "%F %T %Z", ptr_tstruct_gmt);
100 cerr << __AFLOW_FUNC__ << " tstruct_gmt=" << buffer << endl; //%Y:%m:%d %H:%M:%S
101 cerr << __AFLOW_FUNC__ << " ///////////////////////////////////////////////" << endl;
102 }
103 // NB: before the following DST change, the %Z of ptr_struct_gmt is GMT, after it is EST (or EDT)
104 ptr_tstruct_gmt->tm_isdst = -1; // VERY IMPORTANT, forces mktime to figure out dst
105 const time_t t_gmt = std::mktime(ptr_tstruct_gmt);
106 if (LDEBUG) {
107 cerr << __AFLOW_FUNC__ << " ///////////////////////////////////////////////" << endl;
108 cerr << __AFLOW_FUNC__ << " LOOKING AT: ptr_tstruct_gmt (AFTER DST CHANGE)" << endl;
109 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_sec=" << ptr_tstruct_gmt->tm_sec << endl;
110 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_min=" << ptr_tstruct_gmt->tm_min << endl;
111 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_hour=" << ptr_tstruct_gmt->tm_hour << endl;
112 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_mday=" << ptr_tstruct_gmt->tm_mday << endl;
113 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_mon=" << ptr_tstruct_gmt->tm_mon << endl;
114 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_year=" << ptr_tstruct_gmt->tm_year << endl;
115 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_wday=" << ptr_tstruct_gmt->tm_wday << endl;
116 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_yday=" << ptr_tstruct_gmt->tm_yday << endl;
117 cerr << __AFLOW_FUNC__ << " ptr_tstruct_gmt->tm_isdst=" << ptr_tstruct_gmt->tm_isdst << endl;
118 cerr << __AFLOW_FUNC__ << " mktime(ptr_tstruct_gmt)=" << t_gmt << endl;
119 strftime(buffer, 30, "%F %T %Z", ptr_tstruct_gmt);
120 cerr << __AFLOW_FUNC__ << " tstruct_gmt=" << buffer << endl; //%Y:%m:%d %H:%M:%S
121 cerr << __AFLOW_FUNC__ << " ///////////////////////////////////////////////" << endl;
122 }
123 //
124 const long int t_diff = static_cast<long int>(t_inp - t_gmt); // flip to get right sign
125 if (LDEBUG) {
126 cerr << __AFLOW_FUNC__ << " t_diff=" << t_diff << endl;
127 }
128 const double offset = (double) t_diff / 3600.0;
129 offset_hours = (int) std::floor(offset);
130 offset_mins = (int) (std::round((offset - (double) offset_hours) * 60.0));
131 }
132 long int get_date() {
133 const time_t t = time(nullptr);
134 struct tm* ptr_now = localtime(&t);
135 return get_date(*ptr_now);
136 } // CO20200624
137 long int get_date(const tm& tstruct) {
138 return aurostd::get_year(tstruct) * 10000 + aurostd::get_month(tstruct) * 100 + aurostd::get_day(tstruct);
139 } // CO20200624
140 int get_hour() {
141 const time_t t = time(nullptr);
142 struct tm* ptr_now = localtime(&t);
143 return get_hour(*ptr_now);
144 } // CO20200624
145 int get_hour(const tm& tstruct) {
146 return tstruct.tm_hour;
147 } // CO20200624
148 int get_min() {
149 const time_t t = time(nullptr);
150 struct tm* ptr_now = localtime(&t);
151 return get_min(*ptr_now);
152 } // CO20200624
153 int get_min(const tm& tstruct) {
154 return tstruct.tm_min;
155 } // CO20200624
156 int get_sec() {
157 const time_t t = time(nullptr);
158 struct tm* ptr_now = localtime(&t);
159 return get_sec(*ptr_now);
160 } // CO20200624
161 int get_sec(const tm& tstruct) {
162 return tstruct.tm_sec;
163 } // CO20200624
164 long double get_seconds() {
165 timeval tim;
166 gettimeofday(&tim, nullptr);
167 return tim.tv_sec + tim.tv_usec / 1e6;
168 }
169 long double get_seconds(long double reference_seconds) {
170 return get_seconds() - reference_seconds;
171 }
172 long double get_delta_seconds(long double& seconds_begin) {
173 const long double out = get_seconds() - seconds_begin;
174 seconds_begin = get_seconds();
175 return out;
176 }
177 long double get_mseconds() {
178 timeval tim;
179 gettimeofday(&tim, nullptr);
180 return tim.tv_usec / 1000.0;
181 }
182 long double get_mseconds(long double reference_useconds) {
183 return (aurostd::get_useconds() - reference_useconds) / 1000.0;
184 }
185 long double get_delta_mseconds(long double& useconds_begin) {
186 const long double out = (aurostd::get_useconds() - useconds_begin) / 1000.0;
187 useconds_begin = aurostd::get_useconds() / 1000.0;
188 return out;
189 }
190 long double get_useconds() {
191 timeval tim;
192 gettimeofday(&tim, nullptr);
193 return tim.tv_usec;
194 }
195 long double get_useconds(long double reference_useconds) {
196 return aurostd::get_useconds() - reference_useconds;
197 }
198 long double get_delta_useconds(long double& useconds_begin) {
199 const long double out = aurostd::get_useconds() - useconds_begin;
200 useconds_begin = aurostd::get_useconds();
201 return out;
202 }
203 string get_time() {
204 const time_t t = time(nullptr);
205 struct tm* ptr_now = localtime(&t);
206 return get_time(*ptr_now);
207 } // CO20200624
208 string get_time(const tm& tstruct) {
209 const int h = get_hour(tstruct);
210 const int m = get_min(tstruct);
211 const int s = get_sec(tstruct);
212 return (h < 10 ? "0" : "") + aurostd::utype2string(h) + ":" + (m < 10 ? "0" : "") + aurostd::utype2string(m) + ":" + (s < 10 ? "0" : "") + aurostd::utype2string(s);
213 } // CO20200624
214 string get_datetime(bool include_utc_offset) {
215 const time_t t = time(nullptr);
216 struct tm* ptr_now = localtime(&t);
217 return get_datetime(*ptr_now, include_utc_offset);
218 } // CO20200624
219 string get_datetime(const tm& tstruct, bool include_utc_offset) { // CO20200624
220 string datetime = utype2string(get_date(tstruct)) + "_" + get_time(tstruct);
221 if (include_utc_offset) { // CO20210624
222 int offset_hours = 0;
223 int offset_mins = 0;
224 get_offset_utc(tstruct, offset_hours, offset_mins);
225 datetime += "_GMT";
226 datetime += (std::signbit(offset_hours) ? string("-") : string("+")); // sign +/-
227 const bool pad_hours = false; // default AFLOW behavior does not pad hours
228 datetime += aurostd::PaddedNumString(std::abs(offset_hours), (pad_hours ? 2 : 1)); // CO20210624 - PaddedNumString() struggles with negative numbers
229 const bool print_mins = false; // default AFLOW behavior does not print mins
230 if (print_mins || offset_mins != 0) {
231 datetime += ":" + aurostd::PaddedNumString(offset_mins, 2);
232 }
233 }
234 return datetime;
235 }
236 string get_datetime_formatted(const string& date_delim, bool include_time, const string& date_time_sep, const string& time_delim) {
237 const time_t t = time(nullptr);
238 struct tm* ptr_now = localtime(&t);
239 return get_datetime_formatted(*ptr_now, date_delim, include_time, date_time_sep, time_delim);
240 } // CO20171215 //CO20200624
241 string get_datetime_formatted(const tm& tstruct, const string& date_delim, bool include_time, const string& date_time_sep, const string& time_delim) { // CO20171215 //CO20200624
242 stringstream misc_ss;
243 const int y = aurostd::get_year(tstruct);
244 const int b = aurostd::get_month(tstruct);
245 const int d = aurostd::get_day(tstruct); // CO20200624
246 misc_ss << y << date_delim << (b < 10 ? "0" : "") << b << date_delim << (d < 10 ? "0" : "") << d;
247 if (include_time) {
248 const int h = get_hour(tstruct);
249 const int m = get_min(tstruct);
250 const int s = get_sec(tstruct);
251 misc_ss << date_time_sep << (h < 10 ? "0" : "") << h << time_delim << (m < 10 ? "0" : "") << m << time_delim << (s < 10 ? "0" : "") << s; // CO20200624
252 }
253 return misc_ss.str();
254 }
255 bool beep(uint freq, uint duration) {
256 return aurostd::execute("beep -f " + aurostd::utype2string<uint>(freq) + " -l " + aurostd::utype2string<uint>(duration));
257 }
258} // namespace aurostd
259
260// ***************************************************************************
261// aflow_get_time_string
262// ***************************************************************************
264 // OUTPUT: http://www.cplusplus.com/reference/ctime/ctime/
265 // Www Mmm dd hh:mm:ss yyyy
266 // Where Www is the weekday, Mmm the month (in letters), dd the day of the month, hh:mm:ss the time, and yyyy the year.
267 // The string is followed by a new-line character ('\n') and terminated with a null-character.
268 const long ltime = time(nullptr);
269
270 string date = string(ctime(&ltime));
271 if (!date.empty()) {
272 if (date.at(date.length() - 1) == '\n') {
273 date.erase(date.length() - 1);
274 }
275 }
276 return date;
277}
278string aflow_convert_time_ctime2aurostd(const string& time_LOCK) { // CO20200624
279 // refer to aflow_get_time_string()
280 // convert Www Mmm dd hh:mm:ss yyyy style to aurostd::get_datetime() one
281 const bool LDEBUG = (false || XHOST.DEBUG);
282
283 if (LDEBUG) {
284 cerr << __AFLOW_FUNC__ << " BEGIN" << endl;
285 }
286
287 vector<string> tokens;
288 aurostd::string2tokens(time_LOCK, tokens);
289 if (tokens.size() != 5) {
290 return "";
291 }
292
293 // https://en.cppreference.com/w/c/chrono/strftime
294 //'Www Mmm dd hh:mm:ss yyyy' === '%a %b %d %H:%M:%S %Y'
295 // https://stackoverflow.com/questions/19524720/using-strptime-converting-string-to-time-but-getting-garbage
296 tm tstruct;
297 if (!strptime(time_LOCK.c_str(), "%a %b %d %H:%M:%S %Y", &tstruct)) {
298 return "";
299 }
300 tstruct.tm_isdst = -1; // let computer figure it out
301 std::mktime(&tstruct); // get is_dst
302
303 if (LDEBUG) {
304 char buffer[30];
305 strftime(buffer, 30, "%F %T %Z", &tstruct);
306 cerr << __AFLOW_FUNC__ << " tstruct=" << buffer << endl;
307 }
308
309 if (LDEBUG) {
310 cerr << __AFLOW_FUNC__ << " END" << endl;
311 }
312
313 const bool include_utc_offset = true;
314 return aurostd::get_datetime(tstruct, include_utc_offset);
315}
316
317// ***************************************************************************
318// aflow_get_time_string_short
319// ***************************************************************************
321 string date;
322 vector<string> tokens;
324 date = "na";
325 if (tokens.size() > 4) {
326 if (tokens.at(2).size() > 1) {
327 date = tokens.at(4).substr(2, 2) + tokens.at(1) + tokens.at(2);
328 } else {
329 date = tokens.at(4).substr(2, 2) + tokens.at(1) + "0" + tokens.at(2);
330 }
331 aurostd::StringSubstInPlace(date, "Jan", "01");
332 aurostd::StringSubstInPlace(date, "Feb", "02");
333 aurostd::StringSubstInPlace(date, "Mar", "03");
334 aurostd::StringSubstInPlace(date, "Apr", "04");
335 aurostd::StringSubstInPlace(date, "May", "05");
336 aurostd::StringSubstInPlace(date, "Jun", "06");
337 aurostd::StringSubstInPlace(date, "Jul", "07");
338 aurostd::StringSubstInPlace(date, "Aug", "08");
339 aurostd::StringSubstInPlace(date, "Sep", "09");
340 aurostd::StringSubstInPlace(date, "Oct", "10");
341 aurostd::StringSubstInPlace(date, "Nov", "11");
342 aurostd::StringSubstInPlace(date, "Dec", "12");
343 date = date.substr(0, 6);
344 }
345 if (!date.empty()) {
346 if (date.at(date.length() - 1) == '\n') {
347 date.erase(date.length() - 1);
348 }
349 }
350 return date;
351}
_XHOST XHOST
unsigned uint
Definition aurostd.h:39
#define __AFLOW_FUNC__
Definition aurostd.h:43
string aflow_get_time_string()
string aflow_convert_time_ctime2aurostd(const string &time_LOCK)
string aflow_get_time_string_short()
long double get_delta_useconds(long double &useconds_begin)
int get_hour()
void StringSubstInPlace(string &work_string, const string &old_string, const string &new_string)
substitute parts of a strings in place
int get_month()
long double get_delta_seconds(long double &seconds_begin)
string PaddedNumString(const int num, const int ndigits)
long double get_useconds()
bool beep(uint freq, uint duration)
int get_day()
long double get_delta_mseconds(long double &useconds_begin)
uint string2tokens(const string &str, vector< string > &tokens, const string &delimiters=" ", bool consecutive=false) __xprototype
void get_offset_utc(int &offset_hours, int &offset_mins)
string get_datetime(bool include_utc_offset)
string utype2string(const utype &from, int precision=AUROSTD_DEFAULT_PRECISION, char FORMAT=DEFAULT_STREAM) __xprototype
string get_datetime_formatted(const string &date_delim, bool include_time, const string &date_time_sep, const string &time_delim)
int get_sec()
string get_time()
int get_min()
int get_year()
long double get_seconds()
long int get_date()
long double get_mseconds()
bool execute(ostringstream &command)