OGS
DateTools.cpp
Go to the documentation of this file.
1 
15 #include "DateTools.h"
16 
17 #include <cmath>
18 #include <cstdlib>
19 #include <ctime>
20 #include <sstream>
21 
22 #include "Logging.h"
23 
24 namespace BaseLib
25 {
26 int date2int(int y, int m, int d)
27 {
28  if ((y < 1000 || y > 9999) || (m < 1 || m > 12) || (d < 1 || d > 31))
29  {
30  WARN("date2int(): Input not in expected range.");
31  return 0;
32  }
33 
34  int ddate(0);
35  ddate = y * 10000;
36  ddate += (m * 100);
37  ddate += d;
38 
39  return ddate;
40 }
41 
42 std::string int2date(int date)
43 {
44  if (date > 10000000 && date < 22000000)
45  {
46  auto y = static_cast<int>(std::floor(date / 10000.0));
47  auto m = static_cast<int>(std::floor((date - (y * 10000)) / 100.0));
48  int d = date - (y * 10000) - (m * 100);
49  std::stringstream ss;
50  if (d < 10)
51  {
52  ss << "0";
53  }
54  ss << d << ".";
55  if (m < 10)
56  {
57  ss << "0";
58  }
59  ss << m << "." << y;
60  return ss.str();
61  }
62  return "";
63 }
64 
65 std::string date2string(double ddate)
66 {
67  if (ddate < 10000101 || ddate > 99991231)
68  {
69  WARN("date2String(): Input not in expected format.");
70  return "0.0.0000";
71  }
72 
73  auto rest(static_cast<int>(ddate));
74  auto y = static_cast<int>(std::floor(rest / 10000.0));
75  rest = rest % (y * 10000);
76  auto m = static_cast<int>(std::floor(rest / 100.0));
77  if (m < 1 || m > 12)
78  {
79  WARN("date2String(): month not in [1:12].");
80  }
81  rest = rest % (m * 100);
82  int d = rest;
83  if (d < 1 || d > 31)
84  {
85  WARN("date2String(): day not in [1:31].");
86  }
87 
88  std::string day = std::to_string(d);
89  if (d < 10)
90  {
91  day = "0" + day;
92  }
93  std::string month = std::to_string(m);
94  if (m < 10)
95  {
96  month = "0" + month;
97  }
98  std::string s = std::to_string(y) + "-" + month + "-" + day;
99  return s;
100 }
101 
102 int strDate2int(const std::string& s)
103 {
104  std::string str(s);
105  if (s.length() > 10)
106  {
107  str = s.substr(0, 10);
108  }
109  std::size_t sep(str.find('.', 0));
110  int d(atoi(str.substr(0, sep).c_str()));
111  std::size_t sep2(str.find('.', sep + 1));
112  int m(atoi(str.substr(sep + 1, sep2 - (sep + 1)).c_str()));
113  int y(atoi(str.substr(sep2 + 1, s.length() - (sep2 + 1)).c_str()));
114  return date2int(y, m, d);
115 }
116 
117 int xmlDate2int(const std::string& s)
118 {
119  if (s.length() == 10)
120  {
121  int d = atoi(s.substr(8, 2).c_str());
122  if (d < 1 || d > 31)
123  {
124  WARN("xmlDate2double(): day not in [1:31].");
125  }
126  int m = atoi(s.substr(5, 2).c_str());
127  if (m < 1 || m > 12)
128  {
129  WARN("xmlDate2double(): month not in [1:12].");
130  }
131  int y = atoi(s.substr(0, 4).c_str());
132  return date2int(y, m, d);
133  }
134  return 0;
135 }
136 
137 std::string formatDate(
138  std::chrono::time_point<std::chrono::system_clock> const& time)
139 {
140  auto const time_t = std::chrono::system_clock::to_time_t(time);
141  char time_str[100];
142  if (std::strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S%z",
143  std::localtime(&time_t)))
144  {
145  return time_str;
146  }
147  return "FAILED FORMATTING THE GIVEN TIME POINT.";
148 }
149 } // end namespace BaseLib
Definition of date helper functions.
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
std::string date2string(double ddate)
Definition: DateTools.cpp:65
int strDate2int(const std::string &s)
Definition: DateTools.cpp:102
std::string formatDate(std::chrono::time_point< std::chrono::system_clock > const &time)
Definition: DateTools.cpp:137
int date2int(int y, int m, int d)
Definition: DateTools.cpp:26
std::string int2date(int date)
Definition: DateTools.cpp:42
int xmlDate2int(const std::string &s)
Definition: DateTools.cpp:117