Skip to main content
2 of 3
edit 1

There are several approaches you could use.

Edit 1: On your development system, you could write a shell script or a batch file to write a file, say myUTCtime.h, containing a source-code line like char *MyUTCtime = "23:59:01"; and somewhere in your sketch say #include "myUTCtime.h"

• You could define a different time constant, for example by adding -DMyUTCtime="23:59:01" into the gcc or g++ command line. (Substitute an appropriate time.) On Linux, this might look like -DMyUTCtime=$(date -u '+%H:%M:%S'). In your code you would write #MyUTCtime in place of __TIME__. Edit 1: Note, this is difficult to automate when using the Arduino IDE. It is somewhat easier if using an Arduino Makefile. For example, on Linux one might say MyUTCtime=$(date -u '+%H:%M:%S') make (which defines MyUTCtime with the current UTC time, and passes the MyUTCtime into make) and within the makefile, say (eg)

ifdef MyUTCtime
  CPPFLAGS += -D${MyUTCtime}
endif

• You could write your sketch to accept a time-setting command via serial input. To set the time, you would send the time-setting command and the current time in UTC, via serial monitor.

• As Mikael Patel mentioned, you could modify the __TIME__ string to represent UTC before using it to set the RTC. Here is an example of how to add or subtract an appropriate offset to __TIME__ to adjust it to UTC. For example, if your offset is an hour ahead of UTC, you would change

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

to

char *compileTime = __TIME__;
compileTime[1] += 1;
if (compileTime[1] > '9')   // Check for carry
   ++compileTime[0];        // Add the carry
rtc.adjust(DateTime(F(__DATE__), compileTime));

This code has at least two problems:
• If the compile occurs between 23:00:00 and midnight, the resulting hour will show as 24 instead of as 00 on the next day. One could of course adjust the day of the date, and if that overflows, adjust the month, and if that overflows, adjust the year; or, more simply, one could produce an error in the compile.
• While a 1-hour offset may be appropriate during DST, a 2-hour offset may be appropriate most of the year. One could of course check the date and use a date-appropriate offset of 1 or 2.