суббота, 18 апреля 2015 г.

Узнаем день недели в RouterOS.

Скрипт не мой, нашел в Scripting Examples:
#
#   $RCSfile: function-date.rsc,v $
#   $Revision: 1.1 $
#   $Date: 2011/05/27 19:47:46 $
#   $Author: reesejb $, iNet Specialists. Copyright, (C)2007-2011, All Rights Reserved
#
#   Calculates day of the week (number of month, etc.) for a givien date
#
#   Expanded from "day of week" script at:
#      http://wiki.mikrotik.com/wiki/Script_to_find_the_day_of_the_week
#
#   Month: jan,feb,..nov,dec   (must be lower-case)
#   Day: 01-31
#   Year: 1900-2999
#   mmm/dd/yyyy   same format as [/system clock get date]
#   (ex. jul/22/2009)
:local date [/ system clock get date]
# Variables
:global weekday ""
:global monthord ""
:local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"]
:local monthval [:toarray "01,02,03,04,05,06,07,08,09,10,11,12"]
: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"]
:global monthshort [:pick $date 0 3]
:global day [:pick $date 4 6]
:local daysum $day
:local dayc [:pick $date 5 6]
:global century [:pick $date 7 9]
:global year [:pick $date 9 11]
:local yearsum $year
:local yearc [:pick $date 10 11]
# if the first digit of the day or year is a 0 (zero),
# only use the last digit, otherwise the script will fail
:if ([:pick $date 4 5] = 0) do={ :set daysum ($dayc)}
:if ([:pick $date 9 10] = 0) do=[:set yearsum ($yearc)]
# Calculate "sum" to determine which "day of week" (from daytbl) to use
:local sum 0
:set sum ($sum + (2 * (3 - ($century - (($century / 4) * 4)))))
:set sum ($sum + ($yearsum / 4))
:set sum ($sum + $yearsum + $daysum)
:for mindex from=0 to=[:len $months] do={ \
   :if ([:pick $months $mindex] = $monthshort) do={ \
      :set sum ($sum + [:pick $monthtbl $mindex])
      :set monthord [:pick $monthval $mindex]
      } \
   }
:set sum ($sum - (($sum / 7) * 7))
:set weekday [:pick $daytbl $sum]
# Log the result for troubleshooting purposes
:log info "Today is $weekday, $monthshort $day, $century$year ($monthord/$day/$century$year)"


Update 24.01.16
  В какой-то момент скрипт перестал работать, melboyscout предложил скрипт, который есть на официальном форуме, он отрабатывает корректно. Спасибо.
# Calculates day of the week for a givien date
# Month: jan,feb ... nov,dec (must be lower-case)
# Day: 1 - 31
# Year: 1583 - ...
# mmm/dd/yyyy same format as [/system clock get date]
# (ex. feb/19/2012)
# by melboyscout (melboyscout [at] gmail.com)
: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 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
:local aaa 0
:local yyy 0
:local mmm 0
:local nmonth 1
:for mindex from=0 to=[:len $months] do={
:if ([:pick $months $mindex] = $month) do={:set nmonth ($mindex + 1) }
}
:set aaa ((14 - $nmonth) / 12)
:set yyy ($year - $aaa)
:set mmm ($nmonth + 12 * $aaa - 2)
:set sum (7000 + $day + $yyy + ($yyy / 4) - ($yyy / 100) + ($yyy / 400) + ((31 * $mmm) / 12))
:set sum ($sum - (($sum / 7) * 7))
:set result [:pick $daytbl $sum]
:log info "Today is $result"

Подписаться на новые статьи.

5 комментариев:

  1. Не работает :(
    Попробуйте последний скрипт из http://forum.mikrotik.com/viewtopic.php?t=59029

    ОтветитьУдалить
  2. Первый скрипт не работает в високосные года, вроде. А второй скрипт основан на https://ru.wikibooks.org/wiki/%D0%A0%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D0%B8_%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC%D0%BE%D0%B2/%D0%92%D0%B5%D1%87%D0%BD%D1%8B%D0%B9_%D0%BA%D0%B0%D0%BB%D0%B5%D0%BD%D0%B4%D0%B0%D1%80%D1%8C

    ОтветитьУдалить
  3. На этой основе можно сделать что-то типа включения вафли по определенным дням недели.

    ОтветитьУдалить