Esempio n. 1
0
        public void ChangeDate(ATimeSpan span, int direction)
        {
            //
            // First, calculate new Hour,Minute,Second
            //
            double hms1 = this.Hour * 60.0 * 60.0 + this.Minute * 60.0 + this.Second;
            double hms2 = span.Hour * 60.0 * 60.0 + span.Minute * 60.0 + span.Second;

            hms1 += hms2 * direction;

            int day;

            if (0.0 <= hms1 && hms1 < 24.0 * 60.0 * 60.0)
            {
                day = 0;
            }
            else if (hms1 >= 24.0 * 60.0 * 60.0)
            {
                day  = (int)Math.Floor(hms1 / 24.0 / 60.0 / 60.0);
                hms1 = UdMath.fmod(hms1, 24.0 * 60.0 * 60.0);
            }
            else
            {
                day  = (int)Math.Ceiling(hms1 / 24.0 / 60.0 / 60.0) - 1;
                hms1 = UdMath.fmod(hms1, 24.0 * 60.0 * 60.0) + 24.0 * 60.0 * 60.0;
            }

            int    newHour = (int)Math.Floor(hms1 / 60.0 / 60.0);
            int    newMin  = (int)Math.Floor(hms1 / 60.0) - newHour * 60;
            double newSec  = hms1 - ((double)newHour * 60.0 * 60.0 + (double)newMin * 60.0);

            //
            // Next, calculate new Year, Month, Day
            //
            ATime newDate = new ATime(this.Year, this.Month, this.Day, 12, 0, 0.0, 0.0);

            double jd = newDate.JD;

            jd     += day + span.Day * direction;
            newDate = new ATime(jd, 0.0);

            int newYear  = newDate.Year;
            int newMonth = newDate.Month;
            int newDay   = newDate.Day;

            newMonth += span.Month * direction;
            if (1 > newMonth)
            {
                newYear -= newMonth / 12 + 1;
                newMonth = 12 + newMonth % 12;
            }
            else if (newMonth > 12)
            {
                newYear += newMonth / 12;
                newMonth = 1 + (newMonth - 1) % 12;
            }
            newYear += span.Year * direction;

            // check bound between julian and gregorian
            if (newYear == 1582 && newMonth == 10)
            {
                if (5 <= newDay && newDay < 10)
                {
                    newDay = 4;
                }
                else if (10 <= newDay && newDay < 15)
                {
                    newDay = 15;
                }
            }

            newDate  = LimitATime(new ATime(newYear, newMonth, newDay, 12, 0, 0, 0.0));
            newYear  = newDate.Year;
            newMonth = newDate.Month;
            newDay   = newDate.Day;

            this.Year   = newYear;
            this.Month  = newMonth;
            this.Day    = newDay;
            this.Hour   = newHour;
            this.Minute = newMin;
            this.Second = newSec;
            this.JD     = GetJD() - this.Timezone / 24.0;
            this.T      = GetT();
            this.T2     = GetT2();
        }