search tag # rextended mikrotik date to ISO:8601 date date2iso epoch unix time week day date2epoch date2weekday
A first pass to rewrite the function, is try to convert mikrotik "get date, time and gmt-offset" to valid ISO:8601 date like
2021-09-01T
21:10:26
+00:00 (Great Britain) = 2021-09-01T
21:10:26
Z (UTC) = 2021-09-01T
23:10:26
+02:00 (Italy)
The get date return lowercase months name, the date on file is with months on lowercase, etc.
instead last user access date, last link down/link up, date on log lines, and other, start with first letter uppercase,
(nice solution remove first letter on compare, probably I do the same later)
MikroTik programmers do not like uniformity.... Like 3 different type of date on logs....
Why do not use international date 2021-09-01??? Is clear where is the year, the month and the day...
I everytime use global on that examples for rapidly work on terminal, on script replace all "global" occurencies with "local"
/system clock
:global strDate [get date]
:global strTime [get time]
:global intGoff [:tonum [get gmt-offset]]
:if ($intGoff > 0x7FFFFFFF) do={:set intGoff ($intGoff - 0x100000000)}
:global arrMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
:global intYear [:tonum [:pick $strDate 7 11]]
:global strMonth ($arrMonths->[:pick $strDate 0 3])
:global strDay [:pick $strDate 4 6]
:global strHour [:pick $strTime 0 2]
:global strMinute [:pick $strTime 3 5]
:global strSecond [:pick $strTime 6 8]
:global strOffsig "+"
:global strGoff $intGoff
:if ($intGoff < 0) do={:set strOffsig "-"; :set strGoff ($intGoff * -1)}
:global strHoff [:pick [:totime $strGoff] 0 2]
:global strMoff [:pick [:totime $strGoff] 3 5]
:global strISOdate "$intYear-$strMonth-$strDay\54$strHour:$strMinute:$strSecond$strOffsig$strHoff:$strMoff"
:put "$strDate $strTime $strOffsig$intGoff converted to ISO format is $strISOdate"
(EDIT later: upgraded version of this script
viewtopic.php?p=960070#p960070 )
and after that, this is an addon to calculate UNIX "epoch" time. This cont timezione of the routerboard.
I do not count TimeZone seconds because, at the time I write, do not exist any timezone with "seconds"
The script include the calculaton of lap year and the week day.
Still precise until 2100, but probably at that date we are all dead. Eh...
:global arrPreMonDays {"01"=0;"02"=31;"03"=59;"04"=90;"05"=120;"06"=151;"07"=181;"08"=212;"09"=243;"10"=273;"11"=304;"12"=334}
# 1970-01-01 is a Thursday
:global arrWeekDays {"Thu";"Fri";"Sat";"Sun";"Mon";"Tue";"Wed"}
# first bixestile year immediately before 1970 is 1968
:global numTotalDays (($intYear - 1968) / 4)
:global bolLeapYear false
:if ((($intYear - 1968) % 4) = 0) do={:set bolLeapYear true; :set ($arrPreMonDays->"01") -1; :set ($arrPreMonDays->"02") 30}
:set numTotalDays ($numTotalDays + (($intYear - 1970) * 365))
:set numTotalDays ($numTotalDays + ($arrPreMonDays->$strMonth))
:set numTotalDays ($numTotalDays + ([:tonum $strDay] - 1))
:global strWeekDay ($arrWeekDays->($numTotalDays % 7))
:global numTotHours (($numTotalDays * 24) + [:tonum $strHour])
:global numTotMinutes (($numTotHours * 60) + [:tonum $strMinute])
:global numTotSeconds (($numTotMinutes * 60) + [:tonum $strSecond] - $intGoff)
:put "For $strISOdate UNIX time is $numTotSeconds the current year is a lap year:$bolLeapYear and the Week Day is $strWeekDay"
(EDIT later: upgraded version of this script
viewtopic.php?p=960070#p960070 )
Next step is convert all to 3 single functions: date2iso, date2epoch, date2weekday.
Function to check if year is leap year or not is useless. (simply check if this
((($checkYear - 1968) % 4) = 0) is true or not)
The functions already internally must check if the passed date is valid or not,
and accept any "log format type".