From 43af5893fb69d3de9e98dffba58f14308eddb83a Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 19 Oct 2020 21:34:40 +1100 Subject: [PATCH] AP_RTC: added mktime(), used by AP_Filesystem and AP_GPS --- libraries/AP_RTC/AP_RTC.cpp | 43 +++++++++++++++++++++++++++++++++++++ libraries/AP_RTC/AP_RTC.h | 3 +++ 2 files changed, 46 insertions(+) diff --git a/libraries/AP_RTC/AP_RTC.cpp b/libraries/AP_RTC/AP_RTC.cpp index e765a4e986..ef4af285fb 100644 --- a/libraries/AP_RTC/AP_RTC.cpp +++ b/libraries/AP_RTC/AP_RTC.cpp @@ -3,6 +3,7 @@ #include #include #include +#include extern const AP_HAL::HAL& hal; @@ -205,6 +206,48 @@ uint32_t AP_RTC::get_time_utc(int32_t hour, int32_t min, int32_t sec, int32_t ms } +/* + mktime replacement from Samba + */ +time_t AP_RTC::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; +} + // singleton instance AP_RTC *AP_RTC::_singleton; diff --git a/libraries/AP_RTC/AP_RTC.h b/libraries/AP_RTC/AP_RTC.h index ea904d82bb..36ac3def04 100644 --- a/libraries/AP_RTC/AP_RTC.h +++ b/libraries/AP_RTC/AP_RTC.h @@ -44,6 +44,9 @@ public: uint32_t get_time_utc(int32_t hour, int32_t min, int32_t sec, int32_t ms); + // replacement for mktime() + static time_t mktime(const struct tm *t); + // get singleton instance static AP_RTC *get_singleton() { return _singleton;