[SOLVED !] Tomohiko Sakamoto's Algorithm implementation?


Closed Thread
Results 1 to 6 of 6
  1. #1
    Virolay
    Guest Virolay's Avatar

    0 Not allowed!

    Question Tomohiko Sakamoto's Algorithm implementation?

    Hi to all,

    The Tomohiko Sakamoto's algorithm from Wikipedia to find out the day of week from a given date is:


    int dow(int y, int m, int d)
    {
    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    y -= m < 3;
    return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
    }

    That's the basis to implement the Daylight Saving Time.

    Anyone has an implementation in Proton?.

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Prolific Poster hadv215's Avatar
    Join Date
    Sep 2009
    Posts
    1,136
    Thumbs Up
    Received: 66
    Given: 26
    Total Downloaded
    3.61 GB

    0 Not allowed!

    Default Re: Tomohiko Sakamoto's Algorithm implementation?

    How hard can it be?
    Declare 2 variables (m, d) of type byte, one of type word (y) and some temp variables
    create a table with 12 entries and initialize this with the values shown in brackets (0...4)
    next:

    temp1 = y + y/4 - y/100 + y/400 ; note that temp1 needs to be a word
    if m < 3 then dec y
    temp2 = t[m-1] + d

    result = (temp1 + temp2) mod 7

    It is all explained here

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    Prolific Poster shantanu@india's Avatar
    Join Date
    Feb 2007
    Posts
    1,705
    Thumbs Up
    Received: 7
    Given: 21
    Total Downloaded
    827.23 MB

    0 Not allowed!

    Default Re: Tomohiko Sakamoto's Algorithm implementation?

    Hi,
    Maybe I am deviating from the topic but this algorithm needs to know the first day of the year to work.
    Maybe that can be kept hardcoded in flash/EEPROM....(maybe the first day of the year 1900 A.D!!)
    But,essentially, for the daylight saving scheme to work ,the PIC has to know what is the time NOW & for that it either has to be battery powered so that its internal clock keeps on ticking , or else it needs to have access to an external RTC like DS1307 or , maybe ,if budget permits it needs to be a PIC with built-in RTC like 18F24J11.
    If you are using the alternatives 2 or 3 then there is no need for any day-calculating algorithm.
    Shantanu

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    Prolific Poster charliecoultas's Avatar
    Join Date
    Sep 2010
    Posts
    1,414
    Thumbs Up
    Received: 48
    Given: 40
    Total Downloaded
    3.45 GB

    0 Not allowed!

    Default Re: Tomohiko Sakamoto's Algorithm implementation?

    I do this:

    a=(Year_no/4)+Year_no 'eg if year is 2016 then take last two digits: 16
    Dec Month_no '1-12 but lookupl starts at 0 not 1
    temp=LookUpL Month_no,[6,2,2,5,0,3,5,1,4,6,2,4]
    Day_no=(day_no+a+temp)//7 'day_no = 1,2,3 etc on input - 0-6 on output - 0 = Sun (if I remember right)

    Charlie

    Sorry, I said use the whole year (2016 say) but in fact use only the last two digits (16), then it works OK!
    Last edited by charliecoultas; 28th January 2016 at 16:32.

  8. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  9. #5
    Fanatical Contributor Tim's Avatar
    Join Date
    Jan 2003
    Posts
    7,754
    Thumbs Up
    Received: 51
    Given: 96
    Total Downloaded
    1.33 GB

    0 Not allowed!

    Default Re: Tomohiko Sakamoto's Algorithm implementation?

    I have done this a few times

    Here is one implementation for converting unix time

    Code:
    PROCESS_UNIX:    
        Clear GPSUnixTime                                               ; start with a clear GPSUnixTime
        ; compute number of years over 2007
        YearsOver = RTCYear - $07          ; our offset is 2007
        
        ; start addition of seconds according to years
        
        For OverOffsetYear = 0 To YearsOver - 1
            TempYear = UnixYearOffset + OverOffsetYear 
            LeapYear = TempYear & $03 
            If LeapYear = 0 Then           ; so its a leap year
               GPSUnixTime = GPSUnixTime + SecondsPerLeapYear
            Else
               GPSUnixTime = GPSUnixTime + SecondsNLeapYear 
            EndIf        
        Next
    
    
        ; start computimg the seconds according to the curent year's month
        
        FullMonths = RTCMonth - 1   ; zero is january
    
    
        LeapYear = RTCYear & $03
        
        FullMonthsDays = LRead16 DayPerMonth [FullMonths]               ; Lookup the no days in the month
        If LeapYear = 0 Then                                            ; If were currently in a leap year
            TempW1 = FullMonthsDays + RTCDate                           ; Work out how many days were into the year
            If TempW1 >= 60 Then                                         ; If were past 29th Feb 
                Inc FullMonthsDays                                      ; We add one more day
            EndIf
        EndIf
    
    
        ; compute the number of seconds accordingly and add to GPSUnixTime
        TempDW1 = FullMonthsDays * 86400
        GPSUnixTime = GPSUnixTime + TempDW1
        ; add in the curent number of seconds accordingly for the actual day (date)
        TempDW1 = RTCDate - 1
        TempDW1 = TempDW1 * 86400
        GPSUnixTime = GPSUnixTime + TempDW1
        ; now start adding the hour representation in seconds
        TempDW1 = RTChr * 3600
        GPSUnixTime = GPSUnixTime + TempDW1
        ; adding the minute representation in seconds
        TempW1 = RTCMin * 60
        GPSUnixTime = GPSUnixTime + TempW1
        ; finally addin the actual seconds
        GPSUnixTime = GPSUnixTime + RTCSec
        GPSUnixTime = GPSUnixTime + UnixOffset
        Return
    
    DayPerMonth:
        CData As Word 0, 31, 59, 90, 120, 151, 181, 212 ,243, 273, 304, 334, 365
    And another version


    Code:
    '-----------------------------------------------' CALC_DAYS
    ' Routine to calculate the total number of 
    ' days between a set date and 1/1/2000
    ' valid up to 31/12/2099 
    ' Load 
    '
    '--- year calc subs ---------------
    Total_calc:
        Total_Days = Complete_Years_Days + Month_days
        GoTo BRA_EXIT
        
    CALC_DAYS:
        Clrwdt
        Clear TOTAL_DAYS  
        COMPLETE_YEARS = YEAR                       ' GET THE NUMBER OF COMPLETED YEARS 
        
        If COMPLETE_YEARS > 0 Then
           CALC_TEMP = (COMPLETE_YEARS-1)/4
           COMPLETE_YEARS_DAYS =COMPLETE_YEARS * 365 + CALC_TEMP +1 
        EndIf 
        
        CURRENT_YEAR_LEAP = COMPLETE_YEARS//4 ' IF 0 IT IS LEAP 
        
        BranchL MONTH, [BRA_EXIT,JAN,FEB,MAR,APR,MAY,JUN,JULY,AUG,SEPT,OCT,NOV,DECM] 
        
        ' ADD THE DAYS OF COMPLETED MONTHS 
        
    JAN: 
           TOTAL_DAYS= COMPLETE_YEARS_DAYS
           GoTo BRA_EXIT          
    FEB: 
           MONTH_DAYS = 31  
           GoTo Total_calc 
    MAR: 
           MONTH_DAYS =  59 
           GoTo Total_calc       
    APR:
           MONTH_DAYS =  90
           GoTo Total_calc    
    MAY:                        
           MONTH_DAYS =  120
           GoTo Total_calc     
    JUN: 
           MONTH_DAYS =  151
           GoTo Total_calc     
    JULY: 
           MONTH_DAYS =  181
           GoTo Total_calc     
    AUG: 
           MONTH_DAYS =  212
           GoTo Total_calc     
    SEPT: 
           MONTH_DAYS =  243
           GoTo Total_calc     
    OCT: 
           MONTH_DAYS =  273
           GoTo Total_calc     
    NOV: 
           MONTH_DAYS = 304
           GoTo Total_calc     
    DECM: 
           MONTH_DAYS = 334
           GoTo Total_calc     
    BRA_EXIT:
        
        TOTAL_DAYS=TOTAL_DAYS+ DAY                  ' ADD THE CURRENT MONTH DAYS 
        
        If MONTH >2 Then
            If CURRENT_YEAR_LEAP = 0 Then 
                TOTAL_DAYS=TOTAL_DAYS+1 ' ADD 1 IF THE CURRENT YEAR IS LEAP, AND THE MONTH >2 
            EndIf
        EndIf
        
        Return   
    
    
    '---[CHECK_DATE]--------------------------------
    ' 
    ' CHECKS IF DATE IS VALID
    '
    '-----------------------------------------------
                   
    CHECK_DATE:
     
    	 DATE_STATUS = FALSE                        ' SET TO INVALID 
    	 MONTH_MAX_DAYS = LookUp RTCMONTH_TEMP,[0,31,28,31,30,31,30,31,31,30,31,30,31] ' GET MONTH MAXIMUM DAYS	 
    	 If RTCMONTH_TEMP = 2 Then  		 		' CHECK IF MONTH 2
    	 	If RTCYEAR_TEMP//4 = 0 Then Inc  MONTH_MAX_DAYS ' ADJUST FOR LEAP YEAR
    	 End If
         If RTCDATE_TEMP <= MONTH_MAX_DAYS Then DATE_STATUS = 1	 ' CHECK DAY, SET TO VALID IF PASSED
         Return
    Tim

  10. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  11. #6
    Virolay
    Guest Virolay's Avatar

    0 Not allowed!

    Default Re: Tomohiko Sakamoto's Algorithm implementation?

    Thanks a lot!!
    Last edited by Virolay; 28th January 2016 at 13:10. Reason: [SOLVED]

  12. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Vendor-Specific USB Class Implementation in PDS
    By wastrix in forum Proton Plus Compiler v3
    Replies: 4
    Last Post: 28th August 2010, 19:26
  2. Physical implementation of a SD card
    By pdgnr in forum SWright's Feedback Forum
    Replies: 2
    Last Post: 8th June 2007, 13:32
  3. [SOLVED !] duty factor algorithm
    By John7 in forum The Lounge
    Replies: 1
    Last Post: 12th March 2006, 00:33
  4. MD5 algorithm.
    By speeder in forum The Lounge
    Replies: 4
    Last Post: 12th August 2005, 12:10
  5. MD5 algorithm.
    By speeder in forum Proton Plus Compiler v3
    Replies: 0
    Last Post: 11th August 2005, 16:12

Tags for this Thread

Posting Permissions

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