Perhaps this example will demonstrate the use of DATE _TIME macros to set the system clock.
//Software Clock: set to compile/upload time
//with power cycle, reset, or new serial monitor goes to original compile time
//AVR Macro Strings converted to tmElements_t using functions
//getDate() and getTime() functions taken from ds1307 library
#include <TimeLib.h>
tmElements_t tm;
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
void setup(){
Serial.begin (115200);
Serial.println(__DATE__);
Serial.println(__TIME__);
if (getDate(__DATE__) && getTime(__TIME__)) {
Serial.println("AVR Macro strings converted to tmElements.");
}
setTime(makeTime(tm));//set Ardino system clock to compiled time
Serial.println("System millis clock referenced to tmElements.");
Serial.println();
}
void loop() {
Serial.print("Ok, Time = ");
digitalClockDisplay();
delay(2000);
}
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(month());
Serial.print('/');
Serial.print(day());
Serial.print('/');
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}