Time from UTC

Former Member
Former Member
Hi All,

I'm trying to work on some of the internationalization features in my tidal app, is there an API call to get the local time from UTC?

My tides app talks to a companion service (online, not on-phone) that returns a bunch of tide times with UTC datestamps, is there anything in the API I can throw UTC at and get a properly formated date/time back for where the watch user is at that time ?

-Rob
  • I use this code in Clarity watch face. I'd love to have a quicker and better way. Also, a small number of users have reported a wrong calculation of time (can't remember if it was UTC or local that was reported as wrong).

    If anyone finds a better way..

    // Get and stringify the current time
    var UTCstr = "", UTChourString = "", UTCminString = "";
    var time24h = settings.is24Hour;
    var UTCdeltaMin = clockTime.timeZoneOffset / 60;
    var hourString = "";
    var minString = "";
    var ampm = "";
    var dayOverflow = "";
    var pxlOffset = 0;
    var UTCdate = now;


    if (time24h){
    hourString = clockTime.hour.toString();
    if (clockTime.hour < 10){
    // hourString = "0" + hourString;
    }
    ampm = "";
    } else {
    if(clockTime.hour>12){
    hourString = (clockTime.hour - 12).toString();
    if (clockTime.hour - 12 >= 10){
    pxlOffset = 9;
    }
    } else {
    hourString = clockTime.hour.toString();
    if (clockTime.hour >= 10){
    pxlOffset = 9;
    }
    }
    if (clockTime.hour >=12){//noon or afternoon
    ampm = "p";
    } else if (clockTime.hour == 0){//midnight
    hourString = "12";
    ampm = "a";
    } else { //morning
    ampm = "a";
    }
    }
    minString = clockTime.min.toString();
    if (clockTime.min < 10) {
    minString = "0" + minString;
    }

    var secString;
    if (awake) {
    secString = clockTime.sec.toString();
    if (clockTime.sec < 10) {
    secString = "0" + secString;
    }
    } else {
    secString = "";
    }

    var hourDiff = (UTCdeltaMin / 60).toNumber();
    var minDiff = UTCdeltaMin - (hourDiff*60);
    var UTChour = (clockTime.hour - hourDiff + 24) % 24;
    var UTCmin = (clockTime.min - minDiff + 60) % 60;

    if (clockTime.hour * 60 + clockTime.min - UTCdeltaMin < 0){
    var oneDay;
    if (options.watchtype != MILITARY){
    oneDay = new Time.Duration(-24*60*60);
    } else {
    oneDay = new Time.Duration(24*60*60);
    }
    UTCdate = Calendar.info(now.add(oneDay), Time.FORMAT_SHORT);
    dayOverflow = UTCdate.day.toString();
    } else if (clockTime.hour * 60 + clockTime.min - UTCdeltaMin > 24 * 60){
    var oneDay;
    if (options.watchtype != MILITARY){
    oneDay = new Time.Duration(24*60*60);
    } else {
    oneDay = new Time.Duration(-24*60*60);
    }
    UTCdate = Calendar.info(now.add(oneDay), Time.FORMAT_SHORT);
    dayOverflow = UTCdate.day.toString();
    }


    UTChourString = UTChour.toString();
    if (UTChour < 10){
    UTChourString = "0" + UTChourString;
    }
    UTCminString = UTCmin.toString();
    if (clockTime.min < 10) {
    UTCminString = "0" + UTCminString;
    }
    UTCstr = Lang.format("$1$$2$", [UTChourString, UTCminString]);
  • My previous answer may not be exactly what you asked, but basically the idea is to use

    clockTime.timeZoneOffset (in seconds)

    to go from local to UTC and vice-versa.

    There's also daylight saving time, although this is probably where my time calculation issues come from.
  • Former Member
    Former Member over 9 years ago
    Thank you for this, that's kind of what I was trying to avoid, it seems like a function that you'd expect to exist in the watch firmware...

    However, when I come to refactor the tide times in my widget, I'll probably use the code you've pasted above, I'll let you know if I run into any issues / do any refactoring.

    Cheers
    -Rob
  • What is the format of the date/time you're getting? Is it an "epoch" time? (#of seconds since epoch)
  • We have a way to convert a UNIX time stamp to UTC, but nothing to go from local time to UTC. Seems like this would be a useful addition to the API--I'll request it and see what happens.