Community discussions

MikroTik App
 
CenturyG
just joined
Topic Author
Posts: 12
Joined: Tue Nov 29, 2022 4:43 pm

Calculate the difference between two dates

Tue Dec 06, 2022 9:41 pm

Has anyone come up with this type of script that works in 6.48.6. I'm not finding any script that actually works in doing this "Calculate the difference between two dates"?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Calculate the difference between two dates

Tue Dec 06, 2022 11:38 pm

Is for this?
viewtopic.php?p=969986#p970782


Is very easy to compare two dates. I have all already ready...
On my Snippets you can find the method to convert date and time to unix epoch.
Convert both dates to unix epoch and compare it, you have the number of seconds between the two dates.
 
CenturyG
just joined
Topic Author
Posts: 12
Joined: Tue Nov 29, 2022 4:43 pm

Re: Calculate the difference between two dates

Fri Dec 09, 2022 1:35 am

Is for this?
viewtopic.php?p=969986#p970782


Is very easy to compare two dates. I have all already ready...
On my Snippets you can find the method to convert date and time to unix epoch.
Convert both dates to unix epoch and compare it, you have the number of seconds between the two dates.
First question is yes, and I'm back on the rail train in coming up with writing a script for this, can you lead me to your snippet to convert date and time. Does this convert into integers? Boss said it's possible to do this comparison. "if this time is less than or greater than between last link up time than last link down time do this-> disable ports 2-10, else do nothing" checking last link up time and down time as well as current time to determine if ports need to be disabled"

I just need some hand holding in doing the if's and else statements in the script. DO you have any book or something that I can do to understand MikroTik form of programming.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Calculate the difference between two dates

Fri Dec 09, 2022 11:26 am

Sorry, I don't know what to suggest since I taught myself everything:
I can program in other languages and this helped me with routeros, but there isn't much documentation on scripting.

You can look through my Snippets to use as examples and the forum is full of other users who have made example and working scripts.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Calculate the difference between two dates

Fri Dec 09, 2022 11:50 am

up = empty & down = empty: never go up (or down) [and link down counter must be oviously 0]

up = date & down = empty: go up only one time (no down) [and link down counter must be oviously 0] and is up from that datetime to current datetime

up = date & down = date
if up > down, the link is estabilished and go down for "link down" times, the differencies between the dates is how many time the interface is up from last down event.
on the up > down case, the differencie between actual datetime and "up" give how many time the interface is up.
if up = down, is one border case, because milliseconds are not readables, is considered like up > down, but other ckecks must be done
if up < down, the link is estabilished on the past, but go down [for "link down" times] and never go up again
on the up < down case, the differencie between actual datetime and "down" give how many time the interface is down.

This function convert datetime to epoch, and is present one example for the ethernet link up/down compare times.
The function do not check if is provided a valid date+time or not
:global datetime2epoch do={
    :local dtime [:tostr $1]
    /system clock
    :local cyear [get date] ; :if ($cyear ~ "....-..-..") do={:set cyear [:pick $cyear 0 4]} else={:set cyear [:pick $cyear 7 11]}
    :if (([:len $dtime] = 10) or ([:len $dtime] = 11)) do={:set dtime "$dtime 00:00:00"}
    :if ([:len $dtime] = 15) do={:set dtime "$[:pick $dtime 0 6]/$cyear $[:pick $dtime 7 15]"}
    :if ([:len $dtime] = 14) do={:set dtime "$cyear-$[:pick $dtime 0 5] $[:pick $dtime 6 14]"}
    :if ([:len $dtime] =  8) do={:set dtime "$[get date] $dtime"}
    :if ([:tostr $1] = "") do={:set dtime ("$[get date] $[get time]")}
    :local vdate [:pick $dtime 0 [:find $dtime " " -1]]
    :local vtime [:pick $dtime ([:find $dtime " " -1] + 1) [:len $dtime]]
    :local vgmt  [get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
    :if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
    :local arrm  [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
    :local vdoff [:toarray "0,4,5,7,8,10"]
    :local MM    [:pick $vdate ($vdoff->2) ($vdoff->3)]
    :local M     [:tonum $MM]
    :if ($vdate ~ ".../../....") do={
        :set vdoff [:toarray "7,11,1,3,4,6"]
        :set M     ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
        :if ($M>12) do={:set M ($M - 12)}
    }
    :local yyyy  [:pick $vdate ($vdoff->0) ($vdoff->1)] ; :if ((($yyyy - 1968) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
    :local totd  ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($arrm->$M) + ([:pick $vdate ($vdoff->4) ($vdoff->5)] - 1))
    :return      (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}

/interface
:foreach iface in=[find] do={
    :local if  [get $iface name]
    :local dwn [get $if link-downs]
    :local lup [get $if last-link-up-time]
    :local ldn [get $if last-link-down-time]
    :local sup [$datetime2epoch $lup]
    :local sdn [$datetime2epoch $ldn]
    :local snw [$datetime2epoch]
    :local sdf ($sup - $sdn)
    :if ( ([:typeof $lup] != "nil") and ([:typeof $ldn] != "nil")) do={
        :if ($sdf < 0) do={
            :put "\r\n*** WARNING ***: Interface $if go down $dwn times, is never go up again after >$ldn<, $($snw-$sdn) seconds ago!!!\r\n"
        } else={
            :put "The interface $if go down $dwn times. Last up is >$lup< and is up for $($snw-$sup) seconds"
        }
    } else={
        :if ([:typeof $lup] = "nil") do={
            :put "The interface $if is never go up from the start of the device."
        } else={
            :put "The interface $if is never go down, last up is >$lup< and is up for $($snw-$sup) seconds"
        }
    }
}

Who is online

Users browsing this forum: rogerioqueiroz and 15 guests