# Time/Date APIs - Astronomical Twilight

I’m curious how the time and date are derived. Is this done as an API from a service, or pulled internally from the watch?

Specifically I’m looking to create a face that include some astronomical information such as Civil, Nautical, and Astronomical Start and End times. While you can do some math to approximate this, to be specific you need to know the angle of the sun below the horizon. This is different at different longitudes and times during the year. So approximations would vary wildly depending on the user and their location.

Just looking around there is a free API here: https://sunrise-sunset.org/api
Also a paid version here: Astronomy API Service - for Developers

If the API is not an option, can you provide an expression for the latitude and longitude. It is not in the current list of expressions or tag, but based on my research should be provided to the OpenWeather API to derive the watch location.

If we had those two items, I’m sure between me and @Mellin we could figure out how to calculate things using the guide here (Sun rise, set and twilight times calculated using a simple iterative scheme and implemented as a QBASIC program, an Excel spreadsheet using VBA and as a plain spreadsheet.) …maybe.

Sunrise and sunset are often defined as the instant when the upper limb of the Sun’s disk is just touching the observer’s mathematical horizon assuming a spherical earth, and allowing for the atmospheric refraction. This corresponds to an altitude of -0.833 degrees for the Sun. The various ‘twilights’ are usually defined in terms of the Sun’s altitude as follows;

_ Astronomical twilight altitude -18 degrees_
_ Nautical twilight altitude -12 degrees_
_ Civil twilight altitude -6 degrees_

2 Likes

This idea scratches my math itch. Good luck!

@eradicator09! Yes, and interesting subject. I have been running a personally weather Site for +20 years now, and in this is also included, what you try to get, as a possibility on a watch function.
I do still not have the necessary knowledge of the possibilities in Facers tags possibilities, to could make a function to do it.
But I can give you an example of, how it’s done on my Weather Site, out from some calculations, based on latitude, and times, and combined with some math!
The following, is a script (Java) which calculate the Sun’s actual heigt above the Horizon, and when you know this, you can also find the Exact Twilight for Astronomical, Nautical and Civil, for the position you are!

latitude = 54.5 (you’r position)
dateStr = “^vst142^” (is this who picks up the exact Date)
month = parseFloat(dateStr.substring(3,5))
day = parseFloat(dateStr.substring(0,2))
timeStr = “^vst143^” (is this who picks up the exact Time) (it’s a Tag for the program, just as in Facer)
hour = parseFloat(timeStr.substring(0,2))
minute = parseFloat(timeStr.substring(3,5))
riseStr = “^vst144^” (is the strings who pick up the Sunrise, by you’r position) (it’s a Tag for the program, just as in Facer)

risehour = parseFloat(riseStr.substring(0,2))
riseminute = parseFloat(riseStr.substring(3,5))
risetime = (risehour * 60) + riseminute
setStr = “^vst145^” (is the string who picks up the Sunset, by you’r position) (it’s a Tag for the program, just as in Facer)

sethour = parseFloat(setStr.substring(0,2))
sethour = sethour +1 (the Plus, is for getting Summer Time)
setminute = parseFloat(setStr.substring(3,5))
settime = (sethour * 60) + setminute
time = hour * 60 + minute
julian_day = calcJday(month, day)
suntime = sunTime(risetime, settime, time)
sun_elevation = solarAltitude(julian_day, suntime, latitude)
sun_azimuth = solarAzimuth(julian_day, suntime, latitude)

If you (or Mellin) can use it, I don’t know. But it’s a start to ideas

1 Like

I thought I figured out the Julian Day:

Julian Day - (round(367*#Dyyyy#-(7*(#Dyyyy#+((#DM#+9)/12)))/4+(275*#DM#)/9+#Dd#-730530))

but it seems like I’m slightly off. I’ll need to review the formula.

I think I fixed it, now reads 2458000.5 for today:

Julian Day - ((367*#Dyyyy#)-floor(7*(#Dyyyy#+floor((#DM#+9)/12))/4)+floor(275*#DM#/9)+#Dd#+1721013.5)

seems like I may be 1 day off.

@eradicator09 - If I calculate backwards, your calculated Day, it seems to be the 21. of August, this year. So not one, but many day’s beside.
I put in a link here: What's the Current Day Number?
Which hold more ways to calculate Julian Day.
OR, I can send you all these calculations I use on my Weather Site, for calculation of the Sun’s position, and Sunset and Sunrise. (It’s in java).

@eradicator09 - I just made a new calculation (the other I made, was wrong), with these tags used in your formula. And the JD is correct in you Formula, and not 1 day beside, as you write.
Even if you use the long Formula version, it gives correct number:
((367*#Dyyyy#)-floor(7*(#Dyyyy#+floor((#DM#+9)/12))/4)-floor(3*(floor((#Dyyyy#+(#DM#-9)/7)/100)+1)/4)+floor(275*#DM#/9)+#Dd#+1721028.5)

yep, looks right when comparing to this calculator:
http://aa.usno.navy.mil/data/docs/JulianDate.php

I wonder did I just miss a short section in the formula?

Do these require the lat/lon to calculate?

Only the Latitude!
But in fact, you actually only need the actual day (called JD) of the actual year, to use in end calculation for the Sun’s position, and thereby, the Twilight! And not the fully JD.

No! as I recalculate, by using you’r (short) version of the formula, it is still correct.

1 Like

Well at least that’s something. I wonder if there is a way to solve for latitude by knowing the Sunset and Sunrise times, and date (declination).

That’s where I see the problem, with your “project”. And it was to put in the Latitude (or Longitude), into a watch calculation, as I can’t see anywhere, where it should be possible.
If there was a tag, who could give the GPS info, it could in a way be possible, to use it in that way.
I can’t see a way, to recalculate to find position, by knowing Sunrise, Sunset times.

Figured out Declination:

((round(23.45sin(((360/365)(284+#DD#))*pi/180)*100))/100

Displays as 6.18 for today which appears correct.

Here is my working demo of some of the calculations:

What is you’r actual latitude (Livingplace)?

30.4583° N, 91.1403° W

for Baton Rouge, LA USA

I can’t get it to be fully correct (or fit with your’s time). I get 6.44!!
To could get the exact Twilight, it has to be correct on the exact minute. I will try to look more close on all these tags in facer, to learn more about what’s possible. I’m just started, using the system.

Ohhhhh very nice! Would have been perfect for the eclipse on August 21st!

Hi @eradicator09, did you manage to get a working expression for astronomical twighlight?