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?
  • Yup, If I knew how to break it down, I could just do that, but I'd have to consider things like leap years (leap seconds?) and such too...

    if I mod the timestamp by 86400 I can get the time (without the date), so I may just do that for now.
  • Former Member
    Former Member over 10 years ago
    Maybe some example from my timezones widget may help:

    var tera = Time.now();
    var unix = tera.value();

    // unix epoc in string
    var epoc = unix.format("%d");

    var tein = Calendar.info(tera, Time.FORMAT_SHORT);

    // local time
    localtime = Lang.format("$1$:$2$:$3$",
    [tein.hour.format("%2d"), tein.min.format("%02d"), tein.sec.format("%02d")]);

    var clockTime = Sys.getClockTime();
    var utc = tera.add(new Time.Duration(clockTime.timeZoneOffset*-1));
    tein = Calendar.info(utc, Time.FORMAT_SHORT);

    // UTC time
    utctime = Lang.format("$1$:$2$",
    [tein.hour.format("%2d"), tein.min.format("%02d")]);

    // any timezone -5 (FROM UTC OFFSET!!!!) for exaple:
    wlofs=(-5*3600);
    var oo = tera.add(new Time.Duration(wlofs-clockTime.timeZoneOffset));
    var tim = Calendar.info(oo, Time.FORMAT_SHORT);
    ftim = Lang.format("$1$:$2$",
    [tim.hour.format("%2d"), tim.min.format("%02d")]);
  • Former Member
    Former Member over 10 years ago
    I did find a workaround...

    The initialization for Durations does work so...

    var now = Time.Gregorian.moment({:year=>0,:month=>0,:day=>0}); //Moment with value of 0
    var offset = new Time.Duration( epoch );
    now = now.add( offset );


    We are fixing the issue with new Time.Moment() not working.

    I am also plan to evaluate the method and documentation for creating a Moment from a timestamp because we don't have good information available there.
  • Thanks Brian - using duration works!
  • Former Member
    Former Member over 10 years ago
    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.
    Incidently..

    function myExample(epoch) {


    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
    12 31 1989
    10:00:00

    Have we both not run exactly the same code?
  • You'll get different results depending on the time zone set on your computer, but I haven't had enough time to really work out why. If I set my computer clock to UTC+anything, I get:

    1428567577
    12 31 1989
    10:00:00


    UTC-anything results in something like:

    1428567577
    2 6 2126
    1:28:16


    But the time actually adjusts depending on the time zone. We know there are issues with Gregorian stuff, so I'll add these details to the bugs I've already filed to help narrow things down.
  • Former Member
    Former Member over 4 years ago

    Six years later and this is still broken. 

    Initialising a moment using my epoch timestamp results in the date technically being correct, but the time itself is set to 00:00 UTC.

  • It's been working fine for me for years.

    Here's the basics I use:

    var ts1=new Time.Moment(ts);
    var ts2=Calendar.info(ts1, Time.FORMAT_MEDIUM);

    Where ts is the "epoch" time, and then I format the time based on whats in ts2.

  • Former Member
    Former Member over 4 years ago in reply to jim_m_58

    Yeah, that works, but the code you referenced in your original post is still broken.

    var now = Calendar.moment( {:seconds=>timestamp} );

  • That was 6 years ago.  What I posted today is how to do it.