Precision timestamp with miliseconds

Former Member
Former Member
How can I get from system unix epoc with miliseconds?

Sys.getClockTime have hours, min and sec [1]
Moment have timestamp: year, month, day, hour, min, sec [2]
Sys.getTimer have miliseconds but unsynchronized! When seconds on [1] or [2] change value +1, getTimer have for example 473ms!

I must calculate aproxymate offset to getTimer to build "precision" timestamp.
Hello Garmin: this is WATCH! and we need precision, confidential source of time for applications!
  • Former Member
    Former Member over 10 years ago
    How exactly do you know they are out of sync? What have you done to ensure you are checking Sys.getClockTime at the exact moment they change? Here is how I sync seconds to ms

    function startSecTimer()
    {
    mUpdateTimer.stop();
    var ms = System.getTimer() % 1000;

    if(950 < ms)
    {
    ms = 2000 - ms;
    }
    else
    {
    ms = 1000 - ms;
    }
    mUpdateTimer.start(method(:refreshEverySecond),ms,false);
    updateApp();
    }

    function refreshEverySecond()
    {
    mUpdateTimer.start(method(:updateApp),1000,true);
    updateApp();
    }

    function updateApp()
    {
    Ui.requestUpdate();
    }
  • Former Member
    Former Member over 10 years ago
    I'm take a timestamp in seconds, add miliseconds in a timer 123ms and compare curent and previous and I get sometimes curent timestamp less.
    I need timestamp with milisecond for stopwatch. Now I aproximate variable to add to (getTimer() + msdiff) % 1000 and correct it +- if I get less value on next timer.
    But is not confidential time source :) in WATCH I expect BETTER.
  • Former Member
    Former Member over 10 years ago
    Surely you don't expect the code to take 0 execution time?

    With human reactions and responses will a few ms be that problematic. I think that as long as you start your timer with the user input, you have synced the best way possible.
  • Former Member
    Former Member over 10 years ago
    Sub-seconds precision is possible. I without any problems press two, three times start/stop button on my watch in one second. I don't have 0 secund execute code. I have watch witch precision time, precision about 1 level of magnitude more than man can take, from years, days to miliseconds. I expected too much for hundred MHz devices?
    Have You stopwatch in seconds? Write better witout time source?
  • Former Member
    Former Member over 10 years ago
    I think you're talking about two different scenarios. So if you don't have 0 ms execute code then you can't expect to compare 2 millisecond timers against each other. In the case of logging the time of a keypress I believe the ms timer will return a suitable value.
  • I'm not sure why you're trying to mix the two timers. If you capture the unix timestamp and the system timer at application start, you should be able to get a reasonable estimate of a millisecond accurate time. Of course the millisecond value returned by Sys.getTimer() will overflow at some point and you'd have to account for that, but otherwise this seems simple...

    class MyApp extends App.AppBase
    {
    var initial_unix_msec;
    var initial_timer;

    function initialize() {
    initial_unix_msec = Time.now().value() * 1000;
    initial_timer = Sys.getTimer();
    }

    function getTimestamp() {
    var timer = Sys.getTimer();

    // probably have to handle underflow here...

    return initial_unix_msec + (timer - initial_msec_timestamp);
    }
    }


    That said, if you are just creating a stopwatch, why on earth do you need to unix timestamp anyway. Why can't you just use the delta between the calls to Sys.getTimer()?
  • Former Member
    Former Member over 10 years ago
    Because I want to the stopwatch value survived reload od widget? Watch restart?
  • That is a valid use case. How do you expect to handle time lost or gained due to clock drift or shift due to GPS sync?
  • Former Member
    Former Member over 10 years ago
    Slightly off topic. Any idea what the rollover value is for the ms timer? I wondered about it once, but actually decided that I wasn't going to bother checking for it. An known unknown.
  • Former Member
    Former Member over 10 years ago
    BTW Travis. Isn't
    Time.now().value();in seconds? Multiplying by 1000 ain't right is it?