Time.cpp
#include <Time.h>
refreshCache hour( now ) hour( time_t ) hourFormat12( now )
hourFormat12( time_t ) isAM( now ) isAM( time_t ) isPM( now )
isPM( time_t ) minute( now ) minute( time_t ) second( now )
second( time_t ) day( now ) day( time_t ) weekday( now )
weekday( time_t ) month( now ) month( time_t ) year( now )
year( time_t )
ConversionFunctions
breakTime makeTime
Low Level Time Functions
now setTime( time_t ) setTime( ints ) adjustTime
timeStatus setSyncProvider setSyncInterval
/*H*******************************************************
time.c - low level time and date functions
Copyright (c) Michael Margolis 2009-2014
This library is free software; you can redistribute it and/or
modify it under terms of GNU Lesser General Public
License as published by Free Software Foundation; either
version 2.1 of License, or (at your option) any later version.
This library is distributed in hope that it will be useful,
but WITHOUT ANY WARRANTY; without even implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See GNU
Lesser General Public License for more details.
You should have received a copy of GNU Lesser General Public
License along with this library; if not, write to Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1.0  6  Jan 2010 - initial release
1.1  12 Feb 2010 - fixed leap year calculation error
1.2  1  Nov 2010 - fixed setTime bug (thanks to Korman for this)
1.3  24 Mar 2012 - many edits by Paul Stoffregen: fixed timeStatus() to update
             status, updated examples for Arduino 1.0, fixed ARM
             compatibility issues, added TimeArduinoDue and TimeTeensy3
             examples, add error checking and messages to RTC examples,
             add examples to DS1307RTC library.
1.4  5  Sep 2014 - compatibility with Arduino 1.5.7
********************************************************/
#if ARDUINO >= 100
#include  
#else
#include  
#endif
#include "TimeLib.h"

static tmElements_t tm;          // a cache of time elements
static time_t cacheTime;   // time cache was updated
static uint32_t syncInterval = 300;  // time sync will be attempted after this many seconds


/*F******************************************************************** * **********************************************************************/ void refreshCache( time_t t ) { if( t != cacheTime ) { breakTime( t, tm ); cacheTime = t; } }
/*F******************************************************************** * **********************************************************************/ int hour() { // hour now return( hour( now() ) ); }
/*F******************************************************************** * **********************************************************************/ int hour( time_t t ) { // hour for given time refreshCache( t ); return( tm.Hour ); }
/*F******************************************************************** * HOUR NOW IN 12 HOUR FORMAT **********************************************************************/ int hourFormat12() { return( hourFormat12( now()) ); }
/*F******************************************************************** * **********************************************************************/ int hourFormat12( time_t t) { // HOUR FOR GIVEN TIME IN 12 HOUR FORMAT refreshCache(t); if( tm.Hour == 0 ) return( 12 ); // 12 midnight else if( tm.Hour > 12) return( tm.Hour - 12 ); else return( tm.Hour ); }
/*F******************************************************************** * **********************************************************************/ uint8_t isAM() { // RETURNS TRUE IF TIME NOW IS AM return( !isPM( now()) ); }
/*F******************************************************************** * **********************************************************************/ uint8_t isAM( time_t t) { // RETURNS TRUE IF GIVEN TIME IS AM return( !isPM( t ) ); }
/*F******************************************************************** * **********************************************************************/ uint8_t isPM() { // RETURNS TRUE IF PM return( isPM( now() )); }
/*F******************************************************************** * **********************************************************************/ uint8_t isPM( time_t t ) { // RETURNS TRUE IF PM return (hour(t) >= 12); }
/*F******************************************************************** * **********************************************************************/ int minute() { return( minute(now()) ); }
/*F******************************************************************** * **********************************************************************/ int minute( time_t t ) { // MINUTE FOR GIVEN TIME refreshCache( t ); return( tm.Minute ); }
/*F******************************************************************** * **********************************************************************/ int second() { return( second(now()) ); }
/*F******************************************************************** * **********************************************************************/ int second( time_t t ) { // SECOND FOR GIVEN TIME RefreshCache( t ); return( tm.Second ); }
/*F******************************************************************** * **********************************************************************/ int day() { return( day( now())); }
/*F******************************************************************** * **********************************************************************/ int day( time_t t ) { // DAY FOR GIVEN TIME (0-6) refreshCache( t ); return( tm.Day ); }
/*F******************************************************************** * **********************************************************************/ int weekday() { // Sunday is day 1 return( weekday( now())); }
/*F******************************************************************** * **********************************************************************/ int weekday( time_t t ) { refreshCache( t ); return( tm.Wday ); }
/*F******************************************************************** * **********************************************************************/ int month() { return month(now()); }
/*F******************************************************************** * **********************************************************************/ int month( time_t t ) { // MONTH FOR GIVEN TIME refreshCache( t ); return( tm.Month ); }
/*F******************************************************************** * **********************************************************************/ int year() { // AS IN pROCESSING, FULL FOUR DIGIT YEAR: (2009, 2010 ETC) return year( now()); }
/*F******************************************************************** * **********************************************************************/ int year( time_t t ) { // YEAR FOR GIVEN TIME refreshCache( t ); return tmYearToCalendar( tm.Year ); }
/*H******************************************************* functions to convert to and from system time These are for interfacing with time services and are not normally needed in a sketch leap year calculator expects year argument as years offset from 1970 *******************************************************/ #define LEAP_YEAR( Y ) ( ((1970+(Y))>0) && !((1970+(Y))%4) && ( ((1970+(Y))%100) || !((1970+(Y))%400) ) ) static 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
/*F******************************************************************** * break given time_t into time components this is a more compact version of C library localtime function note that year is offset from 1970 !!! **********************************************************************/ void breakTime( time_t timeInput, tmElements_t &tm) { uint8_t year; uint8_t month, monthLength; uint32_t time; unsigned long days; time = (uint32_t)timeInput; tm.Second = time % 60; time /= 60; // NOW IT IS MINUTES tm.Minute = time % 60; time /= 60; // NOW IT IS HOURS tm.Hour = time % 24; time /= 24; // NOW IT IS DAYS tm.Wday = ((time + 4) % 7) + 1; // SUNDAY IS DAY 1 year = 0; days = 0; while( (unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) year++; tm.Year = year; // YEAR IS OFFSET FROM 1970 days -= LEAP_YEAR(year) ? 366 : 365; time -= days; // NOW IT IS DAYS IN THIS YEAR, STARTING AT 0 days =0; month =0; monthLength =0; for( month=0; month<12; month++) { if( month == 1 ) { // FEBRUARY if( LEAP_YEAR(year)) monthLength=29; else monthLength=28; } else monthLength = monthDays[month]; if( time >= monthLength ) time -= monthLength; else break; } tm.Month = month + 1; // JAN IS MONTH 1 tm.Day = time + 1; // DAY OF MONTH }
/*F******************************************************************** * 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 **********************************************************************/ time_t makeTime( const tmElements_t &tm ) { int i; uint32_t seconds; // SECONDS FROM 1970 TILL 1 JAN 00:00:00 OF GIVEN YEAR seconds= tm.Year * ( SECS_PER_DAY * 365 ); for( i = 0; i < tm.Year; i++) { if( LEAP_YEAR(i)) seconds += SECS_PER_DAY; // ADD EXTRA DAYS FOR LEAP YEARS } // add days for this year, months start from 1 for( i = 1; i < tm.Month; i++) { if( (i == 2) && LEAP_YEAR(tm.Year)) seconds += SECS_PER_DAY * 29; else seconds += SECS_PER_DAY * monthDays[i-1];// MONTHdAY ARRAY[0] == 0 } seconds+= (tm.Day -1) * SECS_PER_DAY; seconds+= tm.Hour * SECS_PER_HOUR; seconds+= tm.Minute * SECS_PER_MIN; seconds+= tm.Second; return( (time_t)seconds ); }
/*H******************************************************* Low level system time functions ********************************************************/ static uint32_t sysTime = 0; static uint32_t prevMillis = 0; static uint32_t nextSyncTime = 0; static timeStatus_t Status = timeNotSet; getExternalTime getTimePtr; // pointer to external sync function //setExternalTime setTimePtr; // not used in this version #ifdef TIME_DRIFT_INFO // define this to get drift data time_t sysUnsyncedTime = 0; // time sysTime unadjusted by sync #endif
/*F******************************************************************** * CALCULATE NUMBER OF SECONDS PASSED SINCE LAST CALL TO now() **********************************************************************/ time_t now() { while( millis() - prevMillis >= 1000) { // millis() AND prevMillis BOTH UINTS THUS SUBTRACTION WILL ALWAYS BE ABSOLUTE VALUE OF DIFFERENCE sysTime++; prevMillis += 1000; #ifdef TIME_DRIFT_INFO sysUnsyncedTime++; // THIS CAN BE COMPARED TO SYNCED TIME TO MEASURE LONG TERM DRIFT #endif } if( nextSyncTime <= sysTime ) { if( getTimePtr != 0) { time_t t = getTimePtr(); if( t != 0) setTime(t); else { nextSyncTime = sysTime + syncInterval; Status = (Status == timeNotSet) ? timeNotSet : timeNeedsSync; } } } return( (time_t)sysTime ); }
/*F******************************************************************** * **********************************************************************/ void setTime( time_t t ) { #ifdef TIME_DRIFT_INFO if( sysUnsyncedTime == 0) sysUnsyncedTime = t; // STORE TIME OF FIRST CALL TO SET A VALID TIME #endif sysTime = (uint32_t)t; nextSyncTime = (uint32_t)t + syncInterval; Status = timeSet; prevMillis = millis(); // RESTART COUNTING FROM NOW (THANKS TO KORMAN FOR THIS FIX) }
/*F******************************************************************** * YEAR CAN BE GIVEN AS FULL FOUR DIGIT YEAR OR TWO DIGTS (2010 OR 10 FOR 2010); IT IS CONVERTED TO YEARS SINCE 1970 **********************************************************************/ void setTime( int hr,int min,int sec,int dy, int mnth, int yr ) { if( yr > 99) yr = yr - 1970; else yr += 30; tm.Year = yr; tm.Month = mnth; tm.Day = dy; tm.Hour = hr; tm.Minute = min; tm.Second = sec; setTime( makeTime( tm ) ); }
/*F******************************************************************** * **********************************************************************/ void adjustTime( long adjustment) { sysTime += adjustment; }
/*F******************************************************************** * INDICATES IF TIME HAS BEEN SET AND RECENTLY SYNCHRONIZED **********************************************************************/ timeStatus_t timeStatus() { now(); // REQUIRED TO ACTUALLY UPDATE STATUS return Status; }
/*F******************************************************************** * **********************************************************************/ void setSyncProvider( getExternalTime getTimeFunction ) { getTimePtr = getTimeFunction; nextSyncTime = sysTime; now(); // THIS WILL SYNC CLOCK }
/*F******************************************************************** * SET NUMBER OF SECONDS BETWEEN RE-SYNC **********************************************************************/ void setSyncInterval( time_t interval ) { syncInterval = (uint32_t)interval; nextSyncTime = sysTime + syncInterval; }