IsLeapYear() public method

public IsLeapYear ( int year, int era ) : bool
year int
era int
return bool
Ejemplo n.º 1
0
        public void NpgsqlDateConstructors()
        {
            NpgsqlDate date;
            DateTime   dateTime;

            System.Globalization.Calendar calendar = new System.Globalization.GregorianCalendar();

            date = new NpgsqlDate();
            Assert.AreEqual(1, date.Day);
            Assert.AreEqual(DayOfWeek.Monday, date.DayOfWeek);
            Assert.AreEqual(1, date.DayOfYear);
            Assert.AreEqual(false, date.IsLeapYear);
            Assert.AreEqual(1, date.Month);
            Assert.AreEqual(1, date.Year);

            dateTime = new DateTime(2009, 5, 31);
            date     = new NpgsqlDate(dateTime);
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(2009), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

            //Console.WriteLine(new DateTime(2009, 5, 31).Ticks);
            //Console.WriteLine((new DateTime(2009, 5, 31) - new DateTime(1, 1, 1)).TotalDays);
            // 2009-5-31
            dateTime = new DateTime(633793248000000000); // ticks since 1 Jan 1
            date     = new NpgsqlDate(733557);           // days since 1 Jan 1
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(2009), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

            // copy previous value.  should get same result
            date = new NpgsqlDate(date);
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(2009), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

#if NET6_0_OR_GREATER
            date = new NpgsqlDate(new DateOnly(2012, 3, 4));
            Assert.That(date.Year, Is.EqualTo(2012));
            Assert.That(date.Month, Is.EqualTo(3));
            Assert.That(date.Day, Is.EqualTo(4));
#endif
        }
Ejemplo n.º 2
0
        public void SpecialDates()
        {
            NpgsqlDate date;
            DateTime   dateTime;

            System.Globalization.Calendar calendar = new System.Globalization.GregorianCalendar();

            // a date after a leap year.
            dateTime = new DateTime(2008, 5, 31);
            date     = new NpgsqlDate(dateTime);
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(2008), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

            // A date that is a leap year day.
            dateTime = new DateTime(2000, 2, 29);
            date     = new NpgsqlDate(2000, 2, 29);
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(2000), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

            // A date that is not in a leap year.
            dateTime = new DateTime(1900, 3, 1);
            date     = new NpgsqlDate(1900, 3, 1);
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(1900), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

            // a date after a leap year.
            date = new NpgsqlDate(-1, 12, 31);
            Assert.AreEqual(31, date.Day);
            Assert.AreEqual(DayOfWeek.Sunday, date.DayOfWeek);
            Assert.AreEqual(366, date.DayOfYear);
            Assert.AreEqual(true, date.IsLeapYear);
            Assert.AreEqual(12, date.Month);
            Assert.AreEqual(-1, date.Year);
        }
Ejemplo n.º 3
0
        public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
        {
            ValidateArguments(arguments, 2);
            var numDate1 = ArgToDecimal(arguments, 0);
            var numDate2 = ArgToDecimal(arguments, 1);
            var dt1 = System.DateTime.FromOADate(numDate1);
            var dt2 = System.DateTime.FromOADate(numDate2);

            var calcType = Days360Calctype.Us;
            if (arguments.Count() > 2)
            {
                var european = ArgToBool(arguments, 2);
                if(european) calcType = Days360Calctype.European;
            }

            var startYear = dt1.Year;
            var startMonth = dt1.Month;
            var startDay = dt1.Day;
            var endYear = dt2.Year;
            var endMonth = dt2.Month;
            var endDay = dt2.Day;

            if (calcType == Days360Calctype.European)
            {
                if (startDay == 31) startDay = 30;
                if (endDay == 31) endDay = 30;
            }
            else
            {
                var calendar = new GregorianCalendar();
                var nDaysInFeb = calendar.IsLeapYear(dt1.Year) ? 29 : 28;
               
                 // If the investment is EOM and (Date1 is the last day of February) and (Date2 is the last day of February), then change D2 to 30.
                if (startMonth == 2 && startDay == nDaysInFeb && endMonth == 2 && endDay == nDaysInFeb)
                {
                    endDay = 30;
                }
                 // If the investment is EOM and (Date1 is the last day of February), then change D1 to 30.
                if (startMonth == 2 && startDay == nDaysInFeb)
                {
                    startDay = 30;
                }
                 // If D2 is 31 and D1 is 30 or 31, then change D2 to 30.
                if (endDay == 31 && (startDay == 30 || startDay == 31))
                {
                    endDay = 30;
                }
                 // If D1 is 31, then change D1 to 30.
                if (startDay == 31)
                {
                    startDay = 30;
                }
            }
            var result = (endYear*12*30 + endMonth*30 + endDay) - (startYear*12*30 + startMonth*30 + startDay);
            return CreateResult(result, DataType.Integer);
        }
Ejemplo n.º 4
0
 public void PosTest5()
 {
     System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
     int year;
     bool expectedValue;
     bool actualValue;
     year = myCalendar.MinSupportedDateTime.Year;
     expectedValue = this.IsLeapYear(year);
     actualValue = myCalendar.IsLeapYear(year, 1);
     Assert.Equal(expectedValue, actualValue);
 }
Ejemplo n.º 5
0
 public void PosTest2()
 {
     System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
     int year;
     bool expectedValue;
     bool actualValue;
     year = GetACommonYear(myCalendar);
     expectedValue = this.IsLeapYear(year);
     actualValue = myCalendar.IsLeapYear(year, 1);
     Assert.Equal(expectedValue, actualValue);
 }
Ejemplo n.º 6
0
 public void PosTest4()
 {
     System.Globalization.Calendar kC = new KoreanCalendar();
     System.Globalization.Calendar gC = new GregorianCalendar();
     DateTime dateTime = gC.ToDateTime(1200, 2, 29, 0, 0, 0, 0);
     int year = dateTime.Year;
     int era = gC.GetEra(dateTime);
     bool expectedValue = gC.IsLeapYear(year, era);
     bool actualValue;
     actualValue = kC.IsLeapYear(year + 2333, kC.GetEra(dateTime));
     Assert.Equal(expectedValue, actualValue);
 }
Ejemplo n.º 7
0
        public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
        {
            var functionArguments = arguments as FunctionArgument[] ?? arguments.ToArray();
            ValidateArguments(functionArguments, 2);
            var date1Num = ArgToDecimal(functionArguments, 0);
            var date2Num = ArgToDecimal(functionArguments, 1);
            if (date1Num > date2Num) //Switch to make date1 the lowest date
            {
                var t = date1Num;
                date1Num = date2Num;
                date2Num = t;
                var fa = functionArguments[1];
                functionArguments[1] = functionArguments[0];
                functionArguments[0] = fa;
            }
            var date1 = System.DateTime.FromOADate(date1Num);
            var date2 = System.DateTime.FromOADate(date2Num);

            var basis = 0;
            if (functionArguments.Count() > 2)
            {
                basis = ArgToInt(functionArguments, 2);
                ThrowExcelErrorValueExceptionIf(() => basis < 0 || basis > 4, eErrorType.Num);
            }
            var func = context.Configuration.FunctionRepository.GetFunction("days360");
            var calendar = new GregorianCalendar();
            switch (basis)
            {
                case 0:
                    var d360Result = System.Math.Abs(func.Execute(functionArguments, context).ResultNumeric);
                    // reproducing excels behaviour
                    if (date1.Month == 2 && date2.Day==31)
                    {
                        var daysInFeb = calendar.IsLeapYear(date1.Year) ? 29 : 28;
                        if (date1.Day == daysInFeb) d360Result++;  
                    }
                    return CreateResult(d360Result / 360d, DataType.Decimal);
                case 1:
                    return CreateResult(System.Math.Abs((date2 - date1).TotalDays / CalculateAcutalYear(date1, date2)), DataType.Decimal);
                case 2:
                    return CreateResult(System.Math.Abs((date2 - date1).TotalDays / 360d), DataType.Decimal);
                case 3:
                    return CreateResult(System.Math.Abs((date2 - date1).TotalDays / 365d), DataType.Decimal);
                case 4:
                    var args = functionArguments.ToList();
                    args.Add(new FunctionArgument(true));
                    double? result = System.Math.Abs(func.Execute(args, context).ResultNumeric / 360d);
                    return CreateResult(result.Value, DataType.Decimal);
                default:
                    return null;
            }
        }
Ejemplo n.º 8
0
 private double CalculateAcutalYear(System.DateTime dt1, System.DateTime dt2)
 {
     var calendar = new GregorianCalendar();
     var perYear = 0d;
     var nYears = dt2.Year - dt1.Year + 1;
     for (var y = dt1.Year; y <= dt2.Year; ++y)
     {
         perYear += calendar.IsLeapYear(y) ? 366 : 365;
     }
     if (new System.DateTime(dt1.Year + 1, dt1.Month, dt1.Day) >= dt2)
     {
         nYears = 1;
         perYear = 365;
         if (calendar.IsLeapYear(dt1.Year) && dt1.Month <= 2)
             perYear = 366;
         else if (calendar.IsLeapYear(dt2.Year) && dt2.Month > 2)
             perYear = 366;
         else if (dt2.Month == 2 && dt2.Day == 29)
             perYear = 366;
     }
     return perYear/(double) nYears;  
 }
Ejemplo n.º 9
0
        public string LeapYearCalendar(int anio)
        {
            string result = String.Empty; ;
            GregorianCalendar calendar = new GregorianCalendar();

            if (calendar.IsLeapYear((anio)))
            {
                result = "LeapYear";
            }
            else
            {
                result = "NoLeapYear";
            }
            return result;
        }
Ejemplo n.º 10
0
        private void button1_Click(object sender, EventArgs e)
        {
            long s1,s2;
            bool IsLeapYear;
            long year = 0;
            try
            {
                year = Convert.ToInt64(textBox1.Text);
                if (year <= 0)
                {

                    if (radioButton1.Checked || radioButton3.Checked)
                        throw new MinusException();
                }
                if (radioButton2.Checked)
                {
                    IsLeapYear = false;
                    GregorianCalendar gc = new GregorianCalendar(); 
                    if (DateTime.IsLeapYear((int)year)||gc.IsLeapYear((int)year))
                        IsLeapYear = true;
                    else
                        IsLeapYear = false;
                    if (IsLeapYear)
                        MessageBox.Show("Year " + year + " is a leap year", "Leap Year");
                    else
                        MessageBox.Show("Year " + year + " is not a leap year", "Leap Year");
                }
                
                if (radioButton5.Checked && year>0)
                {
                    IsLeapYear = false;
                    if (year % 4 == 0 && year % 100 != 0)
                        IsLeapYear = true;
                    if (year % 400 == 0)
                        IsLeapYear = true;
                    if (IsLeapYear)
                        MessageBox.Show("Year " + year + " is a leap year", "Leap Year");
                    else
                        MessageBox.Show("Year " + year + " is not a leap year", "Leap Year");
                }
                else if (year < 0 && radioButton5.Checked)
                    MessageBox.Show("Year must be positive","Error");
                if (radioButton1.Checked && year > 0)
                {
                    IsLeapYear = false;
                    s1 = (year + 2346) % 2820;
                    if (s1 == 0)
                    {
                        IsLeapYear = true;
                    }
                    else
                    {
                        s2 = s1 % 128;
                        switch (s2)
                        {
                            case 0:
                                IsLeapYear = true;
                                break;
                            case 5:
                                IsLeapYear = true;
                                break;
                            case 9:
                                IsLeapYear = true;
                                break;
                            case 13:
                                IsLeapYear = true;
                                break;
                            case 17:
                                IsLeapYear = true;
                                break;
                            case 21:
                                IsLeapYear = true;
                                break;
                            case 25:
                                IsLeapYear = true;
                                break;
                            case 29:
                                IsLeapYear = true;
                                break;
                            case 34:
                                IsLeapYear = true;
                                break;
                            case 38:
                                IsLeapYear = true;
                                break;
                            case 42:
                                IsLeapYear = true;
                                break;
                            case 46:
                                IsLeapYear = true;
                                break;
                            case 50:
                                IsLeapYear = true;
                                break;
                            case 54:
                                IsLeapYear = true;
                                break;
                            case 58:
                                IsLeapYear = true;
                                break;
                            case 62:
                                IsLeapYear = true;
                                break;
                            case 67:
                                IsLeapYear = true;
                                break;
                            case 71:
                                IsLeapYear = true;
                                break;
                            case 75:
                                IsLeapYear = true;
                                break;
                            case 79:
                                IsLeapYear = true;
                                break;
                            case 83:
                                IsLeapYear = true;
                                break;
                            case 87:
                                IsLeapYear = true;
                                break;
                            case 91:
                                IsLeapYear = true;
                                break;
                            case 95:
                                IsLeapYear = true;
                                break;
                            case 100:
                                IsLeapYear = true;
                                break;
                            case 104:
                                IsLeapYear = true;
                                break;
                            case 108:
                                IsLeapYear = true;
                                break;
                            case 112:
                                IsLeapYear = true;
                                break;
                            case 116:
                                IsLeapYear = true;
                                break;
                            case 120:
                                IsLeapYear = true;
                                break;
                            case 124:
                                IsLeapYear = true;
                                break;
                            default:
                                IsLeapYear = false;
                                break;
                        }
                    }

                    if (IsLeapYear)
                        MessageBox.Show("سال " + year + " کبیسه است", "سال کبیسه");
                    else
                        MessageBox.Show("سال " + year + " کبیسه نیست", "سال کبیسه");
                }
                if (radioButton3.Checked && year > 0)
                {
                    IsLeapYear = false;
                    s1 = year % 33;
                    switch (s1)
                    {
                        case 1:
                            IsLeapYear = true;
                            break;
                        case 5:
                            IsLeapYear = true;
                            break;
                        case 9:
                            IsLeapYear = true;
                            break;
                        case 13:
                            IsLeapYear = true;
                            break;
                        case 17:
                            IsLeapYear = true;
                            break;
                        case 22:
                            IsLeapYear = true;
                            break;
                        case 26:
                            IsLeapYear = true;
                            break;
                        case 30:
                            IsLeapYear = true;
                            break;

                    }
                    if (IsLeapYear)
                        MessageBox.Show("سال " + year + " کبیسه است", "سال کبیسه");
                    else
                        MessageBox.Show("سال " + year + " کبیسه نیست", "سال کبیسه");
                }
                if (radioButton4.Checked)
                {

                    IsLeapYear = false;
                    PersianCalendar ps = new PersianCalendar();
                    IsLeapYear = ps.IsLeapYear((int)year);
                    if (IsLeapYear)
                        MessageBox.Show("سال " + year + " کبیسه است", "سال کبیسه");
                    else
                        MessageBox.Show("سال " + year + " کبیسه نیست", "سال کبیسه");

                }

            }
            catch (OverflowException)
            {
                if (radioButton1.Checked || radioButton3.Checked)
                    MessageBox.Show("عدد وارد شده بسیار بزرگ است", "Error");
                if (radioButton5.Checked)
                    MessageBox.Show("Your Number is too great","Error");
                if (radioButton2.Checked)
                    MessageBox.Show("Your Year Number is greater than 9999", "Error");
                if (radioButton4.Checked)
                    MessageBox.Show("سال باید از 9378 کوچکتر باشد", "Error");
            }
            catch (FormatException)
            {
                if (radioButton1.Checked || radioButton3.Checked || radioButton4.Checked)
                    MessageBox.Show("ورودی معتبر نیست", "Error");
                if (radioButton2.Checked||radioButton5.Checked)
                    MessageBox.Show("Invalid Input", "Error");
            }
            catch (ArgumentOutOfRangeException)
            {
                if (radioButton2.Checked)
                    MessageBox.Show("Your Year Number must be between 1 to 9999", "Error");
                if (radioButton4.Checked)
                    MessageBox.Show("سال باید عددی طبیعی و کوچکتر از 9378 باشد", "Error");
            }
            catch (MinusException)
            {
                if (radioButton1.Checked || radioButton3.Checked)
                    MessageBox.Show("سال باید مثبت باشد", "Error");
                if (radioButton5.Checked)
                    MessageBox.Show("Year must be positive", "Error");
            }
        }
Ejemplo n.º 11
0
        public void NpgsqlDateConstructors()
        {
            NpgsqlDate date;
            DateTime dateTime;
            System.Globalization.Calendar calendar = new System.Globalization.GregorianCalendar();

            date = new NpgsqlDate();
            Assert.AreEqual(1, date.Day);
            Assert.AreEqual(DayOfWeek.Monday, date.DayOfWeek);
            Assert.AreEqual(1, date.DayOfYear);
            Assert.AreEqual(false, date.IsLeapYear);
            Assert.AreEqual(1, date.Month);
            Assert.AreEqual(1, date.Year);

            dateTime = new DateTime(2009, 5, 31);
            date = new NpgsqlDate(dateTime);
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(2009), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

            //Console.WriteLine(new DateTime(2009, 5, 31).Ticks);
            //Console.WriteLine((new DateTime(2009, 5, 31) - new DateTime(1, 1, 1)).TotalDays);
            // 2009-5-31
            dateTime = new DateTime(633793248000000000); // ticks since 1 Jan 1
            date = new NpgsqlDate(733557); // days since 1 Jan 1
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(2009), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

            // copy previous value.  should get same result
            date = new NpgsqlDate(date);
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(2009), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);
        }
Ejemplo n.º 12
0
        private bool ParseTrek(ClockToken[] dt, ref System.Int32 parsePos, TclDateTime calendar)
        // calendar object to set
        {
            int pos = parsePos;

            if (pos + 3 < dt.Length && dt[pos].is_Renamed(ClockToken.STARDATE) && dt[pos + 1].UNumber && dt[pos + 2].is_Renamed('.') && dt[pos + 3].UNumber)
            {
                GregorianCalendar gcal = new GregorianCalendar();
                int trekYear = dt[pos + 1].Int / 1000 + 2323 - 377;
                int trekDay = 1 + ((dt[pos + 1].Int % 1000) * (gcal.IsLeapYear(trekYear) ? 366 : 365)) / 1000;
                int trekSeconds = dt[pos + 3].Int * 144 * 60;
                calendar.year = trekYear;
                calendar.dateTime = gcal.AddDays(calendar.dateTime, trekDay);
                calendar.second = trekSeconds;
                parsePos = pos + 4;
                return true;
            }
            return false;
        }
Ejemplo n.º 13
0
        private void FormatClock(Interp interp, int clockVal, bool useGMT, string format)
        {
            DateTime date = new DateTime((long)clockVal * 10000 * 1000 + 621355968000000000);

            DateTimeFormatInfo formatInfo = new DateTimeFormatInfo();
            string fmt, locFmt;

            GregorianCalendar calendar = new GregorianCalendar();

            System.Int32[] temp_int_array;
            temp_int_array = new System.Int32[3];
            temp_int_array[0] = 0;
            temp_int_array[1] = 0;
            temp_int_array[2] = 0;
            System.Int32[] fp = temp_int_array;
            StringBuilder result = new StringBuilder();

            if ((System.Object)format == null)
            {
                format = new StringBuilder("%a %b %d %H:%M:%S %Z %Y").ToString();
            }

            if (useGMT)
            {
                date = date.ToUniversalTime();
            }
            if (format.Equals("%Q"))
            {
                // Enterprise Stardate. (seems to be Star Track fan coding)
                // ATK not tested 
                int trekYear = date.Year + 377 - 2323;
                int trekDay = (date.DayOfYear * 1000) / (calendar.IsLeapYear(date.Year) ? 366 : 365);
                int trekHour = (24 * 60 + date.Minute) / 144;

                interp.SetResult("Stardate " + (trekYear < 10 ? "0" : "") + (trekYear * 1000 + trekDay) + '.' + trekHour);
                return;
            }

            for (int ix = 0; ix < format.Length; ix++)
            {
                if (format[ix] == '%' && ix + 1 < format.Length)
                {
                    switch (format[++ix])
                    {

                        case '%':
                            result.Append('%');
                            break;

                        case 'a':
                            result.Append(date.ToString("ddd", formatInfo));
                            break;

                        case 'A':
                            result.Append(date.ToString("dddd", formatInfo));
                            break;
                        case 'b':
                        case 'h':
                            result.Append(date.ToString("MMM", formatInfo));
                            break;
                        case 'B':
                            result.Append(date.ToString("MMMM", formatInfo));
                            break;
                        case 'c':
                            result.Append(date.ToString());
                            break;
                        case 'C':
                            int century = date.Year / 100;
                            result.Append((century < 10 ? "0" : "") + century);
                            break;
                        case 'd':
                            result.Append(date.ToString("dd", formatInfo));
                            break;
                        case 'D':
                            result.Append(date.ToString("MM/dd/yy", formatInfo));
                            break;
                        case 'e':
                            result.Append(date.ToString("%d", formatInfo));
                            break;
                        case 'H':
                            result.Append(date.ToString("HH", formatInfo));
                            break;
                        case 'I':
                            result.Append(date.ToString("hh", formatInfo));
                            break;
                        case 'j':
                            result.Append(date.Year.ToString("0###"));
                            break;
                        case 'k':
                            result.Append(date.ToString("H", formatInfo));
                            break;
                        case 'l':
                            result.Append(date.ToString("%h", formatInfo));
                            break;
                        case 'm':
                            // Month number (01 - 12). 
                            result.Append(date.ToString("MM", formatInfo));
                            break;
                        case 'M':
                            // Minute (00 - 59). 
                            result.Append(date.ToString("mm", formatInfo));
                            break;
                        case 'n':
                            // Insert a newline.
                            result.Append('\n');
                            break;
                        case 'p':
                            // AM/PM indicator. 
                            result.Append(date.ToString("tt", formatInfo));
                            break;
                        case 'r':
                            // %r 
                            //Time in a locale-specific "meridian" format. The "meridian" format in the default "C" locale is "%I:%M:%S %p". 
                            result.Append(date.ToString("hh:mm:ss tt", formatInfo));
                            break;
                        case 'R':
                            //%R 
                            //Time as %H:%M. 
                            result.Append(date.ToString("HH:MM", formatInfo));
                            break;
                        case 's':
                            //%s 
                            //Count of seconds since the epoch, expressed as a decimal integer. 
                            result.Append((date.Ticks / 1000).ToString());
                            break;

                        case 'S':
                            //%S 
                            //Seconds (00 - 59). 
                            result.Append(date.ToString("ss", formatInfo));
                            break;
                        case 't':
                            //%t 
                            //Insert a tab. 
                            result.Append('\t');
                            break;

                        case 'T':
                            //%T 
                            //Time as %H:%M:%S. 
                            result.Append(date.ToString("HH:mm:ss", formatInfo));
                            break;

                        case 'u':
                            //%u 
                            //Weekday number (Monday = 1, Sunday = 7). 
                            if (date.DayOfWeek == DayOfWeek.Sunday)
                            {
                                result.Append("7");
                            }
                            else
                            {
                                result.Append(((int)date.DayOfWeek).ToString());
                            }
                            break;
                        case 'U':
                            //%U 
                            //Week of year (00 - 52), Sunday is the first day of the week. 
                            int weekS = GetWeek(date, System.DayOfWeek.Sunday, false);
                            result.Append((weekS < 10 ? "0" : "") + weekS);
                            break;

                        case 'V':
                            //%V 
                            //Week of year according to ISO-8601 rules. Week 1 of a given year is the week containing 4 January. 
                            int isoWeek = GetWeek(date, System.DayOfWeek.Monday, true);
                            result.Append((isoWeek < 10 ? "0" : "") + isoWeek);
                            break;

                        case 'w':
                            //%w 
                            //Weekday number (Sunday = 0, Saturday = 6). 
                            result.Append(((int)date.DayOfWeek).ToString());
                            break;

                        case 'W':
                            //%W 
                            //Week of year (00 - 52), Monday is the first day of the week. 
                            int weekM = GetWeek(date, System.DayOfWeek.Monday, false);
                            result.Append((weekM < 10 ? "0" : "") + weekM);
                            break;
                        case 'x':
                            //%x 
                            //Locale specific date format. The format for a date in the default "C" locale for Unix/Mac is "%m/%d/%y". On Windows, this value is the locale specific short date format, as specified in the Regional Options control panel settings. 
                            result.Append(date.ToShortDateString());
                            break;

                        case 'X':
                            //%X 
                            //Locale specific 24-hour time format. The format for a 24-hour time in the default "C" locale for Unix/Mac is "%H:%M:%S". On Windows, this value is the locale specific time format, as specified in the Regional Options control panel settings. 
                            result.Append(date.ToShortTimeString());
                            break;
                        case 'y':
                            //%y 
                            //Year without century (00 - 99). 
                            result.Append(date.ToString("yy", formatInfo));
                            break;

                        case 'Y':
                            //%Y 
                            //Year with century (e.g. 1990) 
                            result.Append(date.ToString("yyyy", formatInfo));
                            break;
                        case 'Z':
                            //%Z 
                            //Time zone name. 
                            result.Append(date.ToString("zzz", formatInfo));
                            break;
                        default:
                            result.Append(format[ix]);
                            break;
                    }
                }
                else
                {
                    result.Append(format[ix]);
                }
            }
            interp.SetResult(result.ToString());
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Fixeds to gregorian.
 /// </summary>
 /// <param name="fixedDate">The fixed date.</param>
 /// <param name="year">The year.</param>
 /// <param name="month">The month.</param>
 /// <param name="day">The day.</param>
 private static void FixedToGregorian(int fixedDate, ref int year, ref int month, ref int day)
 {
     GregorianCalendar gregorianCalendar = new GregorianCalendar();
     year = GregorianYearFromFixed(fixedDate);
     int num2 = fixedDate - GregorianToFixed(year, 1, 1);
     int num = (fixedDate < GregorianToFixed(year, 3, 1)) ? 0 : (gregorianCalendar.IsLeapYear(year) ? 1 : 2);
     month = (int)Math.Floor((double)(((double)((12 * (num2 + num)) + 0x175)) / 367.0));
     day = (fixedDate - GregorianToFixed(year, month, 1)) + 1;
 }
Ejemplo n.º 15
0
        public void SpecialDates()
        {
            NpgsqlDate date;
            DateTime dateTime;
            System.Globalization.Calendar calendar = new System.Globalization.GregorianCalendar();

            // a date after a leap year.
            dateTime = new DateTime(2008, 5, 31);
            date = new NpgsqlDate(dateTime);
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(2008), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

            // A date that is a leap year day.
            dateTime = new DateTime(2000, 2, 29);
            date = new NpgsqlDate(2000, 2, 29);
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(2000), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

            // A date that is not in a leap year.
            dateTime = new DateTime(1900, 3, 1);
            date = new NpgsqlDate(1900, 3, 1);
            Assert.AreEqual(dateTime.Day, date.Day);
            Assert.AreEqual(dateTime.DayOfWeek, date.DayOfWeek);
            Assert.AreEqual(dateTime.DayOfYear, date.DayOfYear);
            Assert.AreEqual(calendar.IsLeapYear(1900), date.IsLeapYear);
            Assert.AreEqual(dateTime.Month, date.Month);
            Assert.AreEqual(dateTime.Year, date.Year);

            // a date after a leap year.
            date = new NpgsqlDate(-1, 12, 31);
            Assert.AreEqual(31, date.Day);
            Assert.AreEqual(DayOfWeek.Sunday, date.DayOfWeek);
            Assert.AreEqual(366, date.DayOfYear);
            Assert.AreEqual(true, date.IsLeapYear);
            Assert.AreEqual(12, date.Month);
            Assert.AreEqual(-1, date.Year);
        }
Ejemplo n.º 16
0
 public void NegTest1()
 {
     System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
     int year;
     year = myCalendar.MaxSupportedDateTime.Year + 100;
     Assert.Throws<ArgumentOutOfRangeException>(() =>
     {
         myCalendar.IsLeapYear(year, 1);
     });
 }
        public void IsLeapYearShouldReturnCorrectValueForFiscalYear()
        {
            var target = new GregorianFiscalCalendar( 7 );
            var calendar = new GregorianCalendar();

            for ( var year = 2005; year <= 2015; year++ )
                Assert.Equal( calendar.IsLeapYear( year - 1 ) | calendar.IsLeapYear( year ), target.IsLeapYear( year ) );
        }
Ejemplo n.º 18
0
 public void NegTest3()
 {
     System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
     int year;
     int era;
     year = this.GetAYear(myCalendar);
     era = 2 + _generator.GetInt32(-55) % (int.MaxValue - 1);
     Assert.Throws<ArgumentOutOfRangeException>(() =>
     {
         myCalendar.IsLeapYear(year, era);
     });
 }