UTC time and or sunset/sunrire time

Former Member
Former Member
Hi,

Digital stock watch provides us with utc time and sunset/rise time.
Is there any way to access that data in a watchface made in connect iq ?
The developer documentation doesn't show it or I cannot find it.
If not, could someone help me with the calculation of these times ?
Please no external site with just formulas. I would need an actual Monkey C program for that.

Kind regards,
Dirkvd001
  • Former Member
    Former Member over 9 years ago
    Not what I was looking for

    https://en.wikipedia.org/wiki/Sunrise_equation

    It isn't that difficult to translate.


    Can be. But this was not what I asked for. Even it isn't difficult, I cannot seem to get it right.
    So I would like help with the real deal : Monkey C code.

    Thanks
  • Hello,

    Here is my code to calculate sunrise and sunset. Note that this code for some reason is about 20 minutes off, I am still debugging. But maybe there is some synergy here:
    function calcSRSSAngle()
    {
    var now = Time.now();
    var info = Calendar.info(now, Time.FORMAT_SHORT);
    var d = info.day;
    var m = info.month;
    var y = info.year;

    var lat = 0.0;
    var lon = 0.0;
    var currloc = Act.getActivityInfo().currentLocation;
    if (currloc != null)
    {
    lat = currloc.toDegrees()[0].toFloat();
    lon = -currloc.toDegrees()[1].toFloat();
    //System.println("Location captured.");
    }
    var lat_ = lat/180.0*Math.PI;
    var lon_ = lon/180.0*Math.PI;
    //System.println("lat = "+lat);
    //System.println("lon = "+lon);

    // Julian Date
    var a_j = floor((14.0-m)/12.0);
    var y_j = y+4800.0-a_j;
    var m_j = m+12.0*a_j-3.0;
    //System.println("a_j = "+a_j+"; y_j = "+y_j+"; m_j = "+m_j);
    var JDN = d + floor((153.0*m_j+2)/5) + 365*y_j + floor(y_j/4) - floor(y_j/100) + floor(y_j/400) - 32045;
    //System.println("JDN = "+JDN);

    // Current Julian day since Jan 1, 2000
    var n = JDN - 2451545d + 0.0008;
    //System.println("n = "+n);

    // Mean solar noon
    var Js = lon/360.0 + n;
    //System.println("Js = "+Js);

    // Solar mean anomaly
    var M = floatMod(357.5291 + 0.98560028*Js, 360.0);
    //System.println("M = "+M);
    var M_ = M/180.0*Math.PI;

    // Equation for the center
    var C = 1.9148*Math.sin(M_) + 0.02*Math.sin(2*M_) + 0.0003*Math.sin(3*M_);
    //System.println("C = "+C);

    // Ecliptic longitude
    var la = floatMod(M + C + 180 + 102.9372, 360);
    //System.println("la = "+la);
    var la_ = la/180*Math.PI;

    // Solar transit
    var corr = Js - (0.0053*Math.sin(M_) - 0.0069*Math.sin(2*la_));
    var Jtr = 2451545d + corr;
    //System.println("corr = "+corr);
    //System.println("Jtr = "+Jtr);

    // Declination of the sun
    var sind = Math.sin(la_) * Math.sin(23.44/180*Math.PI);
    var delta = Math.asin(sind);
    //System.println("delta = "+delta);

    // Hour angle
    var cosom = (Math.sin(-0.83/180*Math.PI) - Math.sin(lat_) * sind) / (Math.cos(lat_) * Math.cos(delta));
    //System.println("cosom = "+cosom);
    var om = Math.acos(cosom);
    //System.println("om = "+om);

    //Calculate sunrise and sunset[edit]
    var trdel = om/(2*Math.PI);
    //System.println("trdel = "+trdel);
    var Jset = Jtr + trdel;
    //System.println("Jset = "+Jset);
    var Jrise = Jtr - trdel;
    //System.println("Jrise = "+Jrise);

    var sunset_h = (Jset - (JDN-0.5)) * 24;
    var sunrise_h = (Jrise - (JDN-0.5)) * 24;
    var sunset_m = (sunset_h - sunset_h.toNumber()) * 60;
    var sunrise_m = (sunrise_h - sunrise_h.toNumber()) * 60;
    //System.println("sunrise = "+sunrise_h.toNumber()+":"+sunrise_m.toNumber());
    //System.println("sunset = "+sunset_h.toNumber()+":"+sunset_m.toNumber());

    var result = new [2];
    result[0] = sunrise_h;
    result[1] = sunset_h;

    return result;
    }


    The bit above calls the following two functions:
    function floor(x)
    {
    if (x.abs() == x)
    {
    return x.toNumber();
    }
    else
    {
    var x_ = -x;
    x_ = x_.toNumber();
    return -x_;
    }
    }


    function floatMod(a, b)
    {
    return (a - b * floor(a / b));
    }
  • Former Member
    Former Member over 9 years ago
    Wrong times

    Hi,

    Thanks for your code. Something is wrong though.
    If I use your calculation method, it returns different times everytime I calculate it.
    And I get results like 11:51AM and 25:15 PM ???

    Strange
  • Have you already been able to debug the code?