11#include " hmac_utils.hpp"
22#include < ctime>
3+ #include < cerrno>
34#include < stdexcept>
45#include < limits>
56
@@ -20,8 +21,9 @@ namespace hmac {
2021 if (interval_sec <= 0 ) {
2122 throw std::invalid_argument (" interval_sec must be positive" );
2223 }
24+ errno = 0 ;
2325 std::time_t now = std::time (nullptr );
24- if (now == static_cast <std::time_t >(-1 )) {
26+ if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
2527 throw std::runtime_error (" std::time failed" );
2628 }
2729 std::time_t rounded = (now / interval_sec) * interval_sec;
@@ -32,8 +34,9 @@ namespace hmac {
3234 if (interval_sec <= 0 ) {
3335 throw std::invalid_argument (" interval_sec must be positive" );
3436 }
37+ errno = 0 ;
3538 std::time_t now = std::time (nullptr );
36- if (now == static_cast <std::time_t >(-1 )) {
39+ if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
3740 throw std::runtime_error (" std::time failed" );
3841 }
3942 std::time_t rounded = (now / interval_sec) * interval_sec;
@@ -51,8 +54,9 @@ namespace hmac {
5154 if (interval_sec <= 0 ) {
5255 throw std::invalid_argument (" interval_sec must be positive" );
5356 }
57+ errno = 0 ;
5458 std::time_t now = std::time (nullptr );
55- if (now == static_cast <std::time_t >(-1 )) {
59+ if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
5660 throw std::runtime_error (" std::time failed" );
5761 }
5862 std::time_t rounded = (now / interval_sec) * interval_sec;
@@ -64,8 +68,9 @@ namespace hmac {
6468 if (interval_sec <= 0 ) {
6569 throw std::invalid_argument (" interval_sec must be positive" );
6670 }
71+ errno = 0 ;
6772 std::time_t now = std::time (nullptr );
68- if (now == static_cast <std::time_t >(-1 )) {
73+ if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
6974 throw std::runtime_error (" std::time failed" );
7075 }
7176 std::time_t rounded = (now / interval_sec) * interval_sec;
@@ -144,8 +149,9 @@ namespace hmac {
144149 if (digits < 1 || digits > 9 ) {
145150 throw std::invalid_argument (" TOTP: digits must be in range [1, 9]" );
146151 }
152+ errno = 0 ;
147153 std::time_t now = std::time (nullptr );
148- if (now == static_cast <std::time_t >(-1 )) {
154+ if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
149155 throw std::runtime_error (" std::time failed" );
150156 }
151157 uint64_t timestamp = static_cast <uint64_t >(now);
@@ -192,8 +198,9 @@ namespace hmac {
192198 if (digits < 1 || digits > 9 ) {
193199 throw std::invalid_argument (" TOTP: digits must be in range [1, 9]" );
194200 }
201+ errno = 0 ;
195202 std::time_t now = std::time (nullptr );
196- if (now == static_cast <std::time_t >(-1 )) {
203+ if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
197204 throw std::runtime_error (" std::time failed" );
198205 }
199206 uint64_t timestamp = static_cast <uint64_t >(now);
0 commit comments