From a6cca2cd71fcc8f3717b406cffec0913968f081c Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Thu, 16 Dec 2021 14:21:44 +0100 Subject: [PATCH 1/4] import getLocalTime() from esp32/Arduino follows #8407 --- cores/esp8266/Arduino.h | 2 ++ cores/esp8266/timehelper.cpp | 19 +++++++++++++++ .../LittleFS_Timestamp/LittleFS_Timestamp.ino | 23 ------------------- tests/host/Makefile | 1 + 4 files changed, 22 insertions(+), 23 deletions(-) create mode 100644 cores/esp8266/timehelper.cpp diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 2aab5770c9..e59155adcc 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -279,6 +279,8 @@ inline void configTzTime(const char* tz, const char* server1, configTime(tz, server1, server2, server3); } +bool getLocalTime(struct tm * info, uint32_t ms = 5000); + // Everything we expect to be implicitly loaded for the sketch #include diff --git a/cores/esp8266/timehelper.cpp b/cores/esp8266/timehelper.cpp new file mode 100644 index 0000000000..235bfd0a49 --- /dev/null +++ b/cores/esp8266/timehelper.cpp @@ -0,0 +1,19 @@ + +#include "Arduino.h" + +// /~https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-time.c + +bool getLocalTime(struct tm * info, uint32_t ms) +{ + uint32_t start = millis(); + time_t now; + while((millis()-start) <= ms) { + time(&now); + localtime_r(&now, info); + if(info->tm_year > (2016 - 1900)){ + return true; + } + delay(10); + } + return false; +} diff --git a/libraries/LittleFS/examples/LittleFS_Timestamp/LittleFS_Timestamp.ino b/libraries/LittleFS/examples/LittleFS_Timestamp/LittleFS_Timestamp.ino index dfec9ee301..40e02087dc 100644 --- a/libraries/LittleFS/examples/LittleFS_Timestamp/LittleFS_Timestamp.ino +++ b/libraries/LittleFS/examples/LittleFS_Timestamp/LittleFS_Timestamp.ino @@ -19,29 +19,6 @@ long timezone = 2; byte daysavetime = 1; -bool getLocalTime(struct tm * info, uint32_t ms) { - uint32_t count = ms / 10; - time_t now; - - time(&now); - localtime_r(&now, info); - - if (info->tm_year > (2016 - 1900)) { - return true; - } - - while (count--) { - delay(10); - time(&now); - localtime_r(&now, info); - if (info->tm_year > (2016 - 1900)) { - return true; - } - } - return false; -} - - void listDir(const char * dirname) { Serial.printf("Listing directory: %s\n", dirname); diff --git a/tests/host/Makefile b/tests/host/Makefile index 3a5979c312..338cc4dfb8 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -97,6 +97,7 @@ CORE_CPP_FILES := \ HardwareSerial.cpp \ crc32.cpp \ Updater.cpp \ + timehelper.cpp \ ) \ $(addprefix $(abspath $(LIBRARIES_PATH)/ESP8266SdFat/src)/, \ FatLib/FatFile.cpp \ From b96338f45399cb62760cfe680944f788e09b8a39 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Mon, 3 Jan 2022 17:52:09 +0100 Subject: [PATCH 2/4] +license --- cores/esp8266/timehelper.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cores/esp8266/timehelper.cpp b/cores/esp8266/timehelper.cpp index 235bfd0a49..7e57af1d04 100644 --- a/cores/esp8266/timehelper.cpp +++ b/cores/esp8266/timehelper.cpp @@ -1,3 +1,21 @@ +/* + * timehelper.c - generic time functions + * Copyright (c) 2021 esp8266/Arduino. All rights reserved. + * This file is part of the esp8266 core for Arduino environment. + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + #include "Arduino.h" From dc62b198804c3a6c3c113353b0ff54f0510e13b6 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Mon, 3 Jan 2022 22:12:59 +0100 Subject: [PATCH 3/4] move getLocalTime to time.cpp --- cores/esp8266/time.cpp | 27 ++++++++++++++++++++++++-- cores/esp8266/timehelper.cpp | 37 ------------------------------------ 2 files changed, 25 insertions(+), 39 deletions(-) delete mode 100644 cores/esp8266/timehelper.cpp diff --git a/cores/esp8266/time.cpp b/cores/esp8266/time.cpp index 8e997c72d5..d8308e62d7 100644 --- a/cores/esp8266/time.cpp +++ b/cores/esp8266/time.cpp @@ -20,6 +20,28 @@ * synchronisation of the two through timeshift64 */ +#include + +// /~https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-time.c + +bool getLocalTime(struct tm * info, uint32_t ms) +{ + uint32_t start = millis(); + time_t now; + while((millis()-start) <= ms) { + time(&now); + localtime_r(&now, info); + if(info->tm_year > (2016 - 1900)){ + return true; + } + delay(10); + } + return false; +} + + +#if !defined(CORE_MOCK) + #include #include <../include/time.h> // See issue #6714 #include @@ -33,7 +55,6 @@ extern "C" { #include #include -#include // configTime() extern "C" { @@ -257,4 +278,6 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz) return 0; } -}; +}; // extern "C" + +#endif // !defined(CORE_MOCK) diff --git a/cores/esp8266/timehelper.cpp b/cores/esp8266/timehelper.cpp deleted file mode 100644 index 7e57af1d04..0000000000 --- a/cores/esp8266/timehelper.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * timehelper.c - generic time functions - * Copyright (c) 2021 esp8266/Arduino. All rights reserved. - * This file is part of the esp8266 core for Arduino environment. - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Lesser General Public License for more details. - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - - -#include "Arduino.h" - -// /~https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-time.c - -bool getLocalTime(struct tm * info, uint32_t ms) -{ - uint32_t start = millis(); - time_t now; - while((millis()-start) <= ms) { - time(&now); - localtime_r(&now, info); - if(info->tm_year > (2016 - 1900)){ - return true; - } - delay(10); - } - return false; -} From b2c213ffe6f4c178e5e23f66a4f0648b34553020 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Mon, 3 Jan 2022 22:18:58 +0100 Subject: [PATCH 4/4] fix host makefile --- tests/host/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/host/Makefile b/tests/host/Makefile index 338cc4dfb8..b7c8d1ce20 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -97,7 +97,7 @@ CORE_CPP_FILES := \ HardwareSerial.cpp \ crc32.cpp \ Updater.cpp \ - timehelper.cpp \ + time.cpp \ ) \ $(addprefix $(abspath $(LIBRARIES_PATH)/ESP8266SdFat/src)/, \ FatLib/FatFile.cpp \