Date picker in settings issue (UTC?) or my mistake?

hi mates,

so i did a function which calculate in days/hours/minutes the time between now and since/until an event.

So far, no issue, but i've just had a complain about time offset.

we are ok that the date picker in settings is in UTC time?

and time.now.value also?

here is my code:   

function DayCount(){
	    if (CheckHours()==2400){return "INVALID TIME";} else {
	    	var NOW = Time.now().value(); 
	    	//var TZO = Sys.getClockTime().timeZoneOffset;
	    	var PastFutur = App.getApp().getProperty("ED")+CheckHours();//-TZO;
	    	var T =0;
	    	var PF="";
	    	if (PastFutur<NOW){
	    		T = NOW.toNumber()-PastFutur.toNumber();
	    		PF = "+";
	    	} else {
	    		T = PastFutur.toNumber()-NOW.toNumber();
	    		PF = "-";
	    	}
	    	var D = Math.floor(T/86400);
	    	var H = Math.floor((T - D*86400)/3600);
	    	var M = (T -D*86400 - H*3600)/60;
			return PF+D.format("%02d")+"  D -"+H.format("%02d")+":"+M.format("%02d");
		}
    }

    function CheckHours(){
    	var HM = App.getApp().getProperty("ET").toString();
    	if (HM.toString().length()!=5){return 2400;}
    	else if( HM.substring(0, 2).toNumber()>23){ return 2400;}
    	else if( HM.substring(3, 5).toNumber()>59){ return 2400;}
    	else { return (HM.substring(0, 2).toNumber()*3600) + (HM.substring(3, 5).toNumber()*60);}
    }

so when i test on mine (i'm utc+1), no problem at all.

but few users reported time offfset and everytime it is the utc offset,

i did again a test on simulator and i do have offset too (+1h for me) but not on my device.

do you see something wrong in my code?

have a great day

  • If you have 2 moments then subtracting them should just work ... there must be something odd going on if the UTC offset comes and goes. When you first posted this I thought it was because of timeZoneOffset, but reading the code I don't think that would make sense, and I couldn't see any other potential problems.

    I've never used the date picker setting - but if I was guessing now, that would seem likely to be the cause. The link Jim points out does indicate some dodgy bugs ...

    Is the date from the setting always returned as a whole day? (I.e. just checking you can't set a time as well?) Maybe after getting the setting value you could perform a check and make sure it is at the start of the day local time on the watch - by converting the moment to the date information, removing any hours, minutes, seconds (set them to zero) and then convert the date info back to a moment.

    At least then you are in total control of what time of day the moment for the event is set for.

  • And the things Hermo points out started back when settings was introduced.  I know he's submitted bugs reports, and I was doing some sideload testing form him with Android.

    Last I heard was GCM still has various issues, but the Connect IQ app was working as expected and consistently.

  • i've made some test few weeks ago, the date picker return the beginning of the day,

    i.e: if i select march the 31st it returns unix time for march the 31st at 00:00,

    so i add the hours and minutes set and make substract with both in value.

    on my device no issue but for some others this is not the case,

    may be the datepicker add (or substract) the TZO when returning the value...

  • I wonder what would happen if a user has their watch and their mobile phone/PC set to different time zones?

    Maybe that's another reason to do the calculation to convert the date to a moment yourself, on the watch.

  • hi,

    in case it can help others, here is how i do now

    the only bug is due to DST since timeZoneOffset only works for current time (and .dst also)

    function CheckHours(){
    var HM = App.getApp().getProperty("ET").toString();
    //var HM = "2021-01-01-00:00";
    var verif =HM.toUtf8Array();
    for(var i=0;i<verif.size();i++){
    if(i!=4&&i!=7&&i!=10&&i!=13){
    if(verif[i]>57||verif[i]<48){return 2400;}
    }
    }
    if (HM==null){return 2400;}
    else if (HM.toString().length()!=16){return 2400;}
    else if( HM.substring(0, 4).toNumber()>2037){ return 2400;}
    else if( HM.substring(5, 7).toNumber()>12){ return 2400;}
    else if( HM.substring(8, 10).toNumber()>31){ return 2400;}
    else if( HM.substring(11, 13).toNumber()>23){ return 2400;}
    else if( HM.substring(14, 16).toNumber()>59){ return 2400;}
    //else { return (HM.substring(0, 2).toNumber()*3600) + (HM.substring(3, 5).toNumber()*60);}
    else{
    var options = {
    :year => HM.substring(0, 4).toNumber(),
    :month => HM.substring(5, 7).toNumber(),
    :day => HM.substring(8, 10).toNumber(),
    :hour => HM.substring(11, 13).toNumber(),
    :min => HM.substring(14, 16).toNumber()
    };

    return Date.moment(options).value();
    }
    }

    and

    function DayCount(opt){
    if (CheckHours()==2400){return "INVALID TIME";} else {
    var NOW = Time.now().value()+Sys.getClockTime().timeZoneOffset;
    var PastFutur = CheckHours();
    var T =0;
    var PF="";
    if (PastFutur<NOW){
    T = NOW-PastFutur;
    PF = "+";
    } else {
    T = PastFutur-NOW;
    PF = "-";
    }
    var D = T/86400;
    var H = (T-(D*86400))/3600;
    var M = (T-(D*86400)-(H*3600))/60;
    if(opt==0){return PF+D.format("%02d")+"d "+H.format("%02d")+":"+M.format("%02d");}
    else{return D.format("%02d")+"-"+H.format("%02d")+":"+M.format("%02d");}
    }
    }

    (sorry i can't set the captcha for insert code)

    have a great day