From e213e7e4282e70bffac23294157378e89af378ff Mon Sep 17 00:00:00 2001 From: Randy Mackay Date: Tue, 27 Jun 2023 10:36:58 +0900 Subject: [PATCH] AP_RTC: add get_date_and_time_utc --- libraries/AP_RTC/AP_RTC.cpp | 23 +++++++++++++++++++++++ libraries/AP_RTC/AP_RTC.h | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/libraries/AP_RTC/AP_RTC.cpp b/libraries/AP_RTC/AP_RTC.cpp index f93f129d23..eb2ea15184 100644 --- a/libraries/AP_RTC/AP_RTC.cpp +++ b/libraries/AP_RTC/AP_RTC.cpp @@ -205,6 +205,29 @@ uint32_t AP_RTC::get_time_utc(int32_t hour, int32_t min, int32_t sec, int32_t ms return static_cast(total_delay_ms); } +// get date and time. Returns true on success and fills in year, month, day, hour, min, sec and ms +// year is the regular Gregorian year, month is 0~11, day is 1~31, hour is 0~23, minute is 0~59, second is 0~60 (1 leap second) +bool AP_RTC::get_date_and_time_utc(uint16_t& year, uint8_t& month, uint8_t& day, uint8_t &hour, uint8_t &min, uint8_t &sec) const +{ + // get local time of day in ms + uint64_t time_us = 0; + if (!get_utc_usec(time_us)) { + return false; + } + time_t utc_sec = time_us / (1000U * 1000U); + struct tm* tm = gmtime(&utc_sec); + if (tm == nullptr) { + return false; + } + year = tm->tm_year+1900; /* Year - 1900. */ + month = tm->tm_mon; /* Month. [0-11] */ + day = tm->tm_mday; /* Day. [1-31] */ + hour = tm->tm_hour; /* Hours. [0-23] */ + min = tm->tm_min; /* Minutes. [0-59] */ + sec = tm->tm_sec; /* Seconds. [0-60] (1 leap second) */ + return true; +} + // singleton instance AP_RTC *AP_RTC::_singleton; diff --git a/libraries/AP_RTC/AP_RTC.h b/libraries/AP_RTC/AP_RTC.h index 39b76b7d91..d84a23cfc4 100644 --- a/libraries/AP_RTC/AP_RTC.h +++ b/libraries/AP_RTC/AP_RTC.h @@ -44,6 +44,10 @@ public: uint32_t get_time_utc(int32_t hour, int32_t min, int32_t sec, int32_t ms); + // get date and time. Returns true on success and fills in year, month, day, hour, min, sec and ms + // year is the regular Gregorian year, month is 0~11, day is 1~31, hour is 0~23, minute is 0~59, second is 0~60 (1 leap second) + bool get_date_and_time_utc(uint16_t& year, uint8_t& month, uint8_t& day, uint8_t &hour, uint8_t &min, uint8_t &sec) const; + // get singleton instance static AP_RTC *get_singleton() { return _singleton;