Epoch to readable date Problem

Former Member
Former Member
Hi. i'm creating an app that get a json data from Internet, in the file exist a time data in milliseconds (epoch time or unix), my principal problem is when i tray to convert to a readable time i get incorrect time.

Time from json:
"time":1446483157632

var epoch=value2["time"].toLong("%d");

var now=new Time.Moment(epoch);
var info = Calendar.info(now, Time.FORMAT_SHORT);

var dateStr = Lang.format("$1$/$2$/$3$", [info.day,info.month, info.year]);
var timeStr = Lang.format("$1$:$2$:$3$",[info.hour.format("%2d"), info.min.format("%02d"), info.sec.format("%02d")]);
Sys.println("---time: "+ dateStr+" "+timeStr);


Result Simulator
---time: 2/12/2076 11:49:36

Should be
Mon, 02 Nov 2015 16:52:37

please someone can help me.

thanks
  • I pulled the current epoch value from the Unix_time wikipedia page and then wrote this code...

    var offset = 1447841725; // (2015-11-18T10:15:25Z)
    Sys.println(offset);

    // offset by the local time zone offset because Time.Moment
    // considers the parameter to be in the local time zone. this
    // is only necessary if the offset value above is specified in
    // the UTC time zone. if the value is somehow in your local
    // time zone, you can skip this step.
    offset -= Sys.getClockTime().timeZoneOffset;

    var info = Calendar.info(new Time.Moment(offset), Time.FORMAT_SHORT);

    Sys.println(Lang.format("$1$-$2$-$3$T$4$:$5$:$6$Z", [
    info.year,
    info.month.format("%02d"),
    info.day.format("%02d"),
    info.hour.format("%02d"),
    info.min.format("%02d"),
    info.sec.format("%02d") ]));


    When I run it, I get this as the output...

    Shell Version 0.1.0
    1447841725
    2015-11-18T10:15:25Z


    Which is exactly the time that I expect.

    So, from what I can tell, everything on the ConnectIQ side (in the simulator at least) is doing the right thing.

    Travis
  • I see the problem. The time value that you are looking at is specified in milliseconds. You need to convert that to seconds.

    Unfortunately, it looks like the value returned to you may already be truncated to a 32-bit Number (which you promote to a Long). Once that value is truncated, all is lost. You should try printing the value you get back to see that it matches what you expect to be received or if it is something different. If it is different, you might be looking at a Garmin bug in the JSON parser.

    If the value is okay, then you might be able to write var now = (val2["time"].toFloat() / 1000).toNumber(), or something like this...

    var val = val2["time"];
    val = val.substr(0, val.length() - 3).toNumber();
  • Former Member
    Former Member over 9 years ago
    Thanks

    I see the problem. The time value that you are looking at is specified in milliseconds. You need to convert that to seconds.



    Thanks you travis, actually that was my problem.
    and the offset was a extra. thanks, this is my new fixed code.


    var epoch=value2["time"].toLong("%d")/1000;
    epoch -= Sys.getClockTime().timeZoneOffset;

    var info = Calendar.info(new Time.Moment(epoch), Time.FORMAT_SHORT);

    var dateStr= Lang.format("$1$-$2$-$3$ $4$:$5$:$6$", [
    info.day.format("%02d"),
    info.month.format("%02d"),
    info.year,
    info.hour.format("%02d"),
    info.min.format("%02d"),
    info.sec.format("%02d") ]);

    item.time=dateStr;
  • Former Member
    Former Member over 9 years ago
    Diferent data

    when i make the json call, the expected data is 1447859886920 and i recive 1447862861824.000000, that's way i convert the data to long, but yes, i recive a slightly diferent data, i hope garmin fix this bug.
  • Former Member
    Former Member over 9 years ago
    dont work in fenix 3

    I see the problem. The time value that you are looking at is specified in milliseconds. You need to convert that to seconds.

    Unfortunately, it looks like the value returned to you may already be truncated to a 32-bit Number (which you promote to a Long). Once that value is truncated, all is lost. You should try printing the value you get back to see that it matches what you expect to be received or if it is something different. If it is different, you might be looking at a Garmin bug in the JSON parser.

    If the value is okay, then you might be able to write var now = (val2["time"].toFloat() / 1000).toNumber(), or something like this...

    var val = val2["time"];
    val = val.substr(0, val.length() - 3).toNumber();


    Whit this modification the date works on the simulator, but when i try in the fenix 3 the system show me a wrong date again =(
  • I'm curious what type of object being retrieved from the map. What does the following code produce?

    var val = val2["time"];
    if (val instanceof Lang.String) {
    Sys.println("String: " + val);
    }
    else if (val instanceof Lang.Float) {
    Sys.println("Float: " + val);
    }
    else if (val instanceof Lang.Double) {
    Sys.println("Double: " + val);
    }
    else if (val instanceof Lang.Long) {
    Sys.println("Long: " + val);
    }
    else if (val instanceof Lang.Number) {
    Sys.println("Number: " + val);
    }
    else {
    Sys.println("Unknown: " + val);
    }
  • Former Member
    Former Member over 9 years ago
    Result

    I'm curious what type of object being retrieved from the map. What does the following code produce?

    var val = val2["time"];
    if (val instanceof Lang.String) {
    Sys.println("String: " + val);
    }
    else if (val instanceof Lang.Float) {
    Sys.println("Float: " + val);
    }
    else if (val instanceof Lang.Double) {
    Sys.println("Double: " + val);
    }
    else if (val instanceof Lang.Long) {
    Sys.println("Long: " + val);
    }
    else if (val instanceof Lang.Number) {
    Sys.println("Number: " + val);
    }
    else {
    Sys.println("Unknown: " + val);
    }


    Hi tanks for help me =)

    about your doubt you code get a float val.

    Float: 1447851065344.000000
    Float: 1447335034880.000000
    Float: 1447335034880.000000
    Float: 1447215366144.000000
    Float: 1447152320512.000000
    Float: 1447114309632.000000
    Float: 1447112081408.000000
    Float: 1447089274880.000000


    this is the data that the query retrive on the navigator (just whats you say in the tread before about the bug in the json parse.)

    "time":1447851115280
    "time":1447335027960
    "time":1447335015740
    "time":1447215419870
    "time":1447152375880
    "time":1447114247710
    "time":1447112117920
    "time":1447089273630

    Wed, 18 Nov 2015 12:51:05 GMTv/s Wed, 18 Nov 2015 12:51:55 GMT
  • If the type is already Lang.Float then you should just have to do...

    var offset = (vals2["time"] / 1000).toNumber();
    offset -= Sys.getClockTime().timeZoneOffset;


    I don't see how there could still be a problem.

    Travis
  • Former Member
    Former Member over 9 years ago
    not working =(

    not working on fenix 3, i don't know why in the simulator i get the right date and in the watch i get a different one.

    var epoch= (value2["time"]);
    var timeZoneOff=Sys.getClockTime().timeZoneOffset;

    var res=(epoch-timeZoneOff)/1000.0;
    var now=new Time.Moment(res);

    var info = Calendar.info(now, Time.FORMAT_SHORT);

    var dateStr = Lang.format("$1$/$2$/$3$", [info.day,info.month, info.year]);
    var timeStr = Lang.format("$1$:$2$",[info.hour.format("%2d"), info.min.format("%02d")]);


    Sys.println("-------------------------------------------------");

    Sys.println("--epoch: "+epoch);
    Sys.println("--Off: "+timeZoneOff);
    Sys.println("--res ep: "+res);
    Sys.println("--now: "+now);
    Sys.println("--info: "+info);
    Sys.println("--time: "+timeStr);
    Sys.println("--hour: "+dateStr);


    Simulator Data
    -------------------------------------------------
    --epoch: 1447851065344.000000
    --Off: -10800
    --res ep: 1447851008.000000
    --now: Obj: 52005976
    --info: Obj: 52243712
    --time: 9:50
    --hour: 18/11/2015

    Fenix 3 data
  • What firmware version? It would be useful to see the intermediate values and the input data so we can tell where it goes wrong.