PDA

View Full Version : picbasic-l Calculating the day of Week from a date



Melanie
- 15th March 2004, 10:20
Unfortunately I have problems with my ISP/email addy that I use to interchange with the email forum, therefore I've posted the answer here.

Question: John wrote:-

In Melanie's DS1307 example there is a calculation for the day of the week. It works but I don't understand the logic/maths behind it and I want to adjust it to start with a different day of the week.

This is an extract from Melanie's code:
' Calculate Day of Week & Save
' ----------------------------
' Melanie's fudge for calculating Days of Week
' using PBP's integer math...
' by the time someone's clock displays the
' 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
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

Could anyone explain it to me

Answer:

The above routine looks complicated but in fact it is very simple. We take the Year and turn it into Days. Make an adjustment to include the extra Day for each Leap Year together with the Century anomaly. We convert the months to Days add that in along with the current Day. Mix thoroughly and cook in the oven at Gas Mark 5.

What we’ve cooked up, is basically a LINEAR number of days which we can divide by 7 (we are using MOD or Modulus - see also PICBasic manual 4.17.2 on Math Division and Appendix C - Reserved Words), the remainder being a single integer CounterD which now DIRECTLY references an individual day of the week.

So, you need a different sequence of start and stop days, change the last line RTCWDay=... to fit what you require. The RAW value of CounterD on exit is 0=Tuesday, 1=Wednesday etc all up to 5=Sunday, 6=Monday. I wanted Tuesday to be 1, which is why I simply added a 1 to the end result.

Melanie

Tim B
- 15th March 2004, 23:05
I have just published some interesting code to work with Calendars, no of days between two dates, even displaying a calendar on a Graphics Lcd.

http://users.picbasic.org/index.php?page=howto&cat=13


Tim

johngb
- 16th March 2004, 07:37
Thanks for your help Melamie and Tim - I have done just what you suggested Mel and adjusted the result, however, I hate using code which I don't understand.

Please accept my appologies Melanie for questioning your lack of response. Of all the people in the forum, I value your input the most.

Thanks again for your help.

JohnB

LGabrielson
- 1st August 2012, 22:44
WHERE is the original post showing ALL of Melanie's code?

Darrel Taylor
- 2nd August 2012, 03:25
It's not in it's original location anymore.
But I think this is the one you are looking for ...

LGabrielson
- 3rd August 2012, 01:09
Hey Darrel,
Thanks for that