Localtime_r gives tm_hour in negative for specific timezone(eg - /asia/kabul/)

hariprasad
Posts: 1
Joined: Wed Mar 16, 2022 1:39 pm

Localtime_r gives tm_hour in negative for specific timezone(eg - /asia/kabul/)

Postby hariprasad » Wed Mar 16, 2022 3:27 pm

Hi all,
We are currently working on datetime setting to set time for different time zone. We are using ESP32S2. We are setting the timezone using setenv() and tzset() and used localtime_r() in time.h library to convert epoch time to local time. We followed below steps for setting the time zone and getting the value.
  1. setenv("TZ", "<+430>-4:30",1)
  2. tzset()
  3. time_t epochTimeNow = time(epochTime);
  4. struct tm timeInfo;
  5. localtime_r(&epochTimeNow, &timeInfo);
  6. //Printing time using tm struct
  7. printf("time = %d\n", timeInfo.tm_hour);
For IST we able to get proper time. When we tried to convert time for different time zone such as /asia/kabul<+430>-4:30, /asia/singapore/ <+08>-8 etc. We observed different error case

Case 1
When we set time zone as <+430>-4:30, we are getting hours in negative value.
Output -
time = -396


Case 2
When we set time zone as <+08>-8,
1. we are getting hours incorrectly(few hours behind) when daylight saving is 0.
2. we are getting hours correctly when daylight saving is 1.

We are facing this issue for most for the timezone that has <+430>-4:30 format.
Questions:
1. How the tm_hour gives negative value, since the range of the tm_hour is between 0 to 23?
2. Should we need to handle different method to get local time for different timezone?
3. Should we need to handler daylight saving?
4. Should we need to handle convertion for different timezine format?
5. Are we missing any configuration or settings?

Thanks in advance.

Who is online

Users browsing this forum: No registered users and 116 guests