How could this be implemented?
Thank you :)
A practical way to get reliable morning/afternoon/sunset/night labels for any location is to compute the sun’s actual times (sunrise, sunset, dawn, dusk) for the given date and lat/lon, then derive morning/afternoon from solar noon and use twilight boundaries for the “sunset” window. This answers ’s need to respect different daylight lengths and builds on ’s point about defining inputs/outputs; it also complements ’s suggestion (useful for rough models) by showing the precise astronomical approach for per-location accuracy.
Key formula to use: once you have the solar declination (delta) for the date, the hour angle H for a chosen solar elevation h0 is
cos(H) = (sin(h0) - sin(lat)sin(delta)) / (cos(lat)cos(delta))
Use h0 = -0.83 degrees for geometric sunrise/sunset (accounts for refraction + sun radius). For twilight, use h0 = -6 (civil), -12 (nautical), -18 (astronomical). If |cos(H)| > 1 there is no rise/set that day (polar day/night) — handle that as a special case.
Example workflow (pseudocode):
1. convert input date/time to UTC and compute Julian Day
2. compute solar declination (delta) and equation-of-time (use Meeus/NOAA routine)
3. get solar_noon_utc from the same routine
4. compute H = arccos( (sin(h0) - sin(lat)*sin(delta)) / (cos(lat)*cos(delta)) )
5. sunrise_utc = solar_noon_utc - H/15 ; sunset_utc = solar_noon_utc + H/15
6. compute dawn/dusk using h0 = -6 deg if needed
7. classify timestamp against dawn/sunrise/solar_noon/sunset/dusk; if no rise/set apply polar rules Practical tips: always convert between UTC and local time using a timezone database (IANA tz) rather than longitude alone; use tested libraries (astronomical libs in your language) instead of hand-rolled trig where possible; cache sunrise/sunset per date+location if classifying many timestamps.
Jump to Post— apines 116How about you start and we will help?
Jump to Post— apines 116Then you need to define the problem a little better - what is the input, what is the expected output?
How about you start and we will help?
How about you start and we will help?
I am stuck. I don't know how to start. I know that it is related to the longitude and latitude positions and the regions of where it is dark and bright. I do not know what math is behind it though.
Just FYI, this is for a major project I am developing where I will be classifying data according to time of day.
Then you need to define the problem a little better - what is the input, what is the expected output?
Then you need to define the problem a little better - what is the input, what is the expected output?
Input:
- The current date which includes the day, month, and the year and hours/minutes/seconds.
- Location's the longitude/latitude.
Output: Morning/Afternoon/Sunset/Night status is returned.
Again just to be clear, you want to know the Morning/Night status like 12:00 is noon, 22:00 is night time, or you want to know the light times where you need to consider things like the north poll, where there can be light for days.
Again just to be clear, you want to know the Morning/Night status like 12:00 is noon, 22:00 is night time, or you want to know the light times where you need to consider things like the north poll, where there can be light for days.
Well, I need to divide it based on the light times. Morning and night alone are not enough. Different locations have different lengths of sunlight and darkness- I need to consider this if I want it to work in any city.
Well, then you will have to provide the formulas to how to calculate such a thing (not the code, just the formulas), and then we will try to implement it into code.
If you don't have any formulas to start with, I suggest you start off by looking up some things:
- At which latitude is the north/south polar circle (beyond this one there's midnight sun 24 hours a day during a period in summer and dark 24 hours a day during a period in winter)
- When is the first/last day of midnight sun and when is the first/last day of total darkness
This data should be sufficient. Now, since earth is a sphere that is slightly tilted, spinning around its own axis and travelling in a "spherical" path around the sun, I suggest you start off working with some sine patterns.
(Tip: Start by describing the suns motion over the globe with the sum of two sine curves, one with the period of one year, and one with the period of 24 hours.)
Good luck to you!
Emil Olofsson
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.