Why is it, that when i convert a struct tm
to time_t
via mktime and then this value back to a struct tm
via localtime()
it is not the same?
struct tm actualtime=get_RTC_time();
sprintf(outtext,"==> %d-%d-%d %d:%d:%d\r\n",actualtime.tm_year,actualtime.tm_mon,actualtime.tm_mday,actualtime.tm_hour,actualtime.tm_min,actualtime.tm_sec);
print(RS232,outtext);
actualtime.tm_mon = actualtime.tm_mon-1;
actualtime.tm_year = actualtime.tm_year-1900;
time_t tmp=mktime(&actualtime);
sprintf(outtext,"==> %d-%d-%d %d:%d:%d\r\n",actualtime.tm_year,actualtime.tm_mon,actualtime.tm_mday,actualtime.tm_hour,actualtime.tm_min,actualtime.tm_sec);
print(RS232,outtext);
sprintf(outtext,"==> %lld\r\n",(long long)tmp);
print(RS232,outtext);
tmp+=100;
struct tm *alarm_time=gmtime(&tmp);
sprintf(outtext,"==> %d-%d-%d %d:%d:%d\r\n",alarm_time->tm_year,alarm_time->tm_mon,alarm_time->tm_mday,alarm_time->tm_hour,alarm_time->tm_min,alarm_time->tm_sec);
print(RS232,outtext);
In my code snippet i get the timestamp from a custom function get_RTC_time
and i wanted to add 100 seconds to that timestamp, So i converted it to seconds via mktime()
, added 100 seconds and then converted it back to a struct tm alarm_time
timestamp, but alarm_time
differs way more than 100 seconds from actualtime
.
For example, when i get the timestamp 1900-1-27 9:59:41
from get_RTC_time
and i convert it to a long
I get 1919627533
and when i convert it back to a timestamp i get 130-9-30 21:52:13
and i do not know why.
I read through the manpage, but nothing really explained this behaviour to me
It also occured to me that mentioning, that this code should be is to be running on a microchip
the method get_RTC_time
struct tm get_RTC_time(void)
{
RTC_Twi.chip = RTC_adr;
RTC_Twi.addr[0] = 0x00; // must be adjusted accordingly
RTC_Twi.addr_length = sizeof (uint8_t); //in bit!!!!!!!!!!!!!
RTC_Twi.buffer = &RTC_Array;
RTC_Twi.length = 7;
twi_master_read(RTC_3231_Interface,&RTC_Twi);
uhrzeit.tm_sec = (RTC_Array[0] & 0x0f)+((RTC_Array[0] & 0xf0)>>4)*10;
uhrzeit.tm_min = (RTC_Array[1] & 0x0f)+((RTC_Array[1] & 0xf0)>>4)*10;
uhrzeit.tm_hour = (RTC_Array[2] & 0x0f)+((RTC_Array[2] & 0x10)>>4)*10+((RTC_Array[2]&0x20)>>5)*20; //24h Modus muss aktiv sein!!!
uhrzeit.tm_wday = (RTC_Array[3] & 0x07);
uhrzeit.tm_mday = (RTC_Array[4] & 0x0f)+((RTC_Array[4] & 0xf0)>>4)*10;
uhrzeit.tm_mon = (RTC_Array[5] & 0x0f)+((RTC_Array[5] & 0x10)>>4)*10;
uhrzeit.tm_year = (RTC_Array[6] & 0x0f)+((RTC_Array[6] & 0xf0)>>4)*10+((RTC_Array[5] & 0x80)>>7)*100 + 1900 ;
return uhrzeit;
}