Community discussions

Do you find this script useful?

Poll ended at Tue Nov 05, 2013 9:27 pm

Yes
No votes
No
No votes
Maybe
1 (100%)
I don't know
No votes
 
Total votes: 1
 
adeeadee
just joined
Topic Author
Posts: 4
Joined: Sun Aug 21, 2011 8:23 pm

I did it! Script to compute UNIX time!

Wed Aug 07, 2013 10:27 pm

Hi there.
Since version 6.2 I try to make a script to compute the UNIX time (epoch time / posix time) from RB current date/time.
The script contains 2 functions: first one is JD which calculates Julian Days for specified date and the second one calculates the unix time based on difference between Julian Days and Julian date of 'jan/01/1970'.
I guess is not flawless, but here it is 8) :
:global fncJD do={
:local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"];
:local jd
:local M [:pick $1 0 3];
:local D [:pick $1 4 6];
:local Y [:pick $1 7 11];
:for x from=0 to=([:len $months] - 1) do={
   :if ([:tostr [:pick $months $x]] = $M) do={:set M ($x + 1) } 
   }
:if ( $M = 1 || $M = 2) do={
    :set Y ($Y-1);
    :set M ($M+12);
}
:local A ($Y/100)
:local B ($A/4)
:local C (2-$A+$B)
:local E ((($Y+4716) * 36525)/100)
:local F ((306001*($M+1))/10000)
:local jd ($C+$D+$E+$F-1525)
:return $jd
};

:global timestamp do={
:global fncJD $fncJD
:local currtime [/system clock get time];
:local jdnow [$fncJD [/system clock get date]]
:local days ($jdnow - 2440587)
:local ore [:pick $currtime 0 2]
:local minute [:pick $currtime 3 5]
:local secunde [:pick $currtime 6 8]
:return (($days * 86400) + ($ore * 3600) + ($minute * 60) + $secunde - [/system clock get gmt-offset]);
}
After running the script just declare global function in your script and execute:
:global timestamp $timestamp
:put [$timestamp]
You can always post suggestions or enhancements to this script.
Last edited by adeeadee on Thu Aug 08, 2013 9:35 am, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2407
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: I did it! Script to compute UNIX time!

Wed Aug 07, 2013 10:41 pm

But why are you using Julian date as opposed to Gregorian?

Also, since 6.2 allows functions to accept arguments and :return, consider using that capability, since a timestamp is generally something to be used as part of a greater whole.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
adeeadee
just joined
Topic Author
Posts: 4
Joined: Sun Aug 21, 2011 8:23 pm

Re: I did it! Script to compute UNIX time!

Wed Aug 07, 2013 10:46 pm

Julian date is the simplest algorithm I found that can be implemented to RouterOS in order to compute days since 1970.
 
januszzz
Member Candidate
Member Candidate
Posts: 102
Joined: Wed Oct 07, 2009 9:17 pm

Re: I did it! Script to compute UNIX time!

Wed Oct 30, 2013 12:27 pm

Hello,

so I guess its impossible to get the time in seconds since 1970-01-01 00:00:00 UTC in any current RouterOS directly?

I need to count users' session time and parsing rsyslog precision time is harder than log in date +%s.
 
itnot
just joined
Posts: 1
Joined: Mon Aug 29, 2016 9:02 pm

Re: I did it! Script to compute UNIX time!

Fri Dec 28, 2018 1:36 pm

Hello.

Many thanks to adeeadee.

This script was useful to me.
I use it when transferring GPS data to the monitoring site.

Regards,

Who is online

Users browsing this forum: No registered users and 4 guests