Sun rise / sunset

Could some kind person provide an actual working example of how to get sunrise/sunset info for a watch face? TIA!
  • Former Member
    Former Member over 9 years ago
    Hi

    I'm afraid you'l have to compute it by yourself...

    Here is the algorithm I was using on my Suuto :


    [FONT=Courier New]
    /* PARAMETERS */

    /* Localtime */

    UTC=2; /* Add 2 hours to natural time to get legal time */

    /* Kind of computation */

    /* CONST=0.01454; /* sun center */
    CONST=-0.105; /* civil */
    /* CONST=-0.208; /* nautic */
    /* CONST=-0.309; /* astronomic */
    /* CONST=-0,01065; /* same as Bureau des latitudes */

    /* What do we wanna compute ? */

    /* tocalc = -1; /* sunrise */
    /* tocalc = 0; /* midday */
    tocalc = 1; /* sunfall */

    /***************/
    /* Compute postfix dynamically */

    if( UTC == 0 ) { postfix = "UTC"; }
    else if( UTC == 1 ) { postfix = "UTC+1"; }
    else if( UTC == 2 ) { postfix = "UTC+2"; }
    else if( UTC == 3 ) { postfix = "UTC+3"; }
    else if( UTC == 4 ) { postfix = "UTC+4"; }
    else if( UTC == 5 ) { postfix = "UTC+5"; }
    else if( UTC == 6 ) { postfix = "UTC+6"; }
    else if( UTC == 7 ) { postfix = "UTC+7"; }
    else if( UTC == 8 ) { postfix = "UTC+8"; }
    else if( UTC == 9 ) { postfix = "UTC+9"; }
    else if( UTC == 10 ) { postfix = "UTC+10"; }
    else if( UTC == 11 ) { postfix = "UTC+11"; }
    else if( UTC == 12 ) { postfix = "UTC+12"; }
    else if( UTC == -1 ) { postfix = "UTC-1"; }
    else if( UTC == -2 ) { postfix = "UTC-2"; }
    else if( UTC == -3 ) { postfix = "UTC-3"; }
    else if( UTC == -4 ) { postfix = "UTC-4"; }
    else if( UTC == -5 ) { postfix = "UTC-5"; }
    else if( UTC == -6 ) { postfix = "UTC-6"; }
    else if( UTC == -7 ) { postfix = "UTC-7"; }
    else if( UTC == -8 ) { postfix = "UTC-8"; }
    else if( UTC == -9 ) { postfix = "UTC-9"; }
    else if( UTC == -10 ) { postfix = "UTC-10"; }
    else if( UTC == -11 ) { postfix = "UTC-11"; }
    else if( UTC == -12 ) { postfix = "UTC-12"; }

    j = 1+Suunto.mod(SUUNTO_DAYS_AFTER_1_1_2000*4, 1461)/4;
    decimal=Suunto.mod(j*10,10)/10;
    j = j - decimal;

    /* OK */

    M = 357 + 0.9856 * j;

    C = 1.914 * Suunto.sind(M) + 0.02 * Suunto.sind(2 * M);

    L = 280 + C + 0.9856 * j;

    R = 0.053 * Suunto.sind(4*L)-2.465 * Suunto.sind(2*L);

    ET=(C + R) * 4;

    RESULT=ET;

    sindec = 0.3978 * Suunto.sind(L);

    cosdec = Suunto.sqrt(1-sindec*sindec);

    /* KO */

    declinaison = Suunto.atand2(sindec,cosdec);

    cosh = ( CONST - sind( declinaison ) * sind( SUUNTO_GPS_LATITUDE ) ) /
    ( Suunto.cosd ( declinaison ) * Suunto.cosd ( SUUNTO_GPS_LATITUDE ));

    sinh = Suunto.sqrt(1-cosh*cosh);


    h = Suunto.atand2(sinh,cosh);

    h = h / 15;
    lon = -SUUNTO_GPS_LONGITUDE / 15;
    ET = ET / 60;

    TL = 12 + tocalc * h + ET + lon;

    TL = TL + UTC;

    /* RESULT=TL*3600; */

    [/FONT]

    Should be easy to convert to Monkey C
  • Have a look at my open source widget:

    https://github.com/haraldh/SunCalc/blob/master/source/SunCalc.mc

    You would use it like this:
    using Toybox.Position as Position;
    using Toybox.Time as Time;

    var sc = new SunCalc();
    var info = Position.getInfo();
    if (info != null && info.accuracy != Position.QUALITY_NOT_AVAILABLE) {
    var loc = info.position.toRadians();
    var sunrise_moment = sc.calculate(Time.now.value(), loc[0], loc[1], SUNRISE);
    var sunset_moment = sc.calculate(Time.now.value(), loc[0], loc[1], SUNSET);
    }


    Then convert the moment to a time string like in
    https://github.com/haraldh/SunCalc/blob/master/source/SunCalcView.mc#L122
  • Thank you very much, that is useful!

    var sunrise_moment = sc.calculate(Time.now.value(), loc[0], loc[1], SUNRISE);
    var sunset_moment = sc.calculate(Time.now.value(), loc[0], loc[1], SUNSET);
    }

    but should be like this!?


    var sunrise_moment = sc.calculate(new Time.Moment(Time.now().value()), loc[0], loc[1], SUNRISE);
    var sunset_moment = sc.calculate(new Time.Moment(Time.now().value()), loc[0], loc[1], SUNSET);


    Regards
  • In fact:

    var now = Time.now();
    var sunrise_moment = sc.calculate(now, loc[0], loc[1], SUNRISE);
    var sunset_moment = sc.calculate(now, loc[0], loc[1], SUNSET);
    }
  • But a watchface cannot access position no?
  • But a watchface cannot access position no?


    Activity.info will have your last location on many watches (for a period of time - maybe an hour or two). Once you get it, you can save it off in the object store, so you have something to use if the data in Activity.info is stale.
  • Habing trouble even getting currentLocation for some reason!
  • var loc = ActI.getActivityInfo().currentLocation;
    var latlon = loc.toDegrees();
  • You want to check currentLocation for a null, as it could be that.

    Also, based on the last time you used GPS, you man need to start something that uses GPS, let lit local and record a bit (you can then discard that activity) and check again.

    How long currentLocation is saved probably varies by both the watch and the FW.
  • Former Member
    Former Member over 8 years ago
    Hi

    I'm trying to use this excellent class for calculating sunrise / sunset, and it looks fairly easy thanks to this class.

    But I have a small problem.

    When I add the code below, I get an errormessage: Permission required.
    var info = Position.getInfo();

    But when I try to add permission for positioning in the manifest file, this item is greyed out and can't be selected.

    App minimum SDK version is set to 1.3.X and build target is set to fenix 3 HR

    What am I doing wrong here?

    Thanks a lot in advance

    Kenth