REX and there is no other page?, or another way that is not with a page.
For example this: "http://worldclockapi.com/api/json/vst/now\""
EL DONCITO.
The problem is not the 1st call, is the second, I do not find anything to convert online specific epoch to date…
Hello,
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 have restarted it to install the new 7.7 and it shows me that I have restarted on 01/13/2023 at 05:24 am.
¿?
BR.
For the series “do it yourself”, I revised the unix time generator already on my Snippets.
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.
In meantime I hope to find some unix->datetime online.
The script is updated:
http://forum.mikrotik.com/t/convert-uptime-to-date-and-time/157724/5
OK, thanks.
BR.
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.
Ops! it does not work… ![]()
My Uptime is 02:08:00
My time is approx 00:34 - 01/13/2023
Script output:
RouterBOARD is started 1673558651 (2023-01-13 04:24:11) → quite early hour
BR.
Well, nothing, we have to wait, thank you.
BR.
uhm…
uhm …
Try it with your uptime, it’s the best test.
Italy and Spain have the same time zone.
BR.
What time zone is set on the routerboard???
1673558651 is
12 January 2023 21:24:11 on GMT
and 12 January 2023 22:24:11 on GMT+01:00
uhm …
The logic is correct, I fix the script, now work (if the time on routerboard is syncronized) without using external sites.
search tag # rextended convert Unix Epoch to datetime
: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"
}
search tag # rextended convert time format (like 45w10d14:32:45.37513571) to seconds
:global timetoseconds do={
:local inTime [:totime $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
}
For example, for obtain Unix Epoch Time on v7:
:put [$timetoseconds [:timestamp]]
For example, convert v7 [:timestap] to datetime, using also previous post $unixtodatetime function:
:put [$unixtodatetime [$timetoseconds [:timestamp]]]
Now perfect Rex!
Thanks for your work!
Greetings.
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):
546837
(That numeric value I already have it extracted and assigned to a local variable)
I have designed a simple function to get me out of trouble:
# 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")}
}
Is it possible to improve the part of the days of the month so that the calculation is better depending on the number of days in each month? Right now it calculates the seconds of a 30-day month.
EDIT: I have found in this forum, a code fragment that you posted on another occasion and it is valid since it takes into account leap years:
# 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)]
Thanks.
..
(I do not see any time-zone related consideration)
warning: the calcs on my post are not accurate, but just used for examples.
Let me understand, because I do not understand why you want involve leap years and current month days:
On uptime never matter current time, except you want calc the startup date,
that is not “24mo.11d.8h.54m.5s” but a precise date like the 2020/08/01 16:45:55 (not calculated, just as example)
You want convert 59043245 seconds to 24mo.11d.8h.54m.5s time format?
Just with :put [:totime 59043245] you obtain 97w4d08:54:05 and 97w + 4d = 683d =
22 months (of 30 days) and 23 days, and how is possible than are 24 month and 11d?
24mo.11d.8h.54m.5s (assuming 30 days/month) = 63190445 seconds… 48 days of differency
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.
And if for some reason you like 5y10m3w4d08:54:0 the leap year is relative to uptime, and only the 4th are bisextile, regerdless current year.
And if for some reason you like 70M25d08:54:0 the leap year is still relative to uptime,
and you must add one day each 96 month with 2 month offset, ((month - 2) / 96) regerdless current year.
one uptime of 5 years = 1825 days, not more, because if leap year must be calculated can be 1826 days or… 1827!!
If the leap year is the 2nr, 3rd, or 4th, must be added only one day, if are the 1st, and obviously also the 5th, you must add 2 days…
And if for hypothesys the years are 7, and precisely 2097, 2098, 2099, 2100, 2101, 2102, 2103, no leap year are present…
I do not understand your calc. You want obtain something like 2 year, 0 months, 11 days, 8h 54m 5s, than subtracted to ACTUAL date you obtain the bootup date?
Like if you have 24mo.8d.8h.22m.5s (2 year, 0mo.8d.8h.22m.5s) and you consider today 2023/02/10 11:31:14 date (my timezone) you obtain:
2023 - 2y = 2021
02 (feb) - 0 = 2
10 - 8 = 2
11 - 8 = 3
31 - 22 = 9
14 - 5 = 9
24mo.8d.8h.22m.5s (2 year, 0mo.8d.8h.22m.5s) - 2023/02/10 11:31:14 = device is started @ 2021/02/02 03:09:09
???
Please help me understand, that I did not understand anything about what you want obtain…
Probably this is for you:
{
:local readut "<uptime>546837</uptime>"
:put [:totime [:pick $readut ([:find $readut ">" -1] + 1) [:find $readut "</" -1]]]
}
6d07:53:57