Script to find the day of the week eg sun,mon,tue etc

Hi, I need some help please.

I need a script that will be able to supply me with the day of the week eg. sun,mon,tue.

When I do:

/system clock
:local myDay [:pick [get date] 4 6]

I am able to retrieve the day of the month, however I do not know if the day is a Monday, Tuesday etc. How can I find out what day of the week I have. I want to enabled / disable firewall filter rules depending on the day of the week eg. no gaming during the week.

Regards
Christo

You don’t need script for this.
Use time option in firewall

/ip firewall filter add time=

I have mangle rules that I also need to enabled / disable. The mangle rules I use for Quality of Service connection markings. This is my problem really, I can’t just rely on the firewall time schedules.

This option is available also in mangle and NAT

Hi,

Maybe I’m not making myself clear. Instead of having 2 rules per day for everyday (2 rules because the time can’t overlap past 23:59:59). I would like to have one rule and just enable / disable it with a script when the rule is needed. If I can’t find the day of the week I’ll have to write a php script that will generate a file for me with the answer.

Thanks.

For reference:
http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week#An_algorithm_to_calculate_the_day_of_the_week

I’m looking into writing a script with RouterOS 3.x using this algorithm. In the meantime, let me know if anyone else has a better solution.

The power of Mikrotik scripting!

How about this:

For today (PST time zone):
http://api.changeip.com/v1/mikrotik/dayofweek.ashx

For a specific date:
http://api.changeip.com/v1/mikrotik/dayofweek.ashx?d=2009-01-03

/tool fetch src-path=“/v1/mikrotik/dayofweek.ashx” address=api.changeip.com mode=http host=“api.changeip.com” keep-result=yes dst-path=“dayofweek.txt” ]

You could then parse the dayofweek.txt contents into a variable and do your magic.

Good deal! Works like a champ.

Do you think there would be a need to do this locally (without net access)? After looking over the algorithm, it shouldn’t take too long to come up with something. Let me know if anyone’s interested and I’ll see what I can come up with.

yeah its basically a one liner on our api server, will leave it there forever.

context.Response.Write(d.DayOfWeek)

I’m only posting this for completeness to the original question. Changeip’s solution requires less lines of code while requiring internet access on the router. This solution is a bit bigger but doesn’t require an internet connection.

As I said before, I’m only posting for completeness; use whichever one works best for you.

# Calculates day of the week for a given date
# Month: jan,feb ... nov,dec   (must be lower-case)
# Day: 1 - 31
# Year: 1900 - 2999
# mmm/dd/yyyy   same format as [/system clock get date]
# (ex. jul/22/2009)

:local date [/system clock get date]



# Math Calculation here
:local result ""
:local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"]
:local monthtbl [:toarray "0,3,3,6,1,4,6,2,5,0,3,5"]
:local daytbl [:toarray "sun,mon,tue,wed,thu,fri,sat"]

:local month [:pick $date 0 3]
:local day [:pick $date 4 6]
:local century [:pick $date 7 9]
:local year [:pick $date 9 11]

:local sum 0
:set sum ($sum + (2 * (3 - ($century - (($century / 4) * 4)))))
:set sum ($sum + ($year / 4))
:set sum ($sum + $year + $day)
:for mindex from=0 to=[:len $months] do={
  :if ([:pick $months $mindex] = $month) do={:set sum ($sum + [:pick $monthtbl $mindex]) }
}
:set sum ($sum - (($sum / 7) * 7))
:set result [:pick $daytbl $sum]

# END Math Calculation

:put ($month . "/" . $day . "/" . $century . $year . " is on a " . $result)

awesome! always better to just run it locally. Maybe put a comment at the top of future scripts that says what version it works on - as most new versions seem to break scripts by changing syntax a little here and there.

nice. will look good in Wiki =)

Hi Guys,

Thanks for all the replies. Really interesting stuff. I ended up using

/tool fetch

to hit a php page that returns the current day of the week for me.

I appreciate all your assistance.

Regards

I added the following to the above script. Please see in highlighted:

Calculates day of the week for a given date

Month: jan,feb … nov,dec (must be lower-case)

Day: 1 - 31

Year: 1900 - 2999

mmm/dd/yyyy same format as [/system clock get date]

(ex. jul/22/2009)

:local date [/system clock get date]

Math Calculation here

:local result “”
:local months [:toarray “jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec”]
:local monthtbl [:toarray “0,3,3,6,1,4,6,2,5,0,3,5”]
:local daytbl [:toarray “sun,mon,tue,wed,thu,fri,sat”]

:local month [:pick $date 0 3]
:local day [:pick $date 4 6]
:local dayc [:pick $date 5 6]
:local century [:pick $date 7 9]
:local year [:pick $date 9 11]
:local yearc [:pick $date 10 11]

if the first char is a 0 (zero) only read last char, else script fails

:if ([:pick $date 4 5] = 0) do={ :set day ($dayc)}
:if ([:pick $date 9 10] = 0) do=[:set year ($yearc)]

:local sum 0
:set sum ($sum + (2 * (3 - ($century - (($century / 4) * 4)))))
:set sum ($sum + ($year / 4))
:set sum ($sum + $year + $day)
:for mindex from=0 to=[:len $months] do={
:if ([:pick $months $mindex] = $month) do={:set sum ($sum + [:pick $monthtbl $mindex]) }
}
:set sum ($sum - (($sum / 7) * 7))
:set result [:pick $daytbl $sum]

END Math Calculation

:put ($month . “/” . $day . “/” . $century . $year . " is on a " . $result)

Thanks vash for the revision. The more “safety” checks the better.

I updated the wiki entry with the new revision here:
http://wiki.mikrotik.com/wiki/Script_to_find_the_day_of_the_week

The current script doesn’t work because don’t include leap years months table (see wiki, for leap year, january has 6, february 2). So for the 12 Jan 2012 current scrit show Monday instead of Sunday.
I rewrite the script by Tomohiko Sakamoto algorithm:

# Calculates day of the week for a givien date
# Month: jan,feb ... nov,dec   (must be lower-case)
# Day: 1 - 31
# Year: 1900 - 2999
# mmm/dd/yyyy   same format as [/system clock get date]
# (ex. jul/22/2009)

:local date [/system clock get date]

# Math Calculation here
:local result ""
:local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"]
:local t [:toarray "0,3,2,5,0,3,5,1,4,6,2,4"]
:local daytbl [:toarray "sun,mon,tue,wed,thu,fri,sat"]

:local month [:pick $date 0 3]
:local day [:pick $date 4 6]
:local dayc [:pick $date 5 6]
:local year [:pick $date 7 11]

# if the first char is a 0 (zero) only read last char, else script fails
:if ([:pick $date 4 5] = 0) do={ :set day ($dayc)}

:local sum 0
:for mindex from=0 to=[:len $months] do={
  :if ([:pick $months $mindex] = $month) do={:set month $mindex }
}
:if ($month + 1 < 3) do={set year ($year - 1)}
:set sum ($year + ($year / 4) - ($year / 100) + ($year / 400) + [:pick $t $month] + $day)
:set sum ($sum - (($sum / 7) * 7))
:set result [:pick $daytbl $sum]

# END Math Calculation

:put ([:pick $date 0 3] . "/" . [:pick $date 4 6] . "/" . [:pick $date 7 9] . [:pick $date 9 11] . " is on a " . $result)

Hi, i think there where some changes in the

/system clock get date

command.

Expected: jul/31/2023
Actual: 2023-07-31

So the above scripts don’t work anymore.
I did some changes to make this work again.


# Calculates day of the week for a givien date
# Day: 1 - 31
# Year: 1900 - 2999
# yyyy/mm/dd   same format as [/system clock get date]
# (ex. 2023-07-30)
# modifyed by Philipp Wo

:local date [/system clock get date]

# Math Calculation here
:local result ""
:local months [:toarray "1,2,3,4,5,6,7,8,9,10,11,12"]
:local t [:toarray "0,3,2,5,0,3,5,1,4,6,2,4"]
:local daytbl [:toarray "sun,mon,tue,wed,thu,fri,sat"]

:local month [:pick $date 6 7]
:local day [:pick $date 8 10]
:local century [:pick $date 0 1]
:local year [:pick $date 1 4]


# if the first char is a 0 (zero) only read last char, else script fails
:if ([:pick $date 4 5] = 0) do={ :set day ($dayc)}

:local sum 0
:for mindex from=0 to=[:len $months] do={
  :if ([:pick $months $mindex] = $month) do={:set month $mindex }
}


:if ($month + 1 < 3) do={set year ($year - 1)}
:set sum ($year + ($year / 4) - ($year / 100) + ($year / 400) + [:pick $t $month] + $day)
:set sum ($sum - (($sum / 7) * 7))
:set result [:pick $daytbl $sum]


# END Math Calculation

:log info ($day . "/" . [:pick $date 6 7]. "/" . $century . $year . " is on a " . $result)

Full version of the @rextended script:

http://forum.mikrotik.com/t/date-parts-weekday/162769/1

BR.