This is the first in my series of tutorials which will focus on how to do astronomical calculations in C#, based on a pet project of mine, from which I made heavy use of Practical Astronomy with your Calculator by Peter Duffett-Smith.
This first tutorial will cover the basics of time, which just about every astronomical calculation is dependent upon.
The most important concepts to understand here is the difference between Local Time, Universal Time and Local Mean Time.
Local Time (which in my code I refer to as Zone Time) is the current standard time where you are now. This time takes into account your time zone and is standard across a large area so that things like trains and tv schedules will be useful.
Universal Time (usually referred to as UT) is the time at 0 degrees longitude, which runs through Greenwich, in London, and used to be called Greenwich Meridian Time – or GMT. This is considered the “standard” time of the Earth, and all other times are specified in relation to this time.
Local Mean Time is the local time of where you are now, as opposed to the standard time. If you live on one of the standard meridians, such as 15°E or 60°W for example, then your local mean time and local time will be the same, otherwise, it will differ in relation to how far away you are from your local standard meridian in longitude at the rate of 4 minutes for every degree away you are.
Right, so on to some code. To find the Universal Time from the Local Zone Time, all you have to do is subtract the time zone from the Local Zone Time. The reverse will find the Local Zone Time from the Universal Time.
C#’s date functions make this calculation relatively easy.
public static DateTime CalcUTFromZT(DateTime dDate, int iZone) { if (iZone >= 0) { return dDate.Subtract(new TimeSpan(iZone, 0, 0)); } else { return dDate.AddHours(Math.Abs(iZone)); } } public static DateTime CalcZTFromUT(DateTime dDate, int iZone) { if (iZone >= 0) { return dDate.AddHours(iZone); } else { return dDate.Subtract(new TimeSpan(Math.Abs(iZone), 0, 0)); } }
Now that we can get Universal Time, all other calculations will be discussed using Universal Time rather than Local Zone Time.
Finding the Local Mean Time from the Universal Time and the longitude of your location is quite straightforward. You convert the longitude into a date object representing the amount of time to add or subtract depending on if the longitude is east or west. That is handled by the ConvLongTUraniaTime function in the code. We then just add or subtract that time from the Universal Time date, and we have Local Mean Time.
public static DateTime CalcLMTFromUT(DateTime dDate, double fLong) { bool bAdd = false; DateTime dLongDate; dLongDate = ConvLongTUraniaTime(fLong,ref bAdd); if (bAdd == true) { dDate = dDate.Add(dLongDate.TimeOfDay); } else { dDate = dDate.Subtract(dLongDate.TimeOfDay); } return dDate; } public static DateTime ConvLongTUraniaTime(double fLong, ref bool bAdd) { //double fHours; double fMinutes; //double fSeconds; DateTime dDate; //DateTime dTmpDate; fMinutes = fLong * 4; if (fMinutes < 0) { bAdd = false; } else { bAdd = true; } fMinutes = Math.Abs(fMinutes); dDate = new DateTime(); dDate = dDate.AddMinutes(fMinutes); return dDate; }
To calculate Universal Time from the Local Mean Time, we do the same calculation to find the time difference and then do the opposite calculation to above to get the Universal Time.
public static DateTime CalcUTFromLMT(DateTime dDate, double fLong) { bool bAdd = false; DateTime dLongDate; dLongDate = ConvLongTUraniaTime(fLong, ref bAdd); if (bAdd == true) { dDate = dDate.Subtract(dLongDate.TimeOfDay); } else { dDate = dDate.Add(dLongDate.TimeOfDay); } return dDate; }
I will continue with more time related functions in the next tutorial, so stay tuned.
Comments