:put [/system/resource/get uptime]
5w4d12:38:02
:global currdatetimestr do={
/system clock
:local vdate [get date]
:local vtime [get time]
:local vgmt [:tonum [get gmt-offset]]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
:local prMntDays [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
:local LcaseMnts [:toarray "0,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"]
:local PcaseMnts [:toarray "0,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"]
:local UcaseMnts [:toarray "0,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC"]
:local LcaseWeekDays [:toarray "thu,fri,sat,sun,mon,tue,wed"]
:local PcaseWeekDays [:toarray "Thu,Fri,Sat,Sun,Mon,Tue,Wed"]
:local UcaseWeekDays [:toarray "THU,FRI,SAT,SUN,MON,TUE,WED"]
:local Fzerofill do={:return [:pick (100 + $1) 1 3]}
:local gmtSg "+"; :if ($vgmt < 0) do={:set gmtSg "-"; :set vgmt ($vgmt * -1)}
:local gmtHr [:pick [:totime $vgmt] 0 2]
:local gmtMn [:pick [:totime $vgmt] 3 5]
: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)}
:set MM [:pick (100 + $M) 1 3]
}
:local yyyy [:pick $vdate ($vdoff->0) ($vdoff->1)]
:local Leap "No-Leap" ; :if ((($yyyy - 1968) % 4) = 0) do={:set Leap "Leap"; :set ($prMntDays->1) -1; :set ($prMntDays->2) 30}
:local mmm ($LcaseMnts->$M)
:local Mmm ($PcaseMnts->$M)
:local MMM ($UcaseMnts->$M)
:local dd [:pick $vdate ($vdoff->4) ($vdoff->5)]
:local d [:tonum $dd] ; :local totd ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($prMntDays->$M) + ($d - 1))
:local www ($LcaseWeekDays->($totd % 7))
:local Www ($PcaseWeekDays->($totd % 7))
:local WWW ($UcaseWeekDays->($totd % 7))
:local HH [:pick $vtime 0 2]
:local H [:tonum $HH]
:local hh ([:tonum $HH] % 12); :if ($hh = 0) do={:set hh 12}; :set hh [$Fzerofill $hh]
:local h [:tonum $hh]
:local a "A"; :if ([:tonum $HH] > 11) do={:set a "P"}
:local aa "$a\4D"
:local mm [:pick $vtime 3 5]
:local m [:tonum $mm]
:local ss [:pick $vtime 6 8]
:local s [:tonum $ss]
:local Z "$gmtSg$gmtHr:$gmtMn"
:local Unix (((((($totd * 24) + $H) * 60) + $m) * 60) + $s - $vgmt)
# :return "$yyyy-$MM-$dd\54$HH:$mm:$ss$Z $Www $Leap $Unix"
:return $Unix
}
:global unixtodatetime do={
:local ux [:tonum $1]
:local Fzerofill do={:return [:pick (100 + $1) 1 3]}
:local prMntDays [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
:local vgmt [:tonum [/system clock get gmt-offset]]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
:if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
:local tzepoch ($ux + $vgmt)
:if ($tzepoch < 0) do={:set tzepoch 0} ; # unsupported negative unix epoch
:local yearStart (1970 + ($tzepoch / 31536000))
:local tmpbissex (($yearStart - 1968) / 4) ; :if ((($yearStart - 1968) % 4) = 0) do={:set ($prMntDays->1) -1 ; :set ($prMntDays->2) 30}
:local tmpsec ($tzepoch % 31536000)
:local tmpdays (($tmpsec / 86400) - $tmpbissex)
:if (($tmpsec < (86400 * $tmpbissex)) and ((($yearStart - 1968) % 4) = 0)) do={
:set tmpbissex ($tmpbissex - 1) ; :set ($prMntDays->1) 0 ; :set ($prMntDays->2) 31 ; :set tmpdays ($tmpdays + 1)
}
:if ($tmpsec < (86400 * $tmpbissex)) do={:set yearStart ($yearStart - 1) ; :set tmpdays ($tmpdays + 365)}
:local mnthStart 12 ; :while (($prMntDays->$mnthStart) > $tmpdays) do={:set mnthStart ($mnthStart - 1)}
:local dayStart [$Fzerofill (($tmpdays + 1) - ($prMntDays->$mnthStart))]
:local timeStart (00:00:00 + [:totime ($tmpsec % 86400)])
:return "$yearStart/$[$Fzerofill $mnthStart]/$[$Fzerofill $dayStart] $timeStart"
}
:global timetoseconds do={
:local inTime $1
:local wPos [:find $inTime "w" -1]
:local dPos [:find $inTime "d" -1]
:local itLen [:find $inTime "." -1] ; :if ([:typeof $itLen] = "nil") do={:set itLen [:len $inTime]}
:local itSec [:pick $inTime ($itLen - 2) $itLen]
:local itMin [:pick $inTime ($itLen - 5) ($itLen - 3)]
:local itHou [:pick $inTime ($itLen - 8) ($itLen - 6)]
:local itDay 0
:local itWee 0
:if (([:typeof $wPos] = "nil") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime 0 $dPos] }
:if (([:typeof $wPos] = "num") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime ($wPos + 1) $dPos] }
:if ([:typeof $wPos] = "num") do={:set itWee [:pick $inTime 0 $wPos] }
:local totitSec ($itSec + (60 * $itMin) + (3600 * $itHou) + (86400 * $itDay) + (604800 * $itWee))
:return $totitSec
}
:put "RouterBOARD is started $[$unixtodatetime ([$currdatetimestr] - [$timetoseconds [/system resource get uptime]])]"
:global currdatetimestr do={
/system clock
:local vdate [get date]
:local vtime [get time]
:local vgmt [:tonum [get gmt-offset]]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
:local prMntDays [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
:local LcaseMnts [:toarray "0,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"]
:local PcaseMnts [:toarray "0,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"]
:local UcaseMnts [:toarray "0,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC"]
:local LcaseWeekDays [:toarray "thu,fri,sat,sun,mon,tue,wed"]
:local PcaseWeekDays [:toarray "Thu,Fri,Sat,Sun,Mon,Tue,Wed"]
:local UcaseWeekDays [:toarray "THU,FRI,SAT,SUN,MON,TUE,WED"]
:local Fzerofill do={:return [:pick (100 + $1) 1 3]}
:local gmtSg "+"; :if ($vgmt < 0) do={:set gmtSg "-"; :set vgmt ($vgmt * -1)}
:local gmtHr [:pick [:totime $vgmt] 0 2]
:local gmtMn [:pick [:totime $vgmt] 3 5]
: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)}
:set MM [:pick (100 + $M) 1 3]
}
:local yyyy [:pick $vdate ($vdoff->0) ($vdoff->1)]
:local Leap "No-Leap" ; :if ((($yyyy - 1968) % 4) = 0) do={:set Leap "Leap"; :set ($prMntDays->1) -1; :set ($prMntDays->2) 30}
:local mmm ($LcaseMnts->$M)
:local Mmm ($PcaseMnts->$M)
:local MMM ($UcaseMnts->$M)
:local dd [:pick $vdate ($vdoff->4) ($vdoff->5)]
:local d [:tonum $dd] ; :local totd ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($prMntDays->$M) + ($d - 1))
:local www ($LcaseWeekDays->($totd % 7))
:local Www ($PcaseWeekDays->($totd % 7))
:local WWW ($UcaseWeekDays->($totd % 7))
:local HH [:pick $vtime 0 2]
:local H [:tonum $HH]
:local hh ([:tonum $HH] % 12); :if ($hh = 0) do={:set hh 12}; :set hh [$Fzerofill $hh]
:local h [:tonum $hh]
:local a "A"; :if ([:tonum $HH] > 11) do={:set a "P"}
:local aa "$a\4D"
:local mm [:pick $vtime 3 5]
:local m [:tonum $mm]
:local ss [:pick $vtime 6 8]
:local s [:tonum $ss]
:local Z "$gmtSg$gmtHr:$gmtMn"
:local Unix (((((($totd * 24) + $H) * 60) + $m) * 60) + $s - $vgmt)
# :return "$yyyy-$MM-$dd\54$HH:$mm:$ss$Z $Www $Leap $Unix"
:return $Unix
}
:global unixtodatetime do={
:local ux [:tonum $1]
:local Fzerofill do={:return [:pick (100 + $1) 1 3]}
:local prMntDays [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
:local vgmt [:tonum [/system clock get gmt-offset]]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
:if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
:local tzepoch ($ux + $vgmt)
:if ($tzepoch < 0) do={:set tzepoch 0} ; # unsupported negative unix epoch
:local yearStart (1970 + ($tzepoch / 31536000))
:local tmpbissex (($yearStart - 1968) / 4) ; :if ((($yearStart - 1968) % 4) = 0) do={:set ($prMntDays->1) -1 ; :set ($prMntDays->2) 30}
:local tmpsec ($tzepoch % 31536000)
:local tmpdays (($tmpsec / 86400) - $tmpbissex)
:if (($tmpsec < (86400 * $tmpbissex)) and ((($yearStart - 1968) % 4) = 0)) do={
:set tmpbissex ($tmpbissex - 1) ; :set ($prMntDays->1) 0 ; :set ($prMntDays->2) 31 ; :set tmpdays ($tmpdays + 1)
}
:if ($tmpsec < (86400 * $tmpbissex)) do={:set yearStart ($yearStart - 1) ; :set tmpdays ($tmpdays + 365)}
:local mnthStart 12 ; :while (($prMntDays->$mnthStart) > $tmpdays) do={:set mnthStart ($mnthStart - 1)}
:local dayStart [$Fzerofill (($tmpdays + 1) - ($prMntDays->$mnthStart))]
:local timeStart (00:00:00 + [:totime ($tmpsec % 86400)])
# :return "$yearStart/$[$Fzerofill $mnthStart]/$[$Fzerofill $dayStart] $timeStart"
:return "$[$Fzerofill $dayStart]/$[$Fzerofill $mnthStart]/$yearStart $timeStart"
}
:global timetoseconds do={
:local inTime $1
:local wPos [:find $inTime "w" -1]
:local dPos [:find $inTime "d" -1]
:local itLen [:find $inTime "." -1] ; :if ([:typeof $itLen] = "nil") do={:set itLen [:len $inTime]}
:local itSec [:pick $inTime ($itLen - 2) $itLen]
:local itMin [:pick $inTime ($itLen - 5) ($itLen - 3)]
:local itHou [:pick $inTime ($itLen - 8) ($itLen - 6)]
:local itDay 0
:local itWee 0
:if (([:typeof $wPos] = "nil") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime 0 $dPos] }
:if (([:typeof $wPos] = "num") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime ($wPos + 1) $dPos] }
:if ([:typeof $wPos] = "num") do={:set itWee [:pick $inTime 0 $wPos] }
:local totitSec ($itSec + (60 * $itMin) + (3600 * $itHou) + (86400 * $itDay) + (604800 * $itWee))
:return $totitSec
}
:put "RouterBOARD is started $[$unixtodatetime ([$currdatetimestr] - [$timetoseconds [/system resource get uptime]])]"
:delay 120s
Try to open the page used for the service:
https://helloacm.com/
Is not mine, if do not work, you must wait than it come back.
The script is updated:it seems that the external access of the "uptime" script has failed again, it has been a few days since I got the time correctly (my time zone is Spain).
I repeat:OK, thanks.
BR.
Well, nothing, we have to wait, thank you.I repeat:
I do not make till now a function to convert back unix epoch to date and time, but on future I write a reverse function.
Till I finish the function or I find another URL, the function provide the correct unix epoch start time of the routerboard, but not the correct time on the date & time.
:global unixtodatetime do={
:local ux [:tonum $1]
:local Fzerofill do={:return [:pick (100 + $1) 1 3]}
:local prMntDays [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
:local vgmt [:tonum [/system clock get gmt-offset]]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
:if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
:local tzepoch ($ux + $vgmt)
:if ($tzepoch < 0) do={:set tzepoch 0} ; # unsupported negative unix epoch
:local yearStart (1970 + ($tzepoch / 31536000))
:local tmpbissex (($yearStart - 1968) / 4) ; :if ((($yearStart - 1968) % 4) = 0) do={:set ($prMntDays->1) -1 ; :set ($prMntDays->2) 30}
:local tmpsec ($tzepoch % 31536000)
:local tmpdays (($tmpsec / 86400) - $tmpbissex)
:if (($tmpsec < (86400 * $tmpbissex)) and ((($yearStart - 1968) % 4) = 0)) do={
:set tmpbissex ($tmpbissex - 1) ; :set ($prMntDays->1) 0 ; :set ($prMntDays->2) 31 ; :set tmpdays ($tmpdays + 1)
}
:if ($tmpsec < (86400 * $tmpbissex)) do={:set yearStart ($yearStart - 1) ; :set tmpdays ($tmpdays + 365)}
:local mnthStart 12 ; :while (($prMntDays->$mnthStart) > $tmpdays) do={:set mnthStart ($mnthStart - 1)}
:local dayStart [$Fzerofill (($tmpdays + 1) - ($prMntDays->$mnthStart))]
:local timeStart (00:00:00 + [:totime ($tmpsec % 86400)])
:return "$yearStart/$[$Fzerofill $mnthStart]/$[$Fzerofill $dayStart] $timeStart"
}
:global timetoseconds do={
:local inTime $1
:local wPos [:find $inTime "w" -1]
:local dPos [:find $inTime "d" -1]
:local itLen [:find $inTime "." -1] ; :if ([:typeof $itLen] = "nil") do={:set itLen [:len $inTime]}
:local itSec [:pick $inTime ($itLen - 2) $itLen]
:local itMin [:pick $inTime ($itLen - 5) ($itLen - 3)]
:local itHou [:pick $inTime ($itLen - 8) ($itLen - 6)]
:local itDay 0
:local itWee 0
:if (([:typeof $wPos] = "nil") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime 0 $dPos] }
:if (([:typeof $wPos] = "num") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime ($wPos + 1) $dPos] }
:if ([:typeof $wPos] = "num") do={:set itWee [:pick $inTime 0 $wPos] }
:local totitSec ($itSec + (60 * $itMin) + (3600 * $itHou) + (86400 * $itDay) + (604800 * $itWee))
:return $totitSec
}
:put [$timetoseconds [:timestamp]]
:put [$unixtodatetime [$timetoseconds [:timestamp]]]
Now perfect Rex!The logic is correct, I fix the script, now work (if the time on routerboard is syncronized) without using external sites.
viewtopic.php?p=977167#p930465
(That numeric value I already have it extracted and assigned to a local variable)<uptime>546837</uptime>
# Convert seconds to month.days.hours.minutes.seconds
# Use: [$sec2mdhms 59043245] output: 24mo.11d.8h.54m.5s
# v1.0 by diamuxin
# v1.1 Added code snippet to obtain the days in the current month, by rextended
:global sec2mdhms do={
# number of days in the current month
:local mm [:pick [/system clock get date] 0 3]; :local yyyy [:pick [/system clock get date] 7 11]
:local arrDaysOnMonths {jan="31";feb="28";mar="31";apr="30";may="31";jun="30";jul="31";aug="31";sep="30";oct="31";nov="30";dec="31"}
:if ((($yyyy - 1972) % 4) = 0) do={:set ($arrDaysOnMonths->"feb") 29}
:local dd [:tonum ($arrDaysOnMonths->$mm)]
:local totalSeconds [:tonum $1]
:local Mo ($totalSeconds / 60 / 60 / 24 / $dd)
:local D ($totalSeconds / 60 / 60 / 24 % $dd)
:local H ($totalSeconds / 60 / 60 % 24)
:local M ($totalSeconds / 60 % 60)
:local S ($totalSeconds % 60)
:if ($Mo > 0) do={:return ("$Mo"."mo"."."."$D"."d"."."."$H"."h"."."."$M"."m"."."."$S"."s")}
:if ($D > 0) do={:return ("$D"."d"."."."$H"."h"."."."$M"."m"."."."$S"."s")}
:if ($H > 0) do={:return ("$H"."h"."."."$M"."m"."."."$S"."s")}
:if ($M > 0) do={:return ("$M"."m"."."."$S"."s")}
:if ($S > 0) do={:return ("$S"."s")}
}
# number of days in the current month, by rextended
:local mm [:pick [/system clock get date] 0 3]; :local yyyy [:pick [/system clock get date] 7 11]
:local arrDaysOnMonths {jan="31";feb="28";mar="31";apr="30";may="31";jun="30";jul="31";aug="31";sep="30";oct="31";nov="30";dec="31"}
:if ((($yyyy - 1972) % 4) = 0) do={:set ($arrDaysOnMonths->"feb") 29}
:local dd [:tonum ($arrDaysOnMonths->$mm)]
Ciao rextended,
Today I come with a new challenge, I want to convert an Uptime figure of a remote device that I want to control, the detail that the value is in seconds (I think):
(That numeric value I already have it extracted and assigned to a local variable)<uptime>546837</uptime>
{
:local readut "<uptime>546837</uptime>"
:put [:totime [:pick $readut ([:find $readut ">" -1] + 1) [:find $readut "</" -1]]]
}
6d07:53:57
Uptime is one absolute value and must do not consider leap years or timezone,
is the number of the seconds that CPU is up, regardeless any other conventional date and time rapresentation.
No matter if are seconds or text like 97w4d08:54:05.
Is also why is writed 97w4d08:54:05 and not like 1y10m3w4d08:54:05, because months and years do not are univocal.
{
:global tokenParser
:global unixtodatetime
:global currdatetimestr
:local xmlData ([toarray [/tool fetch mode=https url="http://192.168.8.1/api/device/information" as-value output=user]] ->"data")
:local smsUptime [($tokenParser->"getTag") source=$xmlData tag="uptime"]; # uptime value e.g.: 618694
:put [$unixtodatetime ([$currdatetimestr] - $smsUptime)]
}
Result: 03/02/2023 09:40:27
(the date format I have modified for my internal use)
Is not my choice, but logical choice, represent date and time on logic way: on the left the higher values, like on numbers.Result: 03/02/2023 09:40:27
(the date format I have modified for my internal use)
Hi @rextended!search tags # rextended online uptime start date time
Work correctly only if RouterBOARD time is syncronized.
:put [$unixtodatetime ([$currdatetimestr] - [$timetoseconds [/system resource get uptime]])]
:if ($tmpsec < (86400 * $tmpbissex)) do={ :set yearStart ($yearStart - 1) :set tmpdays ($tmpdays + 365) }When years are calculated, if are not passed already the number of bissextile days, is still the previous year...
:local mnthStart 12 ; :while (($prMntDays->$mnthStart) >= tmpdays) do={:set mnthStart ($mnthStart - 1)}because not exist the month day "0", must go on previous month.
The script works fine now, thanks!Ok, I found two bug, fixed.
Update previous script.
Thanks to you.The script works fine now, thanks!