Community discussions

MikroTik App

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: 2411
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,
 
zfuzesi
just joined
Posts: 1
Joined: Thu Aug 16, 2018 2:00 pm

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

Fri Apr 26, 2019 7:18 pm

Hello, I'm working on automation scripts that also need unix timestamp. Here are my preliminary functions:
:global "parse_date" do={
  :local year [:pick $str 7 11]
  :local month [:find [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"] [:pick $str 0 3]]
  :local day [:pick $str 4 6]

  :set month ($month - 1)
  :if ($month <= 0) do={
    :set month ($month + 12)
    :set year ($year - 1)
  }

  :return (( \
      365 * year + year / 4 - year / 100 + year / 400 \
    + 367 * month / 12 - 30 \
    + day - 1 \
    - 719527 + 31 + 28 \
    ) * 86400)
}

:global "parse_time" do={
  :local hour [:pick $str 0 2]
  :local min [:pick $str 3 5]
  :local sec [:pick $str 6 8]
  :return (hour * 3600 + min * 60 + sec)
}

:global "parse_timestamp" do={
  :global "parse_date"
  :global "parse_time"
  :return ([$"parse_date" str=[:pick $str 0 11]] + [$"parse_time" str=[:pick $str 12 20]])
}

:global "today" do={
  :global "parse_date"
  :return [$"parse_date" str=[/system clock get date]]
}

:global "time" do={
  :global "parse_time"
  :return [$"parse_time" str=[/system clock get time]]
}

:global "now" do={
  :global "today"
  :global "time"
  :return ([$"today"] + [$"time"])
}
 
minks
just joined
Posts: 6
Joined: Sun Feb 23, 2020 10:05 pm
Contact:

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

Mon May 04, 2020 10:05 am

My variant of epoch time, seems the shortest =)
:global EpochTime do={
   :local ds [/system clock get date];
   :local months;
   :if ((([:pick $ds 9 11]-1)/4) != (([:pick $ds 9 11])/4)) do={
      :set months {"an"=0;"eb"=31;"ar"=60;"pr"=91;"ay"=121;"un"=152;"ul"=182;"ug"=213;"ep"=244;"ct"=274;"ov"=305;"ec"=335};
   } else={
      :set months {"an"=0;"eb"=31;"ar"=59;"pr"=90;"ay"=120;"un"=151;"ul"=181;"ug"=212;"ep"=243;"ct"=273;"ov"=304;"dec"=334};
   }
   :set ds (([:pick $ds 9 11]*365)+(([:pick $ds 9 11]-1)/4)+($months->[:pick $ds 1 3])+[:pick $ds 4 6]);
   :local ts [/system clock get time];
   :set ts (([:pick $ts 0 2]*60*60)+([:pick $ts 3 5]*60)+[:pick $ts 6 8]);
   :return ($ds*24*60*60 + $ts + 946684800 - [/system clock get gmt-offset]);
}
How to use:

:put [$EpochTime] 
1588575464
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1789
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

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

Mon May 04, 2020 10:24 am

Thanks for the script.
This is some I have complained to MT support about. Some like this should be built in to the RouterOS software.
As it is now, its impossible to handle time in RouterOS.
Example: From Cli, look at log time. For event less then 24 hour, only time is shown.
For event more than 24, moth in text and date + time is shown. How to then calculate some from this. Impossible..

Could this script be used for Log time as well?
/log print detail

time=apr/29 21:05:26 topics=system,error,critical message="login failure for user xxxx from 192.168.10.178 via ssh"
I would lime this time in epoc and not apr/29 21:05:26

PS, you do not need semicolon ; after each line, only between multiple commands on same lines.
This script sets 2100 as a leap year, but it is not. So far inn to the future that it should not be a problem :)
 
How to use Splunk to monitor your MikroTik Router(s)

MikroTik->Splunk
 
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1789
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

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

Mon May 04, 2020 4:09 pm

Here is an updated version that could be used to both get current time or convert data from input.

Example current time
:put [$EpochTime]
1588597644
Convert time
:put [$EpochTime "may/01 16:23:50"]
1588343030

EpochTime "15:23:50"]
1588598630
When date not give, it uses current day/month/year

:global EpochTime do={
	# Usage
	# $EpochTime [time input]
	# -----
	# Get current time
	# :put [$EpochTime]
	# 
	# Read log time in one of these two format "may/01 16:23:50" or "12:02:23" for log number *323
	# :put [$EpochTime [:log get *323 time]]

	:local ds
	:local ts
	if ([:len $1]=0) do={
		# Get "now time"
		:set ds [/system clock get date]
		:set ts [/system clock get time]
	} else={
	
		if ([:len $1]>8) do={
			# Use remote date and time and convert date
			:set ds "$[:pick $1 0 6]/$[:pick [/system clock get date] 7 11]"
			:set ts [:pick $1 7 15]
		} else={
			# Use remote time and get date
			:set ds [/system clock get date]
			:set ts $1
		}
	}
	:local months
	:if ((([:pick $ds 9 11]-1)/4) != (([:pick $ds 9 11])/4)) do={

		:set months {"an"=0;"eb"=31;"ar"=60;"pr"=91;"ay"=121;"un"=152;"ul"=182;"ug"=213;"ep"=244;"ct"=274;"ov"=305;"ec"=335}
	} else={
		:set months {"an"=0;"eb"=31;"ar"=59;"pr"=90;"ay"=120;"un"=151;"ul"=181;"ug"=212;"ep"=243;"ct"=273;"ov"=304;"ec"=334}
	}
	:set ds (([:pick $ds 9 11]*365)+(([:pick $ds 9 11]-1)/4)+($months->[:pick $ds 1 3])+[:pick $ds 4 6])
	:set ts (([:pick $ts 0 2]*60*60)+([:pick $ts 3 5]*60)+[:pick $ts 6 8])
	:return ($ds*24*60*60 + $ts + 946684800 - [/system clock get gmt-offset])
}
NB, not sure how it handles log around new year change, when year is missing.
NB time laps year 2100, 2200 etc are not handled. But should not be a problem ;)
Last edited by Jotne on Sun Jun 21, 2020 5:34 pm, edited 1 time in total.
 
How to use Splunk to monitor your MikroTik Router(s)

MikroTik->Splunk
 
 
minks
just joined
Posts: 6
Joined: Sun Feb 23, 2020 10:05 pm
Contact:

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

Mon May 04, 2020 7:48 pm

Of course,
i'm create the "shortest variant" for years between 2000 and 2099.
But for my purpose it's enough =)
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1789
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

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

Mon May 04, 2020 10:25 pm

Should do :)
 
How to use Splunk to monitor your MikroTik Router(s)

MikroTik->Splunk
 
 
colin
newbie
Posts: 47
Joined: Mon May 11, 2015 11:11 am

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

Sun Jun 21, 2020 5:05 pm

Here is an updated version that could be used to both get current time or convert data from input.

Example current time
:put [$EpochTime]
1588597644
Convert time
:put [$EpochTime "may/01 16:23:50"]
1588343030

EpochTime "15:23:50"]
1588598630
When date not give, it uses current day/month/year

:global EpochTime do={
	# Usage
	# $EpochTime [time input]
	# -----
	# Get current time
	# :put [$EpochTime]
	# 
	# Read log time in one of these two format "may/01 16:23:50" or "12:02:23" for log number *323
	# :put [$EpochTime [:log get *323 time]]

	:local ds
	:local ts
	if ([:len $1]=0) do={
		# Get "now time"
		:set ds [/system clock get date]
		:set ts [/system clock get time]
	} else={
	
		if ([:len $1]>8) do={
			# Use remote date and time and convert date
			:set ds "$[:pick $1 0 6]/$[:pick [/system clock get date] 7 11]"
			:set ts [:pick $1 7 15]
		} else={
			# Use remote time and get date
			:set ds [/system clock get date]
			:set ts $1
		}
	}
	:local months
	:if ((([:pick $ds 9 11]-1)/4) != (([:pick $ds 9 11])/4)) do={

		:set months {"an"=0;"eb"=31;"ar"=60;"pr"=91;"ay"=121;"un"=152;"ul"=182;"ug"=213;"ep"=244;"ct"=274;"ov"=305;"ec"=335}
	} else={
		:set months {"an"=0;"eb"=31;"ar"=59;"pr"=90;"ay"=120;"un"=151;"ul"=181;"ug"=212;"ep"=243;"ct"=273;"ov"=304;"dec"=334}
	}
	:set ds (([:pick $ds 9 11]*365)+(([:pick $ds 9 11]-1)/4)+($months->[:pick $ds 1 3])+[:pick $ds 4 6])
	:set ts (([:pick $ts 0 2]*60*60)+([:pick $ts 3 5]*60)+[:pick $ts 6 8])
	:return ($ds*24*60*60 + $ts + 946684800 - [/system clock get gmt-offset])
}
NB, not sure how it handles log around new year change, when year is missing.
NB time laps year 2100, 2200 etc are not handled. But should not be a problem ;)

"dec" in this line:
else={
    :set months {"an"=0;"eb"=31;"ar"=59;"pr"=90;"ay"=120;"un"=151;"ul"=181;"ug"=212;"ep"=243;"ct"=273;"ov"=304;"dec"=334}
}
should be "ec":
else={
    :set months {"an"=0;"eb"=31;"ar"=59;"pr"=90;"ay"=120;"un"=151;"ul"=181;"ug"=212;"ep"=243;"ct"=273;"ov"=304;"ec"=334}
}
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1789
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

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

Sun Jun 21, 2020 5:35 pm

Good catch, fixed the original post.
 
How to use Splunk to monitor your MikroTik Router(s)

MikroTik->Splunk
 

Who is online

Users browsing this forum: No registered users and 39 guests