Community discussions

MikroTik App
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1293
Joined: Sun Sep 18, 2011 7:00 pm

saving clock without internet connection

Thu Nov 07, 2013 4:39 pm

Wanted to know
how can I save the clock on the RB ?
every time the system do reset - i get the clock at starter point 01/01/1970

how can I save it if I don't have internet connection ? 2 units working as AP and Station only

Thanks ,
 
User avatar
Stillhard
Frequent Visitor
Frequent Visitor
Posts: 82
Joined: Sun Jun 10, 2012 11:18 am
Location: Banten, Indonesia
Contact:

Re: saving clock without internet connection

Thu Nov 07, 2013 7:03 pm

It's easy, you can use a pc/laptop as your time & web server.
Check my tutorial here: http://www.forummikrotik.com/scripting- ... -http.html
I'm so sorry i can't translate my whole guide/tutorial from that link right now, just use google.

But for your case, edit the time.php from that tutorial to:

php code

<?php
$timestamp = strftime("%Y-%m-%d %H:%M:%S %Y");
echo strftime("/system clock set date=%b/%d/%Y time=%H:%M:%S", strtotime($timestamp))." time-zone-name=Asia/Jakarta";
?>
Or you can use windows / linux as your time server and set the NTP client in RB to collect the data from it.

Good luck :)

It's 00:03 AM here :)
 
User avatar
skot
Long time Member
Long time Member
Posts: 586
Joined: Wed Nov 30, 2011 3:05 am

Re: saving clock without internet connection

Fri Nov 08, 2013 1:04 am

The answer is the same as when you asked previously, there is no reliable way to do this without NTP.

That said, you could run a schedule every X seconds that saves the date/time/tz in the schedule's comment field. Then, when the router reboots, it checks the comment and sets the date/time/tz.

First set the router to the correct time/date/tz.

Create a new schedule using the following code (change first line to match your schedule's name). Make sure and set the schedule's starting date to Jan/01/1970, and the start time to 00:00:00. The script will set the date/time/tz into the schedule's comment field however often you set the interval, and after a reboot it will set them to exactly the same values. If the router reboots and comes back 30 seconds later, the router will be behind by 30 seconds. If a power outage lasts 3 hours, the router will come back and be 3 hours, 30 seconds behind. Remove the "/log info" lines if you don't want them.

ros code

:local name "mySchedule"

:local dt ([/system clock get date]."-".[/system clock get time]."-".[/system clock get time-zone-name])

# if clock's year is not 1970, assume it's accurate and set schedule comment
:if ([:pick $dt 7 11] != 1970) do={
  /log info "[TIME] year != 1970, set comment"
  /system scheduler set $name comment=$dt
# if clock's date is 1970, assume defaults and check schedule comment
} else={
  /log info "[TIME] year = 1970, check comment"
  :local c [/system scheduler get $name comment]
  :if ([:pick $c 7 11] > 1970) do={
    /log info "[TIME] year saved in comment is > 1970, change date/time"
    :local date [:pick $c 0 11]
    :local time [:pick $c 12 20]
    :local tz [:pick $c 21 [:len $c]]
    /system clock set date=$date time=$time time-zone-name=$tz
  } else={
    /log info "[TIME] year saved in comment is not valid, do nothing"
  }
}
If you want to be more accurate, there are multiple problems to consider:
  • The router takes time to reboot
  • At what point during the schedule's interval the router reboots is unknown
  • The longer you set the schedule's interval, the more inaccurate the date/time may become after a reboot
  • Power outage downtime cannot be calculated and could cause date/time to become inaccurate quickly
To try and solve some of these problems, you can factor in a reboot duration, and guess at the schedule's interval (say, just take the interval and divide by 2, giving you at least a middle-of-the-line guess). Then, add these values to the saved time value and get reasonably close. The following script does this, but this causes additional problems. A reboot just before midnight that causes the script to "guess" the correct time as it rolls over will end up with a time stamp that looks sometime like 1d00:01:15, which causes an error when you try to set the time, so it breaks the script. The correct way to solve this is to factor in days, weeks, months, years, etc. Or, do a sloppy shortcut and set the value to 23:59:59, which is what is done here.

When the router reboots, it will add 30 seconds to the time, and if your duration is 10 seconds, it will add 10/2=5 seconds to the new time. This will get you pretty close to the actual time. If a power outage lasts 3 hours, the router will do the same thing as above, and will be behind 3 hours.

ros code

:local name "mySchedule"
:local rebootTime 00:00:30

:local dt ([/system clock get date]."-".[/system clock get time]."-".[/system clock get time-zone-name])

# if clock's year is not 1970, assume it's accurate and set schedule comment
:if ([:pick $dt 7 11] != 1970) do={
  /log info "[TIME] year != 1970, set comment"
  /system scheduler set $name comment=$dt
# if clock's date is 1970, assume defaults and check schedule comment
} else={
  /log info "[TIME] year = 1970, check comment"
  :local c [/system scheduler get $name comment]
  :if ([:pick $c 7 11] > 1970) do={
    /log info "[TIME] year saved in comment is > 1970, change date/time"
    :local date [:pick $c 0 11]
    :local time [:pick $c 12 20]
    :local intervalSplit ([/system scheduler get $name interval] / 2)
    /log info "[TIME] time: $time"
    /log info "[TIME] rebootTime: $rebootTime"
    /log info "[TIME] interval/2: $intervalSplit"
    :local finalTime ($time + $rebootTime + $intervalSplit)
    :if ($finalTime >= 1d) do={
      /log info "[TIME] finalTime > 1d, set to 23:59:59"
      :set finalTime 23:59:59
    }
    /log info "[TIME] finalTime: $finalTime"
    :local tz [:pick $c 21 [:len $c]]
    /system clock set date=$date time=$finalTime time-zone-name=$tz
  } else={
    /log info "[TIME] year saved in comment is not valid, do nothing"
  }
}
If there are no power outages, these methods will start out somewhat accurate but slowly become less and less correct over time. Power outages will just speed up the process. Eventually you'll just be way off :)
I don't need any karma... I have Ιησους Χριστος!
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1293
Joined: Sun Sep 18, 2011 7:00 pm

Re: saving clock without internet connection

Sun Nov 10, 2013 3:49 pm

O.K.
thanks
now , if I want to get the time from the computer its connected to ? and that way the time on the router will be the same as the time on the computer ?

I have try to config ntp server as the computer but it's not good

can it be done? what need to change in to PC (win7 for example) to make it do this?
this is what I have config:
/system ntp client print 
        enabled: yes
           mode: unicast
    primary-ntp: 10.0.0.199
  secondary-ntp: 0.0.0.0
  poll-interval: 16s
  active-server: 10.0.0.199
my computer is 10.0.0.199 connected to RB.

Thanks ,
 
User avatar
Stillhard
Frequent Visitor
Frequent Visitor
Posts: 82
Joined: Sun Jun 10, 2012 11:18 am
Location: Banten, Indonesia
Contact:

Re: saving clock without internet connection

Sun Nov 10, 2013 6:20 pm

O.K.
thanks
now , if I want to get the time from the computer its connected to ? and that way the time on the router will be the same as the time on the computer ?

I have try to config ntp server as the computer but it's not good

can it be done? what need to change in to PC (win7 for example) to make it do this?
this is what I have config:
/system ntp client print 
        enabled: yes
           mode: unicast
    primary-ntp: 10.0.0.199
  secondary-ntp: 0.0.0.0
  poll-interval: 16s
  active-server: 10.0.0.199
my computer is 10.0.0.199 connected to RB.

Thanks ,
Check this link to setup your win7 as NTP server
Or use google for similiar topic :)
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1293
Joined: Sun Sep 18, 2011 7:00 pm

Re: saving clock without internet connection

Mon Nov 11, 2013 1:47 pm

O.K
thanks .
and I can work normally ? I'm mean it won't interfere with anything else in the computer?
 
User avatar
Stillhard
Frequent Visitor
Frequent Visitor
Posts: 82
Joined: Sun Jun 10, 2012 11:18 am
Location: Banten, Indonesia
Contact:

Re: saving clock without internet connection

Tue Nov 12, 2013 6:29 am

O.K
thanks .
and I can work normally ? I'm mean it won't interfere with anything else in the computer?
Of course :)
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1293
Joined: Sun Sep 18, 2011 7:00 pm

Re: saving clock without internet connection

Tue Nov 12, 2013 2:36 pm

O.K
Thanks again !

Who is online

Users browsing this forum: MarcDE, meazz1, mohkhalifa, Onigma, sutrus and 101 guests