Getting ready for prime-time

pull/1/head
Rop 7 years ago
parent 4ccc12f401
commit a9dcebe20c

@ -1,10 +1,14 @@
# ezTime Documentation # ezTime, an Arduino library for all of time <sup>*</sup>
**ezTime, pronounced "Easy Time", is a very easy to use Arduino time and date library that provides NTP network time lookups, extensive timezone support, formatted time and date strings, millisecond precision and more.** **ezTime - pronounced "Easy Time" - is a very easy to use Arduino time and date library that provides NTP network time lookups, extensive timezone support, formatted time and date strings, millisecond precision and more.**
> &nbsp; * limitations may apply, see "2036 and 2038" chapter
&nbsp;
## A brief history of ezTime ## A brief history of ezTime
I was working on M5ez, an interface library to easily make cool-looking programs for the "M5Stack" ESP32 hardware. The status bar of that needed to display the time. I figured I would use [Time](https://github.com/PaulStoffregen/Time), Paul Stoffregen's library to do time things on Arduino. Then I needed to sync that to an NTP server, so I figured I would use [NTPclient](https://github.com/arduino-libraries/NTPClient), one of the existing NTP client libraries. And then I wanted it to show the local time, so I would need some way for the user to set an offset between UTC and local time. I was working on [M5ez](https://github.com/ropg/M5ez), an interface library to easily make cool-looking programs for the "M5Stack" ESP32 hardware. The status bar of that needed to display the time. That was all, I swear. I figured I would use [Time](https://github.com/PaulStoffregen/Time), Paul Stoffregen's library to do time things on Arduino. Then I needed to sync that to an NTP server, so I figured I would use [NTPclient](https://github.com/arduino-libraries/NTPClient), one of the existing NTP client libraries. And then I wanted it to show the local time, so I would need some way for the user to set an offset between UTC and local time.
So far, so good. So far, so good.
@ -12,28 +16,32 @@ Then I remembered how annoyed I always am when daylight savings time comes or go
And then I also wanted to print time in various formats. Wouldn't it be nice to have some function to print formatted time like many programming languages offer them? And then I also wanted to print time in various formats. Wouldn't it be nice to have some function to print formatted time like many programming languages offer them?
Overlooking the battlefield after implementing some of this, it seemed like there had to be a better way. Especially, some way in which all this work would benefit more people. I decided to make the mother of all time libraries. Overlooking the battlefield after implementing some part of this, it seemed like there had to be a better way. Especially, some way in which all this work would benefit more people. This is how ezTime, the project that was once only going to take a few days, came to be.
## ezTime is: ## ezTime is ...
**self-contained**: It only depends on other libraries for networking (to do NTP and timezone data lookups). And if it is running on an ESP32, it will use the `Preferences` library to store cached timezone data to minimize lookups. It uses [timezoneapi.io](https://timezoneapi.io/) to get its timezone data, they provide 50 free lookups per user per day. (You can turn all of this off at compile time, e.g. if you have no networking and/or another time source.) **self-contained**: It only depends on other libraries to get online, but then it doesn't need other libraries for NTP and timezone data lookups.
**precise**: An NTP request to pool.ntp.org only takes 40ms round-trip on my home DSL, so adding sub-second precision to a time library makes sense. ezTime reads the fractional seconds and tries to account for network latency to give you precise time. **precise**: Unlike other libraries, ezTime does not throw away or mangle the fractional second information from the NTP server. An NTP request to pool.ntp.org only takes 40ms round-trip on home DSL these days, so adding sub-second precision to a time library makes sense. ezTime reads the fractional seconds and tries to account for network latency to give you precise time.
**backwards compatible**: Anything written for the existing Arduino time library will still work. You can set which timezone the sketch should be in, or have it be in UTC which is the default. But you can also set and express time referring to multiple timezones, all very easy and intuitive. **backwards compatible**: Anything written for the existing Arduino time library will still work. You can set which timezone the sketch should be in, or have it be in UTC which is the default. But you can also set and express time referring to multiple timezones, all very easy and intuitive.
**robust**: On ESP32 it doesn't fail if the timezone api goes away: it will cache the data for any timezones used. If that server is unreachable it will not initialise new timezones or do a yearly update of the rules, but it will still work on timezones you have already used. **robust**: It doesn't fail if the timezone api goes away: it can use cached data, which ezTime can store in EEPROM (AVR Arduinos) or NVS (ESP32 through Preferences library).
**informative**: No need to guess while you're working on something, ezTime can print messages to the serial port at your desired level of detail, telling you when it gets an NTP update and how much your internal clock was off, for instance. **informative**: No need to guess while you're working on something, ezTime can print messages to the serial port at your desired level of detail, telling you about the timezone's daylight savings info it receives or when it gets an NTP update and by how much your internal clock was off, for instance.
**time-saving**: No more time spent on writing code to print date or time in some nicer way. Print things like "8:20 PM" or "Saturday the 23rd of August 2018" with ease. Prevent display-flicker with minuteChanged() and secondChanged() functions without storing any values to compare. **time-saving**: No more time spent on writing code to print date or time in some nicer way. Print things like "8:20 PM" or "Saturday the 23rd of August 2018" with ease. Prevent display-flicker with minuteChanged() and secondChanged() functions without storing any values to compare.
**Small enough**: Works with all features and full debugging information on an Arduino Uno with an Ethernet Shield, leaving 2/3 of RAM but not too much flash for you to work with. (Even on a good day there isn't that much left if you want to do anything that involves networking.) Various `#define` options let you leave parts of it out if you want to make it smaller: you can even leave out the networking altogether if you have a different time source.
**easy to use**: Don't believe it until you see it. Have a look at some of these examples to see how easy it is to use. **easy to use**: Don't believe it until you see it. Have a look at some of these examples to see how easy it is to use.
&nbsp; &nbsp;
### Timezones ### Timezones
(a complete sketch to show how simple it is)
``` ```
#include <ezTime.h> #include <ezTime.h>
#include <WiFi.h> #include <WiFi.h>
@ -44,33 +52,19 @@ void setup() {
ezTime.waitForSync(); ezTime.waitForSync();
Serial.println("UTC: " + UTC.dateTime()); Serial.println("UTC: " + UTC.dateTime());
Timezone myTZ; Timezone NewZealand;
NewZealand.setLocation("Pacific/Auckland");
// Anything with a slash in it is interpreted as an official timezone name Serial.println("New Zealand time: " + NewZealand.dateTime());
// https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
myTZ.setLocation("Pacific/Auckland");
Serial.println("Auckland: " + myTZ.dateTime());
// Anything else is parsed as an address to see if that resolves
myTZ.setLocation("Paris, Texas");
Serial.println("Paris, Texas: " + myTZ.dateTime());
// The empty string is resolved to the GeoIP location of your IP-address
myTZ.setLocation("");
Serial.println("Your local time: " + myTZ.dateTime());
} }
void loop() { void loop() { }
}
``` ```
``` ```
UTC: Saturday, 25-Aug-18 14:13:03 UTC UTC: Friday, 07-Sep-2018 11:25:10 UTC
Auckland: Sunday, 26-Aug-18 03:13:04 NZST New Zealand time: Friday, 07-Sep-2018 23:25:11 NZST
Paris, Texas: Saturday, 25-Aug-18 09:13:04 CDT
Your local time: Saturday, 25-Aug-18 16:13:04 CEST
``` ```
&nbsp; &nbsp;
@ -78,56 +72,27 @@ Your local time: Saturday, 25-Aug-18 16:13:04 CEST
### Formatted date and time ### Formatted date and time
``` ```
#include <ezTime.h>
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.begin("your-ssid", "your-password");
ezTime.waitForSync();
Serial.println();
Serial.println("Time in various internet standard formats ...");
Serial.println();
Serial.println("ATOM: " + UTC.dateTime(ATOM));
Serial.println("COOKIE: " + UTC.dateTime(COOKIE)); Serial.println("COOKIE: " + UTC.dateTime(COOKIE));
Serial.println("IS8601: " + UTC.dateTime(ISO8601)); Serial.println("IS8601: " + UTC.dateTime(ISO8601));
Serial.println("RFC822: " + UTC.dateTime(RFC822)); Serial.println("RFC822: " + UTC.dateTime(RFC822));
Serial.println("RFC850: " + UTC.dateTime(RFC850)); 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: " + UTC.dateTime(RFC3339));
Serial.println("RFC3339_EXT: " + UTC.dateTime(RFC3339_EXT)); Serial.println("RFC3339_EXT: " + UTC.dateTime(RFC3339_EXT));
Serial.println("RSS: " + UTC.dateTime(RSS)); Serial.println("RSS: " + UTC.dateTime(RSS));
Serial.println("W3C: " + UTC.dateTime(W3C));
Serial.println(); 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("or like " + UTC.dateTime("l ~t~h~e jS ~o~f F Y, g:i A") );
}
void loop() {
}
``` ```
``` ```
Time in various internet standard formats ...
ATOM: 2018-08-25T14:23:45+00:00
COOKIE: Saturday, 25-Aug-2018 14:23:45 UTC COOKIE: Saturday, 25-Aug-2018 14:23:45 UTC
IS8601: 2018-08-25T14:23:45+0000 IS8601: 2018-08-25T14:23:45+0000
RFC822: Sat, 25 Aug 18 14:23:45 +0000 RFC822: Sat, 25 Aug 18 14:23:45 +0000
RFC850: Saturday, 25-Aug-18 14:23:45 UTC RFC850: Saturday, 25-Aug-18 14:23:45 UTC
RFC1036: Sat, 25 Aug 18 14:23:45 +0000
RFC1123: Sat, 25 Aug 2018 14:23:45 +0000
RFC2822: Sat, 25 Aug 2018 14:23:45 +0000
RFC3339: 2018-08-25T14:23:45+00:00 RFC3339: 2018-08-25T14:23:45+00:00
RFC3339_EXT: 2018-08-25T14:23:45.846+00:00 RFC3339_EXT: 2018-08-25T14:23:45.846+00:00
RSS: Sat, 25 Aug 2018 14:23:45 +0000 RSS: Sat, 25 Aug 2018 14:23:45 +0000
W3C: 2018-08-25T14:23:45+00:00
... and any other format, like "Saturday the 25th of August 2018, 2:23 PM" or like Saturday the 25th of August 2018, 2:23 PM
``` ```
&nbsp; &nbsp;
@ -135,39 +100,9 @@ W3C: 2018-08-25T14:23:45+00:00
### milliseconds ### milliseconds
``` ```
#include <ezTime.h>
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.begin("your-ssid", "your-password");
ezTime.setInterval(60);
ezTime.waitForSync();
Serial.println();
for (int n = 0; n < 10; n++) { for (int n = 0; n < 10; n++) {
Serial.println(UTC.dateTime("l, d-M-y H:i:s.v T")); Serial.println(UTC.dateTime("l, d-M-y H:i:s.v T"));
} }
Serial.println();
Serial.println("Those milliseconds between the first and the last line ...");
Serial.println();
Serial.println(" ... most of that is spent sending to the serial port.");
Serial.println();
Serial.println();
Serial.println();
Serial.println("And ezTime is not making those milliseconds up either.");
Serial.println();
Serial.println(" ... Stick around as we do an NTP request every minute.");
ezTime.debug(INFO);
}
void loop() {
now();
delay(1000);
}
``` ```
``` ```
@ -181,20 +116,28 @@ Saturday, 25-Aug-18 14:32:53.293 UTC
Saturday, 25-Aug-18 14:32:53.297 UTC Saturday, 25-Aug-18 14:32:53.297 UTC
Saturday, 25-Aug-18 14:32:53.300 UTC Saturday, 25-Aug-18 14:32:53.300 UTC
Saturday, 25-Aug-18 14:32:53.303 UTC Saturday, 25-Aug-18 14:32:53.303 UTC
```
Those milliseconds between the first and the last line ... > *This is on my ESP32. See how it fills up the serial buffer real fast at first, and then has to wait for the characters to be sent before it can return?*
... most of that is spent sending to the serial port.
&nbsp;
### Rich information and *... oh my just look at these NTP updates*
And ezTime is not making those milliseconds up either. ```
[...]
ezTime.setInterval(60);
ezTime.setDebugLevel(INFO);
}
... Stick around as we do an NTP request every minute. void loop() {
now();
delay(1000);
}
```
```
ezTime debug level set to INFO ezTime debug level set to INFO
Querying pool.ntp.org ... success (round trip 47 ms)
Received time: Saturday, 25-Aug-18 14:33:53.363 UTC (internal clock was 31 ms slow)
Querying pool.ntp.org ... success (round trip 42 ms) Querying pool.ntp.org ... success (round trip 42 ms)
Received time: Saturday, 25-Aug-18 14:34:53.410 UTC (internal clock was 1 ms fast) Received time: Saturday, 25-Aug-18 14:34:53.410 UTC (internal clock was 1 ms fast)
Querying pool.ntp.org ... success (round trip 43 ms) Querying pool.ntp.org ... success (round trip 43 ms)
@ -220,102 +163,78 @@ ezTime is an Arduino library. To start using it with the Arduino IDE:
* Click the row to select the library. * Click the row to select the library.
* Click the Install button to install the library. * Click the Install button to install the library.
in File -> Examples you will now see an ezTime heading down under "Examples from custom libraries". You can try running some of these examples to see if it all works. ezTime is made to be, as the name implies, quite easy to use. So you'll probably understand a lot of how things work from just looking at the examples. in File -> Examples you will now see an ezTime heading down under "Examples from custom libraries". You can try running some of these examples to see if it all works. ezTime is made to be, as the name implies, quite easy to use. So you'll probably understand a lot of how things work from just looking at the examples. Now either just play with those and use the rest of this documentation only when you get stuck, or keep reading to see how things work in ezTime.
# &nbsp;
When you include the ezTime library, it creates two objects for you to interact with. One object is called `ezTime`, and its methods allow you to change defaults like which NTP server is used, the interval at which it is polled as well as many other more general time related things. Then it also creates an instance of the `Timezone` class called `UTC`. UTC's methods allow you to do things that refer to time as specified in UTC. So for instance, `UTC.dateTime()` returns a string representation of the current date and time in UTC. # ezTime User Manual
If your code says > *If it's not in here, it's wrong...*
```
Timezone CEST;
CEST.setPosix("CST-2");
```
it will create a timezone with a very simple rule: it is called CST and it is two hours later there than at UTC. Now you can use `CEST.dateTime()`, to get the current time in CEST. But this will not change to and from Daylight Saving Time if your timezone has that. The more powerful way of initialising a timezone is using 'getLocation' like:
```
Timezone berlin;
berlin.setLocation("Europe/Berlin");
```
## About this manual
# &nbsp; ### Semi-internal functions
Some functions are not necessarily useful for everyday users of this library, but might be useful to someone someday. For instance, this library checks with the NTP servers automatically, there should be no need to ever "manually" get an NTP response. But the function to do that is still exposed to the user. Even some functions that have nothing to do with time, like `urlEncode` are there for you to use, simply because they *might* be useful to someone, and the library needed them internally so they come at no extra cost in terms of size. In this manual, the names of these functions are printed in *italics* in their chapter headings, just to make it a easier for you to see which functions are core functionality and which are really not needed in everyday use.
### Specifying time
I hate documentation that still makes me reach for for the source code, so this manual supplies the function prototype with each function so you can see what types or arguments each function takes and what type the return value is. I took one shortcut though. A lot of functions allow you to specify a time. In the function prototype this looks like:
`time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL`
Throughout this manual, we replace these two optional arguments in the funtion definitions with:
`TIME`
That's because the prior is just a little too long to be repeating a thousand times, and it also makes things look more complicated than they need to be. In most places where you specify a time in ezTime, you are most likely to mean "right now". This can be done by supplying no arguments at all, or `TIME_NOW`. You might make a number of requests in a row, and want to make sure that the time didn't change between them. No need to stick the time value in a variable. After you have made a call specifying no time (meaning `TIME_NOW`), you can specify `LAST_READ` to use the time from the exact moment you made that first call.
Otherwise, you can specify a `time_t` value, a well-known 32-bit signed integer way of specifying time in seconds elapsed since 00:00 Jan 1st 1970. If you specify a value other than `TIME_NOW` or `LAST_READ`, you can then specify whether you mean in UTC or local time, by following it with a second argument that is either `UTC_TIME` or `LOCAL_TIME`.
For example, if you have set up a timezone called Berlin, `Berlin.isDST(1536314299, UTC_TIME)` tells you whether Daylight Savings Time is in effect on that time, as seconds from 00:00 Jan 1st 1970 UTC, as opposed to that many seconds from that time in Berlin (which would be the default). There will be some examples later on, showing you how to create and process such timestamps. Mostly though, you don't need specify anything at all because you just want something time-related about "right now".
> *Time-geek sidenote: ezTime does not have historical information about the daylight davings rules of the past or future, it only applies the rules it has now as if they also applied in the past or future. Check [here](https://www.timeanddate.com/) for historical records for timezones.*
# &nbsp;
# ezTime: complete documentation ## How it all works
## Index ### What happens when you include the library
* ezTime object It all starts when you include the library with `#include <ezTime.h>`. From that point forward there is an object instance called `ezTime` 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).
* breakTime
* clearCache ### No daemons here
* compileTime
* debug 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 synchronizes 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. So periodically when you ask it something, it will discover that it's time to re-synchronize its own clock with the NTP server, and then it'll go out, do that and take, say, 50 ms longer to respond back to your code. But it all happens only when you ask for it.
* error
* errorString ### But I only just woke up !
* getBetween
* makeTime 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 synchronized 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.
* makeUmpteenthTime
* now 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 `ezTime.waitForSync` that simply requests the time until it is synchronized (or until a set number of seconds passes, see below).
* queryNTP
* setInterval
* setServer
* timeStatus
* timezoneAPI
* updateNow
* urlEncode
* waitForSync
* zeropad
* Timezone class
* dateTime
* day
* getOffset
* getPosix
* getTimezoneName
* hour
* isDST
* isDST_UTC
* isDST_local
* minute
* minuteChanged
* month
* ms
* now
* second
* secondChanged
* setDefault
* setPosix
* setTime
* weekday
* year
## Documentation
### Starting it all
It all starts when you include the library with `#include <ezTime.h>`. From that point forward there is an object instance called `ezTime` with methods to control the behaviour of ezTime, as well as a timezone object called `UTC`. But nothing happens until you ask the library what time it is. This can be done by using any of the methods that tell time, like the `.dateTime` method of the timezone object. For instance, your code could do
`Serial.println(UTC.dateTime());` to print a complete textual representation of date and time to the serial port. The library would find out that time had not been synchronized 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 is probably not connected to the WiFi network just yet, and so 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.
## Setting and synchronising time ## 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 synchronized. Subsequent requests will retry the NTP query, but only if they happen at least 3 seconds later. (These 3 seconds are settable with the `NTP_RETRY` define from `ezTime.h`.) The NTP request from the scenario above failed because the network wasn't up yet, so the clock would still not be synchronized. Subsequent requests will retry the NTP query, but only if they happen at least 5 seconds later.
### ezTime.timeStatus ### ezTime.timeStatus
`timeStatus_t ezTime.timeStatus();` `timeStatus_t ezTime.timeStatus();`
Returns what state the clock is in. `ezTime.timeStatus()` will return either `timeNotSet`, `timeNeedsSync` or `timeSet`. Returns what state the clock is in. `ezTime.timeStatus()` will return one of:
* `timeNotSet` means no NTP update or other setting of the clock (with the `.settime` method) has taken place | timeStatus | meaning |
* `timeSet` means the clock should have the current time |----|----|
* `timeNeedsSync` means a scheduled NTP request has been due for more than an hour. (The time an update needs to be due before `timeNeedsSync` is set is configured by the `NTP_STALE_AFTER` define in the `ezTime.h` file.) | `timeNotSet` | No NTP update or manual setting of the clock (by calling the `.setTime` method of a timezone) has taken place |
| `timeSet` | The clock should have the current time |
| `timeNeedsSync` | A scheduled NTP request has been due for more than an hour. (The time an update needs to be due before `timeNeedsSync` is set is configured by the `NTP_STALE_AFTER` define in the `ezTime.h` file.) |
### ezTime.waitForSync ### ezTime.waitForSync
`bool ezTime.waitForSync(uint16_t timeout = 0);` `bool ezTime.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. `ezTime.waitForSync` will wait for the network to connect, and then for the time to be synchronized 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`. 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. `ezTime.waitForSync` will wait for the network to connect, and then for the time to be synchronized 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 further down)
### ezTime.setServer and ezTime.setInterval ### *ezTime.setServer and ezTime.setInterval*
`void ezTime.setServer(String ntp_server = NTP_SERVER);` `void ezTime.setServer(String ntp_server = NTP_SERVER);`
@ -323,13 +242,13 @@ If your code uses timezones other than UTC, it might want to wait to initialise
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 `ezTime.setServer` or a new interval (in seconds) with ezTime.setInterval. If you call setInterval with an interval of 0 seconds or call it as `ezTime.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 `ezTime.setServer` or a new interval (in seconds) with ezTime.setInterval. If you call setInterval with an interval of 0 seconds or call it as `ezTime.setInterval()`, no more NTP queries will be made.
### ezTime.updateNow ### *ezTime.updateNow*
`void ezTime.updateNow();` `void ezTime.updateNow();`
Schedules the next update to happen immediately, and then tries to query the NTP server. If that fails, it will keep retrying every 3 seconds. Schedules the next update to happen immediately, and then tries to query the NTP server. If that fails, it will keep retrying when you ask for new time information, but not more often than once every 5 seconds. (Defined by `NTP_RETRY` in `ezTime.h`.)
### ezTime.queryNTP ### *ezTime.queryNTP*
`bool ezTime.queryNTP(String server, time_t &t, unsigned long &measured_at);` `bool ezTime.queryNTP(String server, time_t &t, unsigned long &measured_at);`
@ -339,63 +258,157 @@ If the time server answers, `ezTime.queryNTP` returns `true`. If `false` is retu
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. 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.
## Errors and debug information ## Timezones, caching and timezoneapi.com
`void ezTime.debug(ezDebugLevel_t level);` ### On timezones in ezTime
`ezError_t ezTime.error();` TODO, also explain that we refer to timezones as `yourTZ` in rest of this document
### yourTZ.setDefault
`void yourTZ.setDefault()`
`#include <ezTime.h>` 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 timzone by calling `yourTZ.setDefault()`. The functions in the root namespace like `hour()` and `minute()` - without a timezone in front - are interpreted as if passed to the default timezone. So if you have ezisting 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.
`String ezTime.errorString(ezError_t err);` ### On timezone data in ezTime
## Timezones Internally, ezTime stores everything it knows about a timezone as two strings. One is the official name of the timezone in "Olsen" format (like `Europe/Berlin`). That name is used to then update, at least once per year, all the other information needed to represent time in that timezone. This is another string, in so-called "posix" format. It's a little longer and for Berlin it is `CET-1CEST,M3.4.0/2,M10.4.0/3`. The elements of this string are as follows:
`#include <ezTime.h>` 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 timzone by calling `someTZ.setDefault()`. | Element | meaning |
| ---- | ---- |
| `CET` | Name of timezone in standard time (CET = Central European Time in this case.)
| `-1` | Hours offset from UTC, meaning subtract one hour from this time to get to UTC. (Note offset is often written elsewhere the other way around (so +1 in this case), just to confuse things.) Could also specify minutes, like `-05:30` for India. |
| `CEST` | Name of timezone in Daylight Saving Time (DST), CEST stands for Central European Summer Time |
| `,M3` | DST starts in March |
| `.4` | On the fourth occurence of
| `.0` | a Sunday |
| `/2` | at 02:00 local time |
| `,M10` | DST ends in October |
| `.4` | on the fourth ocurrence of |
| `.0` | a Sunday |
| `/3` | at 03:00 local time |
`bool someTZ.setPosix(String posix)` ### yourTZ.setPosix
`String someTZ.getPosix()` `bool yourTZ.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
```
Timezone India
India.setPosix("IST-5:30")
Serial.println(India.dateTime());
```
`void someTZ.setDefault()` is enough, because the time in India doesn't go back and forth with the coming and going of Daylight Saving Time (even though the half hour offset to UTC is pretty weird.)
`bool someTZ.isDST_local(time_t t = TIME_NOW)` ### yourTZ.getPosix
`bool someTZ.isDST_UTC(time_t t = TIME_NOW)` `String yourTZ.getPosix()`
`bool someTZ.isDST()` `getPosix` does what you would expect and simply returns the posix string stored in ezTime for a given timezone.
`String someTZ.getTimezoneName(time_t t = TIME_NOW)` ### yourTZ.isDST
`int32_t someTZ.getOffset(time_t t = TIME_NOW)` `bool yourTZ.isDST(time_t t = TIME_NOW, bool local_time = true);`
`bool ezTime.timezoneAPI(String location, String &olsen, String &posix);` 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.
## Getting/setting date and time ### yourTZ.getTimezoneName
`time_t someTZ.now(bool update_last_read = true)` `String getTimezoneName(time_t t = TIME_NOW, bool local_time = true);`
`void someTZ.setTime(time_t t)` Provides the short code for the timezone, like `IST` for India, or `CET` (during standard time) or `CEST` (during Daylight Saving Time) for most of Europe.
`void someTZ.setTime(const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr)` ### yourTZ.getOffset
`int16_t yourTZ.getOffset(time_t t = TIME_NOW, bool local_time = true)`
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, that means -120 would mean 2 hours east of UTC.
### *yourTZ.tzTime*
`time_t yourTZ.tzTime(time_t t = TIME_NOW, bool local_time = true, tzTimeData_t *tztd = NULL);`
This will tell you the time in seconds since 00:00 on Jan 1st 1970 in a timezone when you give it a timne in UTC, or vice versa.
TODO: explain better and also explain tztd
## Getting date and time
### yourTZ.dateTime
``` ```
String someTZ.dateTime(String format = DEFAULT_TIMEFORMAT); String yourTZ.dateTime(String format = DEFAULT_TIMEFORMAT);
String someTZ.dateTime(time_t t, String format = DEFAULT_TIMEFORMAT); String yourTZ.dateTime(time_t t, 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`. Here's the list of characters and what they are replaced by.
| char | replaced by |
| ----- | :----- |
| `d` | Day of the month, 2 digits with leading zeros |
| `D` | First three letters of day in English, like `Tue` |
| `j` | Day of the month without leading zeros |
| `l` | (lowercase L) Day of the week in English, like `Tuesday` |
| `N` | // ISO-8601 numeric representation of the day of the week. (1 = Monday, 7 = Sunday)
| `S` | English ordinal suffix for the day of the month, 2 characters (st, nd, rd, th) |
| `w` | Numeric representation of the day of the week (0 = Sunday) |
| `F` | A month's name, such as `January` |
| `m` | Numeric representation of a month, with leading zeros |
| `M` | Three first letters of a monthin English, like `Apr` |
| `n` | Numeric representation of a month, without leading zeros |
| `t` | Number of days in the given month |
| `Y` | A full numeric representation of the year, 4 digits |
| `y` | Last two digits of the year |
| `a` | am or pm |
| `A` | AM or PM |
| `g` | 12-hour format of an hour without leading zeros |
| `G` | 24-hour format of an hour without leading zeros |
| `h` | 12-hour format of an hour with leading zeros |
| `H` | 24-hour format of an hour with leading zeros |
| `i` | Minutes with leading zeros |
| `s` | Seconds with leading zero |
| `T` | abbreviation for timezone, like `CEST` |
| `v` | milliseconds as three digits |
| `e` | Timezone identifier (Olsen name), like `Europe/Berlin` |
| `O` | Difference to Greenwich time (GMT) in hours and minutes written together, like `+0200`. Here a positive offset means east of UTC. |
| `P` | Same as O but with a colon between hours and minutes, like `+02:00` |
| `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) |
`time_t yourTZ.now(bool update_last_read = true)`
`void yourTZ.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)`
```
String yourTZ.dateTime(String format = DEFAULT_TIMEFORMAT);
String yourTZ.dateTime(time_t t, String format = DEFAULT_TIMEFORMAT);
``` ```
``` ```
uint8_t someTZ.hour(time_t t = TIME_NOW); uint8_t yourTZ.hour(time_t t = TIME_NOW);
uint8_t someTZ.minute(time_t t = TIME_NOW); uint8_t yourTZ.minute(time_t t = TIME_NOW);
uint8_t someTZ.second(time_t t = TIME_NOW); uint8_t yourTZ.second(time_t t = TIME_NOW);
uint16_t someTZ.ms(time_t t = TIME_NOW); uint16_t yourTZ.ms(time_t t = TIME_NOW);
uint8_t someTZ.day(time_t t = TIME_NOW); uint8_t yourTZ.day(time_t t = TIME_NOW);
uint8_t someTZ.weekday(time_t t = TIME_NOW); uint8_t yourTZ.weekday(time_t t = TIME_NOW);
uint8_t someTZ.month(time_t t = TIME_NOW); uint8_t yourTZ.month(time_t t = TIME_NOW);
uint16_t someTZ.year(time_t t = TIME_NOW); uint16_t yourTZ.year(time_t t = TIME_NOW);
``` ```
``` ```
bool someTZ.secondChanged(); uint8_t weekISO(time_t t = TIME_NOW);
bool someTZ.minuteChanged(); uint16_t yearISO(time_t t = TIME_NOW);
```
```
bool yourTZ.secondChanged();
bool yourTZ.minuteChanged();
``` ```
## Compatibility with Arduino `Time` library ## Compatibility with Arduino `Time` library
@ -406,6 +419,37 @@ bool someTZ.minuteChanged();
`void ezTime.breakTime(time_t time, tmElements_t &tm)` `void ezTime.breakTime(time_t time, tmElements_t &tm)`
## Errors and debug information
### ezTime.debugLevel
`void ezTime.debugLevel(ezDebugLevel_t level);`
Sets the level of detail at which ezTime outputs messages on the serial port. Can be set to one of:
| debugLevel | effect |
|---|---|
| `NONE` | ezTime does not output anything on the serial port |
| `ERROR` | ezTime will show when errors occur. Note that these may be transient errros that ezTime recovers from, such as NTP timeouts. |
| `INFO` | Essentially shows you what ezTime is doing in the background. Includes messages about NTP updates, initialising timezones, etc etc. |
| `DEBUG` | Detailed debugging information unlikely to be of much use unless you are trying to get to the bottom of certian internal behaviour of ezTime. |
*Note:* you can specify which level of debug information whould be compiled into the library. This is especially significant for AVR Arduino users that need to limit the flashindia and RAM footprint of ezTtime. See the "Running on small Arduinos" chapter further down. TODO
### ezTime.error
`ezError_t ezTime.error();`
A number of functions in ezTime are booleans, meaning they return `true` or `false` as their return value, where `false` means some error ocurred. `ezTime.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 night happen in what functions.
When you call `error()`, it will also reset the error, so you can clear the last error stored.
### ezTime.errorString
`String ezTime.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.
## Various functions ## Various functions
`time_t ezTime.makeUmpteenthTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t umpteenth, uint8_t wday, uint8_t month, int16_t year);` `time_t ezTime.makeUmpteenthTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t umpteenth, uint8_t wday, uint8_t month, int16_t year);`
@ -419,3 +463,7 @@ bool someTZ.minuteChanged();
`String zeropad(uint32_t number, uint8_t length);` `String zeropad(uint32_t number, uint8_t length);`
`void ezTime.clearCache();` `void ezTime.clearCache();`
## 2036 and 2038
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.

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

@ -26,7 +26,7 @@ void setup() {
Serial.println("And ezTime is not making those milliseconds up either."); Serial.println("And ezTime is not making those milliseconds up either.");
Serial.println(); Serial.println();
Serial.println(" ... Stick around as we do an NTP request every minute."); Serial.println(" ... Stick around as we do an NTP request every minute.");
ezTime.debug(INFO); ezTime.debugLevel(INFO);
} }

@ -11,9 +11,9 @@
"type": "git", "type": "git",
"url": "https://github.com/ropg/ezTime" "url": "https://github.com/ropg/ezTime"
}, },
"version": "0.0.1", "version": "0.6.0",
"framework": "arduino", "framework": "arduino",
"platforms": "espressif32" "platforms": "*"
"build": { "build": {
"libArchive": false "libArchive": false
} }

@ -1,5 +1,5 @@
name=ezTime name=ezTime
version=0.0.2 version=0.6.0
author=Rop Gonggrijp author=Rop Gonggrijp
maintainer=Rop Gonggrijp maintainer=Rop Gonggrijp
sentence=Does NTP, datetime formatted strings, milliseconds and timezones. Drop-in replacement for Arduino Time Library sentence=Does NTP, datetime formatted strings, milliseconds and timezones. Drop-in replacement for Arduino Time Library

File diff suppressed because it is too large Load Diff

@ -1,25 +1,38 @@
#ifndef _EZTIME_H_ #ifndef _EZTIME_H_
#ifdef __cplusplus
#define _EZTIME_H_ #define _EZTIME_H_
// Whether or not to be compatible with Paul Stoffregen's Arduino Time Library.
// (which makes a lot of functions available in your root namespace, additionally to through the
// objects created by ezTime.)
#define ARDUINO_TIMELIB_COMPATIBILITY #define ARDUINO_TIMELIB_COMPATIBILITY
// Compiles in NTP updating and timezoneapi.io fetching // Compiles in NTP updating and timezoneapi.io fetching
#define EZTIME_NETWORK_ENABLE #define EZTIME_NETWORK_ENABLE
// On espressif turn on NVS storage (with "Preferences") // Arduino Ethernet shields
#if defined (ESP32) // This syntax so we can add " || defined (SOMEOTHER" // #define EZTIME_ETHERNET
#define EZTIME_NVS_ENABLE
#endif // 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 ezTime.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
#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc #if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc
typedef unsigned long time_t; typedef unsigned long time_t;
#endif #endif
#include <inttypes.h> #include <inttypes.h>
#ifndef __AVR__ #ifndef __AVR__
#include <sys/types.h> // for __time_t_defined, but avr libc lacks sys/types.h #include <sys/types.h> // for __time_t_defined, but avr libc lacks sys/types.h
#endif #endif
#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc #if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc
@ -27,16 +40,20 @@ typedef unsigned long time_t;
#endif #endif
extern "C++" {
////////// Error handing ////////// Error handing
typedef enum { typedef enum {
NO_ERROR, NO_ERROR,
LAST_ERROR, // Pseudo error: replaced by last error LAST_ERROR, // Pseudo-error: replaced by last error
NO_NETWORK, NO_NETWORK,
TIMEOUT, TIMEOUT,
CONNECT_FAILED, CONNECT_FAILED,
DATA_NOT_FOUND, DATA_NOT_FOUND,
LOCKED_TO_UTC LOCKED_TO_UTC,
NO_CACHE_SET,
CACHE_TOO_SMALL
} ezError_t; } ezError_t;
typedef enum { typedef enum {
@ -46,6 +63,41 @@ typedef enum {
DEBUG DEBUG
} ezDebugLevel_t; } ezDebugLevel_t;
typedef enum {
LOCAL_TIME,
UTC_TIME
} ezLocalOrUTC_t;
#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 (ezTime._debug_level >= ERROR) Serial.print(args)
#define errln(args...) if (ezTime._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 (ezTime._debug_level >= ERROR) Serial.print(args)
#define errln(args...) if (ezTime._debug_level >= ERROR) Serial.println(args)
#define info(args...) if (ezTime._debug_level >= INFO) Serial.print(args)
#define infoln(args...) if (ezTime._debug_level >= INFO) Serial.println(args)
#define debug(args...) ""
#define debugln(args...) ""
#else // nothing specified compiles everything in.
#define err(args...) if (ezTime._debug_level >= ERROR) Serial.print(args)
#define errln(args...) if (ezTime._debug_level >= ERROR) Serial.println(args)
#define info(args...) if (ezTime._debug_level >= INFO) Serial.print(args)
#define infoln(args...) if (ezTime._debug_level >= INFO) Serial.println(args)
#define debug(args...) if (ezTime._debug_level >= DEBUG) Serial.print(args)
#define debugln(args...) if (ezTime._debug_level >= DEBUG) Serial.println(args)
#endif
//////////////////////// ////////////////////////
@ -69,115 +121,120 @@ typedef enum {
} timeStatus_t; } timeStatus_t;
typedef struct { typedef struct {
String std_tzname; uint8_t stdname_end; //Positions in _posix String. stdname_begin fixed at 0
String dst_tzname; uint8_t dstname_begin;
int32_t std_offset; uint8_t dstname_end;
int32_t dst_offset; bool is_dst;
time_t dst_start_in_local; int16_t offset; // offset from UTC in minutes
time_t dst_end_in_local; } tzTimeData_t;
time_t dst_start_in_utc;
time_t dst_end_in_utc;
} tzData_t;
#define TIME_NOW 0xFFFFFFFF #define TIME_NOW 0xFFFFFFFF
#define LAST_READ 0xFFFFFFFE #define LAST_READ 0xFFFFFFFE
#define NTP_PACKET_SIZE 48 #define NTP_PACKET_SIZE 48
#define NTP_LOCAL_PORT 2342 #define NTP_LOCAL_TIME_PORT 2342
#define NTP_SERVER "pool.ntp.org" #define NTP_SERVER "pool.ntp.org"
#define NTP_TIMEOUT 1500 // milliseconds #define NTP_TIMEOUT 1500 // milliseconds
#define NTP_INTERVAL 600 // default update interval in seconds #define NTP_INTERVAL 600 // default update interval in seconds
#define NTP_RETRY 3 // Retry after this many seconds on failed NTP #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_STALE_AFTER 3600 // If update due for this many seconds, set timeStatus to timeNeedsSync
#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
// Various date-time formats // Various date-time formats
#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 ATOM "Y-m-d\\TH:i:sP" #define ATOM "Y-m-d\\TH:i:sP"
#define COOKIE "l, d-M-Y H:i:s T" #define COOKIE "l, d-M-Y H:i:s T"
#define ISO8601 "Y-m-d\\TH:i:sO" #define ISO8601 "Y-m-d\\TH:i:sO"
#define RFC822 "D, d M y H:i:s O" #define RFC822 "D, d M y H:i:s O"
#define RFC850 "l, d-M-y H:i:s T" #define RFC850 COOKIE
#define RFC1036 "D, d M y H:i:s O" #define RFC1036 RFC822
#define RFC1123 "D, d M Y H:i:s O" #define RFC1123 RFC822
#define RFC2822 "D, d M Y H:i:s O" #define RFC2822 RFC822
#define RFC3339 "Y-m-d\\TH:i:sP" #define RFC3339 ATOM
#define RFC3339_EXT "Y-m-d\\TH:i:s.vP" #define RFC3339_EXT "Y-m-d\\TH:i:s.vP"
#define RSS "D, d M Y H:i:s O" #define RSS RFC822
#define W3C "Y-m-d\\TH:i:sP" #define W3C ATOM
#define DEFAULT_TIMEFORMAT RFC850 #define DEFAULT_TIMEFORMAT COOKIE
//
// E Z t i m e c l a s s
//
class EZtime { class EZtime {
friend class Timezone; friend class Timezone; // Allow methods from Timezone class to access private methods of this class
////////// Error handing ////////// Error handing
public: public:
ezError_t error(); // Returns ezError_t enumeration of last error, resets _last_error to OK static ezError_t error(); // Returns ezError_t enumeration of last error, resets _last_error to OK
String errorString(ezError_t err); // Human-readable form of last error. static String errorString(ezError_t err = LAST_ERROR); // Human-readable form of last error.
void debug(ezDebugLevel_t level); // Sets serial printing of debug info to specified ezDebugLevel_t enumeration static void debugLevel(ezDebugLevel_t level); // Sets serial printing of debug info to specified ezDebugLevel_t enumeration
private: private:
void error(ezError_t err); // Used to set an error static void error(ezError_t err); // Used to set an error
void debug(ezDebugLevel_t level, String str); // Used to print debug info static String debugLevelString(ezDebugLevel_t level); // Human-readable form of debug level.
void debugln(ezDebugLevel_t level, String str); // same, just adds \r\n static ezError_t _last_error;
String debugLevelString(ezDebugLevel_t level); // Human-readable form of debug level. static ezDebugLevel_t _debug_level;
ezError_t _last_error;
ezDebugLevel_t _debug_level;
/////////// ///////////
public: public:
EZtime(); EZtime();
timeStatus_t timeStatus(); static timeStatus_t timeStatus();
time_t now(); static time_t now(bool update_last_read = true);
void breakTime(time_t time, tmElements_t &tm); // break time_t into elements static void breakTime(time_t time, tmElements_t &tm); // break time_t into elements
time_t makeTime(tmElements_t &tm); // convert time elements into time_t static time_t makeTime(tmElements_t &tm); // convert time elements into time_t
time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, int16_t year); static time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, uint16_t year);
time_t makeUmpteenthTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t umpteenth, uint8_t wday, uint8_t month, int16_t year); static time_t makeUmpteenthTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t umpteenth, uint8_t wday, uint8_t month, uint16_t year);
time_t compileTime(String compile_date = __DATE__, String compile_time = __TIME__); static time_t compileTime(String compile_date = __DATE__, String compile_time = __TIME__);
String monthString(uint8_t month); static String monthString(uint8_t month);
String dayString(uint8_t day); static String dayString(uint8_t day);
static bool secondChanged();
static bool minuteChanged();
private: private:
tzData_t parsePosix(String posix, int16_t year); static time_t _last_sync_time, _last_read_t;
int32_t millisElapsed(unsigned long m); static uint32_t _last_sync_millis;
time_t _last_sync_time; static bool _ntp_enabled;
int32_t _fudge; static uint16_t _last_read_ms;
unsigned long _last_sync_millis; static timeStatus_t _time_status;
bool _debug_enabled, _ntp_enabled;
uint16_t _last_read_ms; #ifdef EZTIME_NETWORK_ENABLE
timeStatus_t _time_status;
public:
#ifdef EZTIME_NETWORK_ENABLE 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 updateNow();
public: static void setServer(String ntp_server = NTP_SERVER);
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 setInterval(uint16_t seconds = 0); // 0 = no NTP updates
void updateNow(); static bool waitForSync(uint16_t timeout = 0); // timeout in seconds
void setServer(String ntp_server = NTP_SERVER);
void setInterval(uint16_t seconds = 0); // 0 = no NTP updates private:
bool waitForSync(uint16_t timeout = 0); // timeout in seconds static void updateIfNeeded();
#ifdef ESP32 static uint16_t _update_interval; // in seconds
void clearCache(); static uint32_t _update_due;
#endif // ESP32 static uint16_t _update_backoff; // seconds to add to _update_due to retry NTP
static String _ntp_server;
private:
uint16_t _update_interval; // in seconds #endif // EZTIME_NETWORK_ENABLE
time_t _update_due;
uint16_t _update_backoff; // seconds to add to _update_due to retry NTP
uint16_t _ntp_local_port, _ntp_interval, _ntp_max_drift;
String _ntp_server;
#endif // EZTIME_NETWORK_ENABLE
////////// Free extras ... ////////// Free extras ...
public: public:
String urlEncode(String str); // Does what you think it does static String urlEncode(String str); // Does what you think it does
String zeropad(uint32_t number, uint8_t length); // Returns number as string of given length, zero-padded on left if needed static String zeropad(uint32_t number, uint8_t length); // Returns number as string of given length, zero-padded on left if needed
String getBetween(String &haystack, String before_needle, String after_needle = ""); // Returns what's between before_needle and after_needle in haystack, or "" if not found. Returns until end of string if after_needle is empty
bool timezoneAPI(String location, String &olsen, String &posix);
////////// //////////
}; };
extern EZtime ezTime; extern EZtime ezTime; // declares the "ezTime" instance of the "EZtime" class
//
// T i m e z o n e c l a s s
//
class Timezone { class Timezone {
@ -185,108 +242,123 @@ class Timezone {
Timezone(bool locked_to_UTC = false); Timezone(bool locked_to_UTC = false);
bool setPosix(String posix); bool setPosix(String posix);
String getPosix(); String getPosix();
time_t tzTime(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME, tzTimeData_t *tztd = NULL);
void setDefault(); void setDefault();
bool isDST_local(time_t t = TIME_NOW); bool isDST(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
bool isDST_UTC(time_t t = TIME_NOW); String getTimezoneName(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
bool isDST(); int16_t getOffset(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME);
String getTimezoneName(time_t t = TIME_NOW); time_t now();
int32_t getOffset(time_t t = TIME_NOW); void setTime(time_t t, uint16_t ms = 0);
time_t now(bool update_last_read = true);
void setTime(time_t t);
void setTime(const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr); void setTime(const uint8_t hr, const uint8_t min, const uint8_t sec, const uint8_t day, const uint8_t mnth, uint16_t yr);
String dateTime(String format = DEFAULT_TIMEFORMAT); // http://php.net/manual/en/function.date.php for conversion String dateTime(String format = DEFAULT_TIMEFORMAT);
String dateTime(time_t t, String format = DEFAULT_TIMEFORMAT); String dateTime(time_t t, String format = DEFAULT_TIMEFORMAT);
uint8_t hour(time_t t = TIME_NOW); // 0-23 uint8_t hour(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 0-23
uint8_t minute(time_t t = TIME_NOW); // 0-59 uint8_t minute(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 0-59
uint8_t second(time_t t = TIME_NOW); // 0-59 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); // 0-999 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); // 1-31 uint8_t day(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // 1-31
uint8_t weekday(time_t t = TIME_NOW); // Day of the week (1-7), Sunday is day 1 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); // 1-12 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); // four digit year uint16_t year(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // four digit year
bool secondChanged(); // Since last call to something that caused a time read, to avoid excessive calling of, eg, timeString uint16_t dayOfYear(time_t t = TIME_NOW, ezLocalOrUTC_t local_or_utc = LOCAL_TIME); // days from start of year, jan 1st = 0
bool minuteChanged(); 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
private: private:
time_t _readTime(time_t t); String _posix;
bool _locked_to_UTC; bool _locked_to_UTC;
tzData_t _tzdata;
String _posix, _olsen;
time_t _last_read_t;
#ifdef EZTIME_NETWORK_ENABLE #ifdef EZTIME_NETWORK_ENABLE
public: public:
bool setLocation(String location, bool force_lookup = false); bool setLocation(String location = "");
String getOlsen();
#ifdef EZTIME_CACHE_EEPROM
#endif // EZTIME_NETWORK_ENABLE public:
bool setCache(const int16_t address);
private:
int16_t _eeprom_address;
#endif
#ifdef EZTIME_CACHE_NVS
public:
bool setCache(const String name, const String key);
private:
String _nvs_name, _nvs_key;
#endif
#if defined(EZTIME_CACHE_EEPROM) || defined(EZTIME_CACHE_NVS)
public:
bool setCache();
void clearCache(bool delete_section = false);
String getOlsen();
private:
bool writeCache(const String &str);
bool readCache(String &olsen, String &posix, uint8_t &months_since_jan_2018);
uint8_t _cache_month;
#endif
#endif
}; };
extern Timezone UTC; extern Timezone UTC;
extern Timezone& defaultTZ; extern Timezone& defaultTZ;
#ifdef ARDUINO_TIMELIB_COMPATIBILITY #ifdef ARDUINO_TIMELIB_COMPATIBILITY
/*==============================================================================*/ /* Useful Constants */
/* Useful Constants */ #define SECS_PER_MIN (60UL)
#define SECS_PER_MIN (60UL) #define SECS_PER_HOUR (3600UL)
#define SECS_PER_HOUR (3600UL) #define DAYS_PER_WEEK (7UL)
#define DAYS_PER_WEEK (7UL) #define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK)
#define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK) #define SECS_PER_YEAR (SECS_PER_WEEK * 52UL)
#define SECS_PER_YEAR (SECS_PER_WEEK * 52UL) #define SECS_YR_2000 (946684800UL) // the time at the start of y2k
#define SECS_YR_2000 (946684800UL) // the time at the start of y2k
/* Useful Macros for getting elapsed time */
/* Useful Macros for getting elapsed time */ #define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN) #define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN) #define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#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 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 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
#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
// 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
// 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 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 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 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 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
#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)
/* Useful Macros for converting elapsed time to a time_t */ #define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR)
#define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN) #define daysToTime_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011
#define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR) #define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK)
#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 */);
time_t now(); uint8_t hour(time_t t /* = TIME_NOW */);
uint8_t second(time_t t /* = TIME_NOW */); uint8_t day(time_t t /* = TIME_NOW */);
uint8_t minute(time_t t /* = TIME_NOW */); uint8_t weekday(time_t t /* = TIME_NOW */);
uint8_t hour(time_t t /* = TIME_NOW */); uint8_t month(time_t t /* = TIME_NOW */);
uint8_t day(time_t t /* = TIME_NOW */); uint16_t year(time_t t /* = TIME_NOW */);
uint8_t weekday(time_t t /* = TIME_NOW */); uint8_t hourFormat12(time_t t /* = TIME_NOW */);
uint8_t month(time_t t /* = TIME_NOW */); bool isAM(time_t t /* = TIME_NOW */);
uint16_t year(time_t t /* = TIME_NOW */); bool isPM(time_t t /* = TIME_NOW */);
uint8_t hourFormat12(time_t t /* = TIME_NOW */); String monthStr(const uint8_t month);
bool isAM(time_t t /* = TIME_NOW */); String monthShortStr(const uint8_t month);
bool isPM(time_t t /* = TIME_NOW */); String dayStr(const uint8_t month);
String monthStr(const uint8_t month); String dayShortStr(const uint8_t month);
String monthShortStr(const uint8_t month); void setTime(time_t t);
String dayStr(const uint8_t month); 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);
String dayShortStr(const uint8_t month); void breakTime(time_t t, tmElements_t &tm);
void setTime(time_t t); time_t makeTime(tmElements_t &tm);
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); time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, int16_t year);
void breakTime(time_t t, tmElements_t &tm); timeStatus_t timeStatus();
time_t makeTime(tmElements_t &tm);
time_t makeTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, int16_t year);
timeStatus_t timeStatus();
#endif //ARDUINO_TIMELIB_COMPATIBILITY #endif //ARDUINO_TIMELIB_COMPATIBILITY
} // extern "C++"
#endif // __cplusplus
#endif //_EZTIME_H_ #endif //_EZTIME_H_
Loading…
Cancel
Save