diff --git a/api/unittest/plgdtimetest.cpp b/api/unittest/plgdtimetest.cpp index 13ee1e75bb..044c97168c 100644 --- a/api/unittest/plgdtimetest.cpp +++ b/api/unittest/plgdtimetest.cpp @@ -148,6 +148,7 @@ TEST_F(TestPlgdTime, Seconds) oc_clock_time_t start_ts = start / OC_CLOCK_SECOND; EXPECT_LE(start_ts, now_ts); EXPECT_GT(oc_clock_seconds(), now_ts); + EXPECT_GT(oc_clock_seconds_v1(), now_ts); } TEST_F(TestPlgdTime, IsActive) diff --git a/port/android/clock.c b/port/android/clock.c index 208e7d5470..009c587d43 100644 --- a/port/android/clock.c +++ b/port/android/clock.c @@ -56,8 +56,8 @@ oc_clock_time_monotonic(void) (oc_clock_time_t)ceil((double)t.tv_nsec / (1.e09 / OC_CLOCK_SECOND)); } -unsigned long -oc_clock_seconds(void) +uint64_t +oc_clock_seconds_v1(void) { struct timespec t; if (clock_gettime(CLOCK_REALTIME, &t) != -1) { @@ -66,6 +66,12 @@ oc_clock_seconds(void) return 0; } +unsigned long +oc_clock_seconds(void) +{ + return (unsigned long)oc_clock_seconds_v1(); +} + void oc_clock_wait(oc_clock_time_t t) { diff --git a/port/arduino/adapter/oc_clock.c b/port/arduino/adapter/oc_clock.c index 70111575ad..8fe816c0c3 100644 --- a/port/arduino/adapter/oc_clock.c +++ b/port/arduino/adapter/oc_clock.c @@ -54,10 +54,16 @@ oc_clock_time_monotonic(void) return oc_clock_time(); } +uint64_t +oc_clock_seconds_v1(void) +{ + return (uint64_t)oc_clock_seconds_v1(); +} + unsigned long oc_clock_seconds(void) { - return (unsigned long)secondNow(); + return (unsigned long)oc_clock_seconds_v1(); } void diff --git a/port/esp32/adapter/src/clock.c b/port/esp32/adapter/src/clock.c index e17a9b97c0..be681a6152 100644 --- a/port/esp32/adapter/src/clock.c +++ b/port/esp32/adapter/src/clock.c @@ -56,8 +56,8 @@ oc_clock_time_monotonic(void) (double)(esp_timer_get_time() / (1.e06 / OC_CLOCK_SECOND))); } -unsigned long -oc_clock_seconds(void) +uint64_t +oc_clock_seconds_v1(void) { struct timespec t; if (clock_gettime(CLOCK_REALTIME, &t) != -1) { @@ -66,6 +66,12 @@ oc_clock_seconds(void) return 0; } +unsigned long +oc_clock_seconds(void) +{ + return (unsigned long)oc_clock_seconds_v1(); +} + void oc_clock_wait(oc_clock_time_t t) { diff --git a/port/linux/clock.c b/port/linux/clock.c index d69453bb77..6bb6062c1c 100644 --- a/port/linux/clock.c +++ b/port/linux/clock.c @@ -56,8 +56,8 @@ oc_clock_time_monotonic(void) (oc_clock_time_t)ceil((double)t.tv_nsec / (1.e09 / OC_CLOCK_SECOND)); } -unsigned long -oc_clock_seconds(void) +uint64_t +oc_clock_seconds_v1(void) { struct timespec t; if (clock_gettime(CLOCK_REALTIME, &t) != -1) { @@ -66,6 +66,12 @@ oc_clock_seconds(void) return 0; } +unsigned long +oc_clock_seconds(void) +{ + return (unsigned long)oc_clock_seconds_v1(); +} + void oc_clock_wait(oc_clock_time_t t) { diff --git a/port/oc_clock.h b/port/oc_clock.h index 09de18260c..d51e1d8897 100644 --- a/port/oc_clock.h +++ b/port/oc_clock.h @@ -54,6 +54,8 @@ #include "oc_clock_util.h" #include "oc_export.h" +#include "util/oc_compiler.h" + #include #include @@ -121,13 +123,22 @@ oc_clock_time_t oc_clock_time_monotonic(void); /** * Get the current value of the platform seconds. * - * This could be the number of seconds since startup, or - * since a standard epoch. + * This could be the number of seconds since startup, or since a standard epoch. * * \return The value. */ OC_API -unsigned long oc_clock_seconds(void); +uint64_t oc_clock_seconds_v1(void); + +/** + * Get the current value of the platform seconds. + + * @deprecated Use oc_clock_seconds_v1 instead because it returns a 64-bit value + * on all platforms and thus is safe for years 2038+. + */ +OC_API +unsigned long oc_clock_seconds(void) + OC_DEPRECATED("replaced by oc_clock_seconds_v1 in v2.2.5.7"); /** * Wait for a given number of ticks. diff --git a/port/openthread/clock.c b/port/openthread/clock.c index 362686b92d..fa29c0ae5d 100644 --- a/port/openthread/clock.c +++ b/port/openthread/clock.c @@ -56,12 +56,17 @@ oc_clock_time_monotonic(void) return oc_clock_time(); } +uint64_t +oc_clock_seconds_v1(void) +{ + uint64_t time = oc_clock_time() / OC_CLOCK_SECOND; + return time; +} + unsigned long oc_clock_seconds(void) { - unsigned long time = oc_clock_time() / OC_CLOCK_SECOND; - - return time; + return (unsigned long)oc_clock_seconds_v1(); } void diff --git a/port/windows/clock.c b/port/windows/clock.c index b0137e49b2..e7e9940810 100644 --- a/port/windows/clock.c +++ b/port/windows/clock.c @@ -121,10 +121,16 @@ oc_clock_time_monotonic(void) return whole + part; } +uint64_t +oc_clock_seconds_v1(void) +{ + return (uint64_t)time(0); +} + unsigned long oc_clock_seconds(void) { - return (unsigned long)time(0); + return (unsigned long)oc_clock_seconds_v1(); } void diff --git a/port/zephyr/src/clock.c b/port/zephyr/src/clock.c index 88881c1df4..ab02fdac78 100644 --- a/port/zephyr/src/clock.c +++ b/port/zephyr/src/clock.c @@ -41,10 +41,16 @@ oc_clock_time_monotonic(void) return oc_clock_time(); } +uint64_t +oc_clock_seconds_v1(void) +{ + return (oc_clock_time() + OC_CLOCK_SECOND - 1) / OC_CLOCK_SECOND; +} + unsigned long oc_clock_seconds(void) { - return (oc_clock_time() + OC_CLOCK_SECOND - 1) / OC_CLOCK_SECOND; + return (unsigned long)oc_clock_seconds_v1(); } void diff --git a/swig/swig_interfaces/oc_clock.i b/swig/swig_interfaces/oc_clock.i index 89d9e3dbf9..7e9630f7d7 100644 --- a/swig/swig_interfaces/oc_clock.i +++ b/swig/swig_interfaces/oc_clock.i @@ -31,8 +31,10 @@ typedef long long oc_clock_time_t; %rename(clockTime) oc_clock_time; %rename(clockTimeMonotonic) oc_clock_time_monotonic; %rename(clockSeconds) oc_clock_seconds; +%rename(clockSecondsV1) oc_clock_seconds_v1; %rename(clockWait) oc_clock_wait; #define OC_API +#define OC_DEPRECATED(...) #define OC_NONNULL(...) %include "port/oc_clock.h"