From 5d617ca90cbfd09e7d6c4e21f66dbcebd3a60f24 Mon Sep 17 00:00:00 2001 From: Tester23 Date: Mon, 30 Oct 2023 08:44:09 +0100 Subject: [PATCH] add day/year/month access for scripts so we can script a date display --- src/cmnds/cmd_if.c | 21 ++++++++++++++++++++- src/driver/drv_ntp.c | 36 ++++++++++++++++++++++++++++++++++++ src/driver/drv_ntp.h | 3 +++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/cmnds/cmd_if.c b/src/cmnds/cmd_if.c index 9f719b21c..e95f08718 100644 --- a/src/cmnds/cmd_if.c +++ b/src/cmnds/cmd_if.c @@ -252,6 +252,15 @@ float getHour(const char *s) { float getSecond(const char *s) { return NTP_GetSecond(); } +float getYear(const char *s) { + return NTP_GetYear(); +} +float getMonth(const char *s) { + return NTP_GetMonth(); +} +float getMDay(const char *s) { + return NTP_GetMDay(); +} const constant_t g_constants[] = { //cnstdetail:{"name":"MQTTOn", @@ -375,11 +384,21 @@ const constant_t g_constants[] = { //cnstdetail:"descr":"Current second from NTP", //cnstdetail:"requires":""} { "$second", &getSecond }, + ////cnstdetail:{"name":"$mday", + ////cnstdetail:"title":"$mday", + ////cnstdetail:"descr":"Current mday from NTP", + ////cnstdetail:"requires":""} + { "$mday", &getMDay }, ////cnstdetail:{"name":"$month", ////cnstdetail:"title":"$month", ////cnstdetail:"descr":"Current month from NTP", ////cnstdetail:"requires":""} - //{ "$month", &getMonth }, + { "$month", &getMonth }, + ////cnstdetail:{"name":"$year", + ////cnstdetail:"title":"$year", + ////cnstdetail:"descr":"Current Year from NTP", + ////cnstdetail:"requires":""} + { "$year", &getYear }, //cnstdetail:{"name":"$NTPOn", //cnstdetail:"title":"$NTPOn", //cnstdetail:"descr":"Returns 1 if NTP is on and already synced (so device has correct time), otherwise 0.", diff --git a/src/driver/drv_ntp.c b/src/driver/drv_ntp.c index f8e63e6b4..24d80a55b 100644 --- a/src/driver/drv_ntp.c +++ b/src/driver/drv_ntp.c @@ -165,6 +165,42 @@ int NTP_GetSecond() { return ltm->tm_sec; } +int NTP_GetMDay() { + struct tm *ltm; + + // NOTE: on windows, you need _USE_32BIT_TIME_T + ltm = localtime((time_t*)&g_ntpTime); + + if (ltm == 0) { + return 0; + } + + return ltm->tm_mday; +} +int NTP_GetMonth() { + struct tm *ltm; + + // NOTE: on windows, you need _USE_32BIT_TIME_T + ltm = localtime((time_t*)&g_ntpTime); + + if (ltm == 0) { + return 0; + } + + return ltm->tm_mon+1; +} +int NTP_GetYear() { + struct tm *ltm; + + // NOTE: on windows, you need _USE_32BIT_TIME_T + ltm = localtime((time_t*)&g_ntpTime); + + if (ltm == 0) { + return 0; + } + + return ltm->tm_year+1900; +} #if WINDOWS bool b_ntp_simulatedTime = false; void NTP_SetSimulatedTime(unsigned int timeNow) { diff --git a/src/driver/drv_ntp.h b/src/driver/drv_ntp.h index 658f1f9d6..42a4d130e 100644 --- a/src/driver/drv_ntp.h +++ b/src/driver/drv_ntp.h @@ -15,6 +15,9 @@ int NTP_GetWeekDay(); int NTP_GetHour(); int NTP_GetMinute(); int NTP_GetSecond(); +int NTP_GetMDay(); +int NTP_GetMonth(); +int NTP_GetYear(); // for Simulator only, on Windows, for unit testing void NTP_SetSimulatedTime(unsigned int timeNow); // drv_ntp_events.c