Skip to content

Commit 5e9e0bb

Browse files
authored
Merge pull request #10059 from Icinga/IcingaDB-TimestampToMilliseconds-limit
IcingaDB::TimestampToMilliseconds(): limit output to four year digits
2 parents b6b1506 + dc4869c commit 5e9e0bb

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lib/icingadb/icingadb-utility.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "icinga/eventcommand.hpp"
1919
#include "icinga/host.hpp"
2020
#include <boost/algorithm/string.hpp>
21+
#include <cmath>
2122
#include <map>
2223
#include <utility>
2324
#include <vector>
@@ -245,7 +246,18 @@ String IcingaDB::GetLowerCaseTypeNameDB(const ConfigObject::Ptr& obj)
245246
}
246247

247248
long long IcingaDB::TimestampToMilliseconds(double timestamp) {
248-
return static_cast<long long>(timestamp * 1000);
249+
// In addition to the limits of the Icinga DB MySQL (0 - 2^64) and PostgreSQL (0 - 2^63) schemata,
250+
// years not fitting in YYYY may cause problems, see e.g. https://github.com/golang/go/issues/4556.
251+
// RFC 3339: "All dates and times are assumed to be (...) somewhere between 0000AD and 9999AD."
252+
//
253+
// The below upper limit includes a safety buffer to make sure the timestamp is within 9999AD in all time zones:
254+
// $ date -ud @253402214400
255+
// Fri Dec 31 00:00:00 UTC 9999
256+
// $ TZ=Asia/Vladivostok date -d @253402214400
257+
// Fri Dec 31 10:00:00 +10 9999
258+
// $ TZ=America/Juneau date -d @253402214400
259+
// Thu Dec 30 15:00:00 AKST 9999
260+
return std::fmin(std::fmax(timestamp, 0.0), 253402214400.0) * 1000.0;
249261
}
250262

251263
String IcingaDB::IcingaToStreamValue(const Value& value)

0 commit comments

Comments
 (0)