]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Refactor portions of getCurrentTime() to use std::chrono (#1755)
authorAmos Jeffries <yadij@users.noreply.github.com>
Tue, 26 Mar 2024 20:17:04 +0000 (20:17 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Wed, 27 Mar 2024 16:19:49 +0000 (16:19 +0000)
POSIX.1-2008 marks gettimeofday() as obsolete.

src/time/gadgets.cc

index 3ab1b87bcfcbbcfe8ce6396ee252e50c06232a19..d542dfacd4ff738f1ca697568ac5c7a3692169ba 100644 (file)
@@ -11,6 +11,7 @@
 #include "squid.h"
 #include "time/gadgets.h"
 
+#include <chrono>
 #include <iomanip>
 #include <ostream>
 
@@ -21,12 +22,11 @@ time_t squid_curtime = 0;
 time_t
 getCurrentTime()
 {
-#if GETTIMEOFDAY_NO_TZP
-    gettimeofday(&current_time);
-#else
+    using namespace std::chrono;
+    const auto now = system_clock::now().time_since_epoch();
 
-    gettimeofday(&current_time, nullptr);
-#endif
+    current_time.tv_sec = duration_cast<seconds>(now).count();
+    current_time.tv_usec = duration_cast<microseconds>(now).count() % 1000000;
 
     current_dtime = (double) current_time.tv_sec +
                     (double) current_time.tv_usec / 1000000.0;