I have a new library for date and time that is derived from the playground DateTime code but has a different API that is intended to be more flexible and easier to use.
The new API is more consistent with Arduino function style. It also facilitates date and time code that can be used with a variety of distinctly different external time sources with minimum change to sketch logic.
Example sketches illustrate how similar sketch logic can be used with each of the following time sources: a Real Time Clock, internet NTP time service, GPS time data, and Serial time messages from a computer for time synchronization.
This is prototype code intended as proof of concept and has had limited testing.
Feedback on the API and implementation is welcome.
The download is here
Some of the functions available in the library include:
hour(); // the hour now (0-23)
minute(); // the minute now (0-59)
second(); // the second now (0-59)
day(); // the day now (1-31)
weekday(); // day of the week, Sunday is day 0
month(); // the month now (1-12)
year(); // the full four digit year: (2009, 2010 etc)
there are also functions to return the hour in 12 hour format
hour_12(); // the hour now in 12 hour format
AM_PM(); // returns false if AM, true if PM
now(); // returns the current time as seconds since Jan 1 1970
The time and date functions can take an optional parameter for the time:
time_t t = now(); // store the current time in time variable t
hour(t); // returns the hour for the given time t
Storing a snapshot of the time in a variable and using this to get time elements avoids errors if the time rolls to a new second between accessing elements.
Functions for managing the timer services are:
setTime(t); // set the system time to the give time t
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:
timeNotSet // the time has never been set, the clock started at Jan 1 1970
timeNeedsSync // the time had been set but a sync attempt did not succeed
timeSet // the time is set and is synced
Time is not valid if the status is timeNotSet. Otherwise, time can be used but may
have drifted if the status is timeNeedsSync.
There are functions to set the external time source and interval between automatic time synchronization:
setSyncProvider(getTimeFunction); // set the external time provider
setSyncInterval(interval); // set the number of seconds between re-sync
See the readme.txt file in the download for more details.