Stride Reference Manual  - generated for commit 9643b11
TimeStamp.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #include "TimeStamp.h"
22 
23 #include <algorithm>
24 #include <ctime>
25 #include <iomanip>
26 #include <sstream>
27 
28 namespace stride {
29 namespace util {
30 
31 TimeStamp::TimeStamp() : m_tp(std::chrono::system_clock::now()) {}
32 
33 std::string TimeStamp::ToString() const
34 {
35  std::time_t t = std::chrono::system_clock::to_time_t(m_tp);
36  std::string str = std::ctime(&t);
37  // str[str.length() - 1] = ' ';
38  return str.substr(0, str.length() - 1);
39 }
40 
41 std::string TimeStamp::ToTag() const
42 {
43  // This is the C++11 implementation but clang 5 on Travis Linux VM's
44  // still does not implement std::put_time.
45  auto now = std::chrono::system_clock::now();
46  auto in_time_t = std::chrono::system_clock::to_time_t(now);
47  std::stringstream ss;
48  ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d-%X");
49  return ss.str();
50  /*
51  time_t now = time(nullptr);
52  struct tm tstruct{};
53  char buf[80];
54  tstruct = *localtime_r(&now, &tstruct);
55  strftime(buf, sizeof(buf), "%Y%m%d_%H%M%S", &tstruct);
56  return std::string(buf);
57  */
58 }
59 
60 } // namespace util
61 } // namespace stride
std::string ToString() const
Returns string with the time stamp after eliminating newline.
Definition: TimeStamp.cpp:33
TimeStamp()
Constructor marks the current time for the time stamp.
Definition: TimeStamp.cpp:31
STL namespace.
TimeStamp class.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
std::string ToTag() const
Returns string with the time stamp after eliminating newline.
Definition: TimeStamp.cpp:41
std::chrono::system_clock::time_point m_tp
Definition: TimeStamp.h:47