Skip to content

Commit 0ddc08a

Browse files
d-a-vhasenradball
authored andcommitted
import getLocalTime() from esp32/Arduino (esp8266#8413)
* import getLocalTime() from esp32/Arduino follows esp8266#8407
1 parent 0f58236 commit 0ddc08a

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

cores/esp8266/Arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ inline void configTzTime(const char* tz, const char* server1,
279279
configTime(tz, server1, server2, server3);
280280
}
281281

282+
bool getLocalTime(struct tm * info, uint32_t ms = 5000);
283+
282284
// Everything we expect to be implicitly loaded for the sketch
283285
#include <pgmspace.h>
284286

cores/esp8266/time.cpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@
2020
* synchronisation of the two through timeshift64
2121
*/
2222

23+
#include <Arduino.h>
24+
25+
// https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-time.c
26+
27+
bool getLocalTime(struct tm * info, uint32_t ms)
28+
{
29+
uint32_t start = millis();
30+
time_t now;
31+
while((millis()-start) <= ms) {
32+
time(&now);
33+
localtime_r(&now, info);
34+
if(info->tm_year > (2016 - 1900)){
35+
return true;
36+
}
37+
delay(10);
38+
}
39+
return false;
40+
}
41+
42+
43+
#if !defined(CORE_MOCK)
44+
2345
#include <stdlib.h>
2446
#include <../include/time.h> // See issue #6714
2547
#include <sys/time.h>
@@ -33,7 +55,6 @@ extern "C" {
3355
#include <coredecls.h>
3456
#include <Schedule.h>
3557

36-
#include <Arduino.h> // configTime()
3758

3859
extern "C" {
3960

@@ -257,4 +278,6 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
257278
return 0;
258279
}
259280

260-
};
281+
}; // extern "C"
282+
283+
#endif // !defined(CORE_MOCK)

libraries/LittleFS/examples/LittleFS_Timestamp/LittleFS_Timestamp.ino

-23
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,6 @@ long timezone = 2;
1919
byte daysavetime = 1;
2020

2121

22-
bool getLocalTime(struct tm * info, uint32_t ms) {
23-
uint32_t count = ms / 10;
24-
time_t now;
25-
26-
time(&now);
27-
localtime_r(&now, info);
28-
29-
if (info->tm_year > (2016 - 1900)) {
30-
return true;
31-
}
32-
33-
while (count--) {
34-
delay(10);
35-
time(&now);
36-
localtime_r(&now, info);
37-
if (info->tm_year > (2016 - 1900)) {
38-
return true;
39-
}
40-
}
41-
return false;
42-
}
43-
44-
4522
void listDir(const char * dirname) {
4623
Serial.printf("Listing directory: %s\n", dirname);
4724

tests/host/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ CORE_CPP_FILES := \
9797
HardwareSerial.cpp \
9898
crc32.cpp \
9999
Updater.cpp \
100+
time.cpp \
100101
) \
101102
$(addprefix $(abspath $(LIBRARIES_PATH)/ESP8266SdFat/src)/, \
102103
FatLib/FatFile.cpp \

0 commit comments

Comments
 (0)