• Calendar to julian date format


    1.JULIAN DATE 定义

    2.示例:

      定义枚举:

     public enum JulianDateType
        {
            /// <summary>
            /// Julian Date (JD)
            /// </summary>
            JD,
            /// <summary>
            /// Julian Day Number (JDN)
            /// </summary>
            JDN,
            /// <summary>
            /// Chronological JD (CJD)
            /// </summary>
            CJD,
            /// <summary>
            /// Reduced JD (RJD)
            /// </summary>
            RJD,
            /// <summary>
            /// Modified JD (MJD)
            /// </summary>
            MJD,
            /// <summary>
            ///  Truncated JD (TJD)
            /// </summary>
            TJD,
            /// <summary>
            /// Dublin JD (DJD)
            /// </summary>
            DJD,
            /// <summary>
            /// Lilian Date
            /// </summary>
            LD,
            /// <summary>
            /// Military Julian Date(yddd)
            /// </summary>
            JDD,
            /// <summary>
            /// Ordinal date
            /// </summary>
            OD
        }

     2.转换方法

     private static Dictionary<string, int> offsetdictionary = new Dictionary<string, int>()
           {
              {"GMT-12:00",-720},{"GMT-11:00",-660},{"GMT-10:00",-600},{"GMT-09:30",-570},{"GMT-09:00",-540},{"GMT-08:00",-480},
              {"GMT-07:00",-420},{"GMT-06:00",-360},{"GMT-05:00",-300},{"GMT-04:30",-270},{"GMT-04:00",-240},{"GMT-03:30",-210},
              {"GMT-03:00",-180},{"GMT-02:00",-120},{"GMT-01:00",-60},{"GMT-00:00",0},{"GMT+01:00",60},{"GMT+02:00",120},
              {"GMT+03:00",180},{"GMT+03:30",210},{"GMT+04:00",240},{"GMT+04:30",270},{"GMT+05:00",300},{"GMT+05:30",330},
              {"GMT+05:45",345},{"GMT+06:00",360},{"GMT+06:30",390},{"GMT+07:00",420},{"GMT+08:00",480},{"GMT+09:00",540},
              {"GMT+09:30",570},{"GMT+10:00",600},{"GMT+10:30",630},{"GMT+11:00",660},{"GMT+11:30",690},{"GMT+12:00",720},
              {"GMT+12:45",765},{"GMT+13:00",780},{"GMT+14:00",840}
           };

    /// <summary>
            /// Calendar to julian date format
            /// </summary>
            /// <param name="_year">year size 4</param>
            /// <param name="_month">month size 2</param>
            /// <param name="_day">day szie 2</param>
            /// <param name="_hours"> hours szie 2</param>
            /// <param name="_minutes">minutes size 2</param>
            /// <param name="_seconds">seconds size 2</param>
            /// <param name="jdtype">enum JulianDateType</param>
            /// <returns>convert julian date value</returns>
            public static string CalendarDateToJD(int _year, int _month, int _day, int _hours, int _minutes, int _seconds, JulianDateType jdtype)
            {
                string calendarstring = "";
                decimal jddate = 0;
                int dayN = 0, feb = 0;
                int cur_offset = offsetdictionary["GMT+08:00"];
                //验证是否为空
                if (((_year % 4) == 0 && (_year % 100) != 0) || (_year % 400) == 0)
                {
                    // leap year
                    if ((_month == 2) && (_day > 29))
                    {
                        //Msg:"Leap year: February cannot have more than 29 days.
                        return "Leap year: February cannot have more than 29 days";
                    }
                    feb = 29;
                }
                else
                {
                    // nonleap year
                    if ((_month == 2) && (_day > 28))
                    {
                        return "February cannot have more than 28 days.";
                    }
                    feb = 28;
                }

                if (((_month == 4) || (_month == 6) || (_month == 9) || (_month == 11)) && (_day > 30))
                {
                    return "This month cannot have more than 30 days.";
                }
                for (var i = 1; i < _month; i++)
                {
                    if ((i == 4) || (i == 6) || (i == 9) || (i == 11))
                    {
                        dayN += 30;
                    }
                    else if (i == 2)
                    {
                        dayN += feb;
                    }
                    else
                    {
                        dayN += 31;
                    }
                }
                double a = 7 * (_year + (_month + 9) / 12) / 4;

                double b = 3 * ((_year + (_month - 9) / 7) / 100 + 1) / 4;

                double c = 275 * _month / 9;
                jddate = 367 * Convert.ToDecimal(_year) - Convert.ToDecimal(a) - Convert.ToDecimal(b) +

               Convert.ToDecimal(c + _day + 1721028.5) + (Convert.ToDecimal(_hours) +

               (Convert.ToDecimal(_minutes) / 60) + (Convert.ToDecimal(_seconds) / 3600) - (Convert.ToDecimal(cur_offset) / 60)) / 24;
                switch (jdtype)
                {
                    case JulianDateType.JD:
                        calendarstring = Math.Round(jddate, 7).ToString();
                        break;
                    case JulianDateType.JDN:
                        calendarstring = jddate.ToString().Substring(0, jddate.ToString().IndexOf("."));
                        break;
                    case JulianDateType.CJD:
                        string fraction = "0." + cur_offset;
                        if (fraction.IndexOf("-") > -1) fraction = "0";
                        jddate = Math.Round(jddate, 7) + Convert.ToDecimal(0.5) + Convert.ToDecimal(fraction) * 60 / 100;
                        calendarstring = jddate.ToString();
                        break;
                    case JulianDateType.RJD:
                        jddate = jddate - 2400000;
                        calendarstring = Math.Round(jddate, 7).ToString();
                        break;
                    case JulianDateType.MJD:
                        jddate = jddate - Convert.ToDecimal(2400000.5);
                        calendarstring = Math.Round(jddate, 7).ToString();
                        break;
                    case JulianDateType.TJD:
                        decimal intFraction = _hours * 3600 + _minutes * 60 + _seconds;
                        jddate = jddate - Convert.ToDecimal(2440000.5);
                        calendarstring = jddate.ToString().Substring(0, jddate.ToString().IndexOf(".")) + "/" + intFraction;
                        break;
                    case JulianDateType.DJD:
                        calendarstring = Math.Round(jddate - 2415020, 7).ToString();
                        break;
                    case JulianDateType.LD:

                        jddate = jddate - Convert.ToDecimal(2299160.5);
                        calendarstring = jddate.ToString().Substring(0, jddate.ToString().IndexOf("."));
                        break;
                    case JulianDateType.JDD:
                        dayN += _day;
                        calendarstring = (_year.ToString() + dayN.ToString()).Substring(3);
                        break;
                    case JulianDateType.OD:
                        dayN += _day;
                        calendarstring = _year.ToString() + dayN.ToString();
                        break;
                }

                return calendarstring;
            }

  • 相关阅读:
    创建部署规划
    并发容器Map之一:(jdk1.8) ConcurrentHashMap的红黑树实现分析
    CopyOnWrite容器之二:CopyOnWriteArraySet
    jQuery1.5的新特征subclass——jQuery插件机制的救赎
    getElementsByTagName
    facebook是如何管理代码的
    由addClass衍生出来的字符串去重问题
    节点排序
    Sizzle是怎样工作的
    我的模块加载系统 v3
  • 原文地址:https://www.cnblogs.com/linsu/p/Julian.html
Copyright © 2020-2023  润新知