Date arithmetic?

I have a need to perform date arithmetic, but cannot find a method native to ROS, can anyone shed some light on a native process before I go scripting the math manually?

The first date is obtained as [/system clock get date]
I want to add either a standard time (i.e. 2d12:10:09 or 36:10:09 or 12:10:09) or just a number of days (i.e. 2d or 2)
I have also tried using :totime and concatenation, none produce the desired result.

As an example, if performed on February 26th of 2013, the desired output of adding [/system clock get date] and 2d12:00:00 should return feb/28/2013 if performed before noon or mar/01/2013 if run on or after noon.

I have no problems adding time to time in the above manners.

>:put ([/system clock get time]+("72:53:27"))
3d11:37:51

Here are some sample inputs and the resulting output/errors:

> :put ([/system clock get date])             
feb/06/2013
> :put ([/system clock get date]+"2d")                         
Script Error: cannot add string to string
> :put ([/system clock get date]+"2") 
Script Error: cannot add string to string
> :put ([/system clock get date]+2)  
Script Error: cannot add string to time interval
> :put ([/system clock get date]+2d)
Script Error: cannot add string to ip address
> :put ([/system clock get date]+2d12:10:09)    
Script Error: cannot add string to ip address
> :put ([/system clock get date]+36:10:09)    
Script Error: cannot add string to ip address
> :put ([/system clock get date]+"2d12:10:09")
Script Error: cannot add string to string
> :put ([/system clock get date]+"36:10:09")    
Script Error: cannot add string to string
> :put ([/system clock get date]+[:totime [/system clock get time]])            
Script Error: cannot add string to ip address
> :put ([/system clock get date].[:totime [/system clock get time]]) 
feb/06/201311:29:09

Try to use some of the convert stuff here:

http://wiki.mikrotik.com/wiki/Use_Functions_in_CMD_Script

Thanks, but no. The only "function in that script changes the month from 3 letters to it’s respectively number via simple array look up. While that would be the first step in a manual date arithmetic function, it is far from the complete answer. If I do feel like finishing this arithmetic function, I’ll be sure to share it back with the group (just in time for MT to include it natively in ROS).

Haha, It made me really lough. What a twisted way to do conversions. I should print that and… :smiley:

This would be nice, I could have used this type of functionality a few times in the past. Looking forward to what you come up with.

has anyone found the solution?

This post is originally from 2013

It’s 2020 and I’ll be also interested to have a working solution

I have posted a solution in this thread.
http://forum.mikrotik.com/t/i-did-it-script-to-compute-unix-time/68576/1

You then have a script that converts date to epoc time so that you can do the math.
Not an optimal solution, but should work.

I have sent email to support@mikrotik.com for them to implement better date handling, since it just a big mess today.
If more sends email, then it may come some in v7 :slight_smile:

Thanks,

indeed we can:
1- convert mikrotik date / time > epoch (this is inside your thread)
2- apply arithmetic to epoch
3- convert from epoch to mikrotik date time (I am unaware of the existence of such script)

Do you know of a script that converts from epoch to mikrotik date / time?

Thanks

Not seen, but should not be to hard to make.
Problem is that MT uses various from of date logging.

  • If log time is less than 24 hours (or is it from this date, not sure) it uses time only: 13:56:28
  • Older logs with month and day, not year!! may/16 21:56:18
  • /system clock print gives time: 10:29:40 date: may/17/2020

Last one is better, but using name for month only make thing complicated.

Correct/best use should be 17/05/2020 10:29:40, full format, number only and all logget in UTC or Zulu format.

I’ll be interested to see such easy function that takes as input an epoch and outputs some time-format we can link back to the mmm/dd or mmm/dd/yyyy time format of mikrotik even if (agree with you) it’s not the most robust and best format.

This would finally enable this workaround.
Workaround that wouldn’t need to be constructed in the first place in any standard linux box.

The most flexible solution would be with a standard unix-time (ie. time in seconds since 1/1/1970):

Just a concept:

Conversion funcs:
tm = datetime
datetime = tm

Arithmetic funcs:
tm = tm + x
tm = tm - x
where x is number of seconds

>

I see there is already **:totime** available and also arithmetic in scripting.
See also:
https://wiki.mikrotik.com/wiki/Manual:Scripting#Arithmetic_Operators
http://forum.mikrotik.com/t/convert-string-to-time-value/68658/1
and @Jotne above has already posted that link:
http://forum.mikrotik.com/t/i-did-it-script-to-compute-unix-time/68576/1