Stride Reference Manual  1.0
TimeStamp.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * any later version.
7  * The software is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License
12  * along with the software. If not, see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright 2017, Willem L, Kuylen E, Stijven S & Broeckhove J
15  */
16 
22 #include <algorithm>
23 #include <iomanip>
24 #include <sstream>
25 #include <string>
26 #include <ctime>
27 #include <chrono>
28 
29 namespace stride {
30 namespace util {
31 
36 class TimeStamp {
37 public:
39  TimeStamp() : m_tp(std::chrono::system_clock::now()) {}
40 
42  std::string toString() const {
43  std::time_t t = std::chrono::system_clock::to_time_t(m_tp);
44  std::string str = std::ctime(&t);
45  return str.substr(0, str.length() - 1);
46  }
47 
48 
50  std::string toTag() const {
51  // This is the C++11 implementation but gcc (at least up to 4.9)
52  // does not implement std::put_time.
53 
54  time_t now = time(NULL);
55  struct tm tstruct;
56  char buf[80];
57  tstruct = *localtime(&now);
58  strftime(buf, sizeof(buf), "%Y%m%d_%H%M%S", &tstruct);
59  return buf;
60  }
61 
63  std::time_t toTimeT() const {
64  return std::chrono::system_clock::to_time_t(m_tp);
65  }
66 
67 private:
68  std::chrono::system_clock::time_point m_tp;
69 };
70 
74 inline std::ostream&
75 operator<<(std::ostream& os, TimeStamp t) {
76  return (os << t.toString());
77 }
78 
79 }
80 }
81 
std::chrono::system_clock::time_point m_tp
Definition: TimeStamp.h:68
Time Dependent Person DataType.
Definition: NoBehaviour.h:17
Provides wall-clock time stamp using the time call.
Definition: TimeStamp.h:36
TimeStamp()
Constructor marks the time for the time stamp.
Definition: TimeStamp.h:39
std::string toString() const
Returns string with the time stamp after eliminating newline.
Definition: TimeStamp.h:42
STL namespace.
std::string toTag() const
Returns string with the time stamp after eliminating newline.
Definition: TimeStamp.h:50
std::time_t toTimeT() const
Returns time stamp as a time_t.
Definition: TimeStamp.h:63
std::ostream & operator<<(std::ostream &os, const GeoCoordinate &g)