ptth/bare_minimum_crypto/cpp/time_helpers.cpp

36 lines
888 B
C++
Raw Normal View History

2021-01-17 21:45:59 +00:00
#include "time_helpers.h"
#include <chrono>
2021-01-17 21:53:04 +00:00
#include <iostream>
2021-01-17 21:45:59 +00:00
namespace BareMinimumCrypto {
using namespace std;
using namespace chrono;
int64_t get_seconds_since_epoch () {
const auto utc_now = system_clock::now ();
return duration_cast <seconds> (utc_now.time_since_epoch ()).count ();
}
2021-01-17 21:53:04 +00:00
// Most tests will use a virtual clock. But just as a smoke test,
// make sure real time is realistic.
int test_time () {
const auto seconds_since_epoch = get_seconds_since_epoch ();
const auto time_of_writing = 1610844872;
if (seconds_since_epoch < time_of_writing) {
cerr << "Error: Real time is in the past." << endl;
return 1;
}
const int64_t about_100_years = (int64_t)100 * 365 * 86400;
if (seconds_since_epoch > time_of_writing + about_100_years) {
cerr << "Error: Real time is in the far future." << endl;
return 1;
}
return 0;
}
2021-01-17 21:45:59 +00:00
}