UT time to a moment...

I'm getting a UTC time from a website in one of my apps, and simply want to convert it to be displayed in CIQ ( it's unix based on epoch "seconds").

I convert it to a moment using "var now = Calendar.moment( {:seconds=>timestamp} );"
and when I go to display it using the .info method in Gregorian, the year and month are correct, but DOW and day are one behind, and the time is always "17:00:00".

If I mod the number with 86440 (sec's in a day), and just look at the times, it's STILL 17:00:00!

This isn't a local vs UTC issue, as UTC is ahead of me, not behind.

Is this a bug, or just me being stupid?
  • Former Member
    Former Member over 10 years ago
    Join the stupid club? I tried to convert a timestamp to a moment. If I remember rightly it always came back as something like 11:00 in 1998
  • It sounds like you're being bit by the Garmin epoch offset. This post might shed some light on the issue.
  • Former Member
    Former Member over 10 years ago
    Try fill the other dictionary values by 0

    options = { :second => unixtimestamp, :hour => , :minute => 0, :year => 0, :month => 0, :day => 0};
    czas = Calendar.moment(options);

    I'm uset this in calendarwidget and I have only utc/dst issues.
  • Former Member
    Former Member over 10 years ago
    Doesn't work for me. Incidently we have just come off daylight savings time, so my memory was correct, just slightly dyslexic, eg would have been 1989 11:00
    function onStart() {


    var utc = Time.now().value();
    var options = { :second => utc, :hour => 0, :minute => 0, :year => 0, :month => 0, :day => 0};
    var moment = Calendar.moment(options);
    var date = Calendar.info(moment, 0);
    System.println(format("$1$-$2$-$3$T$4$:$5$:$6$",[
    date.year,
    date.month,
    date.day,
    date.hour,
    date.min.format("%02d"),
    date.sec.format("%02d")]));
    }

    1989-12-31T10:00:00
  • I'm only running in the simulator right now (haven't tried on a real device, and there the year is 2015, but the date is a day off, and time is always 17:00. Here's println output of the timestamp moded with 86400, and the resulting time. you can see that the "seconds" are 300 seconds apart (5 mins), and it's doing this based on a 5 minute timer.
    44069
    17:00:00
    44369
    17:00:00


    I've changed the dictionary to be (only had :second before)
    {:second=>secToday, :hour => 0, :minute => 0, :year => 0, :month => 0, :day => 0} and it's "different", but still odd.
    44972
    23:28:16


    The time is always the same, while secToday is changing. Just tried it on a vivoactive, and I get the same time (23:28:16)
  • Former Member
    Former Member over 10 years ago
    The :seconds option (and other options) Toybox.Time.Gregorian.moment API does not accept full offset values. The current implementation is only going to work properly with seconds = [0..59].

    If :year, :month, :day are not specified, it will set them to the current day/month/year.

    We have corrected some of the issues with dates prior to 1990, but I don't think those fixes have rolled out into the SDK or products yet.

    If you want to create a moment with a UNIX timestamp, it is possible to create a new Moment object with that value.
    [e.g. var time = new Toybox.Time.Moment( timestamp );]
  • @brian - just tried as you suggested, but it seems to think something other than "UNIX epoch" in the simulator, as the moment has 2126 as the year!
  • Former Member
    Former Member over 10 years ago
    Can you post a sample of the code you are running? The constructor saves the value directly, so I don't know what could go wrong in that case.
  • Here ya go. ("epoch" is the value for the "epoch tine")

    Sys.println(epoch);
    var now = new Toybox.Time.Moment(epoch);
    var today = Calendar.info(now, Time.FORMAT_SHORT);
    Sys.println(Lang.format("$1$ $2$ $3$", [today.month, today.day,today.year]));
    Sys.println(Lang.format("$1$:$2$:$3$",[today.hour, today.min.format("%02d"),today.sec.format("%02d")]));

    And the results are:
    1428567577
    2 5 2126
    23:28:16


    update. if I use now=Time.now() to get the local tome, and display it (now.value()) , it's with a few hundred second of the "epoch" value I display above. And using the same code for "now" as shown above, it results in the same data and time.
  • Former Member
    Former Member over 10 years ago
    The Moment constructor isn't working the way I would expect it to, so I guess that isn't working right now.

    I'm not finding a good way to do this at all. I've filed a ticket for not being able to create a Moment from a epoch offset.

    If you can break your offset down into year/month/day/hour/minute/second, I guess you can use Gregorian.moment, but that kind of completely defeats the purpose.