Localtime_r gives tm_hour in negative for specific timezone(eg - /asia/kabul/)
Posted: 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.
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.
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.
- setenv("TZ", "<+430>-4:30",1)
- tzset()
- time_t epochTimeNow = time(epochTime);
- struct tm timeInfo;
- localtime_r(&epochTimeNow, &timeInfo);
- //Printing time using tm struct
- printf("time = %d\n", timeInfo.tm_hour);
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.