Exemplo n.º 1
0
        public void Previous(
            int year, int month, int day, IsoDayOfWeek dayOfWeek,
            int expectedYear, int expectedMonth, int expectedDay)
        {
            LocalDate start    = new LocalDate(year, month, day);
            LocalDate actual   = start.With(DateAdjusters.Previous(dayOfWeek));
            LocalDate expected = new LocalDate(expectedYear, expectedMonth, expectedDay);

            Assert.AreEqual(expected, actual);
        }
        private static ImmutableHolidayCalendar GenerateFrankfurt()
        {
            var holidays = new List <LocalDate>(2000);

            for (int year = 1950; year <= 2099; year++)
            {
                // New Year's Day
                holidays.Add(new LocalDate(year, 1, 1));
                // Good Friday
                holidays.Add(Easter(year).PlusDays(-2));
                // Easter Monday
                holidays.Add(Easter(year).PlusDays(1));
                // Labour Day
                holidays.Add(new LocalDate(year, 5, 1));
                // Ascension Day
                holidays.Add(Easter(year).PlusDays(39));
                // Whit Monday
                holidays.Add(Easter(year).PlusDays(50));
                // Corpus Christi
                holidays.Add(Easter(year).PlusDays(60));
                // German Unity
                if (year >= 2000)
                {
                    holidays.Add(new LocalDate(year, 10, 3));
                }

                // Repentance
                if (year <= 1994)
                {
                    // Wednesday before the Sunday that is 2 weeks before first advent, which is 4th Sunday before Christmas
                    holidays.Add(new LocalDate(year, 12, 25)
                                 .With(DateAdjusters.Previous(DayOfWeek.Sunday))
                                 .PlusWeeks(-6)
                                 .PlusDays(-4));
                }

                // Christmas Day
                holidays.Add(new LocalDate(year, 12, 25));
                // St Stephen
                holidays.Add(new LocalDate(year, 12, 26));
                // New Year's Eve
                holidays.Add(new LocalDate(year, 12, 31));
            }

            // Reformation Day
            holidays.Add(new LocalDate(2017, 10, 31));
            RemoveSatSun(holidays);
            return(new ImmutableHolidayCalendar(
                       HolidayCalendarIds.FRA,
                       "The holiday calendar for Frankfurt, Germany, with code 'FRA'",
                       holidays,
                       DayOfWeek.Saturday,
                       DayOfWeek.Sunday));
        }
Exemplo n.º 3
0
        public void WithDateAdjuster()
        {
            LocalDateTime  localDateTime = new LocalDateTime(1985, 10, 26, 1, 18);
            Offset         offset        = Offset.FromHours(-5);
            OffsetDateTime original      = new OffsetDateTime(localDateTime, offset);
            var            dateAdjuster  = DateAdjusters.AddPeriod(Period.FromYears(30));
            OffsetDateTime updated       = Snippet.For(original.With(dateAdjuster));

            Assert.AreEqual(
                new LocalDateTime(2015, 10, 26, 1, 18),
                updated.LocalDateTime);
            Assert.AreEqual(original.Offset, updated.Offset);
        }
Exemplo n.º 4
0
        protected void SetMonth(int month)
        {
            try
            {
                RenderedValue = RenderedValue.With(DateAdjusters.Month(month));
            }
            catch (ArgumentOutOfRangeException)
            {
                RenderedValue = new LocalDate(RenderedValue.Year, month, day: 1, RenderedValue.Calendar).With(DateAdjusters.EndOfMonth);
            }

            State = DatePickerStates.Day;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns the duration to the next day of week and time of day (UTC).
        /// </summary>
        /// <param name="weekly">The weekly time.</param>
        /// <param name="now">The current time instant.</param>
        /// <returns>The next date time (UTC).</returns>
        public static ZonedDateTime GetNextUtc(WeeklyTime weekly, [CanBeDefault] Instant now)
        {
            var localNow  = now.InZone(DateTimeZone.Utc).LocalDateTime;
            var localNext = localNow
                            .Date.With(DateAdjusters.NextOrSame(weekly.DayOfWeek))
                            .At(weekly.Time);

            // Handle "we're already on the right day-of-week, but later in the day"
            if (localNext <= localNow)
            {
                localNext = localNext.PlusWeeks(1);
            }

            return(localNext.InUtc());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Returns a value indicating whether the given instant now is outside the given weekly interval.
        /// </summary>
        /// <param name="start">The start of the weekly interval.</param>
        /// <param name="end">The end of the weekly interval.</param>
        /// <param name="now">The current time instant.</param>
        /// <returns>True if now is outside the given interval, else false.</returns>
        public static bool IsOutsideInterval(WeeklyTime start, WeeklyTime end, [CanBeDefault] Instant now)
        {
            Debug.NotDefault(start.DayOfWeek, nameof(start.DayOfWeek));
            Debug.NotDefault(end.DayOfWeek, nameof(end.DayOfWeek));
            Debug.True(start.DayOfWeek <= end.DayOfWeek, nameof(start.DayOfWeek));

            var localNow = now.InZone(DateTimeZone.Utc).LocalDateTime;

            var localStart = localNow
                             .Date.With(DateAdjusters.NextOrSame(start.DayOfWeek))
                             .At(start.Time);

            var localEnd = localNow
                           .Date.With(DateAdjusters.NextOrSame(end.DayOfWeek))
                           .At(end.Time);

            return(localNow < localStart || localNow > localEnd);
        }
Exemplo n.º 7
0
        public async Task<IActionResult> OnPostAsync(int channelId)
        {
            ScheduledStream stream = StreamTime.ToModel();

            var channel = await _context.Channels
                .Include(x => x.ScheduledStreams)
                .SingleAsync(x => x.Id == channelId);

            channel.ScheduledStreams.Add(stream);

            var zone = DateTimeZoneProviders.Tzdb[channel.TimeZoneId];
            var version = DateTimeZoneProviders.Tzdb.VersionId;
            ZonedClock zonedClock = _clock.InZone(zone);

            LocalDate today = zonedClock.GetCurrentDate();
            LocalDate next = today.With(DateAdjusters.Next(stream.DayOfWeek));

            
            for (int i = 0; i < 52; i++)
            {
                LocalDateTime nextLocalStartDateTime = next + stream.LocalStartTime;
                LocalDateTime nextLocalEndDateTime = next + stream.LocalEndTime;

                var streamSession = new StreamSession
                {
                    TzdbVersionId = version,
                    UtcStartTime = nextLocalStartDateTime.InZoneLeniently(zone).ToInstant(),
                    UtcEndTime = nextLocalEndDateTime.InZoneLeniently(zone).ToInstant(),
                };

                stream.Sessions.Add(streamSession);

                next = next.PlusWeeks(1);
            }

            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");

        }
Exemplo n.º 8
0
        /// <summary>
        /// Get Catholic Trinity Sunday for requested year
        /// https://en.wikipedia.org/wiki/Trinity_Sunday
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public DateTime TrinitySunday(int year)
        {
            var pentecostDate = Pentecost(year);

            return(LocalDate.FromDateTime(pentecostDate, CalendarSystem.Gregorian).With(DateAdjusters.Next(IsoDayOfWeek.Sunday)).ToDateTimeUnspecified());
        }
 /// <summary>
 /// Returns the previous date with the specified day-of-week, or the original date, if the day is already correct.
 /// </summary>
 /// <param name="ld"></param>
 /// <param name="dayOfWeek"></param>
 /// <returns></returns>
 public static LocalDate PreviousOrSame(this LocalDate ld, IsoDayOfWeek dayOfWeek)
 {
     return(DateAdjusters.PreviousOrSame(dayOfWeek)(ld));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Set date
 /// </summary>
 /// <param name="ld"></param>
 /// <param name="day"></param>
 /// <returns></returns>
 public static LocalDate SetDay(this LocalDate ld, int day) =>
 DateAdjusters.DayOfMonth(day)(ld);
Exemplo n.º 11
0
        /// <summary>
        /// Get the end date of the Sacrifice feast for the requested year
        /// https://en.wikipedia.org/wiki/Eid_al-Adha
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        private DateTime EndEidalAdha(int year)
        {
            var EidalAdha         = LocalDate.FromDateTime(new DateTime().FirstDayOfTheYear(year), CalendarSystem.UmAlQura).With(DateAdjusters.Month(12)).With(DateAdjusters.DayOfMonth(12));
            var gregrorianEndDate = EidalAdha.WithCalendar(CalendarSystem.Gregorian);

            return(gregrorianEndDate.ToDateTimeUnspecified());
        }
Exemplo n.º 12
0
        /// <summary>
        /// Gets the start date of the ramadan for a requested year
        /// https://en.wikipedia.org/wiki/Ramadan
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public DateTime StartRamadan(int year)
        {
            var startRamadan   = LocalDate.FromDateTime(new DateTime().FirstDayOfTheYear(year)).WithCalendar(CalendarSystem.UmAlQura).With(DateAdjusters.Month(9)).With(DateAdjusters.StartOfMonth);
            var gregrorianDate = startRamadan.WithCalendar(CalendarSystem.Gregorian);

            return(gregrorianDate.ToDateTimeUnspecified());
        }
 /// <summary>
 /// Returns the last date of the given date's month.
 /// </summary>
 /// <param name="date">The date whose month should be used.</param>
 /// <returns>The last date of the given date's month.</returns>
 public static LocalDate EndOfMonth(this LocalDate date) => DateAdjusters.EndOfMonth(date);
 /// <summary>
 /// Returns the first date of the given date's month.
 /// </summary>
 /// <param name="date">The date whose month should be used.</param>
 /// <returns>The first date of the given date's month.</returns>
 public static LocalDate StartOfMonth(this LocalDate date) => DateAdjusters.StartOfMonth(date);
 /// <summary>
 /// Returns the previous date with the specified day-of-week, or the original date, if the day is already correct.
 /// </summary>
 /// <param name="date">The local date.</param>
 /// <param name="dayOfWeek">The day-of-week to find.</param>
 /// <returns>The previous date with the specified day-of-week, or the original date, if the day is already correct.</returns>
 public static LocalDate PreviousOrSame(this LocalDate date, IsoDayOfWeek dayOfWeek) => DateAdjusters.PreviousOrSame(dayOfWeek)(date);
 /// <summary>
 /// Returns the next date with the specified day-of-week, or the original date, if the day is already correct.
 /// </summary>
 /// <param name="date">The local date.</param>
 /// <param name="dayOfWeek">The day-of-week to find.</param>
 /// <returns>The next date with the specified day-of-week, or the original date, if the day is already correct.</returns>
 public static LocalDate NextOrSame(this LocalDate date, IsoDayOfWeek dayOfWeek) => DateAdjusters.NextOrSame(dayOfWeek)(date);
 /// <summary>
 /// End of month
 /// </summary>
 /// <param name="ld"></param>
 /// <returns></returns>
 public static LocalDate EndOfMonth(this LocalDate ld) => DateAdjusters.EndOfMonth(ld);
Exemplo n.º 18
0
        /// <summary>
        /// Get Catholic Corpus Christi for request year
        /// https://en.wikipedia.org/wiki/Corpus_Christi_(feast)
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public DateTime CorpusChristi(int year)
        {
            var trinitySunday = TrinitySunday(year);

            return(LocalDate.FromDateTime(trinitySunday, CalendarSystem.Gregorian).With(DateAdjusters.Next(IsoDayOfWeek.Thursday)).ToDateTimeUnspecified());
        }
 /// <summary>
 /// Beginning of month
 /// </summary>
 /// <param name="ld"></param>
 /// <returns></returns>
 public static LocalDate BeginningOfMonth(this LocalDate ld) => DateAdjusters.StartOfMonth(ld);
        /// <summary>
        /// Returns the next date with the specified day-of-week
        /// </summary>
        /// <param name="ld"></param>
        /// <param name="dayOfWeek"></param>
        /// <returns></returns>
        public static LocalDate Next(this LocalDate ld, DayOfWeek dayOfWeek)
        {
            var dow = NodaTime.Helpers.DayOfWeekHelper.ToNodaTimeWeek(dayOfWeek);

            return(DateAdjusters.Next(dow)(ld));
        }
Exemplo n.º 21
0
        /// <summary>
        /// Gets the of Ramadan Date for a requested year
        /// https://en.wikipedia.org/wiki/Eid_al-Fitr
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        private DateTime EndOfRamadanDate(int year)
        {
            var endRamadan     = LocalDate.FromDateTime(new DateTime().FirstDayOfTheYear(year)).WithCalendar(CalendarSystem.UmAlQura).With(DateAdjusters.Month(9)).With(DateAdjusters.EndOfMonth);
            var gregrorianDate = endRamadan.WithCalendar(CalendarSystem.Gregorian);

            return(gregrorianDate.ToDateTimeUnspecified());
        }
        private static void UsCommon(List <LocalDate> holidays, int year, bool bumpBack, bool columbusVeteran, int mlkStartYear)
        {
            holidays.Add(BumpSundayToMonday(new LocalDate(year, 1, 1))); // New Year's Day
            if (year >= mlkStartYear)                                    // Martin Luther King day
            {
                holidays.Add(new LocalDate(year, 1, 1).With(DateAdjusters.DayOfWeekInMonth(3, DayOfWeek.Monday)));
            }

            // Washington
            if (year < 1971)
            {
                holidays.Add(BumpSundayToMonday(new LocalDate(year, 2, 22)));
            }
            else
            {
                holidays.Add(new LocalDate(year, 2, 1).With(DateAdjusters.DayOfWeekInMonth(3, DayOfWeek.Monday)));
            }

            // Memorial Day
            if (year < 1971)
            {
                holidays.Add(BumpSundayToMonday(new LocalDate(year, 5, 30)));
            }
            else
            {
                holidays.Add(new LocalDate(year, 2, 1).With(DateAdjusters.LastInMonth(DayOfWeek.Monday)));
            }

            // Labor Day
            holidays.Add(new LocalDate(year, 9, 1).With(DateAdjusters.FirstInMonth(DayOfWeek.Monday)));

            // Columbus Day
            if (columbusVeteran)
            {
                if (year < 1971)
                {
                    holidays.Add(BumpSundayToMonday(new LocalDate(year, 10, 12)));
                }
                else
                {
                    holidays.Add(new LocalDate(year, 10, 1).With(DateAdjusters.DayOfWeekInMonth(2, DayOfWeek.Monday)));
                }
            }

            // Veteran's Day
            if (columbusVeteran)
            {
                if (year >= 1971 && year < 1978)
                {
                    holidays.Add(
                        new LocalDate(year, 10, 1).With(DateAdjusters.DayOfWeekInMonth(4, DayOfWeek.Monday)));
                }
                else
                {
                    holidays.Add(BumpSundayToMonday(new LocalDate(year, 11, 11)));
                }
            }

            // Thanksgiving
            holidays.Add(new LocalDate(year, 11, 1).With(DateAdjusters.DayOfWeekInMonth(4, DayOfWeek.Thursday)));

            // Independence Day & Christmas Day
            if (bumpBack)
            {
                holidays.Add(BumpToFridayOrMonday(new LocalDate(year, 7, 4)));
                holidays.Add(BumpToFridayOrMonday(new LocalDate(year, 12, 25)));
            }
            else
            {
                holidays.Add(BumpSundayToMonday(new LocalDate(year, 7, 4)));
                holidays.Add(BumpSundayToMonday(new LocalDate(year, 12, 25)));
            }
        }
 /// <summary>
 /// Returns the next date with the specified day-of-week, or the original date, if the day is already correct.
 /// </summary>
 /// <param name="ld"></param>
 /// <param name="dayOfWeek"></param>
 /// <returns></returns>
 public static LocalDate NextOrSame(this LocalDate ld, IsoDayOfWeek dayOfWeek)
 {
     return(DateAdjusters.NextOrSame(dayOfWeek)(ld));
 }
Exemplo n.º 24
0
 /// <summary>
 /// Set date
 /// </summary>
 /// <param name="ld"></param>
 /// <param name="month"></param>
 /// <returns></returns>
 public static LocalDate SetMonth(this LocalDate ld, int month) =>
 DateAdjusters.Month(month)(ld);
        private static ImmutableHolidayCalendar GenerateLondon()
        {
            var holidays = new List <LocalDate>(2000);

            for (int year = 1950; year < 2099; year++)
            {
                // New Year
                if (year >= 1974)
                {
                    holidays.Add(BumpToMonday(First(year, 1)));
                }

                // Easter
                holidays.Add(Easter(year).PlusDays(-2));
                holidays.Add(Easter(year).PlusDays(1));

                // Early May
                if (year == 1995)
                {
                    holidays.Add(new LocalDate(1995, 5, 8));
                }
                else if (year >= 1978)
                {
                    holidays.Add(First(year, 5).With(DateAdjusters.FirstInMonth(DayOfWeek.Monday)));
                }

                // Spring
                if (year == 2002)
                {
                    // Golden Jubilee
                    holidays.Add(new LocalDate(2002, 6, 3));
                    holidays.Add(new LocalDate(2002, 6, 4));
                }
                else if (year == 2012)
                {
                    // Diamond Jubilee
                    holidays.Add(new LocalDate(2012, 6, 4));
                    holidays.Add(new LocalDate(2012, 6, 5));
                }
                else if (year == 1967 || year == 1970)
                {
                    holidays.Add(First(year, 5).With(DateAdjusters.LastInMonth(DayOfWeek.Monday)));
                }
                else if (year < 1971)
                {
                    // Whitsun
                    holidays.Add(Easter(year).PlusDays(50));
                }
                else
                {
                    holidays.Add(First(year, 5).With(DateAdjusters.LastInMonth(DayOfWeek.Monday)));
                }

                // Summer
                if (year < 1965)
                {
                    holidays.Add(First(year, 8).With(DateAdjusters.FirstInMonth(DayOfWeek.Monday)));
                }
                else if (year < 1971)
                {
                    holidays.Add(First(year, 8).With(DateAdjusters.LastInMonth(DayOfWeek.Saturday)).PlusDays(2));
                }
                else
                {
                    holidays.Add(First(year, 8).With(DateAdjusters.LastInMonth(DayOfWeek.Monday)));
                }

                // Christmas
                holidays.Add(ChristmasBumpedSaturdaySunday(year));
                holidays.Add(BoxingDayBumpedSaturdaySunday(year));
            }

            holidays.Add(new LocalDate(2011, 4, 29));  // Royal wedding
            holidays.Add(new LocalDate(1999, 12, 31)); // Millenium
            RemoveSatSun(holidays);
            return(new ImmutableHolidayCalendar(
                       HolidayCalendarIds.LON,
                       "The holiday calendar for London, United Kingdom, with code 'LON'",
                       holidays,
                       DayOfWeek.Saturday,
                       DayOfWeek.Sunday));
        }
        /// <summary>
        /// Returns the previous date with the specified day-of-week, or the original date, if the day is already correct.
        /// </summary>
        /// <param name="ld"></param>
        /// <param name="dayOfWeek"></param>
        /// <returns></returns>
        public static LocalDate PreviousOrSame(this LocalDate ld, DayOfWeek dayOfWeek)
        {
            var dow = NodaTime.Helpers.DayOfWeekHelper.ToNodaTimeWeek(dayOfWeek);

            return(DateAdjusters.PreviousOrSame(dow)(ld));
        }