The Time library adds timekeeping functionality to Arduino with or without external timekeeping hardware. It allows a sketch to get the time and date as: second, minute, hour, day, month and year. It also provides time as a standard C time_t so elapsed times can be easily calculated and time values shared across different platforms.
The code is derived from the earlier Playground DateTime library but is updated to provide an API that is more flexible and easier to use.
Additional documentation is included in a readme.txt file
Functional Overview
Functions for printing out string descriptions for day and month
monthStr ()Be aware that a parameter is required for this string functions. The argument is NOT the timestamp, e.g. time_t, but must be something like month(). So monthStr(month()) or dayStr(weekday()) will work.
See also the TimeSerialDateStrings .ino example sketch in the Time library
Functions for managing the timer services are:
There are many convenience macros in TimeLib.h for time constants and conversion of time units.
Using the Library Copy the download to the Library directory. The Time directory contains the Time library and some example sketches illustrating how the library can be used with various time sources:
- TimeSerial.ino 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 if it is running and connected to the Arduino serial port.
- TimeRTC uses a DS1307 Real Time Clock (or DS3231 ChronoDot RTC*) to
provide time synchronization. To run this sketch the DS1307RTC library must be installed. * The I2C ' DS3231' interface is very straightforward and virtually identical to the register addresses of the popular DS1337 and DS1307 RTCs, which means that existing code for the Arduino, Basic Stamp, Cubloc, and other controllers should work with no modification. For reference purposes see >> https://www.adafruit.com/products/255
- 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: https://bitbucket.org/bjoern/arduino_osc/src/tip/libraries/Ethernet/ Note: This is out of date. For Arduino 0022, see the built in Udp NTP example, and also see http://arduino.cc/en/Tutorial/UdpNtpClient .
-TimeGPS gets time from a GPS
This requires the TinyGPS library from Mikal Hart: http://arduiniana.org/libraries/TinyGPS
The test sketch uses a message on the serial port to set the time. A Processing sketch that sends these messages is included in the download but you can test this sketch by sending T1262347200 using the serial monitor (this sets the time to noon on Jan 1 2010). On a Unix system, you can set the time with the shell command:
-- edit: The above code did not work for me in my Linux terminal, I wrote the below script which worked. ( I was using zsh and UnoR3)
/*F******************************************************************** * **********************************************************************/ #include <Time.h> //************************* DEFINES ************************************ #define BAUD 9600 //************************* PROTOTYPES ************************************ //************************* VARIABLES ************************************ #define TIMMSGLEN 11 // TIME SYNC TO PC: HDR FOLLOWED BY Unix TIME_T TEN DIGITS #define TIME_HEADER 'T' // HEADER TAG FOR SERIAL TIME SYNC MESSAGE #define TIME_REQUEST 7 // ASCII BELL CHARACTER REQUESTS A TIME SYNC MESSAGE // T1262347200 //noon Jan 1 2010 /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( BAUD ); } /*F******************************************************************** * **********************************************************************/ void loop() { if( Serial.available() ) processSyncMessage(); if( timeStatus() == timeNotSet) Serial.println( "waiting for sync message" ); else digitalClockDisplay(); delay( 1000 ); } /*F******************************************************************** * digital clock display of the time **********************************************************************/ void digitalClockDisplay() { Serial.print( hour() ); printDigits( minute() ); printDigits( second() ); Serial.print( " " ); Serial.print( day() ); Serial.print( " " ); Serial.print( month() ); Serial.print( " " ); Serial.print( year() ); Serial.println(); } /*F******************************************************************** * utility function **********************************************************************/ void printDigits( int digits ) { // for digital clock display: prints preceding colon and leading 0 Serial.print( ":" ); if( digits < 10 ) Serial.print('0' ); Serial.print( digits ); } /*F******************************************************************** * **********************************************************************/ void processSyncMessage() { // IF TIME SYNC AVAILABLE FROM SERIAL PORT, UPDATE TIME AND RETURN TRUE while( Serial.available() >= TIMMSGLEN ) { // TIME MESSAGE CONSISTS OF HEADEr & 10 ASCII DIGITS char c = Serial.read(); Serial.print( c); if( c == TIME_HEADER ) { time_t pctime = 0; for( int i = 0; i < TIMMSGLEN - 1; i++ ) { c = Serial.read(); if( c >= '0' && c <= '9') { // convert digits to a number pctime = (10 * pctime) + (c - '0' ); } } // Sync Arduino clock to the time received on the serial port setTime( pctime ); } } }
Here is a fragment from the TimeNTP.ino example sketch showing how the syncProvider functionality simplifies the sketch code. This sketch gets time from an Internet time provider (NTP) using the Arduino Ethernet shield. Note that the loop code does not require any logic to maintain time sync. The Time library will automatically monitor NTP and sync the time as necessary.
/*H******************************************************************** * **********************************************************************/ //************************* DEFINES ************************************ //************************* PROTOTYPES ************************************ void digitalClockDisplay(); void printDigits( int digits ); //************************* VARIABLES ************************************ /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( 9600 ); Ethernet.begin( mac, ip, gateway ); Serial.println( "waiting for sync" ); setSyncProvider( getNtpTime ); while( timeStatus() == timeNotSet ) ; // wait until time is set by sync provider } /*F******************************************************************** * **********************************************************************/ void loop() { if( now() != prevDisplay ) // update display only if time has changed { prevDisplay = now(); digitalClockDisplay(); } } /*F******************************************************************** * digital clock display of time **********************************************************************/ void digitalClockDisplay() { Serial.print( hour() ); printDigits( minute()); printDigits( second() ); Serial.print( " " ); Serial.print( day() ); Serial.print( " " ); Serial.print( month() ); Serial.print( " " ); Serial.print( year() ); Serial.println(); } /*F******************************************************************** * digital clock display: prints preceding colon and leading 0 **********************************************************************/ void printDigits( int digits ) { Serial.print( ":" ); if( digits < 10 ) Serial.print( '0' ); Serial.print( digits ); }
The macro to convert days to time_t was corrected as of July 22 2011