ptth/bare_minimum_crypto/cpp/time_helpers.cpp

36 lines
888 B
C++

#include "time_helpers.h"
#include <chrono>
#include <iostream>
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 ();
}
// 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;
}
}