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