Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days.
0

Resetting RTC Time is offset by 8-ish hours
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Resetting RTC Time is offset by 8-ish hours

by adree08 on Wed Dec 08, 2021 8:39 pm

Hey there, I'm trying to get the Adafruit PCF8523 to tell me the accurate time.

I'm using the included sketch to reset the time of it. Here is the snipped that I'm using



Code: Select all | TOGGLE FULL SIZE
void setup() {
 ...
  initializeRTC();
  ...
}


void initializeRTC() {
  //detect if Real Time Clock (RTC) is connected
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while(1) delay(10);
  }

  Serial.println("RESET RTC to now");
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  if (! rtc.initialized() || rtc.lostPower()) {
    Serial.println("RTC is NOT initialized, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  }


  rtc.start();
}



this does *set* the time, but it appears to be off by about 8 hours?

Using https://www.unixtimestamp.com/index.php I examined what the timestamp was coming out of there versus the timestamp coming off of the logging for the Arduino

unixtimestamp.com: 1639008937
arduino: 1638981100

that's a difference of 27,837 seconds or 7.7325 hours

This result seems #1 arbitrary and #2 close to my UTC offset (GMT-8)? But since it isn't exact, I'm thinking that's a red herring.

adree08
 
Posts: 3
Joined: Tue Dec 07, 2021 6:50 pm

Re: Resetting RTC Time is offset by 8-ish hours

by sj_remington on Wed Dec 08, 2021 8:48 pm

What do you see when you add this line to the code that sets the RTC?

Code: Select all | TOGGLE FULL SIZE
  Serial.println(F(__TIME__));

Are you using Windows or Linux for the IDE? (The internal clock is maintained differently in the two OS).

sj_remington
 
Posts: 242
Joined: Mon Jul 27, 2020 4:51 pm

Re: Resetting RTC Time is offset by 8-ish hours

by adree08 on Wed Dec 08, 2021 11:23 pm

That results in 19:20:29 which makes sense since it's 7:20PM right now :/

I'm on a Windows device

adree08
 
Posts: 3
Joined: Tue Dec 07, 2021 6:50 pm

Re: Resetting RTC Time is offset by 8-ish hours

by sj_remington on Wed Dec 08, 2021 11:34 pm

If you want to compare RTC local time with UTC (unix timestamp), take the time zone into account, plus the lag introduced by compiling, linking and uploading the Arduino RTC time set code.

sj_remington
 
Posts: 242
Joined: Mon Jul 27, 2020 4:51 pm

Re: Resetting RTC Time is offset by 8-ish hours

by adree08 on Thu Dec 09, 2021 2:19 pm

But... that doesn't make sense- the whole point of a Unix timestamp is that timezone information is applied AFTER. The idea is that there is a singular "world clock" that we offset for in our locales.

If what you're saying is correct, that means I need to #1 KNOW my timezone when booting up Arduino and #2 that the output of __TIME__ already does this.

adree08
 
Posts: 3
Joined: Tue Dec 07, 2021 6:50 pm

Re: Resetting RTC Time is offset by 8-ish hours

by sj_remington on Thu Dec 09, 2021 10:42 pm

that means I need to #1 KNOW my timezone when booting up Arduino

Yes, of course. In any case, the __TIME__ shortcut does not work very well to set the RTC.

Many people use the TimeLib library to keep the clock synchronized to an external reference, which incidentally also makes dealing with time zone offsets and DST changes, etc. quite trivial.

sj_remington
 
Posts: 242
Joined: Mon Jul 27, 2020 4:51 pm

Please be positive and constructive with your questions and comments.