Melanies RTC code has broken
Hi All,
This is aimed at anyone using Melanies RTC code in a project particularly if you use the Calculate Day of Week & Save part of the code which includes the following warning....
Melanie's fudge for calculating Days of Week using PBP's integer math...
by the time someone's clock displays the wrong Day of Week, I'll hopefully have long retired and be past caring...
I wouldn't however go stick this routine in a Nuclear Power Station to purge the reactor every Monday morning without checking how far it will work before the integer math overflows...
In my routine RTCWDay=1 is a Tuesday (the start of my week) and continues sequentially until RTCWDay=7 which is a Monday
It seems the end may have come earlier than anticipated :(
Melanies original code is as follows...
Code:
CounterA=SetYear+4
CounterB=SetMonth
If SetMonth<3 then
CounterA=CounterA-1
CounterB=CounterB+12
endif
CounterD=(SetDay+(153*CounterB-457)/5+365*CounterA+CounterA/4-CounterA/100+CounterA/400+2) MOD 7
RTCWDay=CounterD+1
My 9 year old daughter took great delight in informing me that the LCD on my current project was displaying
FRI 04 OCT 2006
I knew the code had been working fine so I set about investigating when it went wrong and was using http://www.timeanddate.com/calendar/ as a reference calendar. Listed below is the point where it breaks this year. The problem only occurs when actually setting the RTC so provided you set it early enough in the year you will be fine !!!
SEPTEMBER 2006
03 SUN
04 MON
05 TUE
06 WED
07 THU
08 FRI
09 SAT
10 SUN SAT -1 Day
11 MON SUN -1 Day
12 TUE MON -1 Day
13 WED TUE -1 Day
14 THU WED -1 Day
15 FRI THU -1 Day
16 SAT FRI -1 Day
17 SUN SAT -1 Day
18 MON SUN -1 Day
19 TUE MON -1 Day
20 WED MON -2 Days
21 THU TUE -2 Days
22 FRI WED -2 Days
23 SAT THU -2 Days
24 SUN FRI -2 Days
25 MON SAT -2 Days
26 TUE SUN -2 Days
27 WED MON -2 Days
28 THU TUE -2 Days
29 FRI WED -2 Days
30 SAT WED -3 Days
OCTOBER 2006
01 SUN TUE +2 Days
02 MON WED +2 Days
03 TUE THU +2 Days
04 WED FRI +2 Days
05 THU SAT +2 Days
06 FRI SUN +2 Days
I tried the first of November and December this year and they also came out wrong.
I was surprised find that 01 JAN 2007 came out with the correct day so I tried the first of the month for each month of 2007 and 2008 and in each case OCT, NOV, DEC were wrong. 01 SEP 2007 came out correctly but several random dates tried for Aug 2007 were broken.
I tried "01 JAN 20xx" which failed when I got to 2011
Is this something that is fixable by breaking the calculation into smaller chunks rather than attemting everything in a single calculation?
Its way too late for me to try and manually work out a calculation with that many parts but if someone wants to try with the numbers above then feel free :)
I hope this can be fixed because the DayOfWeek is more important in my application than the actual date!
I would be grateful if others who may be using this code can check if theirs is broken as well.
Sorry Melanie...its me !!!!
Sorry Melanie !!!
Your code is NOT WRONG
I tried re-creating the formula in an Excel spreadsheed forcing results to be INT but it threw up wrong results all over the place.
I then took an alternative approace of getting the PIC to output its information so that I could stick it into a spreadsheet. I then got Excel to calculate the correct weekday for each of the dates. I cant see ANY discrepancies in the 7000+ rows although I havent looked at each one individually.
Here is the section containing the dates that I posted last night....
Code:
Date Weekday DayID RealDay
9-Sep-2006 Sat 5 Saturday
10-Sep-2006 Sun 6 Sunday
11-Sep-2006 Mon 7 Monday
12-Sep-2006 Tue 1 Tuesday
13-Sep-2006 Wed 2 Wednesday
14-Sep-2006 Thu 3 Thursday
15-Sep-2006 Fri 4 Friday
16-Sep-2006 Sat 5 Saturday
17-Sep-2006 Sun 6 Sunday
18-Sep-2006 Mon 7 Monday
19-Sep-2006 Tue 1 Tuesday
20-Sep-2006 Wed 2 Wednesday
21-Sep-2006 Thu 3 Thursday
22-Sep-2006 Fri 4 Friday
23-Sep-2006 Sat 5 Saturday
24-Sep-2006 Sun 6 Sunday
25-Sep-2006 Mon 7 Monday
26-Sep-2006 Tue 1 Tuesday
27-Sep-2006 Wed 2 Wednesday
28-Sep-2006 Thu 3 Thursday
29-Sep-2006 Fri 4 Friday
30-Sep-2006 Sat 5 Saturday
1-Oct-2006 Sun 6 Sunday
2-Oct-2006 Mon 7 Monday
3-Oct-2006 Tue 1 Tuesday
4-Oct-2006 Wed 2 Wednesday
5-Oct-2006 Thu 3 Thursday
6-Oct-2006 Fri 4 Friday
7-Oct-2006 Sat 5 Saturday
8-Oct-2006 Sun 6 Sunday
Date, Weekday and DayID are what I got from the PIC.
RealDay is using the =TEXT(value,"dddd") function in Excel
What I cant figure out is that this is the SAME PROGRAM with just a small section of code copied into a separate subroutine to generate the dates for this test.
As you can see, 6-OCT-2006 gets generated as "FRI" but when I use the date setting routine in my program I end up with "SUN".
Will investigate further but it look like I was predicting the end of the world prematurely. Apologies to Melanie