|
| 1 | +/* |
| 2 | + * time.c - ESP8266-specific functions for SNTP |
| 3 | + * Copyright (c) 2015 Peter Dobler. All rights reserved. |
| 4 | + * This file is part of the esp8266 core for Arduino environment. |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 2.1 of the License, or (at your option) any later version. |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the GNU Lesser General Public License for more details. |
| 13 | + * You should have received a copy of the GNU Lesser General Public |
| 14 | + * License along with this library; if not, write to the Free Software |
| 15 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +#include <time.h> |
| 20 | +#include "sntp.h" |
| 21 | + |
| 22 | + |
| 23 | +#ifndef _TIMEVAL_DEFINED |
| 24 | +#define _TIMEVAL_DEFINED |
| 25 | +struct timeval { |
| 26 | + time_t tv_sec; |
| 27 | + suseconds_t tv_usec; |
| 28 | +}; |
| 29 | +#endif |
| 30 | + |
| 31 | +extern char* sntp_asctime(const struct tm *t); |
| 32 | +extern struct tm* sntp_localtime(const time_t *clock); |
| 33 | + |
| 34 | +// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time) |
| 35 | +#define DIFF1900TO1970 2208988800UL |
| 36 | + |
| 37 | +static int s_daylightOffset_sec = 0; |
| 38 | +static int s_timezone_sec = 0; |
| 39 | +static time_t s_bootTime = 0; |
| 40 | + |
| 41 | +// calculate offset used in gettimeofday |
| 42 | +static void ensureBootTimeIsSet() |
| 43 | +{ |
| 44 | + if (!s_bootTime) |
| 45 | + { |
| 46 | + time_t now = sntp_get_current_timestamp(); |
| 47 | + if (now) |
| 48 | + { |
| 49 | + s_bootTime = - millis() / 1000; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +static void setServer(int id, const char* name_or_ip) |
| 55 | +{ |
| 56 | + if (name_or_ip) |
| 57 | + { |
| 58 | + //TODO: check whether server is given by name or IP |
| 59 | + sntp_setservername(0, (char*) name_or_ip); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +void configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) |
| 64 | +{ |
| 65 | + sntp_stop(); |
| 66 | + |
| 67 | + setServer(0, server1); |
| 68 | + setServer(1, server2); |
| 69 | + setServer(2, server3); |
| 70 | + |
| 71 | + s_timezone_sec = timezone; |
| 72 | + s_daylightOffset_sec = daylightOffset_sec; |
| 73 | + sntp_set_timezone(timezone/3600); |
| 74 | + sntp_init(); |
| 75 | +} |
| 76 | + |
| 77 | +int clock_gettime(clockid_t unused, struct timespec *tp) |
| 78 | +{ |
| 79 | + tp->tv_sec = millis() / 1000; |
| 80 | + tp->tv_nsec = micros() * 1000; |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +// seconds since 1970 |
| 85 | +time_t mktime(struct tm *t) |
| 86 | +{ |
| 87 | + // system_mktime expects month in range 1..12 |
| 88 | + #define START_MONTH 1 |
| 89 | + return DIFF1900TO1970 + system_mktime(t->tm_year, t->tm_mon + START_MONTH, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); |
| 90 | +} |
| 91 | + |
| 92 | +time_t time(time_t * t) |
| 93 | +{ |
| 94 | + time_t seconds = sntp_get_current_timestamp(); |
| 95 | + ensureBootTimeIsSet(); |
| 96 | + if (t) |
| 97 | + { |
| 98 | + *t = seconds; |
| 99 | + } |
| 100 | + return seconds; |
| 101 | +} |
| 102 | + |
| 103 | +char* asctime(const struct tm *t) |
| 104 | +{ |
| 105 | + return sntp_asctime(t); |
| 106 | +} |
| 107 | + |
| 108 | +struct tm* localtime(const time_t *clock) |
| 109 | +{ |
| 110 | + return sntp_localtime(clock); |
| 111 | +} |
| 112 | + |
| 113 | +char* ctime(const time_t *t) |
| 114 | +{ |
| 115 | + struct tm* p_tm = localtime(t); |
| 116 | + char* result = asctime(p_tm); |
| 117 | + return result; |
| 118 | +} |
| 119 | + |
| 120 | +int gettimeofday(struct timeval *tp, void *tzp) |
| 121 | +{ |
| 122 | + if (tp) |
| 123 | + { |
| 124 | + ensureBootTimeIsSet(); |
| 125 | + tp->tv_sec = (s_bootTime + millis()) / 1000; |
| 126 | + tp->tv_usec = micros() * 1000; |
| 127 | + } |
| 128 | +} |
0 commit comments