is this a bug? or am I doing something wrong?

VioBiscu
Posts: 1
Joined: Wed Aug 19, 2020 11:12 am

is this a bug? or am I doing something wrong?

Postby VioBiscu » Wed Aug 19, 2020 11:46 am

  1. void app_main(){
  2.     struct tm  tsEpoch;
  3.     struct tm  tsRTC;
  4.     struct timeval now;
  5.     struct timeval newt;
  6.     char LineClock[20] ={""};
  7.  
  8.     int flag = 1;
  9.     unsigned int epoch = 1597833218;
  10.  
  11.     newt.tv_sec= epoch;
  12.     tsEpoch = *localtime(&newt.tv_sec);
  13.     settimeofday(&newt, NULL);
  14.     gettimeofday(&now, NULL);
  15.  
  16.     tsRTC = *localtime(&now.tv_sec);
  17.  
  18.     ESP_LOGI(TAG,"Epoch    %X", (int)epoch);
  19.     ESP_LOGI(TAG,"Local    %X", (int)newt.tv_sec);
  20.     ESP_LOGI(TAG,"RTC      %X", (int)now.tv_sec);
  21.  
  22.     if (flag){
  23.         strftime(LineClock, sizeof(LineClock),  "%H:%M:%S", &tsEpoch);
  24.         ESP_LOGI(TAG,"Epoch %s",  LineClock);
  25.  
  26.  
  27.         strftime(LineClock, sizeof(LineClock),  "%H:%M:%S", &tsRTC);
  28.         ESP_LOGI(TAG,"RTC   %s", LineClock);
  29.     }
  30. }


with flag = 0
=============
I (357) Main: Epoch 5F3D0002
I (357) Main: Local 5F3D0002
I (357) Main: RTC 5F3D0002


with flag = 1
=============
I (357) Main: Epoch 5F3D0002
I (357) Main: Local 5F3D0002
I (357) Main: RTC 5F3D0433
I (367) Main: Epoch 10:33:38
I (367) Main: RTC 10:51:31

M.Pacheu
Posts: 2
Joined: Wed Oct 27, 2021 12:01 pm

Re: is this a bug? or am I doing something wrong?

Postby M.Pacheu » Fri Apr 08, 2022 5:30 pm

Hi ! I don't know if anyone would read this after that long, but I ran your code as I was facing a similar issue.

I cannot give you an answer of what exactly happens when you compile in the 2 different ways, but here is the trigger of the issue:
Your 'timeval' structures are not initialized. You set after declaration the '.tv_sec' field, but the '.tv_usec' might not be 0 ! Thus, when settimeofday is called, it will apply the microsecond offset. If this value never changes, no matter what you do, you will keep having the same offset. Why this does not happen when your 'flag' is 0 ? I have no idea, but the context changes, maybe the structure will be placed in the stack where the value is already 0 thus having no offset ... Undefined behaviour I would say.

I hope this will help someone having this issue :D

M.Pacheu

Who is online

Users browsing this forum: No registered users and 45 guests