I own an Adafruit Data Logger Shield https://www.adafruit.com/product/1141

I followed this tutorial https://learn.adafruit.com/adafruit-data-logger-shield/using-the-real-time-clock

This part of code

if (! rtc.initialized()) {
  Serial.println("RTC is NOT running!");
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

adjust RTC to computer date/time... but it's using computer local date/time.

I'd prefer to use UTC date/time but I haven't find a convenient way to do this (except modifying computer date/time which is not very convenient as I want to always be able to reprogram Arduino without hassle of changing local time each time).

  • F(__DATE__), F(__TIME__) gives compile time local date/time. There will be a few seconds error as the sketch must be uploaded and run to set the clock. Given this I would suggest that you modify the sketch by reading back the setting and adjust to UTC plus the seconds error. May 20 '17 at 15:45
  • Problem here is that I don't want to adjust my computer to UTC when I'm programming and to local otherwise. It should be possible to have a function on computer side to return UTC time instead of local time.
    – scls
    May 20 '17 at 15:52
  • You are getting a string from the compiler. Modify it to UTC before setting the RTC (in the sketch). May 20 '17 at 15:54
  • Do you know where __DATE__ and __TIME__ macros are defined?
    – scls
    May 20 '17 at 16:32

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". Eg, to create the file in Linux: echo "char *MyUTCtime = \"$(date -u '+%H:%M:%S')\";" > myUTCtime.h which on my system produced file myUTCtime.h containing char *MyUTCtime = "08:20:36";

• 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 += -DMyUTCtime=${MyUTCtime}

• 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__)));


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.

  • I like the second bullet approach but I don't know where I can provide this option flag -D within Arduino IDE. I'm not even sure that's possible forum.arduino.cc/index.php?topic=48693.0
    – scls
    May 21 '17 at 6:25
  • @scls, in Arduino IDE, some compiler flags can be supplied via boards.txt; so, it may be possible but is clumsy and difficult if so. Note, in Edit 1 of the answer I replaced first bullet with a simple approach using an include file, and in bullet 2 made some comments about entering stuff like -DMyUTCtime="23:59:01" as a compiler flag when using a makefile. May 21 '17 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.