Assembly, Date/Time calculations in an MCU

Thread Starter

cmartinez

Joined Jan 17, 2007
8,218
Looking for a bit of inspiration here ... I'm working on a small MCU program, part of which is timekeeping. What I'm doing is feeding it the absolute time in seconds elapsed since Jan 1, year 1 at 0:00:00 and start counting from there. Easy enough.

I'm doing things that way because I'm interfacing the MCU to a vb.net program in my computer, and .NET has a function that does exactly that. The function is DateTime.Now.Ticks, which returns the number of microseconds elapsed from the aforementioned start date.

Anyway. What I'm trying to do is convert those seconds back into a date. And that's proved a little more challenging than I thought.

I'm aware of the Gregorian calendar leap year exception every 100 years but not 400 years. That is, there's a leap year every 4 years, except when the year in question is a multiple of 100, but not a multiple of 400. For instance, the year 2000 is a multiple of 400 and it was a leap year, but the year 1900 is a multiple of 100 and therefore was not a leap year.

Thing is, I'm pulling my hair here trying to turn said logic into a simple formula. For instance, right now the current absolute time in seconds is: 637,932,155,780. Converting into days is easy enough: 63,793,218,220/(60 x 60 x 24) = 738,347.43310 . But converting it into years is the tricky part. According to my reasoning, an average year lasts 365 + 1/4 - 1/100 + 1/400 = 365.2425 days. Therefore, the amount of days I mentioned above result in:
738,347.43310/365.2425 = 2,021.5266 ... which is not even close to today's date (2022, July the 12th)

What gives?
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,158
Looking for a bit of inspiration here ... I'm working on a small MCU program, part of which is timekeeping. What I'm doing is feeding it the absolute time in seconds elapsed since Jan 1, year 1 at 0:00:00 and start counting from there. Easy enough.

I'm doing things that way because I'm interfacing the MCU to a vb.net program in my computer, and .NET has a function that does exactly that. The function is DateTime.Now.Ticks, which returns the number of microseconds elapsed from the aforementioned start date.

Anyway. What I'm trying to do is convert those seconds back into a date. And that's proved a little more challenging than I thought.

I'm aware of the Gregorian calendar leap year exception every 100 years but not 400 years. That is, there's a leap year every 4 years, except when the year in question is a multiple of 100, but not a multiple of 400. For instance, the year 2000 is a multiple of 400 and it was a leap year, but the year 1900 is a multiple of 100 and therefore was not a leap year.

Thing is, I'm pulling my hair here trying to turn said logic into a simple formula. For instance, right now the current absolute time in seconds is: 637,932,155,780. Converting into days is easy enough: 637,932,155,780/(60 x 60 x 24) = 7,383.4740 . But converting it into years is the tricky part. According to my reasoning, an average year lasts 365 + 1/4 - 1/100 + 1/400 = 365.2425 days. Therefore, the amount of days I mentioned above result in:
738,347.4036/365.2425 = 2,021.5265 ... which is not even close to today's date (2022, July the 12th)

What gives?
In 1582 Pope Gregory XIII implemented the Gregorian calendar because the Julian calendar, in use since the time of Julius Caeser, was getting out of synch with the mean solar year which was important for planting and harvesting. [Oh and calculating the correct day for Easter] In the process, 11 days were skipped from Oct 4th to Oct 15th. This may not be the only anomaly, but it is one of the biggest. You're better off using seconds since Jan 1, 1970 which is the basis for Unix time.
 
Last edited:

dl324

Joined Mar 30, 2015
16,839
There was an adjustment to the Gregorian calendar in 1752. From a Solaris cal manpage:
1657639375139.png

I'd look at the cal source for any other discontinuities.
 

Papabravo

Joined Feb 24, 2006
21,158
There was an adjustment to the Gregorian calendar in 1752. From a Solaris cal manpage:
View attachment 271311

I'd look at the cal source for any other discontinuities.
That was 1582 -- the 11 days
Different countries adopted the Gregorian calendar at different points in time rather than all at once. Specifically, the UK waited until 1752 and the Russian Empire until 1867.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,218
In 1582 Pope Gregory XIII implemented the Grigorian calendar because the Julian calendar, in use since the time of Julius Ceaser, was getting out of synch with the mean solar year which was important for planting and harvesting. In the process, 11 days were skipped from Oct 4th to Oct 15th. This may not be the only anomaly, but it is one of the biggest. You're better off using seconds since Jan 1, 1970 which is the basis for Unix time.
RIGHT! ... I had forgotten about that ... also, I remember that's the base date that the Android OS uses.

I remember that in those days, England refused to adapt said calendar and continued with the old one. Because it considered the Gregorian calendar a "Popist conspiration" of sorts (they hated the CC so much they would be willing to do the exact opposite simply out of spite), and it wasn't until many years after that they finally gave in and adopted the current calendar along with the rest of the western world.
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,158
RIGHT! ... I had forgotten about that ... also, I remember that's the base date that the Android OS uses.

I remember that in those days, England refused to adapt said calendar and continued with the old one. Because it considered the Gregorian calendar a "Popist conspiration" of sorts (they hated the CC so much they would be willing to do the exact opposite simply out of spite), and it wasn't until many years after that they finally gave in and adopted the current calendar with the rest of the western world.
Almost 200 years of total obstinacy is something they must be very proud of.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,218
They did have dealing with people who annoyed them down to a fine art.
... you're right ... and don't even get me started about my own culture ... :rolleyes:

Anyway, back to topic ... since I plan to also write an interface for Android smartphones, the sensible thing to do would be to simply offset the number of seconds elapsed since 1970 and start counting MCU time from there. That way the system should be consistent in both OS.

I'm going to try my calculations using that date as a starting point... see if I don't run into more trouble.
 

BobaMosfet

Joined Jul 1, 2009
2,110
and yet ... they keep driving on the left side of the road, and their steering wheels are on the right side of their vehicles ...
That's because they jousted holding the pike in their right arm, so the opposition came at them on their right side.... at least that's what's been explained to me.
 

Ian0

Joined Aug 7, 2020
9,667
Shouldn't you have started at year 0? With a start at year 1, you need to add an extra year, which puts you a mid-way through 2022, which is about right.
The Gregorian calendar thing was to make up for the fact they didn't have the -1/100 for the previous centuries, so the 11 day correction should cancel that out.

Isn't this exercise something similar to Unix time which starts 1st January 1970? There's already software to turn Unix time into day/month/year
 

John P

Joined Oct 14, 2008
2,025
I think you might do better to make the computer do the work, and report the time to your processor in some form that already separates year/month/day/hour/minute/second. Windows certainly has functions to do this, but what's available to you must depend on the environment you're working in.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,218
I think you might do better to make the computer do the work, and report the time to your processor in some form that already separates year/month/day/hour/minute/second. Windows certainly has functions to do this, but what's available to you must depend on the environment you're working in.
Of course, that would've been my first choice. But unfortunately having the MCU report the current date/time along with the proper calculations is a must, since the system will be working separately from a computer.
 

Ian0

Joined Aug 7, 2020
9,667
I've just started using Renesas R7FA4M1 microcontrollers - all of them have a real-time clock (hours/minutes/second/date/month/year) built in. I used to use NXP micros, which claim to have a RTC, but it is just a seconds counter, leaving me to work out the time from it the hard way. I ignored it and used a DS1338.
 

ci139

Joined Jul 11, 2016
1,898
i have some almost working javascript parser that accounts the leap years - but eventually shows the date the 32-th instead of the 1-st
 
Top