[SOLVED !] Tomohiko Sakamoto's Algorithm implementation?

# Thread: Tomohiko Sakamoto's Algorithm implementation? – 1588 days old

1. 0

## 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.

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

3. 0

## 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.

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

5. 0

## 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.

6. Attention
×

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

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

7. 0

## 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!

8. Attention
×

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

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

9. 0

## Re: Tomohiko Sakamoto's Algorithm implementation?

I have done this a few times

Here is one implementation for converting unix time

Code:
```PROCESS_UNIX:
; 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
'
'--- 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```

10. Attention
×

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

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

11. 0

## Re: Tomohiko Sakamoto's Algorithm implementation?

Thanks a lot!!

12. Attention
×

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

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