|
| 1 | +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "esp32-hal.h" |
| 16 | +#include "apps/sntp/sntp.h" |
| 17 | + |
| 18 | +static void setTimeZone(long offset, int daylight) |
| 19 | +{ |
| 20 | + char cst[16] = {0}; |
| 21 | + char cdt[16] = "CDT"; |
| 22 | + char tz[32] = {0}; |
| 23 | + |
| 24 | + if(offset % 3600){ |
| 25 | + sprintf(cst, "CST%ld:%02u:%02u", offset / 3600, abs((offset % 3600) / 60), abs(offset % 60)); |
| 26 | + } else { |
| 27 | + sprintf(cst, "CST%ld", offset / 3600); |
| 28 | + } |
| 29 | + if(daylight != 3600){ |
| 30 | + long tz_dst = offset - daylight; |
| 31 | + if(tz_dst % 3600){ |
| 32 | + sprintf(cdt, "CDT%ld:%02u:%02u", tz_dst / 3600, abs((tz_dst % 3600) / 60), abs(tz_dst % 60)); |
| 33 | + } else { |
| 34 | + sprintf(cdt, "CDT%ld", tz_dst / 3600); |
| 35 | + } |
| 36 | + } |
| 37 | + sprintf(tz, "%s%s", cst, cdt); |
| 38 | + setenv("TZ", tz, 1); |
| 39 | + tzset(); |
| 40 | +} |
| 41 | + |
| 42 | +/* |
| 43 | + * configTime |
| 44 | + * Source: https://github.com/esp8266/Arduino/blob/master/cores/esp8266/time.c |
| 45 | + * */ |
| 46 | +void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) |
| 47 | +{ |
| 48 | + |
| 49 | + if(sntp_enabled()){ |
| 50 | + sntp_stop(); |
| 51 | + } |
| 52 | + sntp_setoperatingmode(SNTP_OPMODE_POLL); |
| 53 | + sntp_setservername(0, (char*)server1); |
| 54 | + sntp_setservername(1, (char*)server2); |
| 55 | + sntp_setservername(2, (char*)server3); |
| 56 | + sntp_init(); |
| 57 | + setTimeZone(gmtOffset_sec, daylightOffset_sec); |
| 58 | +} |
| 59 | + |
| 60 | +bool getLocalTime(struct tm * info, uint32_t ms) |
| 61 | +{ |
| 62 | + uint32_t count = ms / 10; |
| 63 | + time_t now; |
| 64 | + |
| 65 | + time(&now); |
| 66 | + localtime_r(&now, info); |
| 67 | + |
| 68 | + if(info->tm_year > (2016 - 1900)){ |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + while(count--) { |
| 73 | + delay(10); |
| 74 | + time(&now); |
| 75 | + localtime_r(&now, info); |
| 76 | + if(info->tm_year > (2016 - 1900)){ |
| 77 | + return true; |
| 78 | + } |
| 79 | + } |
| 80 | + return false; |
| 81 | +} |
| 82 | + |
0 commit comments