picbasic-l Calculating the day of Week from a date


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2003
    Posts
    2,358

    Default picbasic-l Calculating the day of Week from a date

    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

  2. #2
    Tim B's Avatar
    Tim B Guest


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by Tim B; - 15th March 2004 at 23:10.

  3. #3
    johngb's Avatar
    johngb Guest


    Did you find this post helpful? Yes | No

    Default Calculating day of week

    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

  4. #4
    Join Date
    Jul 2011
    Posts
    35


    Did you find this post helpful? Yes | No

    Default Re: picbasic-l Calculating the day of Week from a date

    WHERE is the original post showing ALL of Melanie's code?

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: picbasic-l Calculating the day of Week from a date

    It's not in it's original location anymore.
    But I think this is the one you are looking for ...
    Attached Files Attached Files
    DT

  6. #6
    Join Date
    Jul 2011
    Posts
    35


    Did you find this post helpful? Yes | No

    Default Re: picbasic-l Calculating the day of Week from a date

    Hey Darrel,
    Thanks for that

Similar Threads

  1. DS1994 Memory/Time iButton
    By NavMicroSystems in forum mel PIC BASIC Pro
    Replies: 47
    Last Post: - 22nd October 2006, 12:55
  2. Need help deciding = BASCOM 0r PICBASIC
    By Chris DeHut in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 26th January 2006, 17:38
  3. Picbasic VS C Compiler
    By koossa in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 11th October 2005, 22:44
  4. Replies: 22
    Last Post: - 12th July 2005, 18:39
  5. PicBasic Fundamentals
    By Billyc in forum General
    Replies: 9
    Last Post: - 4th May 2004, 11:04

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts