Community discussions

 
xootraoox
just joined
Topic Author
Posts: 17
Joined: Fri Jan 31, 2014 5:24 am

Mikrotik time to seconds - Tiempo Mikrotik a segundos

Sun Feb 02, 2014 3:45 am

Sometimes you need to convert Mikrotik time notation in seconds, eg Uptime is delivered in this format "2w5d18: 22:18", the next is to take that string to seconds.

Aveces es necesario pasar la notación de tiempo de Mikrotik a segundos, por ejemplo el Uptime es entregado en este formato "2w5d18:22:18", el siguiente es para llevar ese string a un segundos.


Tested on -> 4.17, 5.26, 6.1, 6.9
Probado en -> 4.17, 5.26, 6.1, 6.9

ros code

:global UptimeSeconds 0;
:local uptime [/system resource get uptime];
:local weekend 0;
:local dayend 0;

:if ([:find $uptime "w" -1]!="") do={ :set weekend [:find $uptime "w" -1]; };
:if ([:find $uptime "d" -1]!="") do={ :set dayend [:find $uptime "d" -1]; }; 

:local weeks [:pick $uptime 0 $weekend];
:local days [:pick $uptime ($weekend+1) $dayend]; 

:local time [:pick $uptime ([:len $uptime]-8) [:len $uptime]]; 

:local hours [:pick $time 0 2];
:local minutes [:pick $time 3 5];
:local seconds [:pick $time 6 8]; 

:set UptimeSeconds [(((((($weeks*86400*7)+($days*86400))+($hours*3600))+($minutes*60))+$seconds))];
In my case I need that value in some scripts, then I run it through a scheduler every 5 seconds and the uptime in seconds it is available in a global variable for other scripts.

En mi caso necesito ese valor en algunos Scripts, entonces, mediante un scheduler lo ejecuto cada 5 segundos y el uptime en segundos esta disponible en una variable global para otros scripts.
Last edited by xootraoox on Tue Feb 04, 2014 7:40 am, edited 4 times in total.
 
User avatar
c0d3rSh3ll
Long time Member
Long time Member
Posts: 558
Joined: Mon Jul 25, 2011 9:42 pm
Location: [admin@Chile] >

Re: Mikrotik time to seconds - Tiempo Mikrotik a segundos

Tue Feb 04, 2014 4:08 am

Thank..
your script have some error, here is ok. Is working un 5.26 but not in 6.9.

el script tiene algunos errores, aca esta corregido. Lo probe en la version 5.26 y esta funcionando pero en la 6.9 no, cuando tenga tiempo lo revizare...

ros code

:local uptime [/system resource get uptime];
:local weekend 0;
:local dayend 0;
:if ([:find $uptime "w" -1]!="") do={ :set weekend [:find $uptime "w" -1]; };
:if ([:find $uptime "d" -1]!="") do={ :set dayend [:find $uptime "d" -1]; };
:local weeks [:pick $uptime 0 $weekend];
:local days [:pick $uptime ($weekend+1) $dayend];
:local time [:pick $uptime ([:len $uptime]-8) [:len $uptime]];
:local hours [:pick $time 0 2];
:local minutes [:pick $time 3 5];
:local seconds [:pick $time 6 8];
:global UptimeSeconds [($weeks*86400*7) + ($days*86400) + ($hours*3600) + ($minutes*60) + ($seconds)]; 
put $UptimeSeconds
nothing
 
xootraoox
just joined
Topic Author
Posts: 17
Joined: Fri Jan 31, 2014 5:24 am

Re: Mikrotik time to seconds - Tiempo Mikrotik a segundos

Tue Feb 04, 2014 5:22 am

You're right, forget to remove escape characters, I have corrected the post... Tnx.

Tienes razón, olvide quitar los escapes, he corregido el post... Gracias.

Script updated to run on ROS 4.17, 5.26, 6.1 and 6.9.
Script actualizado para correr en ROS 4.17, 5.26, 6.1 and 6.9.
 
User avatar
Sky18
newbie
Posts: 42
Joined: Sat Jan 13, 2018 9:21 am
Location: Germany

Re: Mikrotik time to seconds - Tiempo Mikrotik a segundos

Sun Mar 18, 2018 10:16 am

If RB is up and running less than one day / one week the :if condition check for "" makes trouble, at least in my script (ROS 6.41.2):
:if ([:find $uptime "w" -1]!="") do={ :set weekend [:find $uptime "w" -1]; };
:if ([:find $uptime "d" -1]!="") do={ :set dayend [:find $uptime "d" -1]; };

The following code "> 0" solved this trouble:
:if ([:find $uptime "w" -1] > 0) do={ :set weekend [:find $uptime "w" -1]; };
:if ([:find $uptime "d" -1] > 0) do={ :set dayend [:find $uptime "d" -1]; };
Network Rookie - with decades of experience in software development, Windows OS and PC hardware
 
mtrimarchi
just joined
Posts: 8
Joined: Wed Sep 07, 2016 10:43 am

Re: Mikrotik time to seconds - Tiempo Mikrotik a segundos

Thu Nov 22, 2018 6:51 pm

Sometimes you need to convert Mikrotik time notation in seconds, eg Uptime is delivered in this format "2w5d18: 22:18", the next is to take that string to seconds.
[...]


Tested on version 6.42.9:

# Get uptime in seconds
:global UptimeSeconds 0;
:global uptime [/system resource get uptime];
:global weekend 0;
:global dayend 0;
:global weeks 0;
:global days 0;

:if ([:find $uptime "w" -1] > 0) do={
    :set weekend [:find $uptime "w" -1];
    :set weeks [:pick $uptime 0 $weekend];
    :set weekend ($weekend+1);
};

:if ([:find $uptime "d" -1] > 0) do={
    :set dayend [:find $uptime "d" -1];
    :set days [:pick $uptime $weekend $dayend];
};

:global time [:pick $uptime ([:len $uptime]-8) [:len $uptime]]; 

:global hours [:pick $time 0 2];
:global minutes [:pick $time 3 5];
:global seconds [:pick $time 6 8]; 

:set UptimeSeconds [($weeks*86400*7+$days*86400+$hours*3600+$minutes*60+$seconds)];
MTCNA | MTCRE | MTCTCE | MTCINE

Who is online

Users browsing this forum: No registered users and 5 guests