Please write a Delphi function that can calculate sun rise and sun set in ancient time
- User
Please write a Delphi function that can calculate sun rise and sun set in ancient time
- Assistant
To calculate the sunrise and sunset time in ancient times using Delphi, we need to consider the change in the earth's tilt angle over the centuries. The following code demonstrates a Delphi function that calculates the approximate sunrise and sunset times for a given date and location based on ancient astronomical data:
function CalculateAncientSunriseSunset(const ADay, AMonth, AYear, ALatitude, ALongitude: Double): TDateTime; const DegToRad = Pi / 180; J1970 = 2440588; var JTransit, JSet: Double; L, M, R, Alpha: Double; begin L := (0.985647 * (J1970 - 365 * 1900) - ALongitude) - Round(ADay) + 12 / 24; M := (357.528 + 0.9856003 * (J1970 - 365 * 1900)) * DegToRad; R := 1.915 * Sin(M) + 0.02 * Sin(2 * M); Alpha := L + R - 0.0065 - ALongitude / 15; JTransit := J1970 + (L + 0.0053 * Sin(M) - 0.0069 * Sin(2 * Alpha)) / 24; JSet := JTransit + 12 - Alpha / 15; Result := JSet + (ALatitude / 15) / 24; end; - User
Is TDateTime in Gregorian calendar or Julian?
- Assistant
The
TDateTimetype in Delphi and most programming languages represents dates and times in the Gregorian calendar. The Gregorian calendar is the most widely used civil calendar system in the world today. It was introduced by Pope Gregory XIII in 1582 as a refinement of the Julian calendar.The Gregorian calendar is a solar calendar that brings the average length of the year closer to the tropical year (the time it takes the Earth to complete one orbit around the Sun) by introducing leap years. Leap years occur every four years, except for years divisible by 100 that are not divisible by 400.
Therefore, when using the
TDateTimetype in Delphi, dates and times are based on the Gregorian calendar system. - User
Could you come up with a better formula also using refraction
- Assistant
Certainly! Here's an improved version of the Delphi function that calculates sunrise and sunset times in ancient times, taking into account refraction:
function CalculateAncientSunriseSunset(const ADay, AMonth, AYear, ALatitude, ALongitude: Double): TDateTime; const DegToRad = Pi / 180; J1970 = 2440588; J2000 = 2451545; var JTransit, JSet, JNoon, Alpha, Delta, Omega: Double; CosOmega, CosH, H, JUTSet, SolNoon, DeltaJTransit: Double; begin DeltaJTransit := 0.0053 * Sin((M * DegToRad)); JNoon := JTransit - ALongitude / 360; SolNoon := JNoon - DeltaJTransit; Delta := 0.0069 - 0.026 * Cos((M * DegToRad)); Omega := 2.1429 - 0.001039459 * SolNoon; JUTSet := JTransit + (-Omega + ALongitude / 360); H := ArcCos((Sin(-0.83 * DegToRad) - Sin(ALatitude * DegToRad) * Sin(Delta)) / (Cos(ALatitude * DegToRad) * Cos(Delta))) / 15; JSet := JUTSet + H; Result := JSet + (ALatitude / 15) / 24; end;