C#可用的日出日落时间类
生活随笔
收集整理的這篇文章主要介紹了
C#可用的日出日落时间类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
一個現成代碼的公共類庫,復制下來作為一個類文件就可以調用了。一般不需要了解實現過程,各種數學公式太麻煩。
?
調用方法:
SunTimeResult result = SunTimes.GetSunTime(DateTime.Now, double.Parse(Longitude), double.Parse(Latitude)); MessageBox.Show(string.Format("今天日出時間:{0},日落時間:{1}", result.SunriseTime.ToString("HH:mm:ss"), result.SunsetTime.ToString("HH:mm:ss")));DateTime.Now是現在的時間 Longitude是經度 Latitude是緯度
?
類庫代碼如下:
using System;namespace SwitchTime {/// <summary>/// 日出日落時間類/// </summary>public static class SunTimes{#region 公共方法/// <summary>/// 計算日長/// </summary>/// <param name="date">日期</param>/// <param name="longitude">經度</param>/// <param name="latitude">緯度</param>/// <returns>日長</returns>/// <remarks>/// 注:日期最小為2000.1.1號/// </remarks>public static double GetDayLength(DateTime date, double longitude, double latitude){double result = DayLen(date.Year, date.Month, date.Day, longitude, latitude, -35.0 / 60.0, 1);return result;}/// <summary>/// 計算日出日沒時間/// </summary>/// <param name="date">日期</param>/// <param name="longitude">經度</param>/// <param name="latitude">緯度</param>/// <returns>日落日出時間</returns>/// <remarks>/// 注:日期最小為2000.1.1號/// </remarks>public static SunTimeResult GetSunTime(DateTime date, double longitude, double latitude){double start = 0;double end = 0;SunRiset(date.Year, date.Month, date.Day, longitude, latitude, -35.0 / 60.0, 1, ref start, ref end);DateTime sunrise = ToLocalTime(date, start);DateTime sunset = ToLocalTime(date, end);return new SunTimeResult(sunrise, sunset);}#endregion#region 私有方法#region 時間轉換private static DateTime ToLocalTime(DateTime time, double utTime){int hour = Convert.ToInt32(Math.Floor(utTime));double temp = utTime - hour;hour += 8;//轉換為東8區北京時間temp = temp * 60;int minute = Convert.ToInt32(Math.Floor(temp));try{return new DateTime(time.Year, time.Month, time.Day, hour, minute, 0);}catch{return new DateTime(time.Year, time.Month, time.Day, 0, 0, 0);}}#endregion#region 與日出日落時間相關計算private static double DayLen(int year, int month, int day, double lon, double lat,double altit, int upper_limb){double d, /* Days since 2000 Jan 0.0 (negative before) */obl_ecl, /* Obliquity (inclination) of Earth's axis *///黃赤交角,在2000.0歷元下國際規定為23度26分21.448秒,但有很小的時間演化。sr, /* Solar distance, astronomical units */slon, /* True solar longitude */sin_sdecl, /* Sine of Sun's declination *///太陽赤緯的正弦值。cos_sdecl, /* Cosine of Sun's declination */sradius, /* Sun's apparent radius */t; /* Diurnal arc *//* Compute d of 12h local mean solar time */d = Days_since_2000_Jan_0(year, month, day) + 0.5 - lon / 360.0;/* Compute obliquity of ecliptic (inclination of Earth's axis) */obl_ecl = 23.4393 - 3.563E-7 * d;//這個黃赤交角時變公式來歷復雜,很大程度是經驗性的,不必追究。/* Compute Sun's position */slon = 0.0;sr = 0.0;Sunpos(d, ref slon, ref sr);/* Compute sine and cosine of Sun's declination */sin_sdecl = Sind(obl_ecl) * Sind(slon);cos_sdecl = Math.Sqrt(1.0 - sin_sdecl * sin_sdecl);//用球面三角學公式計算太陽赤緯。/* Compute the Sun's apparent radius, degrees */sradius = 0.2666 / sr;//視半徑,同前。/* Do correction to upper limb, if necessary */if (upper_limb != 0)altit -= sradius;/* Compute the diurnal arc that the Sun traverses to reach *//* the specified altitide altit: *///根據設定的地平高度判據計算周日弧長。double cost;cost = (Sind(altit) - Sind(lat) * sin_sdecl) /(Cosd(lat) * cos_sdecl);if (cost >= 1.0)t = 0.0; /* Sun always below altit *///極夜。else if (cost <= -1.0)t = 24.0; /* Sun always above altit *///極晝。else t = (2.0 / 15.0) * Acosd(cost); /* The diurnal arc, hours *///周日弧換算成小時計。return t;}private static void Sunpos(double d, ref double lon, ref double r){double M,//太陽的平均近點角,從太陽觀察到的地球(=從地球看到太陽的)距近日點(近地點)的角度。w, //近日點的平均黃道經度。e, //地球橢圓公轉軌道離心率。E, //太陽的偏近點角。計算公式見下面。x, y,v; //真近點角,太陽在任意時刻的真實近點角。M = Revolution(356.0470 + 0.9856002585 * d);//自變量的組成:2000.0時刻太陽黃經為356.0470度,此后每天約推進一度(360度/365天w = 282.9404 + 4.70935E-5 * d;//近日點的平均黃經。e = 0.016709 - 1.151E-9 * d;//地球公轉橢圓軌道離心率的時間演化。以上公式和黃赤交角公式一樣,不必深究。E = M + e * Radge * Sind(M) * (1.0 + e * Cosd(M));x = Cosd(E) - e;y = Math.Sqrt(1.0 - e * e) * Sind(E);r = Math.Sqrt(x * x + y * y);v = Atan2d(y, x);lon = v + w;if (lon >= 360.0)lon -= 360.0;}private static void Sun_RA_dec(double d, ref double RA, ref double dec, ref double r){double lon, obl_ecl, x, y, z;lon = 0.0;Sunpos(d, ref lon, ref r);//計算太陽的黃道坐標。x = r * Cosd(lon);y = r * Sind(lon);//計算太陽的直角坐標。obl_ecl = 23.4393 - 3.563E-7 * d;//黃赤交角,同前。z = y * Sind(obl_ecl);y = y * Cosd(obl_ecl);//把太陽的黃道坐標轉換成赤道坐標(暫改用直角坐標)。RA = Atan2d(y, x);dec = Atan2d(z, Math.Sqrt(x * x + y * y));//最后轉成赤道坐標。顯然太陽的位置是由黃道坐標方便地直接確定的,但必須轉換到赤//道坐標里才能結合地球的自轉確定我們需要的白晝長度。}/// <summary>/// 日出沒時刻計算/// </summary>/// <param name="year">年</param>/// <param name="month">月</param>/// <param name="day">日</param>/// <param name="lon"></param>/// <param name="lat"></param>/// <param name="altit"></param>/// <param name="upper_limb"></param>/// <param name="trise">日出時刻</param>/// <param name="tset">日沒時刻</param>/// <returns>太陽有出沒現象,返回0 極晝,返回+1 極夜,返回-1</returns>private static int SunRiset(int year, int month, int day, double lon, double lat,double altit, int upper_limb, ref double trise, ref double tset){double d, /* Days since 2000 Jan 0.0 (negative before) *///以歷元2000.0起算的日數。sr, /* Solar distance, astronomical units *///太陽距離,以天文單位計算(約1.5億公里)。 sRA, /* Sun's Right Ascension *///同前,太陽赤經。sdec, /* Sun's declination *///太陽赤緯。sradius, /* Sun's apparent radius *///太陽視半徑,約16分(受日地距離、大氣折射等諸多影響)t, /* Diurnal arc *///周日弧,太陽一天在天上走過的弧長。tsouth, /* Time when Sun is at south */sidtime; /* Local sidereal time *///當地恒星時,即地球的真實自轉周期。比平均太陽日(日常時間)長3分56秒。 int rc = 0; /* Return cde from function - usually 0 *//* Compute d of 12h local mean solar time */d = Days_since_2000_Jan_0(year, month, day) + 0.5 - lon / 360.0;//計算觀測地當日中午時刻對應2000.0起算的日數。/* Compute local sideral time of this moment */sidtime = Revolution(GMST0(d) + 180.0 + lon);//計算同時刻的當地恒星時(以角度為單位)。以格林尼治為基準,用經度差校正。/* Compute Sun's RA + Decl at this moment */sRA = 0.0;sdec = 0.0;sr = 0.0;Sun_RA_dec(d, ref sRA, ref sdec, ref sr);//計算同時刻太陽赤經赤緯。/* Compute time when Sun is at south - in hours UT */tsouth = 12.0 - Rev180(sidtime - sRA) / 15.0;//計算太陽日的正午時刻,以世界時(格林尼治平太陽時)的小時計。/* Compute the Sun's apparent radius, degrees */sradius = 0.2666 / sr;//太陽視半徑。0.2666是一天文單位處的太陽視半徑(角度)。/* Do correction to upper limb, if necessary */if (upper_limb != 0)altit -= sradius;//如果要用上邊緣,就要扣除一個視半徑。/* Compute the diurnal arc that the Sun traverses to reach *///計算周日弧。直接利用球面三角公式。如果碰到極晝極夜問題,同前處理。/* the specified altitide altit: */double cost;cost = (Sind(altit) - Sind(lat) * Sind(sdec)) /(Cosd(lat) * Cosd(sdec));if (cost >= 1.0){rc = -1;t = 0.0;}else{if (cost <= -1.0){rc = +1;t = 12.0; /* Sun always above altit */}elset = Acosd(cost) / 15.0; /* The diurnal arc, hours */}/* Store rise and set times - in hours UT */trise = tsouth - t;tset = tsouth + t;return rc;}#endregion#region 輔助函數/// <summary>/// 歷元2000.0,即以2000年第一天開端為計日起始(天文學以第一天為0日而非1日)。/// 它與UT(就是世界時,格林尼治平均太陽時)1999年末重合。 /// </summary>/// <param name="y"></param>/// <param name="m"></param>/// <param name="d"></param>/// <returns></returns>private static long Days_since_2000_Jan_0(int y, int m, int d){return (367L * (y) - ((7 * ((y) + (((m) + 9) / 12))) / 4) + ((275 * (m)) / 9) + (d) - 730530L);}private static double Revolution(double x){return (x - 360.0 * Math.Floor(x * Inv360));}private static double Rev180(double x){return (x - 360.0 * Math.Floor(x * Inv360 + 0.5));}private static double GMST0(double d){double sidtim0;sidtim0 = Revolution((180.0 + 356.0470 + 282.9404) +(0.9856002585 + 4.70935E-5) * d);return sidtim0;}private static double Inv360 = 1.0 / 360.0;#endregion#region 度與弧度轉換系數,為球面三角計算作準備private static double Sind(double x){return Math.Sin(x * Degrad);}private static double Cosd(double x){return Math.Cos(x * Degrad);}private static double Tand(double x){return Math.Tan(x * Degrad);}private static double Atand(double x){return Radge * Math.Atan(x);}private static double Asind(double x){return Radge * Math.Asin(x);}private static double Acosd(double x){return Radge * Math.Acos(x);}private static double Atan2d(double y, double x){return Radge * Math.Atan2(y, x);}private static double Radge = 180.0 / Math.PI;private static double Degrad = Math.PI / 180.0;#endregion#endregion}/// <summary>/// 日出日落時間結果/// </summary>public class SunTimeResult{#region 構造與析構/// <summary>/// 構造函數/// </summary>/// <param name="sunrise">日出時間</param>/// <param name="sunset">日落時間</param>public SunTimeResult(DateTime sunrise, DateTime sunset){sunriseTime = sunrise;sunsetTime = sunset;}#endregion#region 屬性定義/// <summary>/// 獲取日出時間/// </summary>public DateTime SunriseTime{get{return sunriseTime;}}/// <summary>/// 獲取日落時間/// </summary>public DateTime SunsetTime{get{return sunsetTime;}}#endregion#region 私成員private DateTime sunriseTime;//日出時間private DateTime sunsetTime;//日落時間#endregion} }
轉載于:https://www.cnblogs.com/jhlong/p/5842265.html
總結
以上是生活随笔為你收集整理的C#可用的日出日落时间类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法编程题的心得体会
- 下一篇: (王道408考研数据结构)第三章栈和队列