using System; using System.Collections.Generic; using System.Text; namespace SmartieLc { public class LCDSmartie { protected const double LAT = [LAT]; protected const double LON = [LON]; protected const double ZENITH = 90.83333; public string function1(string a, string b) { DateTime sunset = Sunset(DateTime.Now); if (DateTime.Compare(sunset, DateTime.Now) < 0) sunset = Sunset(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 1)); //doing this subtraction returns a TimeSpan object, whose ToString method I call return (sunset - DateTime.Now).ToString(); } public DateTime Sunset(DateTime date) { int dayOfYear = date.DayOfYear; double tApprox = dayOfYear + ((18 - LON / 15) / 24); double meanAnomaly = (0.9856 * tApprox) - 3.289; double sunTrueLon = meanAnomaly + (1.916 * Sin(meanAnomaly)) + (0.020 * Sin(2 * meanAnomaly)) + 282.634; if (sunTrueLon > 360) while (sunTrueLon > 360) sunTrueLon -= 360; if (sunTrueLon < 0) while (sunTrueLon < 0) sunTrueLon += 360; double rightAscension = Atan(0.91764 * Tan(sunTrueLon)); if (rightAscension > 360) while (rightAscension > 360) rightAscension -= 360; if (rightAscension < 0) while (rightAscension < 0) rightAscension += 360; rightAscension = rightAscension + 90 * (Math.Floor(sunTrueLon / 90) - Math.Floor(rightAscension / 90)); rightAscension = rightAscension / 15; double sinDec = .39782 * Sin(sunTrueLon); double cosDec = Cos(Asin(sinDec)); double cosHour = (Cos(ZENITH) - sinDec * Sin(LAT)) / (cosDec * Cos(LAT)); double hour = Acos(cosHour) / 15; double time = hour + rightAscension - (0.06571 * tApprox) - 6.622; time = time - LON / 15; time -= 7; while (time < 0) time += 24; while (time > 24) time -= 24; DateTime sunset = new DateTime(date.Year, date.Month, date.Day, (int)time, (int)((time - (int)time) * 60), 0); return sunset; } private double Sin(double degrees) { return Math.Sin(Math.PI / 180 * degrees); } private double Cos(double degrees) { return Math.Cos(Math.PI / 180 * degrees); } private double Tan(double degrees) { return Math.Tan(Math.PI / 180 * degrees); } private double Atan(double x) { return 180 / Math.PI * Math.Atan(x); } private double Acos(double x) { return 180 / Math.PI * Math.Acos(x); } private double Asin(double x) { return 180 / Math.PI * Math.Asin(x); } } }