Calculate Day of Week from Date
Posted: Fri Feb 03, 2012 5:18 am
I've tried the script here http://wiki.mikrotik.com/wiki/Script_to ... f_the_week for calculating the day of the week from a given date and it doesn't seem to give me the correct answer. Is it broken, am I doing something wrong?
So in an effort to improve my scripting skills and using that as a base I've come up with this based on my understanding of Zeller's algorithm...
It's gets a little messy without decimal math in the scripting language, but seems to work and give the "right" answers for dates after 1752. Feedback please, I'm sure there are flaws and it can be improved upon?
So in an effort to improve my scripting skills and using that as a base I've come up with this based on my understanding of Zeller's algorithm...
It's gets a little messy without decimal math in the scripting language, but seems to work and give the "right" answers for dates after 1752. Feedback please, I'm sure there are flaws and it can be improved upon?
Code: Select all
#use the system clock date or one of our choosing
#:local date [/system clock get date]
:local date jan/12/2019
#Variables
:local result ""
:local daytbl [:toarray "sun,mon,tue,wed,thu,fri,sat"]
:local MonthNum 0
:local CenturyNum 0
:local YearNum 0
:local SUM 0
#Parse the input date
:local month [:pick $date 0 3]
:local day [:pick $date 4 6]
:local year [:pick $date 7 11]
#perform the month offset per the algorithm
:if ($month = "jan") do={ :set MonthNum 11}
:if ($month = "feb") do={ :set MonthNum 12}
:if ($month = "mar") do={ :set MonthNum 1}
:if ($month = "apr") do={ :set MonthNum 2}
:if ($month = "may") do={ :set MonthNum 3}
:if ($month = "jun") do={ :set MonthNum 4}
:if ($month = "jul") do={ :set MonthNum 5}
:if ($month = "aug") do={ :set MonthNum 6}
:if ($month = "sep") do={ :set MonthNum 7}
:if ($month = "oct") do={ :set MonthNum 8}
:if ($month = "nov") do={ :set MonthNum 9}
:if ($month = "dec") do={ :set MonthNum 10}
#perform the year offset based on month per the algorithm
:if ($MonthNum = 11) do={ :set year ($year-1)}
:if ($MonthNum = 12) do={ :set year ($year-1)}
#start the date math
:set CenturyNum ($year/100)
:set YearNum ($year % 100)
#gets a little weird here without decimals...
:local decimalOne "2.6"
:local decimalTwo ".2"
#do some decimal math
:set SUM ($decimalOne * $MonthNum - $decimalTwo)
#looks like numbers get converted to date data type, strip off what we need
:set SUM [:pick $SUM 6 8]
:set SUM ($SUM + $day + $YearNum)
:set SUM ($SUM + $YearNum/4)
:set SUM ($SUM + $CenturyNum/4)
:set SUM ($SUM - 2 * $CenturyNum)
:set SUM ($SUM % 7)
:if ($SUM < 0) do= {:set SUM ($SUM+7)}
:set result [:pick $daytbl $SUM]
:put $result