Exemple #1
0
 // GetLeMondePrintNumber
 public static int GetPrintNumber(Date date)
 {
     // Le monde - 2012-12-02 - no 21110.pdf
     // Le monde - 2012-10-12 - no 21066.pdf
     // Le monde - 2012-07-19 - no 20993.pdf
     // pas de journal le 1er mai sauf si c'est un dimanche, journal le dimanche 1er mai 2011
     // Test_GetLeMondePrintNumber("2012-04-29"); // ok  20925
     // Test_GetLeMondePrintNumber("2012-05-02"); // ok  20926
     Date dateRef = new Date(2012, 12, 2);
     int noRef = 21110;
     while (date > dateRef)
     {
         if (dateRef.DayOfWeek != DayOfWeek.Sunday && (dateRef.Month != 5 || dateRef.Day != 1 || dateRef.DayOfWeek == DayOfWeek.Sunday))
             noRef++;
         dateRef = dateRef.AddDays(1);
     }
     while (date < dateRef)
     {
         if (dateRef.DayOfWeek != DayOfWeek.Monday && (dateRef.Month != 5 || dateRef.Day != 1 || dateRef.DayOfWeek == DayOfWeek.Sunday))
             noRef--;
         dateRef = dateRef.AddDays(-1);
     }
     if (date != dateRef) throw new PBException("error date not found {0}", date.ToString());
     return noRef;
 }
Exemple #2
0
        public void CanAddDays()
        {
            Date d1 = new Date(2012, 2, 29);
            Date d2 = d1.AddDays(1);

            Assert.AreEqual(2012, d2.Year);
            Assert.AreEqual(3, d2.Month);
            Assert.AreEqual(1, d2.Day);
        }
Exemple #3
0
 public static void Test_02()
 {
     Date date = new Date(2012, 12, 10);
     for (int i = 0; i < 100; i++)
     {
         //wr.Print("{0,-8} {1} {2,6}", GetWeekDayName(date.DayOfWeek), date, GetLeMondePrintNumber(date));
         wr.Print("{0,-10:dddd} {1:yyyy-MM-dd} {2,6}", date, date, le_monde.GetPrintNumber(date));
         date = date.AddDays(-1);
     }
 }
Exemple #4
0
        /// <summary>
        /// Creates a contiguous range of dates within a supplied range
        /// </summary>
        /// <param name="min">The first date in the range</param>
        /// <param name="max">The last date in the range</param>
        /// <returns></returns>
        public static IReadOnlyList <Date> ContiguousDatesTo(this Date min, Date max)
        {
            var dates       = new List <Date>();
            var currentDate = new Date(min.Year, min.Month, min.Day);

            while (currentDate <= max)
            {
                dates.Add(currentDate);
                currentDate = currentDate.AddDays(1);
            }
            return(dates);
        }
Exemple #5
0
        //public static bool IsDayInsideDateGap(int day, Date date, int gapDayBefore = 0, int gapDayAfter = 0)
        public static Date? GetDayInsideDateGap(int day, Date expectedDate, int gapDayBefore = 0, int gapDayAfter = 0)
        {
            int expectedDay = expectedDate.Day;
            if (day == expectedDay)
                //return true;
                return expectedDate;

            if (gapDayBefore > 0)
            {
                Date minDate = expectedDate.AddDays(-gapDayBefore);
                int minDay = minDate.Day;
                if (minDay < expectedDay)
                {
                    if (day >= minDay && day < expectedDay)
                        //return true;
                        return new Date(expectedDate.Year, expectedDate.Month, day);
                }
                else // previous month
                {
                    if (day >= 1 && day < expectedDay)
                        //return true;
                        return new Date(expectedDate.Year, expectedDate.Month, day);
                    if (day >= minDay && day <= Date.DaysInMonth(minDate.Year, minDate.Month))
                        //return true;
                        return new Date(minDate.Year, minDate.Month, day);
                }
            }

            if (gapDayAfter > 0)
            {
                Date maxDate = expectedDate.AddDays(gapDayAfter);
                int maxDay = maxDate.Day;
                if (maxDay > expectedDay)
                {
                    if (day > expectedDay && day <= maxDay)
                        //return true;
                        return new Date(expectedDate.Year, expectedDate.Month, day);
                }
                else // next month
                {
                    if (day > expectedDay && day <= Date.DaysInMonth(expectedDate.Year, expectedDate.Month))
                        //return true;
                        return new Date(expectedDate.Year, expectedDate.Month, day);
                    if (day >= 1 && day <= maxDay)
                        //return true;
                        return new Date(maxDate.Year, maxDate.Month, day);
                }
            }

            //return false;
            return null;
        }
Exemple #6
0
 public static Date GetNearestWeekday(Date date, DayOfWeek weekday)
 {
     int i = (int)weekday - (int)date.DayOfWeek;
     if (i == 0)
         return date;
     if (i > 3)
         return date.AddDays(i - 7);
     if (i < -3)
         return date.AddDays(i + 7);
     return date.AddDays(i);
 }
Exemple #7
0
 /// <summary>
 /// Returns the instant that is one day more than the specified instant
 /// </summary>
 /// <param name="x"></param>
 /// <returns></returns>
 public static Date Tomorrow(this Date x)
 => x.AddDays(1);
Exemple #8
0
 /// <summary>
 /// Returns the instant that is one day less than the specified instant
 /// </summary>
 /// <param name="x"></param>
 /// <returns></returns>
 public static Date Yesterday(this Date x)
 => x.AddDays(-1);
 public void TestDateAddDaysInvalidResults()
 {
     Date date = new Date(1, 1, 1);
     Action test = () => date.AddDays(-2);
     test.ShouldThrow<ArgumentOutOfRangeException>().WithMessage(Strings.Date_InvalidAddedOrSubtractedResults + "\r\nParameter name: value");
 }
 public void TestDateAddDays()
 {
     Date date = new Date(2013, 8, 12);
     Date result = date.AddDays(1);
     result.Should().Be(new Date(2013, 8, 13));
 }
Exemple #11
0
 public static Date GetEveryTwoWeekPrintDate(int printNumber, int refNumber, Date refDate)
 {
     return refDate.AddDays((printNumber - refNumber) * 14);
 }
Exemple #12
0
 public static Date GetWeeklyPrintDate(int printNumber, int refNumber, Date refDate)
 {
     return refDate.AddDays((printNumber - refNumber) * 7);
 }
 private static object GetSampleValue(Type fieldType, bool allowNull, int n)
 {
     if (fieldType == typeof(string))
     {
         return "Sample Data " + n;
     }
     if (allowNull)
     {
         return GetSampleValue(fieldType.GetGenericArguments()[0], false, n);
     }
     if (fieldType == typeof(int))
     {
         return (int)(1 + n);
     }
     if (fieldType == typeof(short))
     {
         return (short)(1 + n);
     }
     if (fieldType == typeof(long))
     {
         return (long)(1 + n);
     }
     if (fieldType == typeof(float))
     {
         return (float)(0.1 + n);
     }
     if (fieldType == typeof(double))
     {
         return (double)(0.1 + n);
     }
     if (fieldType == typeof(bool))
     {
         return (n % 2) == 0;
     }
     if (fieldType == typeof(DateTime))
     {
         var dt = new DateTime(2008, 3, 6, 12, 24, 35);
         return dt.AddDays(n);
     }
     if (fieldType == typeof(Date))
     {
         var d = new Date(2008, 3, 17);
         return d.AddDays(n);
     }
     if (fieldType == typeof(Time))
     {
         var t = new Time(12, 24, 35);
         return t.AddMinutes(n);
     }
     if (fieldType == typeof(Guid))
     {
         return Util.NewGuid();
     }
     if (fieldType == typeof(byte))
     {
         return (byte)n;
     }
     if (fieldType == typeof(sbyte))
     {
         return (sbyte)n;
     }
     if (fieldType == typeof(decimal))
     {
         return (decimal)n;
     }
     if (fieldType == typeof(byte[]))
     {
         return new byte[] { 61, 62, 63 };
     }
     if (fieldType.IsEnum)
     {
         string[] ss = Enum.GetNames(fieldType);
         string name = ss[n % ss.Length];
         return Enum.Parse(fieldType, name);
     }
     return null;
 }
Exemple #14
0
		public void CanCompareDates()
		{
			Date d1 = new Date(2013, 4, 5);
			Date d2 = new Date(2013, 4, 5);
			Date d3 = new Date(2014, 4, 8);

			Assert.IsTrue(d1 == d2);
			Assert.IsTrue(d1 != d3);
			Assert.IsTrue(d1 <= d2);
			Assert.IsTrue(d1 >= d2);
			Assert.IsTrue(d1 < d1.AddDays(3));
			Assert.IsTrue(d1 < d1.AddMonths(4));
			Assert.IsTrue(d1 < d1.AddYears(5));
			Assert.IsTrue(d1 <= d1.AddDays(3));
			Assert.IsTrue(d1 <= d1.AddMonths(4));
			Assert.IsTrue(d1 <= d1.AddYears(5));
			Assert.IsTrue(d1 > d1.AddDays(-3));
			Assert.IsTrue(d1 > d1.AddMonths(-4));
			Assert.IsTrue(d1 > d1.AddYears(-5));
			Assert.IsTrue(d1 >= d1.AddDays(-3));
			Assert.IsTrue(d1 >= d1.AddMonths(-4));
			Assert.IsTrue(d1 >= d1.AddYears(-5));
		}