Skip to content

Commit 418857a

Browse files
authored
Implement sntp_get|set_timezone_in_seconds() (esp8266#5828)
* Implement sntp_get|set_timezone_in_seconds() * Fix typo * Fix typo * Get lwip1.4 to compile This is just a workaround to get lwip1.4 to compile. It doesn't implement sntp_set_timezone_in_seconds() correctly, but rather does pretty much the same as sntp_set_timezone().
1 parent ead83c1 commit 418857a

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

cores/esp8266/sntp-lwip2.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,18 @@ void settimeofday_cb (void (*cb)(void))
5858

5959
static const char stod14[] PROGMEM = "settimeofday() can't set time!\n";
6060
bool sntp_set_timezone(sint8 timezone);
61+
bool sntp_set_timezone_in_seconds(sint32 timezone)
62+
{
63+
return sntp_set_timezone((sint8)(timezone/(60*60))); //TODO: move this to the same file as sntp_set_timezone() in lwip1.4, and implement correctly over there.
64+
}
65+
6166
void sntp_set_daylight(int daylight);
6267

6368
int settimeofday(const struct timeval* tv, const struct timezone* tz)
6469
{
6570
if (tz) /*before*/
6671
{
67-
sntp_set_timezone(tz->tz_minuteswest / 60);
72+
sntp_set_timezone_in_seconds(tz->tz_minuteswest * 60);
6873
// apparently tz->tz_dsttime is a bitfield and should not be further used (cf man)
6974
sntp_set_daylight(0);
7075
}
@@ -89,7 +94,7 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
8994

9095
static uint32 realtime_stamp = 0;
9196
static uint16 dst = 0;
92-
static sint8 time_zone = 8; // espressif HQ's default timezone
97+
static sint32 time_zone = 8 * (60 * 60); // espressif HQ's default timezone
9398
LOCAL os_timer_t sntp_timer;
9499

95100
/*****************************************/
@@ -410,21 +415,36 @@ char* sntp_get_real_time(time_t t)
410415
return sntp_asctime(sntp_localtime (&t));
411416
}
412417

413-
sint8 sntp_get_timezone(void)
418+
/* Returns the set timezone in seconds. If the timezone was set as seconds, the fractional part is floored. */
419+
sint32 sntp_get_timezone_in_seconds(void)
414420
{
415421
return time_zone;
416422
}
417423

418-
bool sntp_set_timezone(sint8 timezone)
424+
/* Returns the set timezone in hours. If the timezone was set as seconds, the fractional part is floored. */
425+
sint8 sntp_get_timezone(void)
426+
{
427+
return (sint8)(time_zone / (60 * 60));
428+
}
429+
430+
/* Sets the timezone in hours. Internally, the timezone is converted to seconds. */
431+
bool sntp_set_timezone_in_seconds(sint32 timezone)
419432
{
420-
if(timezone >= -11 || timezone <= 13) {
433+
if(timezone >= (-11 * (60 * 60)) || timezone <= (13 * (60 * 60))) {
421434
time_zone = timezone;
422435
return true;
423436
} else {
424437
return false;
425438
}
426439
}
427440

441+
/* Sets the timezone in hours. Internally, the timezone is converted to seconds. */
442+
bool sntp_set_timezone(sint8 timezone)
443+
{
444+
return sntp_set_timezone_in_seconds((sint32)timezone * 60 * 60);
445+
}
446+
447+
428448
void sntp_set_daylight(int daylight)
429449
{
430450
dst = daylight;
@@ -437,7 +457,7 @@ void ICACHE_RAM_ATTR sntp_time_inc (void)
437457

438458
static void sntp_set_system_time (uint32_t t)
439459
{
440-
realtime_stamp = t + time_zone * 60 * 60 + dst;
460+
realtime_stamp = t + time_zone + dst;
441461
os_timer_disarm(&sntp_timer);
442462
os_timer_setfn(&sntp_timer, (os_timer_func_t *)sntp_time_inc, NULL);
443463
os_timer_arm(&sntp_timer, 1000, 1);
@@ -447,7 +467,7 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
447467
{
448468
if (tz) /*before*/
449469
{
450-
sntp_set_timezone(tz->tz_minuteswest / 60);
470+
sntp_set_timezone_in_seconds(tz->tz_minuteswest * 60);
451471
// apparently tz->tz_dsttime is a bitfield and should not be further used (cf man)
452472
sntp_set_daylight(0);
453473
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy