getSunrise() Function

I'm playing around with Sunrise and Sunset in a datafield for Edge devices.

I coded some lines, but I get an error in line 12 :

infoSunR = Time.Gregorian.Info( sunRise, Time.FORMAT_LONG );
Error: Unhandled Exception
Exception: UnexpectedTypeException: Expected Method, given Class definition

I check for the values curpos, today and sunRise: all 3 deliever an object.

I assume variable "sunRise" as a time.moment.

Why throws Time.Gregorian.Info( sunRise, Time.FORMAT_LONG ) an error?

        // SUNEVENTS    
        if ( Toybox has :Weather ) {
            //var curpos = info.currentLocation;
            var curpos = Weather.getCurrentConditions().observationLocationPosition; 
            var today = Weather.getCurrentConditions().observationTime;
            var sunRise = null;
            var infoSunR = null;
            if ( curpos != null and today != null )  {
                sunRise = Weather.getSunrise(curpos, today);
                //System.println( curpos + "    " + today + "     " + sunRise);
                if ( sunRise != null ) {
                    infoSunR = Time.Gregorian.Info( sunRise, Time.FORMAT_LONG );
                    System.println( "Hour: " + infoSunR.hour );
                }
            }
        }

Top Replies

All Replies

  • Time.now()

    now() as Time.Moment

    Get a Moment for the current time.

  • That's really simple - tank you! Works, !

    Last question:
    should I check if Time.now() has delivered a valid Moment? - or must it deliver a valid Moment anyway.

    (As far I have learned, a Moment cannot be null ??)

  • I've never seen a case, including in ERA, where it's not a Moment.  For a watchface. Time.now() is commonly used to get the date and time to display..

  • should I check if Time.now() has delivered a valid Moment? - or must it deliver a valid Moment anyway.

    (As far I have learned, a Moment cannot be null ??)

    “a Moment cannot be null ??”

    This is the wrong question to ask. By definition, a Moment is a Moment, and not a null value.

    The right question to ask is whether Time.now() can also return null (or throw an exception). And according to the documentation/typecheck data (and experience), the answer is no.

    The docs type Time.now() as simply returning Moment.

    [https://developer.garmin.com/connect-iq/api-docs/Toybox/Time.html#now-instance_function]

    If Time.now() could return null, it would be typed as returning Moment or Null, not simply Moment. If it could throw an exception, the documentation would say so (hopefully). Ofc we could ask whether a System Error is possible here (as that’s sometimes not documented), but from experience that never happens (and it’s hard to imagine a case where it would.)

    Another valid question would be “are there are any invalid Moment values” (and the answer to that should also “no”).

    For a deeper answer, traditionally most computer systems will not return an invalid value (or throw an exception) when asked for the current (“wall clock”) time (and date), since this is a very basic function of computing. Obviously there are cases where the source of the time is not trusted, but typically there’s a separate function to determine that. For example, CIQ has Time.getCurrentTime() which allows you to select the time source (default, GPS or RTC). If RTC is selected and the RTC hasn’t been synced with GPS, then an exception will be thrown (because that indicates the RTC isn’t to be trusted.)

    As a counter-example, in Java, Clock.instant() is documented as possibly throwing an exception, but the docs note that most implementations won’t throw it.

    [https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#instant--]

  • I'm not sure I understand your question. More precisely: what do you try to achieve?

    I think mcinner1 wants to pass the current date/time to Weather.getSunrise(). Having been unaware of Time.now(), they used Weather.getCurrentConditions().observationTime to get the "current date/time" instead, but they correctly noted that observationTime will be unavailable in some cases.

  • , thank you for your explanations. Yes, I need current date time for sunevents - and as a consequence of the problems with getCurrent.Conditions() I now want to make sure that no crash will happen.

    But I think, with all your help it‘s solved and sorted now.
    Thank you all!