Files
ArduinoTime/Readme.md

166 lines
7.0 KiB
Markdown
Raw Permalink Normal View History

2017-03-20 22:40:51 +01:00
# Arduino Time Library
Time is a library that provides timekeeping functionality for Arduino.
Using the Arduino Library Manager, install "*Time* by *Michael Margolis*".
The code is derived from the Playground DateTime library but is updated
2017-04-15 15:37:32 +01:00
to provide an API that is more flexible and easier to use.
A primary goal was to enable date and time functionality that can be used with
a variety of external time sources with minimum differences required in sketch logic.
Example sketches illustrate how similar sketch code can be used with: a Real Time Clock,
internet NTP time service, GPS time data, and Serial time messages from a computer
for time synchronization.
2017-03-20 22:40:51 +01:00
## Functionality
To use the Time library in an Arduino sketch, include TimeLib.h.
```c
#include <TimeLib.h>
```
2017-03-20 22:40:51 +01:00
The functions available in the library include
```c
hour(); // the hour now (0-23)
2016-11-09 06:28:11 -08:00
minute(); // the minute now (0-59)
second(); // the second now (0-59)
day(); // the day now (1-31)
2016-11-09 06:28:11 -08:00
weekday(); // day of the week (1-7), Sunday is day 1
month(); // the month now (1-12)
2016-11-09 06:28:11 -08:00
year(); // the full four digit year: (2009, 2010 etc)
2017-03-20 22:40:51 +01:00
```
2017-04-15 15:37:32 +01:00
there are also functions to return the hour in 12-hour format
2017-03-20 22:40:51 +01:00
```c
hourFormat12(); // the hour now in 12 hour format
2016-11-09 06:28:11 -08:00
isAM(); // returns true if time now is AM
isPM(); // returns true if time now is PM
2016-11-09 06:28:11 -08:00
now(); // returns the current time as seconds since Jan 1 1970
2017-03-20 22:40:51 +01:00
```
The time and date functions can take an optional parameter for the time. This prevents
errors if the time rolls over between elements. For example, if a new minute begins
2016-11-09 06:28:11 -08:00
between getting the minute and second, the values will be inconsistent. Using the
2017-04-15 15:37:32 +01:00
following functions eliminates this problem
2016-11-09 06:28:11 -08:00
2017-03-20 22:40:51 +01:00
```c
time_t t = now(); // store the current time in time variable t
hour(t); // returns the hour for the given time t
minute(t); // returns the minute for the given time t
second(t); // returns the second for the given time t
day(t); // the day for the given time t
weekday(t); // day of the week for the given time t
month(t); // the month for the given time t
year(t); // the year for the given time t
```
2016-11-09 06:28:11 -08:00
Functions for managing the timer services are:
2017-03-20 22:40:51 +01:00
```c
setTime(t); // set the system time to the give time t
setTime(hr,min,sec,day,mnth,yr); // alternative to above, yr is 2 or 4 digit yr
// (2010 or 10 sets year to 2010)
adjustTime(adjustment); // adjust system time by adding the adjustment value
timeStatus(); // indicates if time has been set and recently synchronized
// returns one of the following enumerations:
2017-04-15 15:37:32 +01:00
timeNotSet // the time has never been set, the clock started on Jan 1, 1970
2017-03-20 22:40:51 +01:00
timeNeedsSync // the time had been set but a sync attempt did not succeed
timeSet // the time is set and is synced
```
2016-11-09 06:28:11 -08:00
Time and Date values are not valid if the status is `timeNotSet`. Otherwise, values can be used but
the returned time may have drifted if the status is `timeNeedsSync`.
2017-03-20 22:40:51 +01:00
```c
setSyncProvider(getTimeFunction); // set the external time provider
setSyncInterval(interval); // set the number of seconds between re-sync
```
2017-03-20 22:40:51 +01:00
There are many convenience macros in the `time.h` file for time constants and conversion
2016-11-09 06:28:11 -08:00
of time units.
To use the library, copy the download to the Library directory.
2017-03-20 22:40:51 +01:00
## Examples
The Time directory contains the Time library and some example sketches
illustrating how the library can be used with various time sources:
2017-03-20 22:40:51 +01:00
- `TimeSerial.pde` shows Arduino as a clock without external hardware.
It is synchronized by time messages sent over the serial port.
A companion Processing sketch will automatically provide these messages
2016-11-09 06:28:11 -08:00
if it is running and connected to the Arduino serial port.
- `TimeSerialDateStrings.pde` adds day and month name strings to the sketch above.
2019-01-28 10:47:34 +07:00
Short (3 characters) and long strings are available to print the days of
2016-11-09 06:28:11 -08:00
the week and names of the months.
2019-01-28 10:47:34 +07:00
- `TimeRTC` uses a DS1307 real-time clock to provide time synchronization.
The basic [DS1307RTC library][1] must be downloaded and installed,
in order to run this sketch.
- `TimeRTCSet` is similar to the above and adds the ability to set the Real Time Clock.
2017-03-20 22:40:51 +01:00
- `TimeRTCLog` demonstrates how to calculate the difference between times.
2017-04-15 15:37:32 +01:00
It is a very simple logger application that monitors events on digital pins
2016-11-09 06:28:11 -08:00
and prints (to the serial port) the time of an event and the time period since
the previous event.
2017-03-20 22:40:51 +01:00
- `TimeNTP` uses the Arduino Ethernet shield to access time using the internet NTP time service.
The NTP protocol uses UDP and the UdpBytewise library is required, see:
<http://bitbucket.org/bjoern/arduino_osc/src/14667490521f/libraries/Ethernet/>
- `TimeGPS` gets time from a GPS.
This requires the TinyGPS library from Mikal Hart:
<http://arduiniana.org/libraries/TinyGPS>
2017-03-20 22:40:51 +01:00
## Differences
Differences between this code and the playground DateTime library
although the Time library is based on the DateTime codebase, the API has changed.
Changes in the Time library API:
2017-03-20 22:40:51 +01:00
- time elements are functions returning `int` (they are variables in DateTime)
2016-11-09 06:28:11 -08:00
- Years start from 1970
- days of the week and months start from 1 (they start from 0 in DateTime)
2017-04-15 15:37:32 +01:00
- DateStrings do not require a separate library
- time elements can be accessed non-atomically (in DateTime they are always atomic)
2017-04-15 15:37:32 +01:00
- function added to automatically sync time with external source
2017-03-20 22:40:51 +01:00
- `localTime` and `maketime` parameters changed, `localTime` renamed to `breakTime`
2016-11-09 06:28:11 -08:00
## Technical Notes
2017-03-20 22:40:51 +01:00
Internal system time is based on the standard Unix `time_t`.
2017-04-15 15:37:32 +01:00
The value is the number of seconds since Jan 1, 1970.
System time begins at zero when the sketch starts.
2016-11-09 06:28:11 -08:00
The internal time can be automatically synchronized at regular intervals to an external time source.
2017-03-20 22:40:51 +01:00
This is enabled by calling the `setSyncProvider(provider)` function - the provider argument is
the address of a function that returns the current time as a `time_t`.
See the sketches in the examples directory for usage.
2016-11-09 06:28:11 -08:00
The default interval for re-syncing the time is 5 minutes but can be changed by calling the
2017-03-20 22:40:51 +01:00
`setSyncInterval(interval)` method to set the number of seconds between re-sync attempts.
The Time library defines a structure for holding time elements that is a compact version of the C `tm` structure.
All the members of the Arduino `tm` structure are bytes and the year is offset from 1970.
Convenience macros provide conversion to and from the Arduino format.
2020-02-10 08:28:16 +01:00
Low-level functions to convert between system time and individual time elements are provided:
2017-03-20 22:40:51 +01:00
```c
breakTime(time, &tm); // break time_t into elements stored in tm struct
makeTime(&tm); // return time_t from elements stored in tm struct
2017-03-20 22:40:51 +01:00
```
This [DS1307RTC library][1] provides an example of how a time provider
2017-04-15 15:37:32 +01:00
can use the low-level functions to interface with the Time library.
[1]:<https://github.com/PaulStoffregen/DS1307RTC>