Initial commit

pull/1/head
Rop 7 years ago
commit 225358bb79

2
.gitattributes vendored

@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto

2
.gitignore vendored

@ -0,0 +1,2 @@
.development

@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

@ -0,0 +1,81 @@
# ezTime Documentation
## A brief history of ezTime
## Getting started
## Some quick examples
# ezTime: complete documentation
## NTP, staying in sync
**`void ezTime.setServer(String ntp_server = NTP_SERVER)`**
**`void ezTime.setInterval(uint16_t seconds = 0)`**
**`bool ezTime.waitForSync(uint16_t timeout = 0)`**
**`void ezTime.updateNow()`**
**`timeStatus_t ezTime.timeStatus()`**
**`bool ezTime.queryNTP(String server, time_t &t, unsigned long &measured_at)`**
**`time_t ezTime.now()`**
**`void ezTime.breakTime(time_t time, tmElements_t &tm)`**
**`time_t ezTime.makeTime(tmElements_t &tm)`**
**`time_t ezTime.makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, int16_t year)`**
**`time_t ezTime.makeUmpteenthTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t umpteenth, uint8_t wday, uint8_t month, int16_t year)`**
## Timezones
bool setPosix(String posix);
String getPosix();
bool setLocation(String location, bool force_lookup = false);
String getOlsen();
void setDefault();
bool isDST_local(time_t t = TIME_NOW);
bool isDST_UTC(time_t t = TIME_NOW);
bool isDST();
String getTimezoneName(time_t t = TIME_NOW);
int32_t getOffset(time_t t = TIME_NOW);
time_t now(bool update_last_read = true);
void setTime(time_t t);
void setTime(const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr);
String dateTime(String format = DEFAULT_TIMEFORMAT); // http://php.net/manual/en/function.date.php for conversion
String dateTime(time_t t, String format = DEFAULT_TIMEFORMAT);
uint8_t hour(time_t t = TIME_NOW); // 0-23
uint8_t minute(time_t t = TIME_NOW); // 0-59
uint8_t second(time_t t = TIME_NOW); // 0-59
uint16_t ms(time_t t = TIME_NOW); // 0-999
uint8_t day(time_t t = TIME_NOW); // 1-31
uint8_t weekday(time_t t = TIME_NOW); // Day of the week (1-7), Sunday is day 1
uint8_t month(time_t t = TIME_NOW); // 1-12
uint16_t year(time_t t = TIME_NOW); // four digit year
bool secondChanged(); // Since last call to something that caused a time read, to avoid excessive calling of, eg, timeString
bool minuteChanged();
**`void ezTime.clearCache()`**
## Error handling, debug information
**`ezError_t ezTime.error()`**
**`String ezTime.errorString(ezError_t err)`**
**`void ezTime.debug(ezDebugLevel_t level)`**
## Free extras
String urlEncode(String str); // Does what you think it does
String zeropad(uint32_t number, uint8_t length); // Returns number as string of given length, zero-padded on left if needed
String getBetween(String &haystack, String before_needle, String after_needle = ""); // Returns what's between before_needle and after_needle in haystack, or "" if not found. Returns until end of string if after_needle is empty
bool timezoneAPI(String location, String &olsen, String &posix);

@ -0,0 +1,7 @@
#
# Main Makefile. This is basically the same as a component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
COMPONENT_SRCDIRS := src
COMPONENT_ADD_INCLUDEDIRS := src

@ -0,0 +1,32 @@
#include <ezTime.h>
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.begin("your-ssid", "your-password");
ezTime.waitForSync();
Serial.println();
Serial.println("Time in various internet standard formats ...");
Serial.println();
Serial.println("ATOM: " + UTC.dateTime(ATOM));
Serial.println("COOKIE: " + UTC.dateTime(COOKIE));
Serial.println("IS8601: " + UTC.dateTime(ISO8601));
Serial.println("RFC822: " + UTC.dateTime(RFC822));
Serial.println("RFC850: " + UTC.dateTime(RFC850));
Serial.println("RFC1036: " + UTC.dateTime(RFC1036));
Serial.println("RFC1123: " + UTC.dateTime(RFC1123));
Serial.println("RFC2822: " + UTC.dateTime(RFC2822));
Serial.println("RFC3339: " + UTC.dateTime(RFC3339));
Serial.println("RFC3339_EXT: " + UTC.dateTime(RFC3339_EXT));
Serial.println("RSS: " + UTC.dateTime(RSS));
Serial.println("W3C: " + UTC.dateTime(W3C));
Serial.println();
Serial.println(" ... and any other format, like \"" + UTC.dateTime("l ~t~h~e jS ~o~f F Y, g:i A") + "\"");
}
void loop() {
}

@ -0,0 +1,33 @@
#include <ezTime.h>
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.begin("your-ssid", "your-password");
// Uncomment the line below to see what it does behind the scenes
// ezTime.debug(INFO);
ezTime.waitForSync();
Serial.println();
Serial.println("UTC: " + UTC.dateTime());
Timezone myTZ;
myTZ.setLocation("Pacific/Auckland"); // Anything with a slash in it is interpreted as an official timezone name
Serial.println("Auckland: " + myTZ.dateTime()); // https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
myTZ.setLocation("Paris, Texas"); // Anything else is parsed as an address to see if that resolves
Serial.println("Paris, Texas: " + myTZ.dateTime());
myTZ.setLocation(""); // The empty string is resolved to the GeoIP location of your IP-address
Serial.println("Your local time: " + myTZ.dateTime());
}
void loop() {
}

@ -0,0 +1,36 @@
#include <ezTime.h>
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.begin("your-ssid", "your-password");
ezTime.setInterval(60);
ezTime.waitForSync();
Serial.println();
for (int n = 0; n < 10; n++) {
Serial.println(UTC.dateTime("H:i:s.v"));
}
Serial.println();
Serial.println("Those milliseconds between the first and the last line ...");
Serial.println();
Serial.println(" ... that's how long it took to get them out the serial port.");
Serial.println();
Serial.println();
Serial.println();
Serial.println("And ezTime is not making those milliseconds up either.");
Serial.println();
Serial.println(" ... Stick around as we do an NTP request every minute.");
ezTime.debug(INFO);
}
void loop() {
now();
delay(1000);
}

@ -0,0 +1,20 @@
{
"name": "ezTime",
"description": "An ESP32 time library, drop-in replacement for Arduino TimeLib, does NTP, supports timezones",
"keywords": "esp32",
"authors": {
"name": "Rop Gonggrijp",
"url": "https://github.com/ropg"
"maintainer": true
},
"repository": {
"type": "git",
"url": "https://github.com/ropg/ezTime"
},
"version": "0.0.1",
"framework": "arduino",
"platforms": "espressif32"
"build": {
"libArchive": false
}
}

@ -0,0 +1,10 @@
name=ezTime
version=0.0.1
author=Rop Gonggrijp
maintainer=Rop Gonggrijp
sentence=An ESP32 time library, drop-in replacement for Arduino TimeLib, does NTP, supports timezones.
paragraph=See more on https://github.com/ropg/ezTime
category=Timing
url=https://github.com/ropg/ezTime
architectures=esp32
includes=ezTime.h

File diff suppressed because it is too large Load Diff

@ -0,0 +1,250 @@
#ifndef _EZTIME_H_
#define _EZTIME_H_
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
////////// Error handing
typedef enum {
NO_ERROR,
LAST_ERROR, // Pseudo error: replaced by last error
NO_NETWORK,
TIMEOUT,
CONNECT_FAILED,
DATA_NOT_FOUND
} ezError_t;
typedef enum {
NONE,
ERROR,
INFO,
DEBUG
} ezDebugLevel_t;
////////////////////////
#define LEAP_YEAR(Y) ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) )
#define SECS_PER_DAY (86400UL)
typedef struct {
uint8_t Second;
uint8_t Minute;
uint8_t Hour;
uint8_t Wday; // day of week, sunday is day 1
uint8_t Day;
uint8_t Month;
uint8_t Year; // offset from 1970;
} tmElements_t;
typedef enum {
timeNotSet,
timeNeedsSync,
timeSet
} timeStatus_t;
typedef struct {
String std_tzname;
String dst_tzname;
int32_t std_offset;
int32_t dst_offset;
time_t dst_start_in_local;
time_t dst_end_in_local;
time_t dst_start_in_utc;
time_t dst_end_in_utc;
} tzData_t;
#define ARDUINO_TIMELIB_COMPATIBILITY
#define TIME_NOW 0xFFFFFFFF
#define LAST_READ 0xFFFFFFFE
#define NTP_PACKET_SIZE 48
#define NTP_LOCAL_PORT 2342
#define NTP_SERVER "pool.ntp.org"
#define NTP_TIMEOUT 1500 // milliseconds
#define UPDATE_INTERVAL 600 // default update interval in seconds
// Various date-time formats
#define ATOM "Y-m-d\\TH:i:sP"
#define COOKIE "l, d-M-Y H:i:s T"
#define ISO8601 "Y-m-d\\TH:i:sO"
#define RFC822 "D, d M y H:i:s O"
#define RFC850 "l, d-M-y H:i:s T"
#define RFC1036 "D, d M y H:i:s O"
#define RFC1123 "D, d M Y H:i:s O"
#define RFC2822 "D, d M Y H:i:s O"
#define RFC3339 "Y-m-d\\TH:i:sP"
#define RFC3339_EXT "Y-m-d\\TH:i:s.vP"
#define RSS "D, d M Y H:i:s O"
#define W3C "Y-m-d\\TH:i:sP"
#define DEFAULT_TIMEFORMAT RFC850
static const char * english_months[] PROGMEM = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
static const char * english_days[] PROGMEM = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" , "Saturday" };
static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; // API starts months from 1, this array starts from 0
class EZtime {
friend class Timezone;
////////// Error handing
public:
ezError_t error(); // Returns ezError_t enumeration of last error, resets _last_error to OK
String errorString(ezError_t err); // Human-readable form of last error.
void debug(ezDebugLevel_t level); // Sets serial printing of debug info to specified ezDebugLevel_t enumeration
private:
void error(ezError_t err); // Used to set an error
void debug(ezDebugLevel_t level, String str); // Used to print debug info
void debugln(ezDebugLevel_t level, String str); // same, just adds \r\n
String debugLevelString(ezDebugLevel_t level); // Human-readable form of debug level.
ezError_t _last_error;
ezDebugLevel_t _debug_level;
///////////
public:
EZtime();
void setServer(String ntp_server = NTP_SERVER);
void setInterval(uint16_t seconds = 0); // 0 = no NTP updates
bool waitForSync(uint16_t timeout = 0); // timeout in seconds
timeStatus_t timeStatus();
bool queryNTP(String server, time_t &t, unsigned long &measured_at); // measured_at: millis() at measurement, t is converted to secs since 1970
void updateNow();
time_t now();
void clearCache();
void breakTime(time_t time, tmElements_t &tm); // break time_t into elements
time_t makeTime(tmElements_t &tm); // convert time elements into time_t
time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, int16_t year);
time_t makeUmpteenthTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t umpteenth, uint8_t wday, uint8_t month, int16_t year);
private:
uint16_t _update_interval; // in seconds
tzData_t parsePosix(String posix, int16_t year);
time_t _last_sync_time, _update_due;
unsigned long _last_sync_millis;
bool _debug_enabled, _ntp_enabled;
uint16_t _ntp_local_port, _ntp_interval, _ntp_max_drift, _last_read_ms;
String _ntp_server;
timeStatus_t _time_status;
////////// Free extras ...
public:
String urlEncode(String str); // Does what you think it does
String zeropad(uint32_t number, uint8_t length); // Returns number as string of given length, zero-padded on left if needed
String getBetween(String &haystack, String before_needle, String after_needle = ""); // Returns what's between before_needle and after_needle in haystack, or "" if not found. Returns until end of string if after_needle is empty
bool timezoneAPI(String location, String &olsen, String &posix);
//////////
};
extern EZtime ezTime;
class Timezone {
public:
Timezone(bool locked_to_UTC = false);
bool setPosix(String posix);
String getPosix();
bool setLocation(String location, bool force_lookup = false);
String getOlsen();
void setDefault();
bool isDST_local(time_t t = TIME_NOW);
bool isDST_UTC(time_t t = TIME_NOW);
bool isDST();
String getTimezoneName(time_t t = TIME_NOW);
int32_t getOffset(time_t t = TIME_NOW);
time_t now(bool update_last_read = true);
void setTime(time_t t);
void setTime(const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr);
String dateTime(String format = DEFAULT_TIMEFORMAT); // http://php.net/manual/en/function.date.php for conversion
String dateTime(time_t t, String format = DEFAULT_TIMEFORMAT);
uint8_t hour(time_t t = TIME_NOW); // 0-23
uint8_t minute(time_t t = TIME_NOW); // 0-59
uint8_t second(time_t t = TIME_NOW); // 0-59
uint16_t ms(time_t t = TIME_NOW); // 0-999
uint8_t day(time_t t = TIME_NOW); // 1-31
uint8_t weekday(time_t t = TIME_NOW); // Day of the week (1-7), Sunday is day 1
uint8_t month(time_t t = TIME_NOW); // 1-12
uint16_t year(time_t t = TIME_NOW); // four digit year
bool secondChanged(); // Since last call to something that caused a time read, to avoid excessive calling of, eg, timeString
bool minuteChanged();
private:
time_t _readTime(time_t t);
bool _locked_to_UTC;
tzData_t _tzdata;
String _posix, _olsen;
time_t _last_read_t;
};
extern Timezone UTC;
extern Timezone& defaultTZ;
#ifdef ARDUINO_TIMELIB_COMPATIBILITY
/*==============================================================================*/
/* Useful Constants */
#define SECS_PER_MIN (60UL)
#define SECS_PER_HOUR (3600UL)
#define DAYS_PER_WEEK (7UL)
#define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK)
#define SECS_PER_YEAR (SECS_PER_WEEK * 52UL)
#define SECS_YR_2000 (946684800UL) // the time at the start of y2k
/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define dayOfWeek(_time_) ((( _time_ / SECS_PER_DAY + 4) % DAYS_PER_WEEK)+1) // 1 = Sunday
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) // this is number of days since Jan 1 1970
#define elapsedSecsToday(_time_) (_time_ % SECS_PER_DAY) // the number of seconds since last midnight
// The following macros are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971
// Always set the correct time before settting alarms
#define previousMidnight(_time_) (( _time_ / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day
#define nextMidnight(_time_) ( previousMidnight(_time_) + SECS_PER_DAY ) // time at the end of the given day
#define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + ((dayOfWeek(_time_)-1) * SECS_PER_DAY) ) // note that week starts on day 1
#define previousSunday(_time_) (_time_ - elapsedSecsThisWeek(_time_)) // time at the start of the week for the given time
#define nextSunday(_time_) ( previousSunday(_time_)+SECS_PER_WEEK) // time at the end of the week for the given time
/* Useful Macros for converting elapsed time to a time_t */
#define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN)
#define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR)
#define daysToTime_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011
#define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK)
time_t now();
uint8_t second(time_t t /* = TIME_NOW */);
uint8_t minute(time_t t /* = TIME_NOW */);
uint8_t hour(time_t t /* = TIME_NOW */);
uint8_t day(time_t t /* = TIME_NOW */);
uint8_t weekday(time_t t /* = TIME_NOW */);
uint8_t month(time_t t /* = TIME_NOW */);
uint16_t year(time_t t /* = TIME_NOW */);
uint8_t hourFormat12(time_t t /* = TIME_NOW */);
bool isAM(time_t t /* = TIME_NOW */);
bool isPM(time_t t /* = TIME_NOW */);
String monthStr(const uint8_t month);
String monthShortStr(const uint8_t month);
String dayStr(const uint8_t month);
String dayShortStr(const uint8_t month);
void setTime(time_t t);
void setTime(const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t month, const uint16_t yr);
void breakTime(time_t t, tmElements_t &tm);
time_t makeTime(tmElements_t &tm);
time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, int16_t year);
timeStatus_t timeStatus();
#endif //ARDUINO_TIMELIB_COMPATIBILITY
#endif //_EZTIME_H_
Loading…
Cancel
Save