Links for implementing calendar functions in the embedded system?

I have a built-in system that currently tracks seconds until an event occurs, using a real-time clock controlled by the crystal of the clock.

Now he needs to keep track of the actual date and time. So I need to calculate the day, month, year, hour, minute and second from the start / time date and offset in seconds.

Can someone point me in the right direction to account for leap years, daylight saving time (DST) and other complications?


Hardware solutions are not an option, as this feature is added to an existing product. The RTC peripheral is integrated into the MCU selected for the next generation device.

+3
source share
4 answers

C fragment archive contains some functions of date and time . Update: unfortunately, the C snippets archive now does not work. I updated the link to point to the web archive of the page.

See also " Julian day ", Wikipedia , which includes formulas for Julian date calculation .

A " calculation of the Julian date " A Google search should reveal more if you want to continue the search.

+1
source

I am bored, I could not resist trying to solve. Here's a prototype in ruby ​​- should be clear enough to translate to C.

offset , : Baseyear, Baseday, Basesec, 0 = 1,

#initialize outputs
year= Baseyear
day = Baseday
sec = Basesec+offset

#days & seconds remaining in the current year
is_leap = is_leap_year(year)
days_remaining = 365+(is_leap ? 1 : 0) - day
secs_remaining = SEC_PER_DAY*days_remaining

#advance by year
while (sec>=secs_remaining)
  sec-=secs_remaining
  year+=1
  is_leap = is_leap_year(year)
  days_remaining = 365+(is_leap ? 1 : 0)
  secs_remaining = SEC_PER_DAY*days_remaining
  day=0 
end

#sec holds seconds into the current year, split into days+seconds
day += sec / SEC_PER_DAY
day = day.to_i #cast to int
sec %= SEC_PER_DAY

#lookup month
for i in (0..11)
  dpm = DAYS_PER_MONTH[i] # =[31,28,31,30,...]
  if (i==1 && is_leap) 
   dpm+=1
  end
  if day < dpm
   month = i
   break
  else
    day-=dpm
  end
end

day+=1 #1-based
hour = sec/3600
min = (sec%3600)/60
sec = sec%60
puts "%s %d, %d @ %02d:%02d:%02d" % [MONTHNAME[month],day,year, hour, min, sec]

, DST .

+2
+1

, :

bool is_leap_year(int year)
{
    return ((0 == year % 400) || ((0 == year % 4) && (0 != year % 100)));
}
0

Source: https://habr.com/ru/post/1724392/


All Articles