Community discussions

MikroTik App
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Convert uptime to date and time

Sun May 01, 2022 8:57 pm

Greetings to all and Happy Labor Day, there is the possibility that with a script you can convert the UPTIME of a routerboard in the time and date of for example this:

1w 3d 13:42:38.





EL DONCITO.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Convert uptime to date and time

Sun May 01, 2022 10:52 pm

Convert this:
:put [/system/resource/get uptime]
5w4d12:38:02
to what?
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Convert uptime to date and time

Sun May 01, 2022 10:58 pm

Thanks for answering JOTNE, convert 5w3d13:35:38 in the time and date the routerboard was rebooted.







EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Mon May 02, 2022 3:29 am

Is extremly tedious to do that, on mikrotik "datetime" do not exist and is not possible to simply subtract from current "datetime" the uptime value.
Also, with subsequent subtraction, is not impossible, but overcomplicated because you must count how many days are in one month, if have Feb29 or not, etc.

You have a craze for these uptime and startup time.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Mon May 02, 2022 5:06 am

search tags # rextended online uptime start date time

Work correctly only if RouterBOARD time is syncronized.

(compatible also with new date format on RouterOS 7.10+)
: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]])]"
Last edited by rextended on Thu May 11, 2023 3:03 am, edited 30 times in total.
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Convert uptime to date and time

Tue May 03, 2022 4:10 am

Grateful for your help friend Rextended, and sorry for so much trouble but the date instead of being in the YY-MM-DD format, you could change it to DD-MM-YY.



RouterBOARD is started 2022-05-02 12:36:45
RouterBOARD is started 02-05-2022 12:36:45.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Tue May 03, 2022 10:43 am

You have already all the other scripts maded for you, apply the other scripts to the $datetimeStart at the end.

Is time you study and try for yourself... :lol:
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Convert uptime to date and time

Tue May 03, 2022 2:52 pm

REX friend don't be selfish with me, tell me how I can change.
And I don't bother you anymore for now.



2022-05-02

02-05-2022



EL DONCITO
Last edited by eldoncito2019 on Tue May 03, 2022 3:00 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Tue May 03, 2022 3:00 pm

Selfish? After all the scripts I made for you? What ungrateful... 😥
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Convert uptime to date and time

Tue May 03, 2022 3:02 pm

Help me please friend REX

This is my last question for now.





EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Tue May 03, 2022 3:02 pm

You never bother me when you ask for something new and interesting...

Ok, wait some minutes... :roll:
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time  [SOLVED]

Tue May 03, 2022 3:14 pm

For you...

Special Venezuelan version for El Doncito:
: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]])]"

The "International ISO Version" is here:
viewtopic.php?p=930833#p930465
Last edited by rextended on Thu May 11, 2023 3:04 am, edited 21 times in total.
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Convert uptime to date and time

Tue May 03, 2022 3:14 pm

Thank you very much friend REX as we say here in my country, Thank you my "PANA".







EL DONCITO.
Last edited by eldoncito2019 on Tue May 03, 2022 3:18 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Tue May 03, 2022 3:16 pm

take a closer look: expecting you, I've already done it.. even with the Venezuelan months... :lol:
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Convert uptime to date and time

Tue May 03, 2022 3:26 pm

Eres mi "PANA" REX.





EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Tue May 03, 2022 3:32 pm

Grazie...
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Convert uptime to date and time

Wed May 04, 2022 3:28 am

REX friend, why does this appear when I execute the script in the console?

failure: connection timeout
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Wed May 04, 2022 4:03 am

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.
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Convert uptime to date and time

Wed May 04, 2022 1:38 pm

OK, and tell me one last thing, should I set the script to start when the ROUTERBOARD starts?







EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Wed May 04, 2022 1:49 pm

yes, but first put a delay until internet is not available
:delay 120s
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Convert uptime to date and time

Wed May 04, 2022 8:02 pm

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.


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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Wed May 04, 2022 9:33 pm

The problem is not the 1st call, is the second, I do not find anything to convert online specific epoch to date....
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Thu Jan 12, 2023 11:34 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Jan 13, 2023 1:03 am

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Jan 13, 2023 1:10 am

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).
The script is updated:

viewtopic.php?p=930465#p930465
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Fri Jan 13, 2023 1:17 am

OK, thanks.

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Jan 13, 2023 1:24 am

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.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Fri Jan 13, 2023 1:36 am

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.
Last edited by diamuxin on Fri Jan 13, 2023 1:41 am, edited 1 time in total.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Fri Jan 13, 2023 1:39 am

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.
Well, nothing, we have to wait, thank you.

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Jan 13, 2023 1:48 am

uhm....
Last edited by rextended on Fri Jan 13, 2023 2:06 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Jan 13, 2023 2:05 am

uhm ...
Last edited by rextended on Fri Jan 13, 2023 2:38 am, edited 1 time in total.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Fri Jan 13, 2023 2:24 am

Try it with your uptime, it's the best test.

Italy and Spain have the same time zone.

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Jan 13, 2023 2:37 am

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Jan 13, 2023 2:41 am

uhm ...
Last edited by rextended on Fri Jan 13, 2023 3:22 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Jan 13, 2023 3:19 am

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Jan 13, 2023 3:39 am

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"
}
Last edited by rextended on Thu May 11, 2023 3:06 am, edited 6 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Jan 13, 2023 4:10 am

search tag # rextended convert time format (like 45w10d14:32:45.37513571) to seconds
: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
}

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]]]
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Fri Jan 13, 2023 12:52 pm

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
Now perfect Rex!

Thanks for your work!

Greetings.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Thu Feb 09, 2023 8:03 pm

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):
<uptime>546837</uptime>
(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.

..
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Feb 10, 2023 12:54 pm

(I do not see any time-zone related consideration)

warning: the calcs on my post are not accurate, but just used for examples.

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):
<uptime>546837</uptime>
(That numeric value I already have it extracted and assigned to a local variable)

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]]]
}

results code

6d07:53:57
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Fri Feb 10, 2023 1:42 pm

Yes, I want as I said in the previous post is, starting from an uptime in seconds by value XXXXXXXXX sg, convert it to an easily understandable value, it does not matter if it is with Mo+Days+Hours+Minutes+Seconds format or YYYY-MM-DD hh.mm.ss. I don't know if I have explained myself well.

I understand the explanation, clarified.
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.

EDIT: Ops! I did not know the :totime function. Much simpler, thanks.


..
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Fri Feb 10, 2023 2:33 pm

Even better, taking the functions you created for the Mikrotik Uptime, it can work for me as well and is quite accurate.

viewtopic.php?p=977167#p930465

Now it would look better:
{
: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)
BR.
Last edited by diamuxin on Fri Feb 10, 2023 2:53 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Fri Feb 10, 2023 2:43 pm

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.
(assuming 3 is day, and 2 the month)
The "ISO" date 2023-02-03 (or without the "-" 20230203) is directly sortable,
any other format (regardless the separator is "/", ".", "-" or others, NOT... :lol:
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Tue Feb 28, 2023 2:52 am

search tags # rextended online uptime start date time

Work correctly only if RouterBOARD time is syncronized.
Hi @rextended!

With this "approximate" data, I think there is a small mismatch in your script, because there is something strange in the result:

Uptime: 00:25:39
Local Date: 28/02/2023 01:39:10
:put [$unixtodatetime ([$currdatetimestr] - [$timetoseconds [/system resource get uptime]])]
current output: 00/03/2023 01:13:39

The date should be: 28/02/2023 01:13:39

Can you check it? Thanks.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Tue Feb 28, 2023 3:26 am

Ok, I found two bug, fixed.
Update previous script.

For curiosity, added this:

x code

    :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...
And I can not know the bissexstile years passed until I do not calc the total number of years :lol: ..

and added = after >

x code

    :local mnthStart 12 ; :while (($prMntDays->$mnthStart) >= tmpdays) do={:set mnthStart ($mnthStart - 1)}
because not exist the month day "0", must go on previous month.

I hope is not present other bugs.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert uptime to date and time

Tue Feb 28, 2023 11:44 am

Ok, I found two bug, fixed.
Update previous script.
The script works fine now, thanks!

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert uptime to date and time

Tue Feb 28, 2023 12:54 pm

The script works fine now, thanks!
Thanks to you.

Please, if you find a bug on any of my scripts, notify me.
Thanks.

Who is online

Users browsing this forum: rextended and 25 guests