From e659667f0320a8ae9a1564017edac79aa61d2641 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Thu, 22 Jun 2023 14:23:22 +1000 Subject: [PATCH] AP_Common: move implemenation of ap_mktime (NFC) --- libraries/AP_Common/time.cpp | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 libraries/AP_Common/time.cpp diff --git a/libraries/AP_Common/time.cpp b/libraries/AP_Common/time.cpp new file mode 100644 index 0000000000..81f81b0cfd --- /dev/null +++ b/libraries/AP_Common/time.cpp @@ -0,0 +1,44 @@ +#include "time.h" + +/* + mktime replacement from Samba + */ +time_t ap_mktime(const struct tm *t) +{ + time_t epoch = 0; + int n; + int mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, y, m, i; + const unsigned MINUTE = 60; + const unsigned HOUR = 60*MINUTE; + const unsigned DAY = 24*HOUR; + const unsigned YEAR = 365*DAY; + + if (t->tm_year < 70) { + return (time_t)-1; + } + + n = t->tm_year + 1900 - 1; + epoch = (t->tm_year - 70) * YEAR + + ((n / 4 - n / 100 + n / 400) - (1969 / 4 - 1969 / 100 + 1969 / 400)) * DAY; + + y = t->tm_year + 1900; + m = 0; + + for (i = 0; i < t->tm_mon; i++) { + epoch += mon [m] * DAY; + if (m == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) { + epoch += DAY; + } + + if (++m > 11) { + m = 0; + y++; + } + } + + epoch += (t->tm_mday - 1) * DAY; + epoch += t->tm_hour * HOUR + t->tm_min * MINUTE + t->tm_sec; + + return epoch; +} +