示例#1
0
        /// <summary>
        /// New date from components
        /// </summary>
        public UniversalTime(int year, int month, int day, double hour)
            : this()
        {
            var jd = SweDate.DateToJulianDay(year, month, day, hour, DateCalendar.Julian);

            SweDate.JulianDayToDate(jd, DateCalendar.Julian, out _Year, out _Month, out _Day, out _Hours, out _Minutes, out _Seconds);
        }
示例#2
0
        /// <summary>
        /// Sub offset
        /// </summary>
        public static UniversalTime operator -(UniversalTime date, TimeSpan offset)
        {
            var jd = SweDate.DateToJulianDay(date, DateCalendar.Gregorian);

            jd -= offset.TotalDays;
            return(SweDate.JulianDayToDate(jd, DateCalendar.Gregorian));
        }
示例#3
0
 /// <summary>
 /// Create a new Julian Day from a DateUT
 /// </summary>
 /// <param name="date">Date source</param>
 /// <param name="calendar">Calendar source</param>
 public JulianDay(UniversalTime date, DateCalendar?calendar = null)
     : this()
 {
     this.Calendar = calendar ?? SweDate.GetCalendar(date.Year, date.Month, date.Day);
     this.Value    = SweDate.DateToJulianDay(date, this.Calendar);
 }
示例#4
0
        /// <summary>
        /// To string with format
        /// </summary>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            if (String.IsNullOrWhiteSpace(format))
            {
                format = DefaultFormat;
            }
            System.Globalization.DateTimeFormatInfo dfi = (formatProvider ?? System.Globalization.CultureInfo.CurrentCulture)
                                                          .GetFormat(typeof(System.Globalization.DateTimeFormatInfo)) as System.Globalization.DateTimeFormatInfo;
            dfi = dfi ?? System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
            StringBuilder result = new StringBuilder();
            int           cnt = 0, fl = format.Length;

            for (int i = 0; i < fl; i++)
            {
                char c = format[i];
                switch (c)
                {
                case '\\':
                    i++;
                    if (i < format.Length)
                    {
                        result.Append(format[i]);
                    }
                    else
                    {
                        result.Append('\\');
                    }
                    break;

                case 'd':
                    cnt = 0;
                    while (i < fl && format[i] == 'd')
                    {
                        cnt++; i++;
                    }
                    i--;
                    var jd = SweDate.DateToJulianDay(this);
                    int nd = ((int)SweDate.DayOfWeek(jd)) + 1;
                    if (nd >= 7)
                    {
                        nd -= 7;
                    }
                    if (cnt == 1)
                    {
                        result.Append(this.Day);
                    }
                    else if (cnt == 2)
                    {
                        result.Append(this.Day.ToString("D2"));
                    }
                    else if (cnt == 3)
                    {
                        result.Append(dfi.AbbreviatedDayNames[nd]);
                    }
                    else
                    {
                        result.Append(dfi.DayNames[nd]);
                    }
                    break;

                case 'M':
                    cnt = 0;
                    while (i < fl && format[i] == 'M')
                    {
                        cnt++; i++;
                    }
                    i--;
                    if (cnt == 1)
                    {
                        result.Append(this.Month);
                    }
                    else if (cnt == 2)
                    {
                        result.Append(this.Month.ToString("D2"));
                    }
                    else if (cnt == 3)
                    {
                        result.Append(dfi.AbbreviatedMonthNames[this.Month - 1]);
                    }
                    else
                    {
                        result.Append(dfi.MonthNames[this.Month - 1]);
                    }
                    break;

                case 'y':
                    cnt = 0;
                    while (i < fl && format[i] == 'y')
                    {
                        cnt++; i++;
                    }
                    i--;
                    if (cnt == 1)
                    {
                        result.Append(this.Year % 100);
                    }
                    else if (cnt == 2)
                    {
                        result.Append((this.Year % 100).ToString("D2"));
                    }
                    else
                    {
                        result.Append(this.Year);
                    }
                    break;

                case 'h':
                    cnt = 0;
                    while (i < fl && format[i] == 'h')
                    {
                        cnt++; i++;
                    }
                    i--;
                    if (cnt == 1)
                    {
                        result.Append(this.Hours % 12);
                    }
                    else
                    {
                        result.Append((this.Hours % 12).ToString("D2"));
                    }
                    break;

                case 'H':
                    cnt = 0;
                    while (i < fl && format[i] == 'H')
                    {
                        cnt++; i++;
                    }
                    i--;
                    if (cnt == 1)
                    {
                        result.Append(this.Hours);
                    }
                    else
                    {
                        result.Append(this.Hours.ToString("D2"));
                    }
                    break;

                case 'm':
                    cnt = 0;
                    while (i < fl && format[i] == 'm')
                    {
                        cnt++; i++;
                    }
                    i--;
                    if (cnt == 1)
                    {
                        result.Append(this.Minutes);
                    }
                    else
                    {
                        result.Append(this.Minutes.ToString("D2"));
                    }
                    break;

                case 's':
                    cnt = 0;
                    while (i < fl && format[i] == 's')
                    {
                        cnt++; i++;
                    }
                    i--;
                    if (cnt == 1)
                    {
                        result.Append(this.Seconds);
                    }
                    else
                    {
                        result.Append(this.Seconds.ToString("D2"));
                    }
                    break;

                case 't':
                    cnt = 0;
                    while (i < fl && format[i] == 't')
                    {
                        cnt++; i++;
                    }
                    i--;
                    String des = this.Hours < 12 ? dfi.AMDesignator : dfi.PMDesignator;
                    if (cnt == 1)
                    {
                        result.Append(des[0]);
                    }
                    else
                    {
                        result.Append(des);
                    }
                    break;

                //case '/':
                //    result.Append(DateTime.MinValue.ToString("/"));
                //    break;
                default:
                    result.Append(c);
                    break;
                }
            }
            return(result.ToString());
        }