diff --git a/README.md b/README.md
index c37f341..20bf562 100644
--- a/README.md
+++ b/README.md
@@ -4,13 +4,7 @@
* limitations may apply, see "[2036 and 2038](#2036-and-2038)" chapter
-
-
-
-
-
-| **Major renaming/restructuring still taking place. (The "time" object conflicts on some platforms.)
Please test with it but do not use it in for serious things until a few days from now.** |
-|----|
+![](images/moving-clock.gif)
@@ -60,7 +54,7 @@ void setup() {
Serial.begin(115200);
WiFi.begin("your-ssid", "your-password");
- time.waitForSync();
+ waitForSync();
Serial.println("UTC: " + UTC.dateTime());
@@ -136,13 +130,12 @@ Saturday, 25-Aug-18 14:32:53.303 UTC
```
[...]
- time.setInterval(60);
- time.setDebugLevel(INFO);
+ setInterval(60);
+ setDebugLevel(INFO);
}
void loop() {
- time.events();
- delay(1000);
+ events();
}
```
@@ -199,49 +192,62 @@ in File -> Examples you will now see an ezTime heading down under "Examples from
* [No daemons here](#no-daemons-here)
* [But I only just woke up !](#but-i-only-just-woke-up-)
* [Setting and synchronising time](#setting-and-synchronising-time)
- * [time.timeStatus](#timetimestatus)
- * [time.waitForSync](#timewaitforsync)
- * [time.setServer and time.setInterval](#timesetserver-and-timesetinterval)
- * [time.updateNTP](#timeupdatentp)
- * [time.queryNTP](#timequeryntp)
+ * [timeStatus](#timestatus)
+ * [waitForSync](#waitforsync)
+ * [setServer and setInterval](#setserver-and-setinterval)
+ * [updateNTP](#updatentp)
+ * [queryNTP](#queryntp)
* [Timezones](#timezones-1)
- * [yourTZ.setDefault](#yourtzsetdefault)
- * [yourTZ.setPosix](#yourtzsetposix)
- * [yourTZ.getPosix](#yourtzgetposix)
- * [yourTZ.isDST](#yourtzisdst)
- * [yourTZ.getTimezoneName](#yourtzgettimezonename)
- * [yourTZ.getOffset](#yourtzgetoffset)
- * [yourTZ.setLocation](#yourtzsetlocation)
+ * [tz.setDefault](#tzsetdefault)
+ * [tz.setPosix](#tzsetposix)
+ * [[tz.]getPosix](#tzgetposix)
+ * [[tz.]isDST](#tzisdst)
+ * [[tz.]getTimezoneName](#tzgettimezonename)
+ * [[tz.]getOffset](#tzgetoffset)
+ * [tz.setLocation](#tzsetlocation)
* [Timezone caching, timezoneapi.io, EEPROM or NVS](#timezone-caching-timezoneapiio-eeprom-or-nvs)
* [setCache](#setcache)
* [clearCache](#clearcache)
+ * [Crazy timezones](#crazy-timezones)
+ * [Chatham Islands and Nepal](#chatham-islands-and-nepal)
+ * [Morocco](#morocco)
* [Getting date and time](#getting-date-and-time)
- * [yourTZ.dateTime](#yourtzdatetime)
+ * [[tz.]dateTime](#tzdatetime)
* [Built-in date and time formats](#built-in-date-and-time-formats)
* [Time and date as numbers](#time-and-date-as-numbers)
- * [yourTZ.weekISO and yourTZ.yearISO](#yourtzweekiso-and-yourtzyeariso)
- * [time.secondChanged and time.minuteChanged](#timesecondchanged-and-timeminutechanged)
+ * [[tz.]weekISO and [tz.]yearISO](#tzweekiso-and-tzyeariso)
+ * [secondChanged and minuteChanged](#secondchanged-and-minutechanged)
+ * [[tz.]militaryTZ(TIME)](#tzmilitarytztime)
* [Events](#events)
- * [yourTZ.setEvent](#yourtzsetevent)
- * [time.deleteEvent](#timedeleteevent)
+ * [[tz.]setEvent](#tzsetevent)
+ * [deleteEvent](#deleteevent)
* [Setting date and time manually](#setting-date-and-time-manually)
- * [yourTZ.setTime](#yourtzsettime)
+ * [[tz.]setTime](#tzsettime)
* [Working with time values](#working-with-time-values)
- * [time.breakTime](#timebreaktime)
- * [time.makeTime](#timemaketime)
+ * [breakTime](#breaktime)
+ * [makeTime](#maketime)
* [makeOrdinalTime](#makeordinaltime)
* [compileTime](#compiletime)
* [tzTime](#tztime)
* [Various functions](#various-functions)
- * [time.urlEncode](#timeurlencode)
- * [time.zeropad](#timezeropad)
+ * [urlEncode](#urlencode)
+ * [zeropad](#zeropad)
* [Errors and debug information](#errors-and-debug-information)
- * [time.debugLevel](#timedebuglevel)
- * [time.error](#timeerror)
- * [time.errorString](#timeerrorstring)
+ * [debugLevel](#debuglevel)
+ * [error](#error)
+ * [errorString](#errorstring)
* [Compatibility with Arduino Time library](#compatibility-with-arduino-time-library)
* [Smaller footprint, AVR Arduinos](#smaller-footprint-avr-arduinos)
* [2036 and 2038](#2036-and-2038)
+ * [ezTime on various Arduino platforms](#eztime-on-various-arduino-platforms)
+ * [DSD Tech ESP8266](#dsd-tech-esp8266)
+ * [Teensy 3.2](#teensy-32)
+ * [Arduino Uno R3 (clone) with Ethernet Shield W5100](#arduino-uno-r3-clone-with-ethernet-shield-w5100)
+ * [M5Stack (ESP32)](#m5stack-esp32)
+ * [Arduino Micro](#arduino-micro)
+ * [Arduino Due](#arduino-due)
+ * [Arduino MKR1000](#arduino-mkr1000)
+ * [Arduino Nano](#arduino-nano)
## About this manual
@@ -272,33 +278,33 @@ For example, if you have set up a timezone called Berlin, `Berlin.isDST(15363142
### What happens when you include the library
-It all starts when you include the library with `#include `. From that point forward there is an object instance called `time` with methods to control the behaviour of ezTime, as well as a timezone object called `UTC`, and a reference to this object called `defaultTZ` (which you may point to a different timezone later).
+It all starts when you include the library with `#include `. From that point forward you can use the functions in this manual to control the behaviour of ezTime. There will then also be a timezone object called `UTC`, which will be set as the default timezone for all commands that take an optional timezone prefix.
### No daemons here
It is important to understand what ezTime does NOT do. It does not somehow create a background process that keeps time, contacts servers, or whatever. The Arduino does the timekeeping for us with its `millis()` counter, which keeps the time in milliseconds since the Arduino started. All ezTime does when it synchronises time is to store a time (in seconds since 1970) and the position of the millis counter when that was. By seeing how much the millis counter has advanced and adding that starting point since 1970, ezTime tells time. But that internal clock isn't perfect, it may — very slowly — drift away from the actual time. That's why there is a periodic event set to synchronise the clock with the NTP server.
-If you want events to happen — whether your own or the NTP updates that ezTime does periodically) — you should have `time.events()` in the main loop of your program.
+If you want events to happen — whether your own or the NTP updates that ezTime does periodically) — you should have `events()` in the main loop of your program.
### But I only just woke up !
Your code might call `Serial.println(UTC.dateTime());` to print a complete textual representation of date and time in the default format to the serial port. The library would find out that time had not been synchronised yet, and it would send off an NTP request to one of the NTP servers that `pool.ntp.org` resolves to. If your Arduino has just woken up, it probably hasn't gotten its DHCP information, or is not connected to the WiFi network just yet. And so the time lookup would fail and the call to `.dateTime` would return a String with the date and time just after midnight on the 1st of January 1970: the zero-point for the unix-style time counter used by ezTime. It would later correct to the real time, but that's not pretty.
-Worse is when you set up a timezone for which you would like to retrieve the daylight savings rules from the server: it can't do that if the connection isn't up yet. So that's why there's a function called `time.waitForSync` that simply calls `time.events()` until it is synchronized (or until a set number of seconds passes, see below).
+Worse is when you set up a timezone for which you would like to retrieve the daylight savings rules from the server: it can't do that if the connection isn't up yet. So that's why there's a function called `waitForSync` that simply calls `events()` until it is synchronized (or until a set number of seconds passes, see below).
## Setting and synchronising time
-The NTP request from the scenario above failed because the network wasn't up yet, so the clock would still not be synchronised. Subsequent requests will retry the NTP query, but only if they happen at least 5 seconds later.
+The NTP request from the scenario above failed because the network wasn't up yet, so the clock would still not be synchronised. A new request will be scheduled for 5 seconds later, and sent when your code (or `waitForSync`) calls `events`.
-### time.timeStatus
+### timeStatus
-`timeStatus_t time.timeStatus();`
+`timeStatus_t timeStatus();`
-Returns what state the clock is in. `time.timeStatus()` will return one of:
+Returns what state the clock is in. `timeStatus()` will return one of:
| timeStatus | meaning |
|----|----|
@@ -308,39 +314,39 @@ Returns what state the clock is in. `time.timeStatus()` will return one of:
-### time.waitForSync
+### waitForSync
-`bool time.waitForSync(uint16_t timeout = 0);`
+`bool waitForSync(uint16_t timeout = 0);`
-If your code uses timezones other than UTC, it might want to wait to initialise them until there is a valid time to see if the cached timezone definitions are still current. And if you are displaying a calendar or clock, it might look silly if it first says midnight on January 1st 1970 before showing the real time. `time.waitForSync` will wait for the network to connect, and then for the time to be synchronised before returning `true`. If you specify a timeout (in seconds), it will return after that many seconds even if the clock is not in sync yet, returning `false`. (ezTime error `TIMEOUT`, see the [chapter on error and debug messages](#errors-and-debug-information) further down)
+If your code uses timezones other than UTC, it might want to wait to initialise them until there is a valid time to see if the cached timezone definitions are still current. And if you are displaying a calendar or clock, it might look silly if it first says midnight on January 1st 1970 before showing the real time. `waitForSync` will wait for the network to connect, and then for the time to be synchronised before returning `true`. If you specify a timeout (in seconds), it will return after that many seconds even if the clock is not in sync yet, returning `false`. (ezTime error `TIMEOUT`, see the [chapter on error and debug messages](#errors-and-debug-information) further down)
-### *time.setServer and time.setInterval*
+### *setServer and setInterval*
-`void time.setServer(String ntp_server = NTP_SERVER);`
+`void setServer(String ntp_server = NTP_SERVER);`
-`void time.setInterval(uint16_t seconds = 0);`
+`void setInterval(uint16_t seconds = 0);`
-By default, ezTime is set to poll `pool.ntp.org` every 10 minutes. These defaults should work for most people, but you can change them by specifying a new server with `time.setServer` or a new interval (in seconds) with time.setInterval. If you call setInterval with an interval of 0 seconds or call it as `time.setInterval()`, no more NTP queries will be made.
+By default, ezTime is set to poll `pool.ntp.org` every 10 minutes. These defaults should work for most people, but you can change them by specifying a new server with `setServer` or a new interval (in seconds) with setInterval. If you call setInterval with an interval of 0 seconds or call it as `setInterval()`, no more NTP queries will be made.
-### *time.updateNTP*
+### *updateNTP*
-`void time.updateNTP();`
+`void updateNTP();`
Updates the time from the NTP server immediately. Will keep retrying every 5 seconds (defined by `NTP_RETRY` in `ezTime.h`), will schedule the next update to happen after the normal interval.
-### *time.queryNTP*
+### *queryNTP*
-`bool time.queryNTP(String server, time_t &t, unsigned long &measured_at);`
+`bool queryNTP(String server, time_t &t, unsigned long &measured_at);`
This will send a single query to the NTP server your specify. It will put, in the `t` and `measured_at` variables passed by reference, the UTC unix-time and the `millis()` counter at the time the exact second happened. It does this by subtracting from `millis()` the fractional seconds received in the answer, as well as half the time it took to get an answer. This means it assumes the network delay was symmetrical, meaning it took just as long for the request to get to the server as for the answer to get back.
-If the time server answers, `time.queryNTP` returns `true`. If `false` is returned, `time.error()` will return either `NO_NETWORK` (if the WiFi is not connected) or `TIMEOUT` if a response took more than 1500 milliseconds (defined by `NTP_TIMEOUT` in `ezTime.h`).
+If the time server answers, `queryNTP` returns `true`. If `false` is returned, `error()` will return either `NO_NETWORK` (if the WiFi is not connected) or `TIMEOUT` if a response took more than 1500 milliseconds (defined by `NTP_TIMEOUT` in `ezTime.h`).
Note that this function is used internally by ezTime, but does not by itself set the time ezTime keeps. You will likely never need to call this from your code.
@@ -372,17 +378,17 @@ Internally, ezTime stores everything it knows about a timezone as two strings. O
-### yourTZ.setDefault
+### tz.setDefault
-`void yourTZ.setDefault()`
+`void tz.setDefault()`
`#include ` includes the library, creates `ezTime` object and `UTC` instance of `Timezone` class, as well as `defaultTZ`, which is a reference to UTC unless you set it to another timezone by calling `yourTZ.setDefault()`. ezTime is compatible with the classic Arduino time library, and thus you can call various functions in the root namespace like `hour()` and `minute()` — without a timezone in front. They are interpreted as if passed to the default timezone. So if you have existing code, just setting up a timezone and making it the default should cause that code to work as if the time was set in local time. New code that depends on ezTime should probably explicitly mention the timezone.
-### yourTZ.setPosix
+### tz.setPosix
-`bool yourTZ.setPosix(String posix)`
+`bool tz.setPosix(String posix)`
Allows you to directly enter the posix information for a timezone. For simple timezones, you could set things up manually. For example for India, a mere
@@ -396,41 +402,41 @@ is enough, because the time in India doesn't go back and forth with the coming a
-### yourTZ.getPosix
+### [tz.]getPosix
-`String yourTZ.getPosix()`
+`String [tz.]getPosix()`
`getPosix` does what you would expect and simply returns the posix string stored in ezTime for a given timezone.
-### yourTZ.isDST
+### [tz.]isDST
-`bool yourTZ.isDST(TIME);`
+`bool [tz.]isDST(TIME);`
Tells you whether DST is in effect at a given time in this timezone. If you do not provide arguments, it's interpreted as 'right now'. You can also specify a time (in seconds since 1970, we'll get back to that) in the first argument. If you want to know for a time in UTC, you can set the second argument to `false`, otherwise you are assumed to mean in local time.
-### yourTZ.getTimezoneName
+### [tz.]getTimezoneName
-`String getTimezoneName(TIME);`
+`String [tz.]getTimezoneName(TIME);`
Provides the current short code for the timezone, like `IST` for India, or `CET` (during standard time) or `CEST` (during Daylight Saving Time) for most of Europe.
-### yourTZ.getOffset
+### [tz.]getOffset
-`int16_t yourTZ.getOffset(TIME)`
+`int16_t [tz.]getOffset(TIME)`
Provide the offset from UTC in minutes at the indicated time (or now if you do not specify anything). The offset here is in the same direction as the posix information, so -120 means 2 hours east of UTC.
-### yourTZ.setLocation
+### tz.setLocation
-`bool yourTZ.setLocation(String location = "")`
+`bool tz.setLocation(String location = "")`
With `setLocation` you can provide a string to do an internet lookup for a timezone. If the string contains a forward slash, the string is taken to be on Olsen timezone name, like `Europe/Berlin`. If it does not, it is parsed as a free form address, for which the system will try to find a timezone. You can enter "Paris" and get the info for "Europe/Paris", or enter "Paris, Texas" and get the timezone info for "America/Chicago", which is the Central Time timezone that Texas is in. After the information is retrieved, it is loaded in the current timezone, and cached if a cache is set (see below). `setLocation` will return `false` (Setting either `NO_NETWORK`, `CONNECT_FAILED` or `DATA_NOT_FOUND`) if it cannot find the information online.
@@ -442,23 +448,23 @@ If you use setLocation, the timezone information comes from timezoneapi.io, a se
That is why you can create a place for ezTime to store the data about the timezone. That way, it doens't need to get the information anew every time the Arduino boots. You can store the cache for a timezone in EEPROM (the default) or NVS.
-If your code says `yourTZ.setCache(0)` it will try to read and interpret the data from EEPROM location 0 immediately, and it will store any future updated data it receives for that timezone there. Some programs might want to just start up in whatever timezone the user has set before, so they just call `yourTZ.setCache(0)` when they start and `yourTZ.setLocation` when the user goes to settings to select a different timezone. Simple programs might do:
+If your code says `tz.setCache(0)` it will try to read and interpret the data from EEPROM location 0 immediately, and it will store any future updated data it receives for that timezone there. Some programs might want to just start up in whatever timezone the user has set before, so they just call `yourTZ.setCache(0)` when they start and `yourTZ.setLocation` when the user goes to settings to select a different timezone. Simple programs might do:
```
-if (!yourTZ.setCache(0)) yourTZ.setLocation("Europe/Berlin");
+if (!someTZ.setCache(0)) someTZ.setLocation("Europe/Berlin");
```
-To only get the timezone data from the internet when the cache is empty or outdated and use the cached information all the other times.
+To only get the timezone data from the internet when the cache is empty or outdated and use the cached information all the other times. (Note that if you change the city in the above example it will still get the Berlin information from the cache and not execute the `setLocation` until you run `someTZ.clearCache()`.
### setCache
-`bool yourTZ.setCache(const int16_t address)`
+`bool tz.setCache(const int16_t address)`
If your ezTime is compiled with `#define EZTIME_CACHE_EEPROM` (which is the default), you can supply an EEPROM location. A single timezone needs 50 bytes to cache. The data is written in compressed form so that the Olsen and Posix strings fit in 3/4 of the space they would normally take up, and along with it is stored a checksum, a length field and a single byte for the month in which the cache was retrieved, in months after January 2018.
-`bool yourTZ.setCache(const String name, const String key)`
+`bool tz.setCache(const String name, const String key)`
On ESP32 and possibly other platforms, there is an emulation for the EEPROM in flash, bu there is also a nicer mechanism that stores keys and values in flash. You can use this by enabling `#define EZTIME_CACHE_NVS` in `ezTime.h` You can then supply a section name and a key to serve as the cache storage location for a given timezone.
@@ -466,19 +472,30 @@ On ESP32 and possibly other platforms, there is an emulation for the EEPROM in f
### clearCache
-`void time.clearCache(bool delete_section = false);`
+`void clearCache(bool delete_section = false);`
Clears the cache for a timezone. If you use EEPROM the bytes are overwritten with zeroes, if you use NVS, the key is deleted. If you provide the argument `true` using NVS the entire section is deleted. Do this only if that section does not contain anything else that you want to keep.
+
-## Getting date and time
+### Crazy timezones
+
+#### Chatham Islands and Nepal
+
+The Chatham Islands are in Pacific about 800 kilometres east of New Zealand. Some 600 people live there, but they have their own timezone (UTC+12:45). It is one of only three time zones with a 45-minute offset from UTC, the others being Nepal Time (UTC+05:45) and the unofficial Australian Central Western Time (UTC+08:45). These timezones work fine in ezTime.
+
+#### Morocco
+
+Morocco goes on and off Daylight Saving Time twice per year. This currently breaks ezTime as timezoneapi.io gives us a posix string that only contains the first of the periods. Fortunately they will stop doing this in 2020: the Moroccans probably got tired of all the clocks that did not adjust properly.
-### yourTZ.dateTime
+## Getting date and time
+
+### [tz.]dateTime
```
-String yourTZ.dateTime(TIME, String format = DEFAULT_TIMEFORMAT);
+String [tz.]dateTime(TIME, String format = DEFAULT_TIMEFORMAT);
```
We'll start with one of the most powerful functions of ezTime. With `dateTime` you can represent a date and/or a time in any way you want. You do this in the same way you do in many programming languages: by providing a special formatting string. Many characters in this string have special meanings and will be replaced. What this means is that `UTC.dateTime("l, d-M-y H:i:s.v T")` might return `Saturday, 25-Aug-18 14:32:53.282 UTC`. Below is the list of characters and what they are replaced by. Any characters not on this list are simply not replaced and stay as is. See the last two entries for a way to use characters on this list in your string.
@@ -515,7 +532,8 @@ We'll start with one of the most powerful functions of ezTime. With `dateTime` y
| `Z` | Timezone offset in seconds. West of UTC is negative, east of UTC is positive.
| `z` | The day of the year (starting from 0)
| `W` | ISO-8601 week number. See right below for explanation link.
-| `X` | ISO-8601 year for year-week notation as four digit year. Warning: Not guaranteed to be same as current year, may be off by one at start or end of year. See [here](https://en.wikipedia.org/wiki/ISO_week_date)
+| `X` | ISO-8601 year for year-week notation as four digit year. Warning: Not guaranteed to be same as current year, may be off by one at start or end of year. See [here](https://en.wikipedia.org/wiki/ISO_week_date)
+| `B` | One-letter military code for the timezone, or `?` if the offset is not a whole number of hours.
| `\` | Not printed, but escapes the following character, meaning it will not be replaced. But inserting a backslash in the string means you have to supply two backslashes `\\` to be interpreted as one.
| `~` | (tilde) Same as backslash above, except easier to insert in the string. Example: `~t~h~e` will print the word `the` in the string. Letters should be escaped even if they are not on the list because they may be replaced in future versions.
@@ -547,13 +565,13 @@ There are built-in values to specify some standard date and time formats. For ex
### Time and date as numbers
-`time_t yourTZ.now()`
+`time_t [tz.]now()`
Returns the current time in seconds since midnight Jan 1st 1970 in the timezone specified.
-`uint8_t yourTZ.hour(TIME)`
`uint8_t yourTZ.minute(TIME)`
`uint8_t yourTZ.second(TIME)`
`uint16_t yourTZ.ms(TIME)`
`uint8_t yourTZ.day(TIME)`
`uint8_t yourTZ.weekday(TIME)`
`uint8_t yourTZ.month(TIME)`
`uint16_t yourTZ.year(TIME);`
+`uint8_t [tz.]hour(TIME)`
`uint8_t [tz.]minute(TIME)`
`uint8_t [tz.]second(TIME)`
`uint16_t [tz.]ms(TIME)`
`uint8_t [tz.]day(TIME)`
`uint8_t [tz.]weekday(TIME)`
`uint8_t [tz.]month(TIME)`
`uint16_t [tz.]year(TIME);`
-These functions return the various elements of date or time for right now (no arguments) or for a given time in seconds sinds 1970. The `yourTZ.weekday` returns a number starting with 1 for Sunday.
+These functions return the various elements of date or time for right now (no arguments) or for a given time in seconds sinds 1970. `weekday` returns a number starting with 1 for Sunday.
If you want to compare you can use the defines for names of days and months, like:
@@ -565,47 +583,57 @@ if (UTC.weekday() == TUESDAY) Serial.print("Tuesday!!");
if (UTC.month() == FEBRUARY && UTC.day() == 14) Serial.print("Valentine's day!");
```
-### *yourTZ.weekISO and yourTZ.yearISO*
+### *[tz.]weekISO and [tz.]yearISO*
-`uint8_t yourTZ.weekISO(TIME)`
`uint16_t yourTZ.yearISO(TIME)`
+`uint8_t [tz.]weekISO(TIME)`
`uint16_t [tz.]yearISO(TIME)`
These functions return the ISO-8601 Year-week notation year and week number. Note that the year returned here can differ one from the current year at the first or last days or the year. ISO-8601 defines the first year of the week as the first week that has a Thursday in it. Meaning the start of the ISO-year can be a few days earlier (in December) or a few days later (in January).
-### time.secondChanged and time.minuteChanged
+### secondChanged and minuteChanged
-`bool time.secondChanged()`
`bool time.minuteChanged()`
+`bool secondChanged()`
`bool minuteChanged()`
You might have code that put the time on a display in some really nice-looking format, using `dateTime`. The main loop wants to keep the time updated, but not every time the main loop runs, because it would cause the display to flicker. The classic solution for this is to store the time, recreate the string every time and compare to see if it changed. With `secondChanged` and `minuteChanged` you can just write something like:
```
-if (time.minuteChanged()) WriteToSomeDisplay(UTC.dateString("H:i"));
+if (minuteChanged()) WriteToSomeDisplay(UTC.dateString("H:i"));
```
+### *[tz.]militaryTZ(TIME)*
+
+`String [tz.]militaryTZ(TIME)`
+
+Returns the one-letter military code for the timezone. See [here](https://www.timeanddate.com/time/zones/military) for details. If the offset for the current timezone is not a whole number of hours, "?" is returned.
+
+
+
## Events
-### yourTZ.setEvent
+### [tz.]setEvent
-`uint8_t yourTZ.setEvent(void (*function)(), TIME)`
+`uint8_t [tz.]setEvent(void (*function)(), TIME)`
-`uint8_t yourTZ.setEvent(void (*function)(), const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr)`
+`uint8_t [tz.]setEvent(void (*function)(), const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr)`
-With ezTime, you can set your own events to run at a specified time. Simply run `setEvent` specifying the name of the function you would like to call (without the brackets) and a time you would like to call it. The first time `time.events` runs and notices that it is at or after the time you specified it will run the event and delete the event. If you want an event to recur, simply set a new event in the function that gets called. You can have a maximum of 8 events by default (easily changed by changing `MAX_EVENTS` in `ezTime.h`). ezTime uses one event internally to trigger the next NTP update.
+With ezTime, you can set your own events to run at a specified time. Simply run `setEvent` specifying the name of the function you would like to call (without the brackets) and a time you would like to call it. The first time `events` runs and notices that it is at or after the time you specified it will run the event and delete the event. If you want an event to recur, simply set a new event in the function that gets called. You can have a maximum of 8 events by default (easily changed by changing `MAX_EVENTS` in `ezTime.h`). ezTime uses one event internally to trigger the next NTP update.
-`yourTZ.setevent` returns an 8-bit event handle between 1 and MAX_EVENTS which you can store in a variable and use to delete the event with `time.deleteEvent` should your program need to. Zero is returned and the error `TOO_MANY_EVENTS` set if there are no more free slots for your new event.
+`[tz.]setevent` returns an 8-bit event handle between 1 and MAX_EVENTS which you can store in a variable and use to delete the event with `deleteEvent` should your program need to. Zero is returned and the error `TOO_MANY_EVENTS` set if there are no more free slots for your new event.
-### time.deleteEvent
+### deleteEvent
+
+`void deleteEvent(uint8_t event_handle)`
-`void time.deleteEvent(uint8_t event_handle)`
+Deletes the event with the handle as returned by `setEvent`.
-`void time.deleteEvent(void (*function)())`
+`void deleteEvent(void (*function)())`
-Buy you can also call `time.deleteEvent` with the name of the function (again without the brackets) to delete all events that would have executed that function.
+Buy you can also call `deleteEvent` with the name of the function (again without the brackets) to delete all events that would have executed that function.
@@ -614,21 +642,21 @@ Buy you can also call `time.deleteEvent` with the name of the function (again wi
![](images/setting-clock.jpg)
-### yourTZ.setTime
+### [tz.]setTime
-`void yourTZ.setTime(time_t t, uint16_t ms = 0)`
+`void [tz.]setTime(time_t t, uint16_t ms = 0)`
-`void yourTZ.setTime(const uint8_t hr, const uint8_t min, const uint8_t sec,`
`const uint8_t day, const uint8_t mnth, uint16_t yr)`
+`void [tz.]setTime(const uint8_t hr, const uint8_t min, const uint8_t sec,`
`const uint8_t day, const uint8_t mnth, uint16_t yr)`
`setTime` pretty much does what it says on the package: it sets the time to the time specified, either as separate elements or as a time_t value in seconds since Jan 1st 1970. If you have another source of time — say, a GPS receiver — you can use `setTime` to set the time in the UTC timezone. Or you can set the local time in any other timezone you have set up and ezTime will set it's internal offset to the corresponding time in UTC so all timezones stay at the correct time.
-It's important to realise however that NTP updates will still become due and when they do time will be set to the time returned by the NTP server. If you do not want that, you can turn off NTP updates with `eztime.setInterval()`. If you do not use NTP updates at all and do not use the network lookups for timezone information either, you can compile ezTime with no network support by commenting out `#define EZTIME_NETWORK_ENABLE` in the `ezTime.h` file, creating a smaller library.
+It's important to realise however that NTP updates will still become due and when they do time will be set to the time returned by the NTP server. If you do not want that, you can turn off NTP updates with `ezsetInterval()`. If you do not use NTP updates at all and do not use the network lookups for timezone information either, you can compile ezTime with no network support by commenting out `#define EZTIME_NETWORK_ENABLE` in the `ezTime.h` file, creating a smaller library.
## Working with time values
-### *time.breakTime*
+### *breakTime*
-`void time.breakTime(time_t time, tmElements_t &tm)`
+`void breakTime(time_t time, tmElements_t &tm)`
If you create a `tmElements_t` structure and pass it to `breakTime`, it will be filled with the various numeric elements of the time value specified. tmElements_t looks as follows:
@@ -648,7 +676,7 @@ Meaning this code would print the hour:
```
tmElements_t tm;
-time.breakTime(UTC.now(), tm);
+breakTime(UTC.now(), tm);
Serial.print(tm.Hour);
```
@@ -656,13 +684,13 @@ But `Serial.println(UTC.hour())` also works and is much simpler. `breakTime` is
-### time.makeTime
+### makeTime
-`time_t time.makeTime(tmElements_t &tm);`
+`time_t makeTime(tmElements_t &tm);`
-This does the opposite of `time.breakTime`: it takes a `tmElements_t` structure and turns it into a `time_t` value in seconds since Jan 1st 1970.
+This does the opposite of `breakTime`: it takes a `tmElements_t` structure and turns it into a `time_t` value in seconds since Jan 1st 1970.
-`time_t time.makeTime(uint8_t hour, uint8_t minute, uint8_t second,`
`uint8_t day, uint8_t month, int16_t year);`
+`time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second,`
`uint8_t day, uint8_t month, int16_t year);`
This version takes the various numeric elements as arguments. Note that you can pass the year both as years since 1970 and as full four digit years.
@@ -670,12 +698,12 @@ This version takes the various numeric elements as arguments. Note that you can
### *makeOrdinalTime*
-`time_t time.makeOrdinalTime(uint8_t hour, uint8_t minute, uint8_t second,`
`uint8_t ordinal, uint8_t wday, uint8_t month, int16_t year);`
+`time_t makeOrdinalTime(uint8_t hour, uint8_t minute, uint8_t second,`
`uint8_t ordinal, uint8_t wday, uint8_t month, int16_t year);`
With `makeOrdinalTime` you can get the `time_t` value for a date written as "the second Tuesday in March". The `ordinal` value is 1 for first, 2 for second, 3 for third, 4 for fourth and either 5 or 0 for the last of that weekday in the month. `wday` is weekdays starting with Sunday as 1. You can use the names of ordinals, months and weekdays in all caps as they are compiler defines. So the following would find the `time_t` value for midnight at the start of the first Thursday of the year in variable `year`.
```
-time.makeOrdinalTime(0, 0, 0, FIRST, THURSDAY, JANUARY, year)
+makeOrdinalTime(0, 0, 0, FIRST, THURSDAY, JANUARY, year)
```
> *This is actually a fragment of ezTime's own code, as it can print ISO week numbers and the first ISO week in a year is defined as the week that has the first Thursday in it.*
@@ -684,17 +712,17 @@ time.makeOrdinalTime(0, 0, 0, FIRST, THURSDAY, JANUARY, year)
### *compileTime*
-`time_t time.compileTime(String compile_date = __DATE__, String compile_time = __TIME__);`
+`time_t compileTime(String compile_date = __DATE__, String compile_time = __TIME__);`
### *tzTime*
-`time_t yourTZ.tzTime(TIME)`
+`time_t [tz.]tzTime(TIME)`
This is the internal workhorse function that converts `time_t` in UTC to `time_t` in a timezone or vice versa. It is used by almost all the functions that apply to a timezone, and it takes `TIME` — meaning nothing for "right now", or a `time_t` value and an optional argument to specify whether that is `LOCAL_TIME` or `UTC_TIME`, and then it will convert to the opposite. `TIME_NOW` and `LAST_READ` are always output as `time_t` in that timezone.
-`time_t yourTZ.tzTime(time_t t, ezLocalOrUTC_t local_or_utc, String &tzname, bool &is_dst, int16_t &offset)`
+`time_t [tz.]tzTime(time_t t, ezLocalOrUTC_t local_or_utc, String &tzname, bool &is_dst, int16_t &offset)`
In this second form you have to supply all arguments, and it will fill your `tzname`, `is_dst` and `offset` variables with the appropriate values, the offset is in minutes west of UTC. Note that there are easier functions for you to get this information: `getTimezoneName`, `isDST` and `getOffset` respectively. If your code calls all three in a tight loop you might consider using `tzTime` instead as the other functions each do the whole parsing using `tzTime`, so you would be calling it three times and it does quite a bit.
@@ -704,17 +732,17 @@ In this second form you have to supply all arguments, and it will fill your `tzn
These functions are available for you to use because ezTime needed them internally, so they come at no extra cost, so to speak.
-### *time.urlEncode*
+### *urlEncode*
-`String time.urlEncode(String str);`
+`String urlEncode(String str);`
Does what is says on the package: url-encodes a string.
-### *time.zeropad*
+### *zeropad*
-`String time.zeropad(uint32_t number, uint8_t length);`
+`String zeropad(uint32_t number, uint8_t length);`
Pads `number` with zeroes to the left until the resulting string is `length` places long.
@@ -722,9 +750,9 @@ Pads `number` with zeroes to the left until the resulting string is `length` pla
## Errors and debug information
-### *time.debugLevel*
+### *debugLevel*
-`void time.debugLevel(ezDebugLevel_t level);`
+`void debugLevel(ezDebugLevel_t level);`
Sets the level of detail at which ezTime outputs messages on the serial port. Can be set to one of:
@@ -739,19 +767,19 @@ Sets the level of detail at which ezTime outputs messages on the serial port. Ca
-### *time.error*
+### *error*
-`ezError_t time.error(bool reset = false);`
+`ezError_t error(bool reset = false);`
-A number of functions in ezTime are booleans, meaning they return `true` or `false` as their return value, where `false` means some error occurred. `time.error` will return an `ezError_t` enumeration, something like `NO_NETWORK` (obvious) or `LOCKED_TO_UTC` (when you try to load some new timezone info to the UTC object). You can test for these specific errors and this document will mention which errors might happen in what functions.
+A number of functions in ezTime are booleans, meaning they return `true` or `false` as their return value, where `false` means some error occurred. `error` will return an `ezError_t` enumeration, something like `NO_NETWORK` (obvious) or `LOCKED_TO_UTC` (when you try to load some new timezone info to the UTC object). You can test for these specific errors and this document will mention which errors might happen in what functions.
-When you call `time.error(true)`, it will also reset the error to `OK`, so you can make sure no new errors happened after a certain point.
+When you call `error(true)`, it will also reset the error to `OK`, so you can make sure no new errors happened after a certain point.
-### *time.errorString*
+### *errorString*
-`String time.errorString(ezError_t err = LAST_ERROR);`
+`String errorString(ezError_t err = LAST_ERROR);`
This will give you a string representation of the error specified. The pseudo-error `LAST_ERROR`, which is the default, will give you the textual representation of the last error. This will not reset the last error stored.
@@ -759,7 +787,7 @@ This will give you a string representation of the error specified. The pseudo-er
## Compatibility with Arduino Time library
-The classic Arduino time library has a lot of functions and defines that end up in the root namespace, meaning you can just run `hour()` instead of `YourTZ.hour()`. These functions are supported by ezTime and will act as if they are prefixed with the defaultTZ. This is UTC by default, but you can make any timezone the default by writing `yourTZ.setDefault();`
+The classic Arduino time library has a lot of functions and defines that end up in the root namespace, meaning you can just run `hour()` instead of `someTZ.hour()`. These functions are supported by ezTime and will act as if they are prefixed with the defaultTZ. This is UTC by default, but you can make any timezone the default by writing `someTZ.setDefault();`
If you do not wish to have these functions in your namespace, you can comment out `#define ARDUINO_TIMELIB_COMPATIBILITY` in `ezTime.h`. New code depending on ezTime should probably explicitly state the timezone, especially in code with multiple timezones.
@@ -767,7 +795,7 @@ If you do not wish to have these functions in your namespace, you can comment ou
## Smaller footprint, AVR Arduinos
-This library still compiles on an Arduino Uno with an Ethernet shield. However, it will use up almost all of the flash on that. Which is fine if you were making a date and time display anyway, but if your code is bigger than that, you will want to make it smaller. By uncommenting `#define EZTIME_MAX_DEBUGLEVEL_NONE` in `ezTime.h` you get no debugging information and no textual errors, which saves a couple of kilobytes. If you do not use networking, you should also comment out `#define EZTIME_NETWORK_ENABLE`, that will save a *ton* of space: not just in ezTime but also because the networking library does not get loaded.
+This library compiles on an Arduino Uno with an Ethernet shield. However, it will use up almost all of the flash on that, which is fine if you were making a date and time display anyway. But if your code is bigger than that, you will want to make it smaller. By uncommenting `#define EZTIME_MAX_DEBUGLEVEL_NONE` in `ezTime.h` you get no debugging information and no textual errors, which saves a couple of kilobytes. If you do not use networking, you should also comment out `#define EZTIME_NETWORK_ENABLE`, that will save a *ton* of space: not just in ezTime but also because the networking library does not get loaded.
@@ -775,4 +803,113 @@ This library still compiles on an Arduino Uno with an Ethernet shield. However,
The NTP timestamps used here run until the 7th of February 2036. NTP itself has 128 bits of time precision, I haven't looked into it much. Didn't have to, because just a little later, on the 19th of January 2038, the time_t 32 bit signed integer overflows. This is 20 years from today, in 2018. The Arduino world, if it still exists around then, will have come together around some solution that probably involves 64-bit time like in many operating systems of 2018. If you use this library in your nuclear generating station (**NOOOOO!**), make sure you're not around when these timers wrap around.
-Should you be the one doing maintenance on this is some far-ish future: For ezTime I created another overflowing counter: the cache age for the timezone information is written as a single unsigned byte in months after January 2018, so that could theoretically cause problems in 2039, but I think everything will just roll over and use 2039 as the new anchor date.
\ No newline at end of file
+Should you be the one doing maintenance on this is some far-ish future: For ezTime I created another overflowing counter: the cache age for the timezone information is written as a single unsigned byte in months after January 2018, so that could theoretically cause problems in 2039, but I think everything will just roll over and use 2039 as the new anchor date.
+
+
+
+## ezTime on various Arduino platforms
+
+If your Arduino has anything like normal Arduino networking, we can make it work. In some cases it might take an exception in the code if it needs a special header file or so, but no big deal. And if it has `EEPROM.h` or `Preferences.h` to store things in flash, we can make the cache work too. Please open an issue on [github](htttps://github.com/ropg/ezTime) to tell me if something doesn't work. Here's a list of boards that ezTime has been tested on.
+
+### DSD Tech ESP8266
+
+![](images/ESP8266.jpg)
+
+ezTime 0.7.2 ran fine. Board: Generic ESP8266, Reset Method: nodemcu. Don't forget to replace `#include ` with `#include ` in your sketch.
+
+
+
+### Teensy 3.2
+
+![](images/Teensy-3.2.jpg)
+
+ezTime 0.7.2 ran fine. Did not test networking, so compiled with `#define EZTIME_NETWORK_ENABLE` commented out, used NoNetwork example.
+
+
+
+### Arduino Uno R3 (clone) with Ethernet Shield W5100
+
+![](images/Uno-with-Ethernet.jpg)
+
+ezTime 0.7.2 ran, but the EthernetShield example leaves only 2k of flash:
+
+```
+Sketch uses 30372 bytes (94%) of program storage space. Maximum is 32256 bytes.
+Global variables use 771 bytes (37%) of dynamic memory, leaving 1277 bytes for local variables. Maximum is 2048 bytes.
+```
+
+By setting `#define EZTIME_MAX_DEBUGLEVEL_NONE` in `eztime.h` we free up some memory:
+
+```
+Sketch uses 27170 bytes (84%) of program storage space. Maximum is 32256 bytes.
+Global variables use 761 bytes (37%) of dynamic memory, leaving 1287 bytes for local variables. Maximum is 2048 bytes.
+```
+
+ezTime and NoNetwork example without `#define EZTIME_NETWORK_ENABLE` (if you have another time source):
+
+```
+Sketch uses 11490 bytes (35%) of program storage space. Maximum is 32256 bytes.
+Global variables use 376 bytes (18%) of dynamic memory, leaving 1672 bytes for local variables. Maximum is 2048 bytes.
+```
+
+
+
+### M5Stack (ESP32)
+
+![](images/M5Stack.jpg)
+
+ezTime 0.7.2 ran fine.
+
+
+
+### Arduino Micro
+
+![](images/Arduino-Micro.jpg)
+
+USB took a while to be recognized on my Mac, and then I took a while to discover that this is one that needs the
+
+```
+while (!Serial) { ; } // wait for serial port to connect. Needed for native USB port only
+```
+
+line that you see in many sketches. But then ezTime 0.7.2 ran fine using NoNetwork example.
+
+
+
+### Arduino Due
+
+![](images/Arduino-Due.jpg)
+
+ezTime 0.7.2 runs fine (No networking on board, so tested with NoNetwork example). If you use the native USB port it also needs the
+
+```
+while (!Serial) { ; } // wait for serial port to connect. Needed for native USB port only
+```
+
+and you need to change all the `Serial.` to `SerialUSB.` in your sketch.
+
+
+
+### Arduino MKR1000
+
+![](images/Arduino-MKR-1000.jpg)
+
+ezTime 0.7.2 worked, eventually. But I didn't like this one. Getting online is difficult. Install Wifi101 library from the library manager and make sure to start your sketch with:
+
+```
+#include
+#include
+```
+
+* Test sketch complained about WiFi firmware / driver mismatch. Couldn't get the firmware update tool to work, but WiFi worked anyway.
+* The WiFi object does not have the `isConnected` method so I wrote some detection for ezTime to skip the NO_NETWORK checks. This means that if you have debugLevel at ERROR or higher, waitForSync will throw some NTP TIMEOUT errors (and then continue just fine after wifi is online).
+* It doesn't have `EEPROM.h` or `Preferences.h` but some proprietary `FlashStorage.h`. So no cache for the moment. (Turn off both cache defines at the beginning of `ezTime.h`. I'll write it if the third person wants it.
+
+
+
+### Arduino Nano
+
+![](images/Arduino-Nano.jpg)
+
+ezTime 0.7.2 runs fine (No networking on board, so tested with NoNetwork example)
+
diff --git a/examples/EventsAndOrdinalTime/EventsAndOrdinalTime.ino b/examples/EventsAndOrdinalTime/EventsAndOrdinalTime.ino
index 6fad798..748d17e 100644
--- a/examples/EventsAndOrdinalTime/EventsAndOrdinalTime.ino
+++ b/examples/EventsAndOrdinalTime/EventsAndOrdinalTime.ino
@@ -9,18 +9,19 @@
void setup() {
Serial.begin(115200);
+ while (!Serial) { ; } // wait for Serial port to connect. Needed for native USB port only
WiFi.begin("your-ssid", "your-password");
- time.waitForSync();
+ waitForSync();
// Set the event to trigger for the first time
- UTC.setEvent( itIsTheSecondTuesday, nextSecondTuesday() );
+ setEvent( itIsTheSecondTuesday, nextSecondTuesday() );
}
void loop() {
- time.events();
+ events();
}
@@ -29,7 +30,7 @@ void itIsTheSecondTuesday() {
Serial.println(UTC.dateTime());
// The event then sets a new event for the next time
- UTC.setEvent( itIsTheSecondTuesday, nextSecondTuesday() );
+ setEvent( itIsTheSecondTuesday, nextSecondTuesday() );
}
time_t nextSecondTuesday() {
@@ -40,7 +41,7 @@ time_t nextSecondTuesday() {
while (t <= UTC.now()) {
// Try in current month first, if that has passed, loop once more for next month
- t = time.makeOrdinalTime(12, 0, 0, SECOND, TUESDAY, m, y);
+ t = makeOrdinalTime(12, 0, 0, SECOND, TUESDAY, m, y);
m++;
if (m == 13) {
m = 1;
diff --git a/examples/NoNetwork/NoNetwork.ino b/examples/NoNetwork/NoNetwork.ino
index ce6ba60..3c37a40 100644
--- a/examples/NoNetwork/NoNetwork.ino
+++ b/examples/NoNetwork/NoNetwork.ino
@@ -7,30 +7,34 @@
* offset to UTC.
*
* If you do not want to look up the posix string you can simply provide a name and
- * the current UTC offset, like "PDT+7"
+ * the current UTC offset in hours _west_ of UTC, like "PDT+7"
*/
#include
-#define LOCALTZ_POSIX "PST+8PDT,M3.2.0/2,M11.1.0/2" // US Pacific time
+#define LOCALTZ_POSIX "CET-1CEST,M3.4.0/2,M10.4.0/3" // Time in Berlin
Timezone local;
-Timezone berlin;
+Timezone pacific;
void setup() {
Serial.begin(115200);
+ while (!Serial) { ; } // wait for Serial port to connect. Needed for native USB port only
Serial.println();
local.setPosix(LOCALTZ_POSIX);
- local.setTime(time.compileTime());
- Serial.println("Local time : " + local.dateTime());
+ local.setTime(compileTime());
+ Serial.print(F("Local time : "));
+ Serial.println(local.dateTime());
- berlin.setPosix("CET-1CEST,M3.4.0/2,M10.4.0/3");
- Serial.println("Berlin time : " + berlin.dateTime());
+ pacific.setPosix(F("PST+8PDT,M3.2.0/2,M11.1.0/2"));
+ Serial.print(F("Pacific time : "));
+ Serial.println(pacific.dateTime());
- Serial.println("UTC : " + UTC.dateTime());
+ Serial.print(F("UTC : "));
+ Serial.println(UTC.dateTime());
}
diff --git a/examples/TimeFormats/TimeFormats.ino b/examples/TimeFormats/TimeFormats.ino
index 745f2f0..4259e3d 100644
--- a/examples/TimeFormats/TimeFormats.ino
+++ b/examples/TimeFormats/TimeFormats.ino
@@ -4,29 +4,30 @@
void setup() {
Serial.begin(115200);
+
WiFi.begin("your-ssid", "your-password");
-
- time.waitForSync();
+ while (!Serial) { ; } // wait for Serial port to connect. Needed for native USB port only
+ 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("ATOM: " + dateTime(ATOM));
+ Serial.println("COOKIE: " + dateTime(COOKIE));
+ Serial.println("IS8601: " + dateTime(ISO8601));
+ Serial.println("RFC822: " + dateTime(RFC822));
+ Serial.println("RFC850: " + dateTime(RFC850));
+ Serial.println("RFC1036: " + dateTime(RFC1036));
+ Serial.println("RFC1123: " + dateTime(RFC1123));
+ Serial.println("RFC2822: " + dateTime(RFC2822));
+ Serial.println("RFC3339: " + dateTime(RFC3339));
+ Serial.println("RFC3339_EXT: " + dateTime(RFC3339_EXT));
+ Serial.println("RSS: " + dateTime(RSS));
+ Serial.println("W3C: " + 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") + "\"");
+ Serial.println(" ... and any other format, like \"" + dateTime("l ~t~h~e jS ~o~f F Y, g:i A") + "\"");
}
void loop() {
-
+ events();
}
diff --git a/examples/Timezones/Timezones.ino b/examples/Timezones/Timezones.ino
index e0bf51a..251fff6 100644
--- a/examples/Timezones/Timezones.ino
+++ b/examples/Timezones/Timezones.ino
@@ -4,12 +4,13 @@
void setup() {
Serial.begin(115200);
+ while (!Serial) { ; } // wait for Serial port to connect. Needed for native USB port only
WiFi.begin("your-ssid", "your-password");
// Uncomment the line below to see what it does behind the scenes
// ezTime.debugLevel(INFO);
- time.waitForSync();
+ waitForSync();
Serial.println();
Serial.println("UTC: " + UTC.dateTime());
@@ -24,5 +25,5 @@ void setup() {
}
void loop() {
-
+ events();
}
diff --git a/examples/milliseconds/milliseconds.ino b/examples/milliseconds/milliseconds.ino
index 1890a18..cfac54a 100644
--- a/examples/milliseconds/milliseconds.ino
+++ b/examples/milliseconds/milliseconds.ino
@@ -4,10 +4,11 @@
void setup() {
Serial.begin(115200);
+ while (!Serial) { ; } // wait for Serial port to connect. Needed for native USB port only
WiFi.begin("your-ssid", "your-password");
-
- time.setInterval(60);
- time.waitForSync();
+
+ setInterval(60);
+ waitForSync();
Serial.println();
@@ -26,11 +27,10 @@ void setup() {
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.debugLevel(INFO);
+ debugLevel(INFO);
}
void loop() {
- time.events();
- delay(1000);
+ events();
}
diff --git a/images/Arduino-Due.jpg b/images/Arduino-Due.jpg
new file mode 100644
index 0000000..7002d7d
Binary files /dev/null and b/images/Arduino-Due.jpg differ
diff --git a/images/Arduino-MKR-1000.jpg b/images/Arduino-MKR-1000.jpg
new file mode 100644
index 0000000..f1f2fee
Binary files /dev/null and b/images/Arduino-MKR-1000.jpg differ
diff --git a/images/Arduino-Micro.jpg b/images/Arduino-Micro.jpg
new file mode 100644
index 0000000..0e6822c
Binary files /dev/null and b/images/Arduino-Micro.jpg differ
diff --git a/images/Arduino-Nano.jpg b/images/Arduino-Nano.jpg
new file mode 100644
index 0000000..9de2509
Binary files /dev/null and b/images/Arduino-Nano.jpg differ
diff --git a/images/ESP8266.jpg b/images/ESP8266.jpg
new file mode 100644
index 0000000..6d22f38
Binary files /dev/null and b/images/ESP8266.jpg differ
diff --git a/images/M5Stack.jpg b/images/M5Stack.jpg
new file mode 100644
index 0000000..7ae6945
Binary files /dev/null and b/images/M5Stack.jpg differ
diff --git a/images/Teensy-3.2.jpg b/images/Teensy-3.2.jpg
new file mode 100644
index 0000000..1a5c929
Binary files /dev/null and b/images/Teensy-3.2.jpg differ
diff --git a/images/Uno-with-Ethernet.jpg b/images/Uno-with-Ethernet.jpg
new file mode 100644
index 0000000..64975ed
Binary files /dev/null and b/images/Uno-with-Ethernet.jpg differ
diff --git a/keywords.txt b/keywords.txt
index 3e08c5f..af09d6b 100644
--- a/keywords.txt
+++ b/keywords.txt
@@ -1,6 +1,3 @@
-# ezTime object
-
-time KEYWORD3
error KEYWORD2
errorString KEYWORD2
debugLevel KEYWORD2
@@ -51,6 +48,7 @@ year KEYWORD2
dayOfYear KEYWORD2
weekISO KEYWORD2
yearISO KEYWORD2
+militaryTZ KEYWORD2
setLocation KEYWORD2
setCache KEYWORD2
clearCache KEYWORD2
diff --git a/src/ezTime.cpp b/src/ezTime.cpp
index e4a7d0b..775aa50 100644
--- a/src/ezTime.cpp
+++ b/src/ezTime.cpp
@@ -12,6 +12,10 @@
#if defined(ESP8266)
#include
#include
+ #elif defined(ARDUINO_SAMD_MKR1000)
+ #include
+ #include
+ #include
#elif defined(EZTIME_ETHERNET)
#include
#include
@@ -21,35 +25,92 @@
#endif
#endif
+#if defined(EZTIME_MAX_DEBUGLEVEL_NONE)
+ #define err(args...) ""
+ #define errln(args...) ""
+ #define info(args...) ""
+ #define infoln(args...) ""
+ #define debug(args...) ""
+ #define debugln(args...) ""
+#elif defined(EZTIME_MAX_DEBUGLEVEL_ERROR)
+ #define err(args...) if (_debug_level >= ERROR) Serial.print(args)
+ #define errln(args...) if (_debug_level >= ERROR) Serial.println(args)
+ #define info(args...) ""
+ #define infoln(args...) ""
+ #define debug(args...) ""
+ #define debugln(args...) ""
+#elif defined(EZTIME_MAX_DEBUGLEVEL_INFO)
+ #define err(args...) if (_debug_level >= ERROR) Serial.print(args)
+ #define errln(args...) if (_debug_level >= ERROR) Serial.println(args)
+ #define info(args...) if (_debug_level >= INFO) Serial.print(args)
+ #define infoln(args...) if (_debug_level >= INFO) Serial.println(args)
+ #define debug(args...) ""
+ #define debugln(args...) ""
+#else // nothing specified compiles everything in.
+ #define err(args...) if (_debug_level >= ERROR) Serial.print(args)
+ #define errln(args...) if (_debug_level >= ERROR) Serial.println(args)
+ #define info(args...) if (_debug_level >= INFO) Serial.print(args)
+ #define infoln(args...) if (_debug_level >= INFO) Serial.println(args)
+ #define debug(args...) if (_debug_level >= DEBUG) Serial.print(args)
+ #define debugln(args...) if (_debug_level >= DEBUG) Serial.println(args)
+#endif
+
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
+// The private things go in an anonymous namespace
+namespace {
+
+ ezError_t _last_error = NO_ERROR;
+ ezDebugLevel_t _debug_level = NONE;
+ ezEvent_t _events[MAX_EVENTS];
+ time_t _last_sync_time = 0;
+ time_t _last_read_t = 0;
+ uint32_t _last_sync_millis = 0;
+ uint16_t _last_read_ms;
+ timeStatus_t _time_status;
+ bool _initialised = false;
+ #ifdef EZTIME_NETWORK_ENABLE
+ bool _ntp_enabled = true;
+ uint16_t _ntp_interval = NTP_INTERVAL;
+ String _ntp_server = NTP_SERVER;
+ #endif
-ezError_t EZtime::_last_error = NO_ERROR;
-ezDebugLevel_t EZtime::_debug_level = NONE;
-time_t EZtime::_last_sync_time = 0;
-time_t EZtime::_last_read_t = 0;
-uint32_t EZtime::_last_sync_millis = 0;
-uint16_t EZtime::_last_read_ms;
-timeStatus_t EZtime::_time_status = timeNotSet;
-ezEvent_t EZtime::_events[MAX_EVENTS];
-bool EZtime::_initialised = false;
+ void error(ezError_t err) {
+ _last_error = err;
+ if (_last_error) {
+ err(F("ERROR: "));
+ errln(errorString(err));
+ }
+ }
-#ifdef EZTIME_NETWORK_ENABLE
- bool EZtime::_ntp_enabled = true;
- uint16_t EZtime::_ntp_interval = NTP_INTERVAL;
- String EZtime::_ntp_server = NTP_SERVER;
-#endif
+ String debugLevelString(ezDebugLevel_t level) {
+ switch (level) {
+ case NONE: return F("NONE");
+ case ERROR: return F("ERROR");
+ case INFO: return F("INFO");
+ default: return F("DEBUG");
+ }
+ }
+ time_t nowUTC(bool update_last_read = true) {
+ time_t t;
+ uint32_t m = millis();
+ t = _last_sync_time + ((m - _last_sync_millis) / 1000);
+ if (update_last_read) {
+ _last_read_t = t;
+ _last_read_ms = (m - _last_sync_millis) % 1000;
+ }
+ return t;
+ }
-EZtime::EZtime() {
- for (uint8_t n = 0; n < MAX_EVENTS; n++) EZtime::_events[n] = { 0, NULL };
}
+
////////// Error handing
-String EZtime::errorString(ezError_t err /* = LAST_ERROR */) {
+String errorString(ezError_t err /* = LAST_ERROR */) {
switch (err) {
case NO_ERROR: return F("OK");
case LAST_ERROR: return errorString(_last_error);
@@ -65,30 +126,15 @@ String EZtime::errorString(ezError_t err /* = LAST_ERROR */) {
}
}
-String EZtime::debugLevelString(ezDebugLevel_t level) {
- switch (level) {
- case NONE: return F("NONE");
- case ERROR: return F("ERROR");
- case INFO: return F("INFO");
- case DEBUG: return F("DEBUG");
- }
-}
-ezError_t EZtime::error(bool reset /* = false */) {
+
+ezError_t error(bool reset /* = false */) {
ezError_t tmp = _last_error;
if (reset) _last_error = NO_ERROR;
return tmp;
}
-void EZtime::error(ezError_t err) {
- _last_error = err;
- if (_last_error) {
- err(F("ERROR: "));
- errln(errorString(err));
- }
-}
-
-void EZtime::debugLevel(ezDebugLevel_t level) {
+void debugLevel(ezDebugLevel_t level) {
_debug_level = level;
info(F("\r\nezTime debug level set to "));
infoln(debugLevelString(level));
@@ -96,7 +142,7 @@ void EZtime::debugLevel(ezDebugLevel_t level) {
////////////////////////
-String EZtime::monthString(uint8_t month) {
+String monthString(uint8_t month) {
switch(month) {
case 1: return F("January");
case 2: return F("February");
@@ -114,7 +160,7 @@ String EZtime::monthString(uint8_t month) {
return "";
}
-String EZtime::dayString(uint8_t day) {
+String dayString(uint8_t day) {
switch(day) {
case 1: return F("Sunday");
case 2: return F("Monday");
@@ -128,30 +174,19 @@ String EZtime::dayString(uint8_t day) {
}
-timeStatus_t EZtime::timeStatus() { return _time_status; }
+timeStatus_t timeStatus() { return _time_status; }
-time_t EZtime::now(bool update_last_read /* = true */) {
- time_t t;
- uint32_t m = millis();
- t = _last_sync_time + ((m - _last_sync_millis) / 1000);
- if (update_last_read) {
- _last_read_t = t;
- _last_read_ms = (m - _last_sync_millis) % 1000;
+void events() {
+ if (!_initialised) {
+ for (uint8_t n = 0; n < MAX_EVENTS; n++) _events[n] = { 0, NULL };
+ #ifdef EZTIME_NETWORK_ENABLE
+ updateNTP(); // Start the cycle of updateNTP running and then setting an event for its next run
+ #endif
+ _initialised = true;
}
- return t;
-}
-
-void EZtime::events() {
- #ifdef EZTIME_NETWORK_ENABLE
- if (!_initialised) {
- // Start the cycle of updateNTP running and then setting an event for its next run
- updateNTP();
- _initialised = true;
- }
- #endif
// See if any events are due
for (uint8_t n = 0; n < MAX_EVENTS; n++) {
- if (_events[n].function && now() >= _events[n].time) {
+ if (_events[n].function && nowUTC() >= _events[n].time) {
debug(F("Running event (#")); debug(n + 1); debug(F(") set for ")); debugln(UTC.dateTime(_events[n].time));
void (*tmp)() = _events[n].function;
_events[n] = { 0, NULL }; // reset the event
@@ -160,14 +195,14 @@ void EZtime::events() {
}
}
-void EZtime::deleteEvent(uint8_t event_handle) {
+void deleteEvent(uint8_t event_handle) {
if (event_handle && event_handle <= MAX_EVENTS) {
debug(F("Deleted event (#")); debug(event_handle); debug(F("), set for ")); debugln(UTC.dateTime(_events[event_handle - 1].time));
_events[event_handle - 1] = { 0, NULL };
}
}
-void EZtime::deleteEvent(void (*function)()) {
+void deleteEvent(void (*function)()) {
for (uint8_t n = 0; n< MAX_EVENTS; n++) {
if (_events[n].function == function) {
debug(F("Deleted event (#")); debug(n + 1); debug(F("), set for ")); debugln(UTC.dateTime(_events[n].time));
@@ -176,7 +211,7 @@ void EZtime::deleteEvent(void (*function)()) {
}
}
-void EZtime::breakTime(time_t timeInput, tmElements_t &tm){
+void breakTime(time_t timeInput, tmElements_t &tm){
// break the given time_t into time components
// this is a more compact version of the C library localtime function
// note that year is offset from 1970 !!!
@@ -229,7 +264,7 @@ void EZtime::breakTime(time_t timeInput, tmElements_t &tm){
tm.Day = time + 1; // day of month
}
-time_t EZtime::makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, uint16_t year) {
+time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, uint16_t year) {
tmElements_t tm;
tm.Hour = hour;
tm.Minute = minute;
@@ -244,7 +279,7 @@ time_t EZtime::makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t da
return makeTime(tm);
}
-time_t EZtime::makeTime(tmElements_t &tm){
+time_t makeTime(tmElements_t &tm){
// assemble time elements into time_t
// note year argument is offset from 1970 (see macros in time.h to convert to other formats)
// previous version used full four digit year (or digits since 2000),i.e. 2009 was 2009 or 9
@@ -281,7 +316,7 @@ time_t EZtime::makeTime(tmElements_t &tm){
// makeOrdinalTime allows you to resolve "second thursday in September in 2018" into a number of seconds since 1970
// (Very useful for the timezone calculations that ezTime does internally)
// If ordinal is 0 or 5 it is taken to mean "the last $wday in $month"
-time_t EZtime::makeOrdinalTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t ordinal, uint8_t wday, uint8_t month, uint16_t year) {
+time_t makeOrdinalTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t ordinal, uint8_t wday, uint8_t month, uint16_t year) {
if (year <= 68 ) year = 1970 + year; // fix user intent
if (ordinal == 5) ordinal = 0;
uint8_t m = month;
@@ -301,13 +336,12 @@ time_t EZtime::makeOrdinalTime(uint8_t hour, uint8_t minute, uint8_t second, uin
return t;
}
-String EZtime::urlEncode(String str) {
+String urlEncode(String str) {
String encodedString="";
char c;
char code0;
char code1;
- char code2;
- for (int i = 0; i < str.length(); i++) {
+ for (uint16_t i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c == ' ') {
encodedString += '+';
@@ -331,7 +365,7 @@ String EZtime::urlEncode(String str) {
return encodedString;
}
-String EZtime::zeropad(uint32_t number, uint8_t length) {
+String zeropad(uint32_t number, uint8_t length) {
String out;
out.reserve(length);
out = String(number);
@@ -339,7 +373,7 @@ String EZtime::zeropad(uint32_t number, uint8_t length) {
return out;
}
-time_t EZtime::compileTime(String compile_date /* = __DATE__ */, String compile_time /* = __TIME__ */) {
+time_t compileTime(String compile_date /* = __DATE__ */, String compile_time /* = __TIME__ */) {
uint8_t hrs = compile_time.substring(0,2).toInt();
uint8_t min = compile_time.substring(3,5).toInt();
@@ -356,14 +390,14 @@ time_t EZtime::compileTime(String compile_date /* = __DATE__ */, String compile_
return 0;
}
-bool EZtime::secondChanged() {
- time_t t = now(false);
+bool secondChanged() {
+ time_t t = nowUTC(false);
if (_last_read_t != t) return true;
return false;
}
-bool EZtime::minuteChanged() {
- time_t t = now(false);
+bool minuteChanged() {
+ time_t t = nowUTC(false);
if (_last_read_t / 60 != t / 60) return true;
return false;
}
@@ -371,8 +405,8 @@ bool EZtime::minuteChanged() {
#ifdef EZTIME_NETWORK_ENABLE
- void EZtime::updateNTP() {
- time.deleteEvent(updateNTP); // Delete any events pointing here, in case called manually
+ void updateNTP() {
+ deleteEvent(updateNTP); // Delete any events pointing here, in case called manually
time_t t;
unsigned long measured_at;
if (queryNTP(_ntp_server, t, measured_at)) {
@@ -400,20 +434,22 @@ bool EZtime::minuteChanged() {
if (_ntp_interval) UTC.setEvent(updateNTP, t + _ntp_interval);
_time_status = timeSet;
} else {
- UTC.setEvent(updateNTP, now() + NTP_RETRY);
+ UTC.setEvent(updateNTP, nowUTC() + NTP_RETRY);
}
}
// This is a nice self-contained NTP routine if you need one: feel free to use it.
// It gives you the seconds since 1970 (unix epoch) and the millis() on your system when
// that happened (by deducting fractional seconds and estimated network latency).
- bool EZtime::queryNTP(String server, time_t &t, unsigned long &measured_at) {
+ bool queryNTP(String server, time_t &t, unsigned long &measured_at) {
info(F("Querying "));
info(server);
info(F(" ... "));
#ifndef EZTIME_ETHERNET
- if (!WiFi.isConnected()) { error(NO_NETWORK); return false; }
+ #ifndef ARDUINO_SAMD_MKR1000
+ if (!WiFi.isConnected()) { error(NO_NETWORK); return false; }
+ #endif
WiFiUDP udp;
#else
EthernetUDP udp;
@@ -467,19 +503,19 @@ bool EZtime::minuteChanged() {
return true;
}
- void EZtime::setInterval(uint16_t seconds /* = 0 */) {
+ void setInterval(uint16_t seconds /* = 0 */) {
deleteEvent(updateNTP);
_ntp_interval = seconds;
- if (seconds) UTC.setEvent(updateNTP, now() + _ntp_interval);
+ if (seconds) UTC.setEvent(updateNTP, nowUTC() + _ntp_interval);
}
- void EZtime::setServer(String ntp_server /* = NTP_SERVER */) { _ntp_server = ntp_server; }
+ void setServer(String ntp_server /* = NTP_SERVER */) { _ntp_server = ntp_server; }
- bool EZtime::waitForSync(uint16_t timeout /* = 0 */) {
+ bool waitForSync(uint16_t timeout /* = 0 */) {
unsigned long start = millis();
- #ifndef EZTIME_ETHERNET
+ #if !defined(EZTIME_ETHERNET) && !defined(ARDUINO_SAMD_MKR1000)
if (!WiFi.isConnected()) {
info(F("Waiting for WiFi ... "));
while (!WiFi.isConnected()) {
@@ -505,12 +541,6 @@ bool EZtime::minuteChanged() {
#endif // EZTIME_NETWORK_ENABLE
-EZtime time;
-
-
-
-
-
//
// Timezone class
@@ -519,24 +549,33 @@ EZtime time;
Timezone::Timezone(bool locked_to_UTC /* = false */) {
_locked_to_UTC = locked_to_UTC;
_posix = "UTC";
- #ifdef EZTIME_CACHE_EEPROM
- _cache_month = 0;
- _eeprom_address = -1;
- #endif
- #ifdef EZTIME_CACHE_NVS
- _cache_month = 0;
- _nvs_name = "";
- _nvs_key = "";
+ #ifdef EZTIME_NETWORK_ENABLE
+ #ifdef EZTIME_CACHE_EEPROM
+ _cache_month = 0;
+ _eeprom_address = -1;
+ #endif
+ #ifdef EZTIME_CACHE_NVS
+ _cache_month = 0;
+ _nvs_name = "";
+ _nvs_key = "";
+ #endif
+ _olsen = "";
#endif
}
bool Timezone::setPosix(String posix) {
- if (_locked_to_UTC) { time.error(LOCKED_TO_UTC); return false; }
+ if (_locked_to_UTC) { error(LOCKED_TO_UTC); return false; }
_posix = posix;
- _olsen = "";
+ #ifdef EZTIME_NETWORK_ENABLE
+ _olsen = "";
+ #endif
+ return true;
}
-time_t Timezone::tzTime(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME) {
+time_t Timezone::now() { return tzTime(); }
+
+time_t Timezone::tzTime(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
+ if (_locked_to_UTC) return nowUTC(); // just saving some time and memory
String tzname;
bool is_dst;
int16_t offset;
@@ -546,10 +585,10 @@ time_t Timezone::tzTime(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL
time_t Timezone::tzTime(time_t t, ezLocalOrUTC_t local_or_utc, String &tzname, bool &is_dst, int16_t &offset) {
if (t == TIME_NOW) {
- t = time.now();
+ t = nowUTC();
local_or_utc = UTC_TIME;
} else if (t == LAST_READ) {
- t = time._last_read_t;
+ t = _last_read_t;
local_or_utc = UTC_TIME;
}
@@ -712,11 +751,11 @@ time_t Timezone::tzTime(time_t t, ezLocalOrUTC_t local_or_utc, String &tzname, b
int16_t dst_offset = std_offset - dst_shift_hr * 60 - dst_shift_min;
// to find the year
tmElements_t tm;
- time.breakTime(t, tm);
+ breakTime(t, tm);
// in local time
- time_t dst_start = time.makeOrdinalTime(start_time_hr, start_time_min, 0, start_week, start_dow, start_month, tm.Year + 1970);
- time_t dst_end = time.makeOrdinalTime(end_time_hr, end_time_min, 0, end_week, end_dow, end_month, tm.Year + 1970);
+ time_t dst_start = makeOrdinalTime(start_time_hr, start_time_min, 0, start_week, start_dow, start_month, tm.Year + 1970);
+ time_t dst_end = makeOrdinalTime(end_time_hr, end_time_min, 0, end_week, end_dow, end_month, tm.Year + 1970);
if (local_or_utc == UTC_TIME) {
dst_start -= std_offset;
@@ -751,17 +790,17 @@ String Timezone::getPosix() { return _posix; }
info(F("Timezone lookup for: "));
infoln(location);
- if (_locked_to_UTC) { time.error(LOCKED_TO_UTC); return false; }
+ if (_locked_to_UTC) { error(LOCKED_TO_UTC); return false; }
- #ifndef EZTIME_ETHERNET
- if (!WiFi.isConnected()) { time.error(NO_NETWORK); return false; }
+ #if !defined(EZTIME_ETHERNET) && !defined(ARDUINO_SAMD_MKR1000)
+ if (!WiFi.isConnected()) { error(NO_NETWORK); return false; }
#endif
String path;
if (location.indexOf("/") != -1) {
- path = F("/api/timezone/?"); path += time.urlEncode(location);
+ path = F("/api/timezone/?"); path += urlEncode(location);
} else if (location != "") {
- path = F("/api/address/?"); path += time.urlEncode(location);
+ path = F("/api/address/?"); path += urlEncode(location);
} else {
path = F("/api/ip");
}
@@ -772,7 +811,7 @@ String Timezone::getPosix() { return _posix; }
EthernetClient client;
#endif
- if (!client.connect("timezoneapi.io", 80)) { time.error(CONNECT_FAILED); return false; }
+ if (!client.connect("timezoneapi.io", 80)) { error(CONNECT_FAILED); return false; }
client.print(F("GET "));
client.print(path);
@@ -818,45 +857,65 @@ String Timezone::getPosix() { return _posix; }
}
}
debugln(F("\r\n\r\n"));
- if (search_state != 4 || tzinfo == "") { time.error(DATA_NOT_FOUND); return false; }
+ if (search_state != 4 || tzinfo == "") { error(DATA_NOT_FOUND); return false; }
infoln(F("success."));
info(F("Found: ")); infoln(tzinfo);
+ _olsen = tzinfo.substring(0, tzinfo.indexOf(' '));
+ _posix = tzinfo.substring(tzinfo.indexOf(' ') + 1);
+
#if defined(EZTIME_CACHE_EEPROM) || defined(EZTIME_CACHE_NVS)
- writeCache(tzinfo);
+ writeCache(tzinfo); // caution, byref to save memory, tzinfo mangled afterwards
#endif
- _posix = tzinfo.substring(tzinfo.indexOf(' ') + 1);
return true;
}
-
- #ifdef EZTIME_CACHE_EEPROM
- bool Timezone::setCache(const int16_t address) {
- if (address + EEPROM_CACHE_LEN > EEPROM.length()) { time.error(CACHE_TOO_SMALL); return false; }
- _eeprom_address = address;
- return setCache();
- }
- #endif
- #ifdef EZTIME_CACHE_NVS
- bool Timezone::setCache(const String name, const String key) {
- _nvs_name = name;
- _nvs_key = key;
- return setCache();
- }
- #endif
+ String Timezone::getOlsen() {
+ return _olsen;
+ }
+
#if defined(EZTIME_CACHE_EEPROM) || defined(EZTIME_CACHE_NVS)
+
+ #if defined(ESP32) || defined(ESP8266)
+ #define eepromBegin() EEPROM.begin(4096)
+ #define eepromEnd() EEPROM.end()
+ #define eepromLength() (4096)
+ #else
+ #define eepromBegin() ""
+ #define eepromEnd() ""
+ #define eepromLength() EEPROM.length()
+ #endif
+
+ #ifdef EZTIME_CACHE_EEPROM
+ bool Timezone::setCache(const int16_t address) {
+ eepromBegin();
+ if (address + EEPROM_CACHE_LEN > eepromLength()) { error(CACHE_TOO_SMALL); return false; }
+ _eeprom_address = address;
+ eepromEnd();
+ return setCache();
+ }
+ #endif
+
+ #ifdef EZTIME_CACHE_NVS
+ bool Timezone::setCache(const String name, const String key) {
+ _nvs_name = name;
+ _nvs_key = key;
+ return setCache();
+ }
+ #endif
bool Timezone::setCache() {
String olsen, posix;
uint8_t months_since_jan_2018;
if (readCache(olsen, posix, months_since_jan_2018)) {
- _posix = posix;
+ setPosix(posix);
_olsen = olsen;
_cache_month = months_since_jan_2018;
if ( (year() - 2018) * 12 + month(LAST_READ) - months_since_jan_2018 > MAX_CACHE_AGE_MONTHS) {
+ infoln(F("Cache stale, getting fresh"));
setLocation(olsen);
}
return true;
@@ -867,39 +926,40 @@ String Timezone::getPosix() { return _posix; }
void Timezone::clearCache(bool delete_section /* = false */) {
#ifdef EZTIME_CACHE_EEPROM
- if (_eeprom_address < 0) { time.error(NO_CACHE_SET); return; }
+ eepromBegin();
+ if (_eeprom_address < 0) { error(NO_CACHE_SET); return; }
for (int16_t n = _eeprom_address; n < _eeprom_address + EEPROM_CACHE_LEN; n++) EEPROM.write(n, 0);
+ eepromEnd();
#endif
#ifdef EZTIME_CACHE_NVS
- if (_nvs_name = "" || _nvs_key = "") { time.error(NO_CACHE_SET); return; }
+ if (_nvs_name == "" || _nvs_key == "") { error(NO_CACHE_SET); return; }
Preferences prefs;
- prefs.begin(_nvs_name, false);
+ prefs.begin(_nvs_name.c_str(), false);
if (delete_section) {
prefs.clear();
} else {
- prefs.remove(_nvs_key);
+ prefs.remove(_nvs_key.c_str());
}
prefs.end();
#endif
}
- String Timezone::getOlsen() {
- return _olsen;
- }
-
- bool Timezone::writeCache(const String &str) {
+ bool Timezone::writeCache(String &str) {
uint8_t months_since_jan_2018 = 0;
if (year() >= 2018) months_since_jan_2018 = (year(LAST_READ) - 2018) * 12 + month(LAST_READ) - 1;
#ifdef EZTIME_CACHE_EEPROM
if (_eeprom_address < 0) return false;
+
info(F("Caching timezone data "));
- if (str.length() > MAX_CACHE_PAYLOAD) { time.error(CACHE_TOO_SMALL); return false; }
+ if (str.length() > MAX_CACHE_PAYLOAD) { error(CACHE_TOO_SMALL); return false; }
uint16_t last_byte = _eeprom_address + EEPROM_CACHE_LEN - 1;
uint16_t addr = _eeprom_address;
+ eepromBegin();
+
// First byte is cache age, in months since 2018
EEPROM.write(addr++, months_since_jan_2018);
@@ -943,16 +1003,18 @@ String Timezone::getPosix() { return _posix; }
for (uint16_t n = _eeprom_address; n < last_byte; n++) checksum += EEPROM.read(n);
checksum += 42;
EEPROM.write(last_byte, checksum);
+ eepromEnd();
infoln();
return true;
#endif
#ifdef EZTIME_CACHE_NVS
- if (_nvs_name = "" || _nvs_key = "") return false;
+ if (_nvs_name == "" || _nvs_key == "") return false;
infoln(F("Caching timezone data"));
Preferences prefs;
- prefs.begin(_nvs_name, false);
- prefs.putString(_nvs_key, String(months_since_jan_2018) + " " + str);
+ prefs.begin(_nvs_name.c_str(), false);
+ String tmp = String(months_since_jan_2018) + " " + str;
+ prefs.putString(_nvs_key.c_str(), tmp);
prefs.end();
return true;
#endif
@@ -962,8 +1024,8 @@ String Timezone::getPosix() { return _posix; }
bool Timezone::readCache(String &olsen, String &posix, uint8_t &months_since_jan_2018) {
#ifdef EZTIME_CACHE_EEPROM
- if (_eeprom_address < 0) { time.error(NO_CACHE_SET); return false; }
-
+ if (_eeprom_address < 0) { error(NO_CACHE_SET); return false; }
+ eepromBegin();
uint16_t last_byte = _eeprom_address + EEPROM_CACHE_LEN - 1;
for (uint16_t n = _eeprom_address; n <= last_byte; n++) {
@@ -976,13 +1038,13 @@ String Timezone::getPosix() { return _posix; }
uint8_t checksum = 0;
for (uint16_t n = _eeprom_address; n < last_byte; n++) checksum += EEPROM.read(n);
checksum += 42;
- if (checksum != EEPROM.read(last_byte)) return false;
+ if (checksum != EEPROM.read(last_byte)) { eepromEnd(); return false; }
debugln(F("Checksum OK"));
// Return false if length impossible
uint8_t len = EEPROM.read(_eeprom_address + 1);
debug("Length: "); debugln(len);
- if (len > MAX_CACHE_PAYLOAD) return false;
+ if (len > MAX_CACHE_PAYLOAD) { eepromEnd(); return false; }
// OK, we're gonna decompress
olsen.reserve(len + 3); // Everything goes in olsen first. Decompression might overshoot 3
@@ -1022,25 +1084,30 @@ String Timezone::getPosix() { return _posix; }
}
}
info(F("Cache read. Olsen: ")); info(olsen); info (F(" Posix: ")); infoln(posix);
+ eepromEnd();
return true;
#endif
#ifdef EZTIME_CACHE_NVS
- if (_nvs_name = "" || _nvs_key = "") { time.error(NO_CACHE_SET); return; }
+ if (_nvs_name == "" || _nvs_key == "") { error(NO_CACHE_SET); return false; }
Preferences prefs;
- prefs.begin(_nvs_name, true);
- String olsen = prefs.getString(_nvs_key);
+ prefs.begin(_nvs_name.c_str(), true);
+ String read_string = prefs.getString(_nvs_key.c_str());
+ read_string.trim();
prefs.end();
- if (!olsen) return false;
+ if (read_string == "") return false;
- uint8_t first_space = olsen.indexOf(' ');
- uint8_t second_space = olsen.indexOf(' ', first_space + 1);
- months_since_jan_2018 = olsen.toInt();
- posix = olsen.substring(second_space + 1);
- olsen = substring(olsen, first_space + 1, second_space);
- info(F("Cache read. Olsen: ")); info(olsen); info (F(" Posix: ")); infoln(posix);
- return true;
+ uint8_t first_space = read_string.indexOf(' ');
+ uint8_t second_space = read_string.indexOf(' ', first_space + 1);
+ if (first_space && second_space) {
+ months_since_jan_2018 = read_string.toInt();
+ posix = read_string.substring(second_space + 1);
+ olsen = read_string.substring(first_space + 1, second_space);
+ info(F("Cache read. Olsen: ")); info(olsen); info (F(" Posix: ")); infoln(posix);
+ return true;
+ }
+ return false;
#endif
}
@@ -1052,6 +1119,7 @@ String Timezone::getPosix() { return _posix; }
void Timezone::setDefault() {
defaultTZ = this;
+ debug(F("Default timezone set to ")); debug(_olsen); debug(F(" "));debugln(_posix);
}
bool Timezone::isDST(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
@@ -1079,30 +1147,30 @@ int16_t Timezone::getOffset(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_ut
}
uint8_t Timezone::setEvent(void (*function)(), const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr) {
- time_t t = time.makeTime(hr, min, sec, day, mnth, yr);
+ time_t t = makeTime(hr, min, sec, day, mnth, yr);
return setEvent(function, t);
}
-uint8_t Timezone::setEvent(void (*function)(), time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME) {
+uint8_t Timezone::setEvent(void (*function)(), time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
t = tzTime(t, local_or_utc);
for (uint8_t n = 0; n < MAX_EVENTS; n++) {
- if (!time._events[n].function) {
- time._events[n].function = function;
- time._events[n].time = t;
+ if (!_events[n].function) {
+ _events[n].function = function;
+ _events[n].time = t;
debug(F("Set event (#")); debug(n + 1); debug(F(") to trigger on: ")); debugln(UTC.dateTime(t));
return n + 1;
}
}
- time.error(TOO_MANY_EVENTS);
+ error(TOO_MANY_EVENTS);
return 0;
}
void Timezone::setTime(time_t t, uint16_t ms /* = 0 */) {
int16_t offset;
offset = getOffset(t);
- time._last_sync_time = t + offset * 60;
- time._last_sync_millis = millis() - ms;
- time._time_status = timeSet;
+ _last_sync_time = t + offset * 60;
+ _last_sync_millis = millis() - ms;
+ _time_status = timeSet;
}
void Timezone::setTime(const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr) {
@@ -1120,7 +1188,7 @@ void Timezone::setTime(const uint8_t hr, const uint8_t min, const uint8_t sec, c
tm.Hour = hr;
tm.Minute = min;
tm.Second = sec;
- setTime(time.makeTime(tm));
+ setTime(makeTime(tm));
}
String Timezone::dateTime(String format /* = DEFAULT_TIMEFORMAT */) {
@@ -1143,7 +1211,7 @@ String Timezone::dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format /
String out = "";
tmElements_t tm;
- time.breakTime(t, tm);
+ breakTime(t, tm);
int8_t hour12 = tm.Hour % 12;
if (hour12 == 0) hour12 = 12;
@@ -1152,7 +1220,7 @@ String Timezone::dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format /
bool escape_char = false;
- for (int8_t n = 0; n < format.length(); n++) {
+ for (uint8_t n = 0; n < format.length(); n++) {
char c = (char) format.c_str()[n];
@@ -1168,16 +1236,16 @@ String Timezone::dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format /
escape_char = true;
break;
case 'd': // Day of the month, 2 digits with leading zeros
- out += time.zeropad(tm.Day, 2);
+ out += zeropad(tm.Day, 2);
break;
case 'D': // A textual representation of a day, three letters
- out += time.dayString(tm.Wday).substring(0,3);
+ out += dayString(tm.Wday).substring(0,3);
break;
case 'j': // Day of the month without leading zeros
out += String(tm.Day);
break;
case 'l': // (lowercase L) A full textual representation of the day of the week
- out += time.dayString(tm.Wday);
+ out += dayString(tm.Wday);
break;
case 'N': // ISO-8601 numeric representation of the day of the week. ( 1 = Monday, 7 = Sunday )
tmpint8 = tm.Wday - 1;
@@ -1204,13 +1272,13 @@ String Timezone::dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format /
out += String(tm.Wday);
break;
case 'F': // A full textual representation of a month, such as January or March
- out += time.monthString(tm.Month);
+ out += monthString(tm.Month);
break;
case 'm': // Numeric representation of a month, with leading zeros
- out += time.zeropad(tm.Month, 2);
+ out += zeropad(tm.Month, 2);
break;
case 'M': // A short textual representation of a month, three letters
- out += time.monthString(tm.Month).substring(0,3);
+ out += monthString(tm.Month).substring(0,3);
break;
case 'n': // Numeric representation of a month, without leading zeros
out += String(tm.Month);
@@ -1222,7 +1290,7 @@ String Timezone::dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format /
out += String(tm.Year + 1970);
break;
case 'y': // A two digit representation of a year
- out += time.zeropad((tm.Year + 1970) % 100, 2);
+ out += zeropad((tm.Year + 1970) % 100, 2);
break;
case 'a': // am or pm
out += (tm.Hour < 12) ? F("am") : F("pm");
@@ -1237,34 +1305,36 @@ String Timezone::dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format /
out += String(tm.Hour);
break;
case 'h': // 12-hour format of an hour with leading zeros
- out += time.zeropad(hour12, 2);
+ out += zeropad(hour12, 2);
break;
case 'H': // 24-hour format of an hour with leading zeros
- out += time.zeropad(tm.Hour, 2);
+ out += zeropad(tm.Hour, 2);
break;
case 'i': // Minutes with leading zeros
- out += time.zeropad(tm.Minute, 2);
+ out += zeropad(tm.Minute, 2);
break;
case 's': // Seconds with leading zeros
- out += time.zeropad(tm.Second, 2);
+ out += zeropad(tm.Second, 2);
break;
case 'T': // abbreviation for timezone
out += tzname;
break;
case 'v': // milliseconds as three digits
- out += time.zeropad(time._last_read_ms, 3);
+ out += zeropad(_last_read_ms, 3);
break;
- case 'e': // Timezone identifier (Olsen)
- out += getOlsen();
- break;
+ #ifdef EZTIME_NETWORK_ENABLE
+ case 'e': // Timezone identifier (Olsen)
+ out += getOlsen();
+ break;
+ #endif
case 'O': // Difference to Greenwich time (GMT) in hours and minutes written together (+0200)
case 'P': // Difference to Greenwich time (GMT) in hours and minutes written with colon (+02:00)
o = offset;
out += (o < 0) ? "+" : "-"; // reversed from our offset
if (o < 0) o = 0 - o;
- out += time.zeropad(o / 60, 2);
+ out += zeropad(o / 60, 2);
out += (c == 'P') ? ":" : "";
- out += time.zeropad(o % 60, 2);
+ out += zeropad(o % 60, 2);
break;
case 'Z': //Timezone offset in seconds. West of UTC is negative, east of UTC is positive.
out += String(0 - offset * 60);
@@ -1273,12 +1343,14 @@ String Timezone::dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format /
out += String(dayOfYear(t)); // The day of the year (starting from 0)
break;
case 'W':
- out += time.zeropad(weekISO(t), 2); // ISO-8601 week number of year, weeks starting on Monday
+ out += zeropad(weekISO(t), 2); // ISO-8601 week number of year, weeks starting on Monday
break;
case 'X':
out += String(yearISO(t)); // ISO-8601 year-week notation year, see https://en.wikipedia.org/wiki/ISO_week_date
break;
-
+ case 'B':
+ out += militaryTZ(t);
+ break;
default:
out += String(c);
@@ -1289,6 +1361,18 @@ String Timezone::dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format /
return out;
}
+String Timezone::militaryTZ(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
+ t = tzTime(t, local_or_utc);
+ int16_t o = getOffset(t);
+ if (o % 60) return "?"; // If it's not a whole hour from UTC, it's not a timezone with a military letter code
+ o = o / 60;
+ if (o > 0) return String((char)('M' + o));
+ if (o < 0 && o >= -9) return String((char)('A' - o - 1)); // Minus a negative number == plus 1
+ if (o < -9) return String((char)('A' - o)); // Crazy, they're skipping 'J'
+ return "Z";
+}
+
+
uint8_t Timezone::hour(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
t = tzTime(t, local_or_utc);
return t / 3600 % 24;
@@ -1304,44 +1388,44 @@ uint8_t Timezone::second(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /
return t % 60;
}
-uint16_t Timezone::ms(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
+uint16_t Timezone::ms(time_t t /*= TIME_NOW */) {
// Note that here passing anything but TIME_NOW or LAST_READ is pointless
- if (t == TIME_NOW) { time.now(); return time._last_read_ms; }
- if (t == LAST_READ) return time._last_read_ms;
+ if (t == TIME_NOW) { nowUTC(); return _last_read_ms; }
+ if (t == LAST_READ) return _last_read_ms;
return 0;
}
uint8_t Timezone::day(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
t = tzTime(t, local_or_utc);
tmElements_t tm;
- time.breakTime(t, tm);
+ breakTime(t, tm);
return tm.Day;
}
uint8_t Timezone::weekday(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
t = tzTime(t, local_or_utc);
tmElements_t tm;
- time.breakTime(t, tm);
+ breakTime(t, tm);
return tm.Wday;
}
uint8_t Timezone::month(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
t = tzTime(t, local_or_utc);
tmElements_t tm;
- time.breakTime(t, tm);
+ breakTime(t, tm);
return tm.Month;
}
uint16_t Timezone::year(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
t = tzTime(t, local_or_utc);
tmElements_t tm;
- time.breakTime(t, tm);
+ breakTime(t, tm);
return tm.Year + 1970;
}
uint16_t Timezone::dayOfYear(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
t = tzTime(t, local_or_utc);
- time_t jan_1st = time.makeTime(0, 0, 0, 1, 1, year(t));
+ time_t jan_1st = makeTime(0, 0, 0, 1, 1, year(t));
return (t - jan_1st) / SECS_PER_DAY;
}
@@ -1351,7 +1435,7 @@ uint16_t Timezone::dayOfYear(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_u
// definition for week 01 is the week with the Gregorian year's first Thursday in it.
// See https://en.wikipedia.org/wiki/ISO_week_date
//
-#define startISOyear(year...) time.makeOrdinalTime(0, 0, 0, FIRST, THURSDAY, JANUARY, year) - 3UL * SECS_PER_DAY;
+#define startISOyear(year...) makeOrdinalTime(0, 0, 0, FIRST, THURSDAY, JANUARY, year) - 3UL * SECS_PER_DAY;
uint8_t Timezone::weekISO(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
t = tzTime(t, local_or_utc);
int16_t yr = year(t);
@@ -1359,50 +1443,58 @@ uint8_t Timezone::weekISO(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc
time_t prev_year = startISOyear(yr - 1);
time_t next_year = startISOyear(yr + 1);
if (t < this_year) this_year = prev_year;
- if (t > next_year) this_year = next_year;
+ if (t >= next_year) this_year = next_year;
return (t - this_year) / ( SECS_PER_DAY * 7UL) + 1;
}
uint16_t Timezone::yearISO(time_t t /*= TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) {
t = tzTime(t, local_or_utc);
- int16_t yr = year(LAST_READ);
+ int16_t yr = year(t);
time_t this_year = startISOyear(yr);
- time_t prev_year = startISOyear(yr - 1);
time_t next_year = startISOyear(yr + 1);
if (t < this_year) return yr - 1;
- if (t > next_year) return yr + 1;
+ if (t >= next_year) return yr + 1;
return yr;
}
Timezone UTC;
-Timezone& defaultTZ = UTC;
-
-
-#ifdef ARDUINO_TIMELIB_COMPATIBILITY
-
- // Can't be macros because then it would also expand the explicit class member calls
-
- time_t now() { return (defaultTZ.now()); }
- uint8_t second(time_t t = TIME_NOW) { return (defaultTZ.second(t)); }
- uint8_t minute(time_t t = TIME_NOW) { return (defaultTZ.minute(t)); }
- uint8_t hour(time_t t = TIME_NOW) { return (defaultTZ.hour(t)); }
- uint8_t day(time_t t = TIME_NOW) { return (defaultTZ.day(t)); }
- uint8_t weekday(time_t t = TIME_NOW) { return (defaultTZ.weekday(t)); }
- uint8_t month(time_t t = TIME_NOW) { return (defaultTZ.month(t)); }
- uint16_t year(time_t t = TIME_NOW) { return (defaultTZ.year(t)); }
- uint8_t hourFormat12(time_t t = TIME_NOW) { return (defaultTZ.hour(t) % 12); }
- bool isAM(time_t t = TIME_NOW) { return (defaultTZ.hour(t) < 12) ? true : false; }
- bool isPM(time_t t = TIME_NOW) { return (defaultTZ.hour(t) >= 12) ? true : false; }
- String monthStr(const uint8_t month) { return time.monthString(month); }
- String monthShortStr(const uint8_t month) { return time.monthString(month).substring(0,3); }
- String dayStr(const uint8_t day) { return time.dayString(day); }
- String dayShortStr(const uint8_t day) { return time.dayString(day).substring(0,3); }
- void setTime(time_t t) { defaultTZ.setTime(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) { defaultTZ.setTime(hr, min, sec, day, month, yr); }
- void breakTime(time_t t, tmElements_t &tm) { time.breakTime(t, tm); }
- time_t makeTime(tmElements_t &tm) { return time.makeTime(tm); }
- time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, int16_t year) { return time.makeTime(hour, minute, second, day, month, year); }
- timeStatus_t timeStatus() { return time.timeStatus(); }
-
-#endif
\ No newline at end of file
+Timezone *defaultTZ = &UTC;
+
+
+
+// Original time lib compatibility
+String dayShortStr(const uint8_t day) { return dayString(day).substring(0,3); }
+String dayStr(const uint8_t day) { return dayString(day); }
+String monthShortStr(const uint8_t month) { return monthString(month).substring(0,3); }
+String monthStr(const uint8_t month) { return monthString(month); }
+
+
+// All bounce-throughs to defaultTZ
+String dateTime(String format /* = DEFAULT_TIMEFORMAT */) { return (defaultTZ->dateTime(format)); }
+String dateTime(time_t t, String format /* = DEFAULT_TIMEFORMAT */) { return (defaultTZ->dateTime(t, format)); }
+String dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format /* = DEFAULT_TIMEFORMAT */) { return (defaultTZ->dateTime(t, local_or_utc, format)); }
+uint8_t day(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->day(t, local_or_utc)); }
+uint16_t dayOfYear(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->dayOfYear(t, local_or_utc)); }
+int16_t getOffset(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->getOffset(t, local_or_utc)); }
+String getTimezoneName(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->getTimezoneName(t, local_or_utc)); }
+uint8_t hour(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->hour(t, local_or_utc)); }
+uint8_t hourFormat12(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->hour(t, local_or_utc) % 12); }
+bool isAM(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->hour(t, local_or_utc) < 12) ? true : false; }
+bool isDST(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->isDST(t, local_or_utc)); }
+bool isPM(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->hour(t, local_or_utc) >= 12) ? true : false; }
+String militaryTZ(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->militaryTZ(t, local_or_utc)); }
+uint8_t minute(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->minute(t, local_or_utc)); }
+uint8_t month(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->month(t, local_or_utc)); }
+uint16_t ms(time_t t /* = TIME_NOW */) { return (defaultTZ->ms(t)); }
+time_t now() { return (defaultTZ->now()); }
+uint8_t second(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->second(t, local_or_utc)); }
+uint8_t setEvent(void (*function)(), const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr) { return (defaultTZ->setEvent(function,hr, min, sec, day, mnth, yr)); }
+uint8_t setEvent(void (*function)(), time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->setEvent(function, t, local_or_utc)); }
+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) { defaultTZ->setTime(hr, min, sec, day, month, yr); }
+void setTime(time_t t) { defaultTZ->setTime(t); }
+uint8_t weekISO(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->weekISO(t, local_or_utc)); }
+uint8_t weekday(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->weekday(t, local_or_utc)); }
+uint16_t year(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->year(t, local_or_utc)); }
+uint16_t yearISO(time_t t /* = TIME_NOW */, ezLocalOrUTC_t local_or_utc /* = LOCAL_TIME */) { return (defaultTZ->yearISO(t, local_or_utc)); }
+
diff --git a/src/ezTime.h b/src/ezTime.h
index 87f16da..1c216f1 100644
--- a/src/ezTime.h
+++ b/src/ezTime.h
@@ -8,21 +8,24 @@
// objects created by ezTime.)
#define ARDUINO_TIMELIB_COMPATIBILITY
-// Compiles in NTP updating and timezoneapi.io fetching
+// Compiles in NTP updating, timezoneapi.io fetching and caching
#define EZTIME_NETWORK_ENABLE
// Arduino Ethernet shields
// #define EZTIME_ETHERNET
// Uncomment one of the below to only put only messages up to a certain level in the compiled code
-// (You still need to turn them on with time.debugLevel(someLevel) to see them)
+// (You still need to turn them on with debugLevel(someLevel) to see them)
// #define EZTIME_MAX_DEBUGLEVEL_NONE
// #define EZTIME_MAX_DEBUGLEVEL_ERROR
// #define EZTIME_MAX_DEBUGLEVEL_INFO
// Cache mechanism, either EEPROM or NVS, not both. (See README)
-#define EZTIME_CACHE_EEPROM
-// #define EZTIME_CACHE_NVS
+// #define EZTIME_CACHE_EEPROM
+#define EZTIME_CACHE_NVS
+
+
+// Warranty void if edited below this point...
@@ -71,7 +74,6 @@ typedef enum {
// Defines that can make your code more readable. For example, if you are looking for the first
// Thursday in a year, you could write: time.makeOrdinalTime(0, 0, 0, JANUARY, FIRST, THURSDAY, year)
-// (As is done within ezTime to calculate ISO weeks)
#define SUNDAY 1
#define MONDAY 2
@@ -100,36 +102,6 @@ typedef enum {
#define FOURTH 4
#define LAST 5
-#if defined(EZTIME_MAX_DEBUGLEVEL_NONE)
- #define err(args...) ""
- #define errln(args...) ""
- #define info(args...) ""
- #define infoln(args...) ""
- #define debug(args...) ""
- #define debugln(args...) ""
-#elif defined(EZTIME_MAX_DEBUGLEVEL_ERROR)
- #define err(args...) if (time._debug_level >= ERROR) Serial.print(args)
- #define errln(args...) if (time._debug_level >= ERROR) Serial.println(args)
- #define info(args...) ""
- #define infoln(args...) ""
- #define debug(args...) ""
- #define debugln(args...) ""
-#elif defined(EZTIME_MAX_DEBUGLELEL_INFO)
- #define err(args...) if (time._debug_level >= ERROR) Serial.print(args)
- #define errln(args...) if (time._debug_level >= ERROR) Serial.println(args)
- #define info(args...) if (time._debug_level >= INFO) Serial.print(args)
- #define infoln(args...) if (time._debug_level >= INFO) Serial.println(args)
- #define debug(args...) ""
- #define debugln(args...) ""
-#else // nothing specified compiles everything in.
- #define err(args...) if (time._debug_level >= ERROR) Serial.print(args)
- #define errln(args...) if (time._debug_level >= ERROR) Serial.println(args)
- #define info(args...) if (time._debug_level >= INFO) Serial.print(args)
- #define infoln(args...) if (time._debug_level >= INFO) Serial.println(args)
- #define debug(args...) if (time._debug_level >= DEBUG) Serial.print(args)
- #define debugln(args...) if (time._debug_level >= DEBUG) Serial.println(args)
-#endif
-
////////////////////////
@@ -157,24 +129,24 @@ typedef struct {
void (*function)();
} ezEvent_t;
-#define MAX_EVENTS 8
+#define MAX_EVENTS 8
-#define TIME_NOW 0xFFFFFFFF
-#define LAST_READ 0xFFFFFFFE
+#define TIME_NOW (int32_t)0x7FFFFFFF // Two special-meaning time_t values ...
+#define LAST_READ (int32_t)0x7FFFFFFE // (So yes, ezTime might malfunction two seconds before everything else...)
-#define NTP_PACKET_SIZE 48
+#define NTP_PACKET_SIZE 48
#define NTP_LOCAL_TIME_PORT 2342
-#define NTP_SERVER "pool.ntp.org"
-#define NTP_TIMEOUT 1500 // milliseconds
-#define NTP_INTERVAL 600 // default update interval in seconds
-#define NTP_RETRY 5 // Retry after this many seconds on failed NTP
-#define NTP_STALE_AFTER 3600 // If update due for this many seconds, set timeStatus to timeNeedsSync
+#define NTP_SERVER "pool.ntp.org"
+#define NTP_TIMEOUT 1500 // milliseconds
+#define NTP_INTERVAL 600 // default update interval in seconds
+#define NTP_RETRY 5 // Retry after this many seconds on failed NTP
+#define NTP_STALE_AFTER 3600 // If update due for this many seconds, set timeStatus to timeNeedsSync
-#define TIMEZONEAPI_TIMEOUT 2000 // milliseconds
+#define TIMEZONEAPI_TIMEOUT 2000 // milliseconds
#define EEPROM_CACHE_LEN 50
#define MAX_CACHE_PAYLOAD ((EEPROM_CACHE_LEN - 3) / 3) * 4 + ( (EEPROM_CACHE_LEN - 3) % 3) // 2 bytes for len and date, then 4 to 3 (6-bit) compression on rest
-#define MAX_CACHE_AGE_MONTHS 2
+#define MAX_CACHE_AGE_MONTHS 6
// Various date-time formats
#define ATOM "Y-m-d\\TH:i:sP"
@@ -189,80 +161,35 @@ typedef struct {
#define RFC3339_EXT "Y-m-d\\TH:i:s.vP"
#define RSS RFC822
#define W3C ATOM
-#define ISO8601_YWD "X-\\Ww-N" // Note that ISO-8601 Year/Week/Day notation may be one year + or - one at beginning or end of year
+#define ISO8601_YWD "X-\\WW-N"
#define DEFAULT_TIMEFORMAT COOKIE
-
-//
-// E Z t i m e c l a s s
-//
-
-class EZtime {
-
- friend class Timezone; // Allow methods from Timezone class to access private methods of this class
-
- ////////// Error handing
- public:
- static ezError_t error(bool reset = false); // Returns ezError_t enumeration of last error
- static String errorString(ezError_t err = LAST_ERROR); // Human-readable form of last error.
- static void debugLevel(ezDebugLevel_t level); // Sets serial printing of debug info to specified ezDebugLevel_t enumeration
- private:
- static void error(ezError_t err); // Used to set an error
- static String debugLevelString(ezDebugLevel_t level); // Human-readable form of debug level.
- static ezError_t _last_error;
- static ezDebugLevel_t _debug_level;
- ///////////
-
- public:
- EZtime();
- static timeStatus_t timeStatus();
- static void events();
- static void breakTime(time_t time, tmElements_t &tm); // break time_t into elements
- static time_t makeTime(tmElements_t &tm); // convert time elements into time_t
- static time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, uint16_t year);
- static time_t makeOrdinalTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t ordinal, uint8_t wday, uint8_t month, uint16_t year);
- static time_t compileTime(String compile_date = __DATE__, String compile_time = __TIME__);
- static String monthString(uint8_t month);
- static String dayString(uint8_t day);
- static bool secondChanged();
- static bool minuteChanged();
- static void deleteEvent(uint8_t event_handle);
- static void deleteEvent(void (*function)());
-
- private:
- static time_t now(bool update_last_read = true);
- static ezEvent_t _events[MAX_EVENTS];
- static time_t _last_sync_time, _last_read_t;
- static uint32_t _last_sync_millis;
- static bool _ntp_enabled;
- static uint16_t _last_read_ms;
- static timeStatus_t _time_status;
- static bool _initialised;
-
- #ifdef EZTIME_NETWORK_ENABLE
-
- public:
- static bool queryNTP(String server, time_t &t, unsigned long &measured_at); // measured_at: millis() at measurement, t is converted to secs since 1970
- static void updateNTP();
- static void setServer(String ntp_server = NTP_SERVER);
- static void setInterval(uint16_t seconds = 0); // 0 = no NTP updates
- static bool waitForSync(uint16_t timeout = 0); // timeout in seconds
-
- private:
- static uint16_t _ntp_interval; // in seconds
- static String _ntp_server;
-
- #endif // EZTIME_NETWORK_ENABLE
-
- ////////// Free extras ...
- public:
- static String urlEncode(String str); // Does what you think it does
- static String zeropad(uint32_t number, uint8_t length); // Returns number as string of given length, zero-padded on left if needed
- //////////
-
-};
-
-extern EZtime time; // declares the "time" instance of the "EZtime" class
+void breakTime(time_t time, tmElements_t &tm);
+time_t compileTime(String compile_date = __DATE__, String compile_time = __TIME__);
+String dayString(uint8_t day);
+void debugLevel(ezDebugLevel_t level);
+void deleteEvent(uint8_t event_handle);
+void deleteEvent(void (*function)());
+ezError_t error(bool reset = false);
+String errorString(ezError_t err = LAST_ERROR);
+void events();
+time_t makeOrdinalTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t ordinal, uint8_t wday, uint8_t month, uint16_t year);
+time_t makeTime(tmElements_t &tm);
+time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, uint16_t year);
+bool minuteChanged();
+String monthString(uint8_t month);
+bool secondChanged();
+timeStatus_t timeStatus();
+String urlEncode(String str);
+String zeropad(uint32_t number, uint8_t length);
+
+#ifdef EZTIME_NETWORK_ENABLE
+ bool queryNTP(String server, time_t &t, unsigned long &measured_at);
+ void setInterval(uint16_t seconds = 0);
+ void setServer(String ntp_server = NTP_SERVER);
+ void updateNTP();
+ bool waitForSync(uint16_t timeout = 0);
+#endif
//
@@ -273,33 +200,34 @@ class Timezone {
public:
Timezone(bool locked_to_UTC = false);
- bool setPosix(String posix);
- String getPosix();
- time_t tzTime(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
- time_t tzTime(time_t t, ezLocalOrUTC_t local_or_utc, String &tzname, bool &is_dst, int16_t &offset);
- void setDefault();
- bool isDST(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
- String getTimezoneName(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
- int16_t getOffset(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
- time_t now();
- uint8_t setEvent(void (*function)(), time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
- uint8_t setEvent(void (*function)(), const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr);
- void setTime(time_t t, uint16_t ms = 0);
- 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);
String dateTime(time_t t, String format = DEFAULT_TIMEFORMAT);
String dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format = DEFAULT_TIMEFORMAT);
+ uint8_t day(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 1-31
+ uint16_t dayOfYear(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // days from start of year, jan 1st = 0
+ int16_t getOffset(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+ String getPosix();
+ String getTimezoneName(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
uint8_t hour(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 0-23
+ bool isDST(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+ String militaryTZ(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
uint8_t minute(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 0-59
+ uint8_t month(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 1-12
+ uint16_t ms(time_t t = TIME_NOW); // 0-999
+ time_t now();
uint8_t second(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 0-59
- uint16_t ms(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 0-999
- uint8_t day(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 1-31
+ void setDefault();
+ uint8_t setEvent(void (*function)(), const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr);
+ uint8_t setEvent(void (*function)(), time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+ bool setPosix(String posix);
+ 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);
+ void setTime(time_t t, uint16_t ms = 0);
+ time_t tzTime(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+ time_t tzTime(time_t t, ezLocalOrUTC_t local_or_utc, String &tzname, bool &is_dst, int16_t &offset);
+ uint8_t weekISO(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // ISO-8601 week number (weeks starting on Monday)
uint8_t weekday(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // Day of the week (1-7), Sunday is day 1
- uint8_t month(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 1-12
uint16_t year(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // four digit year
- uint16_t dayOfYear(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // days from start of year, jan 1st = 0
- uint8_t weekISO(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // ISO-8601 week number (weeks starting on Monday)
- uint16_t yearISO(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // ISO-8601 year, can differ from actual year, plus or minus one
+ uint16_t yearISO(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // ISO-8601 year, can differ from actual year, plus or minus one
private:
String _posix, _olsen;
bool _locked_to_UTC;
@@ -325,7 +253,7 @@ class Timezone {
void clearCache(bool delete_section = false);
private:
bool setCache();
- bool writeCache(const String &str);
+ bool writeCache(String &str);
bool readCache(String &olsen, String &posix, uint8_t &months_since_jan_2018);
uint8_t _cache_month;
#endif
@@ -334,64 +262,77 @@ class Timezone {
};
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
+extern Timezone *defaultTZ;
+
+
+// These bounce through to same-named methods in defaultTZ
+String dateTime(String format = DEFAULT_TIMEFORMAT);
+String dateTime(time_t t, String format = DEFAULT_TIMEFORMAT);
+String dateTime(time_t t, ezLocalOrUTC_t local_or_utc, String format = DEFAULT_TIMEFORMAT);
+uint8_t day(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+uint16_t dayOfYear(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+int16_t getOffset(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+String getTimezoneName(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+uint8_t hour(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+uint8_t hourFormat12(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+bool isAM(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+bool isDST(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+bool isPM(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+String militaryTZ(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+uint8_t minute(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+uint8_t month(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+uint16_t ms(time_t t = TIME_NOW);
+time_t now();
+uint8_t second(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+uint8_t setEvent(void (*function)(), const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr);
+uint8_t setEvent(void (*function)(), time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+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 setTime(time_t t);
+uint8_t weekISO(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+uint8_t weekday(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+uint16_t year(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+uint16_t yearISO(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
+
+
+
+// The following defines all copied from the original Time lib to keep existing code working
+
+/* 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)
+
+// Aliases to match original Time library
+String monthStr(const uint8_t month);
+String monthShortStr(const uint8_t month);
+String dayStr(const uint8_t month);
+String dayShortStr(const uint8_t month);
+
} // extern "C++"
#endif // __cplusplus