internal SimpleWeekYearRule(int minDaysInFirstWeek, IsoDayOfWeek firstDayOfWeek, bool irregularWeeks) { Preconditions.DebugCheckArgumentRange(nameof(minDaysInFirstWeek), minDaysInFirstWeek, 1, 7); Preconditions.CheckArgumentRange(nameof(firstDayOfWeek), (int)firstDayOfWeek, 1, 7); this.minDaysInFirstWeek = minDaysInFirstWeek; this.firstDayOfWeek = firstDayOfWeek; this.irregularWeeks = irregularWeeks; }
public void WeekYearDifferentToYear(int year, int month, int day, int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) { var date = new LocalDate(year, month, day); Assert.AreEqual(weekYear, WeekYearRules.Iso.GetWeekYear(date)); Assert.AreEqual(weekOfWeekYear, WeekYearRules.Iso.GetWeekOfWeekYear(date)); Assert.AreEqual(dayOfWeek, date.IsoDayOfWeek); Assert.AreEqual(date, WeekYearRules.Iso.GetLocalDate(weekYear, weekOfWeekYear, dayOfWeek)); }
/// <summary> /// Converts from the Noda Time <see cref="IsoDayOfWeek"/> enum to the equivalent BCL /// <see cref="DayOfWeek"/> value. Other than Sunday, the BCL and ISO values are the same - /// but ISO 8601 defines Sunday as day 7, and the BCL defines it as day 0. /// </summary> /// <param name="isoDayOfWeek">ISO day of week value to convert.</param> /// <returns>The ISO day of week value equivalent to the one passed in.</returns> /// <exception cref="ArgumentOutOfRangeException">The <paramref name="isoDayOfWeek"/> parameter /// is not a valid day of the week.</exception> public static DayOfWeek ToDayOfWeek(IsoDayOfWeek isoDayOfWeek) { if (isoDayOfWeek < IsoDayOfWeek.Monday || isoDayOfWeek > IsoDayOfWeek.Sunday) { throw new ArgumentOutOfRangeException(nameof(isoDayOfWeek)); } return isoDayOfWeek == IsoDayOfWeek.Sunday ? DayOfWeek.Sunday : (DayOfWeek)isoDayOfWeek; }
/// <summary> /// A date adjuster to move to the previous specified day-of-week, but return the /// original date if the day is already correct. /// </summary> /// <param name="dayOfWeek">The day-of-week to adjust dates to.</param> /// <returns>An adjuster which advances a date to the previous occurrence of the /// specified day-of-week, or the original date if the day is already corret.</returns> public static Func<LocalDate, LocalDate> PreviousOrSame(IsoDayOfWeek dayOfWeek) { // Avoids boxing... if (dayOfWeek < IsoDayOfWeek.Monday || dayOfWeek > IsoDayOfWeek.Sunday) { throw new ArgumentOutOfRangeException(nameof(dayOfWeek)); } return date => date.IsoDayOfWeek == dayOfWeek ? date : date.Previous(dayOfWeek); }
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); }
internal YearMonthDay GetYearMonthDay(int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) { // TODO: Is this really valid? It means we can't necessarily round-trip. Maybe be more lenient, but then check the resulting days-since-epoch. Preconditions.CheckArgumentRange(nameof(weekYear), weekYear, yearMonthDayCalculator.MinYear, yearMonthDayCalculator.MaxYear); Preconditions.CheckArgumentRange(nameof(weekOfWeekYear), weekOfWeekYear, 1, GetWeeksInWeekYear(weekYear)); // TODO: Work out what argument validation we actually want here. Preconditions.CheckArgumentRange(nameof(dayOfWeek), (int)dayOfWeek, 1, 7); unchecked { int days = GetWeekYearDaysSinceEpoch(weekYear) + (weekOfWeekYear - 1) * 7 + ((int) dayOfWeek - 1); return yearMonthDayCalculator.GetYearMonthDay(days); } }
internal YearMonthDay GetYearMonthDay(int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) { // TODO: Is this really valid? It means we can't necessarily round-trip. Maybe be more lenient, but then check the resulting days-since-epoch. Preconditions.CheckArgumentRange(nameof(weekYear), weekYear, yearMonthDayCalculator.MinYear, yearMonthDayCalculator.MaxYear); Preconditions.CheckArgumentRange(nameof(weekOfWeekYear), weekOfWeekYear, 1, GetWeeksInWeekYear(weekYear)); // TODO: Work out what argument validation we actually want here. Preconditions.CheckArgumentRange(nameof(dayOfWeek), (int)dayOfWeek, 1, 7); unchecked { int days = GetWeekYearDaysSinceEpoch(weekYear) + (weekOfWeekYear - 1) * 7 + ((int)dayOfWeek - 1); return(yearMonthDayCalculator.GetYearMonthDay(days)); } }
public static string InHebrew(this IsoDayOfWeek dow) { return(dow switch { IsoDayOfWeek.None => "", IsoDayOfWeek.Sunday => "ראשון", IsoDayOfWeek.Monday => "שני", IsoDayOfWeek.Tuesday => "שלישי", IsoDayOfWeek.Wednesday => "רביעי", IsoDayOfWeek.Thursday => "חמישי", IsoDayOfWeek.Friday => "שישי", IsoDayOfWeek.Saturday => "שבת", _ => throw new ArgumentOutOfRangeException(nameof(dow), dow, null) });
/// <inheritdoc /> public LocalDate GetLocalDate(int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek, CalendarSystem calendar) { Preconditions.CheckNotNull(calendar, nameof(calendar)); ValidateWeekYear(weekYear, calendar); // The actual message for this won't be ideal, but it's clear enough. Preconditions.CheckArgumentRange(nameof(dayOfWeek), (int)dayOfWeek, 1, 7); var yearMonthDayCalculator = calendar.YearMonthDayCalculator; var maxWeeks = GetWeeksInWeekYear(weekYear, calendar); if (weekOfWeekYear < 1 || weekOfWeekYear > maxWeeks) { throw new ArgumentOutOfRangeException(nameof(weekOfWeekYear)); } unchecked { int startOfWeekYear = GetWeekYearDaysSinceEpoch(yearMonthDayCalculator, weekYear); // 0 for "already on the first day of the week" up to 6 "it's the last day of the week". int daysIntoWeek = ((dayOfWeek - firstDayOfWeek) + 7) % 7; int days = startOfWeekYear + (weekOfWeekYear - 1) * 7 + daysIntoWeek; if (days < calendar.MinDays || days > calendar.MaxDays) { throw new ArgumentOutOfRangeException(nameof(weekYear), $"The combination of {nameof(weekYear)}, {nameof(weekOfWeekYear)} and {nameof(dayOfWeek)} is invalid"); } LocalDate ret = new LocalDate(yearMonthDayCalculator.GetYearMonthDay(days).WithCalendar(calendar)); // For rules with irregular weeks, the calculation so far may end up computing a date which isn't // in the right week-year. This will happen if the caller has specified a "short" week (i.e. one // at the start or end of the week-year which is not seven days long due to the week year changing // part way through a week) and a day-of-week which corresponds to the "missing" part of the week. // Examples are in SimpleWeekYearRuleTest.GetLocalDate_Invalid. // The simplest way to find out is just to check what the week year is, but we only need to do // the full check if the requested week-year is different to the calendar year of the result. // We don't need to check for this in regular rules, because the computation we've already performed // will always be right. if (irregularWeeks && weekYear != ret.Year) { if (GetWeekYear(ret) != weekYear) { throw new ArgumentOutOfRangeException(nameof(weekYear), $"The combination of {nameof(weekYear)}, {nameof(weekOfWeekYear)} and {nameof(dayOfWeek)} is invalid"); } } return(ret); } }
private Instant GetNextByDayOfWeek(IsoDayOfWeek dayOfWeek, Instant now, string userTimeZone) { var zone = GetZone(userTimeZone); var date = new ZonedDateTime(now, zone).Date; if (date.DayOfWeek != dayOfWeek) { date = date.Next(dayOfWeek); } var dateTime = zone.AtStrictly(date + Time); return(dateTime.ToInstant()); }
private bool IsNotWeekend(IsoDayOfWeek dayOfWeek) { switch (dayOfWeek) { case IsoDayOfWeek.Monday: case IsoDayOfWeek.Tuesday: case IsoDayOfWeek.Wednesday: case IsoDayOfWeek.Thursday: case IsoDayOfWeek.Friday: return(true); default: return(false); } }
/// <inheritdoc /> public LocalDate GetLocalDate(int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek, [NotNull] CalendarSystem calendar) { Preconditions.CheckNotNull(calendar, nameof(calendar)); ValidateWeekYear(weekYear, calendar); // The actual message for this won't be ideal, but it's clear enough. Preconditions.CheckArgumentRange(nameof(dayOfWeek), (int)dayOfWeek, 1, 7); var yearMonthDayCalculator = calendar.YearMonthDayCalculator; var maxWeeks = GetWeeksInWeekYear(weekYear, calendar); if (weekOfWeekYear < 1 || weekOfWeekYear > maxWeeks) { throw new ArgumentOutOfRangeException(nameof(weekOfWeekYear)); } unchecked { int startOfWeekYear = GetWeekYearDaysSinceEpoch(yearMonthDayCalculator, weekYear); // 0 for "already on the first day of the week" up to 6 "it's the last day of the week". int daysIntoWeek = ((dayOfWeek - firstDayOfWeek) + 7) % 7; int days = startOfWeekYear + (weekOfWeekYear - 1) * 7 + daysIntoWeek; if (days < calendar.MinDays || days > calendar.MaxDays) { throw new ArgumentOutOfRangeException(nameof(weekYear), $"The combination of {nameof(weekYear)}, {nameof(weekOfWeekYear)} and {nameof(dayOfWeek)} is invalid"); } LocalDate ret = new LocalDate(yearMonthDayCalculator.GetYearMonthDay(days).WithCalendar(calendar)); // For rules with irregular weeks, the calculation so far may end up computing a date which isn't // in the right week-year. This will happen if the caller has specified a "short" week (i.e. one // at the start or end of the week-year which is not seven days long due to the week year changing // part way through a week) and a day-of-week which corresponds to the "missing" part of the week. // Examples are in SimpleWeekYearRuleTest.GetLocalDate_Invalid. // The simplest way to find out is just to check what the week year is, but we only need to do // the full check if the requested week-year is different to the calendar year of the result. // We don't need to check for this in regular rules, because the computation we've already performed // will always be right. if (irregularWeeks && weekYear != ret.Year) { if (GetWeekYear(ret) != weekYear) { throw new ArgumentOutOfRangeException(nameof(weekYear), $"The combination of {nameof(weekYear)}, {nameof(weekOfWeekYear)} and {nameof(dayOfWeek)} is invalid"); } } return ret; } }
public static bool IsDateTimeBetweenDays(DateTime dateTime, IsoDayOfWeek fromDay, int fromHour, int fromYearWeek, IsoDayOfWeek toDay, int toHour, int toYearWeek) { var rule = WeekYearRules.Iso; var fromYear = CalculateYear(fromYearWeek); var fromWeek = CalculateWeek(fromYearWeek); var fromLocalDate = rule.GetLocalDate(fromYear, fromWeek, fromDay); var fromDate = new DateTime(fromLocalDate.Year, fromLocalDate.Month, fromLocalDate.Day, fromHour, 0, 0); var toYear = CalculateYear(toYearWeek); var toWeek = CalculateWeek(toYearWeek); var toLocalDate = rule.GetLocalDate(toYear, toWeek, toDay); var toDate = new DateTime(toLocalDate.Year, toLocalDate.Month, toLocalDate.Day, toHour, 0, 0); return(dateTime < toDate && dateTime > fromDate); }
/// <summary> /// Converts the short three-letter string representation /// of the day of week to NodaTime.IsoDayOfWeek enum value. /// /// This parser accepts only short three-letter abbreviations /// (e.g. Mon), not the full name (e.g. Monday). /// /// Set result to default enum value (None) and return /// false if the conversion fails. /// </summary> public static bool TryParse(string s, out IsoDayOfWeek result) { switch (s) { // Empty string is converted to IsoDayOfWeek.None case "": result = IsoDayOfWeek.None; return(true); case "Mon": result = IsoDayOfWeek.Monday; return(true); case "Tue": result = IsoDayOfWeek.Tuesday; return(true); case "Wed": result = IsoDayOfWeek.Wednesday; return(true); case "Thu": result = IsoDayOfWeek.Thursday; return(true); case "Fri": result = IsoDayOfWeek.Friday; return(true); case "Sat": result = IsoDayOfWeek.Saturday; return(true); case "Sun": result = IsoDayOfWeek.Sunday; return(true); default: // Conversion failed, return false and set the // result to the default value of None result = IsoDayOfWeek.None; return(false); } }
public Occurance SetRepeat(IsoDayOfWeek day, Occurance val) { switch (day) { case IsoDayOfWeek.Monday: return(this.Monday = val); case IsoDayOfWeek.Tuesday: return(this.Tuesday = val); case IsoDayOfWeek.Wednesday: return(this.Wednesday = val); case IsoDayOfWeek.Thursday: return(this.Thursday = val); case IsoDayOfWeek.Friday: return(this.Friday = val); case IsoDayOfWeek.Saturday: return(this.Saturday = val); case IsoDayOfWeek.Sunday: return(this.Sunday = val); } return(val); }
public static DayOfWeek ToSystemWeek(IsoDayOfWeek week) { switch (week) { case IsoDayOfWeek.Sunday: return(DayOfWeek.Sunday); case IsoDayOfWeek.Monday: return(DayOfWeek.Monday); case IsoDayOfWeek.Tuesday: return(DayOfWeek.Saturday); case IsoDayOfWeek.Wednesday: return(DayOfWeek.Wednesday); case IsoDayOfWeek.Thursday: return(DayOfWeek.Thursday); case IsoDayOfWeek.Friday: return(DayOfWeek.Friday); case IsoDayOfWeek.Saturday: return(DayOfWeek.Saturday); default: throw new InvalidOperationException("Unknown day of week"); } }
private static Event.Rule.Day GetDay(IsoDayOfWeek day) { switch (day) { case IsoDayOfWeek.Monday: return(Event.Rule.Day.Monday); case IsoDayOfWeek.Tuesday: return(Event.Rule.Day.Tuesday); case IsoDayOfWeek.Wednesday: return(Event.Rule.Day.Wednesday); case IsoDayOfWeek.Thursday: return(Event.Rule.Day.Thursday); case IsoDayOfWeek.Friday: return(Event.Rule.Day.Friday); case IsoDayOfWeek.Saturday: return(Event.Rule.Day.Saturday); case IsoDayOfWeek.Sunday: return(Event.Rule.Day.Sunday); } throw new Exception("unrecognized day of week: " + day); }
public void Gregorian(int year, IsoDayOfWeek firstDayOfYear, int maxMinDaysInFirstWeekForSameWeekYear) { var startOfCalendarYear = new LocalDate(year, 1, 1); Assert.AreEqual(firstDayOfYear, startOfCalendarYear.IsoDayOfWeek); // Rules which put the first day of the calendar year into the same week year for (int i = 1; i <= maxMinDaysInFirstWeekForSameWeekYear; i++) { var rule = WeekYearRules.ForMinDaysInFirstWeek(i); Assert.AreEqual(year, rule.GetWeekYear(startOfCalendarYear)); Assert.AreEqual(1, rule.GetWeekOfWeekYear(startOfCalendarYear)); } // Rules which put the first day of the calendar year into the previous week year for (int i = maxMinDaysInFirstWeekForSameWeekYear + 1; i <= 7; i++) { var rule = WeekYearRules.ForMinDaysInFirstWeek(i); Assert.AreEqual(year - 1, rule.GetWeekYear(startOfCalendarYear)); Assert.AreEqual(rule.GetWeeksInWeekYear(year - 1), rule.GetWeekOfWeekYear(startOfCalendarYear)); } }
private static Instant GetNextTime(this IClock clock, IsoDayOfWeek dayOfWeek, LocalTime timeOnDay) { var currentTime = clock.GetCurrentInstant(); var nyc = EasternTimeZone; var localDateTime = currentTime.InZone(nyc).LocalDateTime; LocalDate currentDate = localDateTime.Date; LocalTime currentLocalTime = localDateTime.TimeOfDay; LocalDate nextPublicRevealDate; if (currentDate.DayOfWeek == dayOfWeek) { nextPublicRevealDate = currentLocalTime >= timeOnDay?currentDate.Next(dayOfWeek) : currentDate; } else { nextPublicRevealDate = currentDate.Next(dayOfWeek); } LocalDateTime dateTime = nextPublicRevealDate + timeOnDay; return(dateTime.InZoneStrictly(nyc).ToInstant()); }
public static IEnumerable <Interval> GetDayIntervals( Interval input, DateTimeZone zone, IsoDayOfWeek dayOfWeek) { // Get a range of dates that covers the input interval. This is deliberately // larger than it may need to be, to handle days starting at different instants // in different time zones. LocalDate startDate = input.Start.InZone(DateTimeZone.Utc).Date.PlusDays(-2); LocalDate endDate = input.End.InZone(DateTimeZone.Utc).Date.PlusDays(2); var dates = GetDates(startDate, endDate, dayOfWeek); // Convert those dates into intervals, each of which may or may not overlap // with our input. var intervals = dates.Select(date => GetIntervalForDate(date, zone)); // Find the intersection of each date-interval with our input, and discard // any non-overlaps return(intervals.Select(dateInterval => Intersect(dateInterval, input)) .Where(x => x != null) .Select(x => x.Value)); }
public static bool GetIsOccuring(this Event self) { ZonedDateTime zdt = TimeUtils.Now.InZone(TimeUtils.Sydney); IsoDayOfWeek day = zdt.DayOfWeek; Occurance? occurance = self.GetRepeatOccurance(day); if (occurance == null) { return(false); } Instant starts = occurance.GetInstant(zdt.Date, zdt.TimeOfDay); Instant ends = starts + occurance.GetDuration(); if (starts < TimeUtils.Now && ends > TimeUtils.Now) { return(true); } return(false); }
internal static string ToStringAbbr(this IsoDayOfWeek isoDayOfWeek, System.Globalization.CultureInfo culture = null) { // Todo: #5 // Return the Localized version of the week name if (culture == null) { culture = System.Globalization.CultureInfo.InvariantCulture; } switch (isoDayOfWeek) { case IsoDayOfWeek.Monday: return("Mo"); case IsoDayOfWeek.Tuesday: return("Tu"); case IsoDayOfWeek.Wednesday: return("We"); case IsoDayOfWeek.Thursday: return("Th"); case IsoDayOfWeek.Friday: return("Fr"); case IsoDayOfWeek.Saturday: return("Sa"); case IsoDayOfWeek.Sunday: return("Su"); case IsoDayOfWeek.None: default: return(string.Empty); } }
internal static string DayOfWeek(IsoDayOfWeek dayOfWeek, IEnumerable <IsoDayOfWeek> daysOfWeekDisabled) { var strClass = "dow"; if (dayOfWeek.IsWeekday()) { strClass += " weekday"; } else { strClass += " weekend"; } if (daysOfWeekDisabled != null) { if (daysOfWeekDisabled.Contains(dayOfWeek)) { strClass += " disabled"; } } return(strClass); }
/// <summary> /// Creates a pattern of weekly recurrence on the specified day, hour of the day (i.e. 24-hour basis), and minute, in local time. "Fall-back" time changes /// can cause more than one occurrence per week. /// </summary> public static OperationRecurrencePattern CreateWeekly(IsoDayOfWeek day, int hour, int minute) { return(new OperationRecurrencePattern( (beginDateTime, endDateTime) => { if (endDateTime - Period.FromWeeks(1) >= beginDateTime) { return true; } var date = beginDateTime.Date; while (date <= endDateTime.Date) { if (date.DayOfWeek == day) { var dayBeginDateTime = date.AtMidnight(); if (beginDateTime > dayBeginDateTime) { dayBeginDateTime = beginDateTime; } var dayEndDateTime = date.PlusDays(1).AtMidnight(); if (endDateTime < dayEndDateTime) { dayEndDateTime = endDateTime; } var time = new LocalTime(hour, minute); if (dayBeginDateTime.TimeOfDay <= time && time < dayEndDateTime.TimeOfDay) { return true; } } date += Period.FromDays(1); } return false; })); }
public void HebrewCalendar(int year, IsoDayOfWeek expectedFirstDay, int isoYear, int isoMonth, int isoDay, // Mostly for documentation int expectedWeeks, int expectedWeekYearOfFirstDay, int expectedWeekOfWeekYearOfFirstDay) { var civilDate = new LocalDate(year, 1, 1, HebrewCivil); var rule = WeekYearRules.Iso; Assert.AreEqual(expectedFirstDay, civilDate.IsoDayOfWeek); Assert.AreEqual(civilDate.WithCalendar(CalendarSystem.Iso), new LocalDate(isoYear, isoMonth, isoDay)); Assert.AreEqual(expectedWeeks, rule.GetWeeksInWeekYear(year, HebrewCivil)); Assert.AreEqual(expectedWeekYearOfFirstDay, rule.GetWeekYear(civilDate)); Assert.AreEqual(expectedWeekOfWeekYearOfFirstDay, rule.GetWeekOfWeekYear(civilDate)); Assert.AreEqual(civilDate, rule.GetLocalDate(expectedWeekYearOfFirstDay, expectedWeekOfWeekYearOfFirstDay, expectedFirstDay, HebrewCivil)); // The scriptural month numbering system should have the same week-year and week-of-week-year. var scripturalDate = civilDate.WithCalendar(HebrewScriptural); Assert.AreEqual(expectedWeeks, rule.GetWeeksInWeekYear(year, HebrewScriptural)); Assert.AreEqual(expectedWeekYearOfFirstDay, rule.GetWeekYear(scripturalDate)); Assert.AreEqual(expectedWeekOfWeekYearOfFirstDay, rule.GetWeekOfWeekYear(scripturalDate)); Assert.AreEqual(scripturalDate, rule.GetLocalDate(expectedWeekYearOfFirstDay, expectedWeekOfWeekYearOfFirstDay, expectedFirstDay, HebrewScriptural)); }
public void Valid(ProtoDayOfWeek input, IsoDayOfWeek expectedOutput) => Assert.AreEqual(expectedOutput, input.ToIsoDayOfWeek());
public void ToDayOfWeek_InvalidValues(IsoDayOfWeek noda) { Assert.Throws<ArgumentOutOfRangeException>(() => BclConversions.ToDayOfWeek(noda)); }
public DayModel(string dayName, IsoDayOfWeek dayOfWeek) { DisplayName = dayName; DayOfWeek = dayOfWeek; }
/// <summary> /// Returns the local date corresponding to the given "week year", "week of week year", and "day of week" /// in the ISO calendar system. /// </summary> /// <param name="weekYear">ISO-8601 week year of value to return</param> /// <param name="weekOfWeekYear">ISO-8601 week of week year of value to return</param> /// <param name="dayOfWeek">ISO-8601 day of week to return</param> /// <returns>The date corresponding to the given week year / week of week year / day of week.</returns> public static LocalDate FromWeekYearWeekAndDay(int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) { YearMonthDay yearMonthDay = CalendarSystem.Iso.GetYearMonthDayFromWeekYearWeekAndDayOfWeek(weekYear, weekOfWeekYear, dayOfWeek); return(new LocalDate(yearMonthDay, CalendarSystem.Iso)); }
/// <summary> /// Returns the local date corresponding to a particular occurrence of a day-of-week /// within a year and month. For example, this method can be used to ask for "the third Monday in April 2012". /// </summary> /// <remarks> /// The returned date is always in the ISO calendar. This method is unrelated to week-years and any rules for /// "business weeks" and the like - if a month begins on a Friday, then asking for the first Friday will give /// that day, for example. /// </remarks> /// <param name="year">The year of the value to return.</param> /// <param name="month">The month of the value to return.</param> /// <param name="occurrence">The occurrence of the value to return, which must be in the range [1, 5]. The value 5 can /// be used to always return the last occurrence of the specified day-of-week, even if there are only 4 /// occurrences of that day-of-week in the month.</param> /// <param name="dayOfWeek">The day-of-week of the value to return.</param> /// <returns>The date corresponding to the given year and month, on the given occurrence of the /// given day of week.</returns> public static LocalDate FromYearMonthWeekAndDay(int year, int month, int occurrence, IsoDayOfWeek dayOfWeek) { // This validates year and month as well as getting us a useful date. LocalDate startOfMonth = new LocalDate(year, month, 1); Preconditions.CheckArgumentRange(nameof(occurrence), occurrence, 1, 5); Preconditions.CheckArgumentRange(nameof(dayOfWeek), (int)dayOfWeek, 1, 7); // Correct day of week, 1st week of month. int week1Day = dayOfWeek - startOfMonth.DayOfWeek + 1; if (week1Day <= 0) { week1Day += 7; } int targetDay = week1Day + (occurrence - 1) * 7; if (targetDay > CalendarSystem.Iso.GetDaysInMonth(year, month)) { targetDay -= 7; } return(new LocalDate(year, month, targetDay)); }
public int Previous(int dayOfMonth, IsoDayOfWeek targetDayOfWeek) { LocalDate start = new LocalDate(2011, 11, dayOfMonth); LocalDate target = start.Previous(targetDayOfWeek); Assert.AreEqual(2011, target.Year); Assert.AreEqual(11, target.Month); return target.Day; }
/// <summary> /// Converts a <see cref="IsoDayOfWeek"/> into the corresponding <see cref="DayOfWeek"/>. /// </summary> /// <remarks>This is a convenience method which calls <see cref="BclConversions.ToDayOfWeek"/>.</remarks> /// <param name="isoDayOfWeek">The <c>IsoDayOfWeek</c> to convert.</param> /// <returns>The <c>DayOfWeek</c> equivalent to <paramref name="isoDayOfWeek"/></returns> public static DayOfWeek ToDayOfWeek(this IsoDayOfWeek isoDayOfWeek) => BclConversions.ToDayOfWeek(isoDayOfWeek);
public void Next_InvalidArgument(IsoDayOfWeek targetDayOfWeek) { LocalDateTime start = new LocalDateTime(2011, 1, 1, 15, 25, 30, 100, 5000); Assert.Throws<ArgumentOutOfRangeException>(() => start.Next(targetDayOfWeek)); }
/// <summary> /// Creates a week year rule where the boundary between one week-year and the next /// is parameterized in terms of how many days of the first week of the week /// year have to be in the new calendar year, and also by which day is deemed /// to be the first day of the week. /// </summary> /// <remarks> /// <paramref name="minDaysInFirstWeek"/> determines when the first week of the week-year starts. /// For any given calendar year X, consider the week that includes the first day of the /// calendar year. Usually, some days of that week are in calendar year X, and some are in calendar year /// X-1. If <paramref name="minDaysInFirstWeek"/> or more of the days are in year X, then the week is /// deemed to be the first week of week-year X. Otherwise, the week is deemed to be the last week of /// week-year X-1, and the first week of week-year X starts on the following <paramref name="firstDayOfWeek"/>. /// </remarks> /// <param name="minDaysInFirstWeek">The minimum number of days in the first week (starting on /// <paramref name="firstDayOfWeek" />) which have to be in the new calendar year for that week /// to count as being in that week-year. Must be in the range 1 to 7 inclusive. /// </param> /// <param name="firstDayOfWeek">The first day of the week.</param> /// <returns>A <see cref="SimpleWeekYearRule"/> with the specified minimum number of days in the first /// week and first day of the week.</returns> public static IWeekYearRule ForMinDaysInFirstWeek(int minDaysInFirstWeek, IsoDayOfWeek firstDayOfWeek) => new SimpleWeekYearRule(minDaysInFirstWeek, firstDayOfWeek, false);
public void FromYearMonthWeekAndDay(int year, int month, int occurrence, IsoDayOfWeek dayOfWeek, int expectedDay) { var date = LocalDate.FromYearMonthWeekAndDay(year, month, occurrence, dayOfWeek); Assert.AreEqual(year, date.Year); Assert.AreEqual(month, date.Month); Assert.AreEqual(expectedDay, date.Day); }
public void DayOfWeek_BothWaysValid(DayOfWeek bcl, IsoDayOfWeek noda) { Assert.AreEqual(bcl, BclConversions.ToDayOfWeek(noda)); Assert.AreEqual(noda, BclConversions.ToIsoDayOfWeek(bcl)); }
private void AssertFromWeekYearWeekAndDay(LocalDate expected, int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) { var actual = LocalDate.FromWeekYearWeekAndDay(weekYear, weekOfWeekYear, dayOfWeek); Assert.AreEqual(expected, actual); // Check that reading the properties works too... Assert.AreEqual(weekYear, actual.WeekYear); Assert.AreEqual(weekOfWeekYear, actual.WeekOfWeekYear); Assert.AreEqual(dayOfWeek, actual.IsoDayOfWeek); }
/// <summary> /// Returns the local date corresponding to the given "week year", "week of week year", and "day of week" /// in this calendar system. /// </summary> /// <param name="weekYear">ISO-8601 week year of value to return</param> /// <param name="weekOfWeekYear">ISO-8601 week of week year of value to return</param> /// <param name="dayOfWeek">ISO-8601 day of week to return</param> /// <returns>The date corresponding to the given week year / week of week year / day of week.</returns> internal YearMonthDay GetYearMonthDayFromWeekYearWeekAndDayOfWeek(int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) { return weekYearCalculator.GetYearMonthDay(weekYear, weekOfWeekYear, dayOfWeek); }
/// <summary> /// Convenience method to call <see cref="IWeekYearRule.GetLocalDate(int, int, IsoDayOfWeek, CalendarSystem)"/> /// passing in the ISO calendar system. /// </summary> /// <param name="rule">The rule to delegate the call to.</param> /// <param name="weekYear">The week-year of the new date. Implementations provided by Noda Time allow any /// year which is a valid calendar year, and sometimes one less than the minimum calendar year /// and/or one more than the maximum calendar year, to allow for dates near the start of a calendar /// year to fall in the previous week year, and similarly for dates near the end of a calendar year.</param> /// <param name="weekOfWeekYear">The week of week-year of the new date. Valid values for this parameter /// may vary depending on <paramref name="weekYear"/>, as the length of a year in weeks varies.</param> /// <param name="dayOfWeek">The day-of-week of the new date. Valid values for this parameter may vary /// depending on <paramref name="weekYear"/> and <paramref name="weekOfWeekYear"/>.</param> /// <exception cref="ArgumentOutOfRangeException">The parameters do not combine to form a valid date.</exception> /// <returns>A <see cref="LocalDate"/> corresponding to the specified values.</returns> public static LocalDate GetLocalDate([NotNull] this IWeekYearRule rule, int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) => Preconditions.CheckNotNull(rule, nameof(rule)).GetLocalDate(weekYear, weekOfWeekYear, dayOfWeek, CalendarSystem.Iso);
/// <summary> /// 转换为 系统星期几 /// </summary> /// <param name="week">NodaTime 星期几</param> public static DayOfWeek ToSystemWeek(IsoDayOfWeek week) => week switch {
public int Next(int dayOfMonth, IsoDayOfWeek targetDayOfWeek) { LocalDateTime start = new LocalDateTime(2011, 11, dayOfMonth, 15, 25, 30, 100, 5000); LocalDateTime target = start.Next(targetDayOfWeek); Assert.AreEqual(2011, target.Year); Assert.AreEqual(11, target.Month); Assert.AreEqual(start.TimeOfDay, target.TimeOfDay); return target.Day; }
public DayOfWeek(IsoDayOfWeek dayIndex) : base(TemporalExpressionType.DayOfWeek) { _dayIndex = dayIndex; }
public void Next_InvalidArgument(IsoDayOfWeek targetDayOfWeek) { LocalDateTime start = new LocalDateTime(2011, 1, 1, 15, 25, 30, 100, 5000); Assert.Throws <ArgumentOutOfRangeException>(() => start.Next(targetDayOfWeek)); }
// We might want to add more tests here... public void NonMondayFirstDayOfWeek(int minDaysInFirstWeek, IsoDayOfWeek firstDayOfWeek, int weekYear, int week, IsoDayOfWeek dayOfWeek, int expectedYear, int expectedMonth, int expectedDay) { var rule = WeekYearRules.ForMinDaysInFirstWeek(minDaysInFirstWeek, firstDayOfWeek); var actual = rule.GetLocalDate(weekYear, week, dayOfWeek); var expected = new LocalDate(expectedYear, expectedMonth, expectedDay); Assert.AreEqual(expected, actual); Assert.AreEqual(weekYear, rule.GetWeekYear(actual)); Assert.AreEqual(week, rule.GetWeekOfWeekYear(actual)); }
private static int WeekStartAdjustment(IsoDayOfWeek dayOfWeek, IsoDayOfWeek?workWeekStart) { var difference = workWeekStart.GetValueOrDefault(IsoDayOfWeek.Monday) - dayOfWeek; return(workWeekStart <= dayOfWeek ? difference : difference - 7); }
/// <summary> /// Returns the local date corresponding to the given "week year", "week of week year", and "day of week" /// in the ISO calendar system, using the ISO week-year rules. /// </summary> /// <param name="weekYear">ISO-8601 week year of value to return</param> /// <param name="weekOfWeekYear">ISO-8601 week of week year of value to return</param> /// <param name="dayOfWeek">ISO-8601 day of week to return</param> /// <returns>The date corresponding to the given week year / week of week year / day of week.</returns> public static LocalDate FromWeekYearWeekAndDay(int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) => WeekYearRules.Iso.GetLocalDate(weekYear, weekOfWeekYear, dayOfWeek, CalendarSystem.Iso);
public void Previous_InvalidArgument(IsoDayOfWeek targetDayOfWeek) { LocalDate start = new LocalDate(2011, 1, 1); Assert.Throws<ArgumentOutOfRangeException>(() => start.Previous(targetDayOfWeek)); }
/// <summary> /// A date adjuster to move to the next specified day-of-week, adding /// a week if the day is already correct. /// </summary> /// <remarks> /// This is the adjuster equivalent of <see cref="LocalDate.Next"/>. /// </remarks> /// <param name="dayOfWeek">The day-of-week to adjust dates to.</param> /// <returns>An adjuster which advances a date to the next occurrence of the /// specified day-of-week.</returns> public static Func<LocalDate, LocalDate> Next(IsoDayOfWeek dayOfWeek) { // Avoids boxing... if (dayOfWeek < IsoDayOfWeek.Monday || dayOfWeek > IsoDayOfWeek.Sunday) { throw new ArgumentOutOfRangeException(nameof(dayOfWeek)); } return date => date.Next(dayOfWeek); }
public void Iso(int year, int month, int day, int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) { var viaCalendar = new LocalDate(year, month, day); var rule = WeekYearRules.Iso; Assert.AreEqual(weekYear, rule.GetWeekYear(viaCalendar)); Assert.AreEqual(weekOfWeekYear, rule.GetWeekOfWeekYear(viaCalendar)); Assert.AreEqual(dayOfWeek, viaCalendar.IsoDayOfWeek); var viaRule = rule.GetLocalDate(weekYear, weekOfWeekYear, dayOfWeek); Assert.AreEqual(viaCalendar, viaRule); }
/// <summary> /// Returns the local date corresponding to the given "week year", "week of week year", and "day of week" /// in this calendar system. /// </summary> /// <param name="weekYear">ISO-8601 week year of value to return</param> /// <param name="weekOfWeekYear">ISO-8601 week of week year of value to return</param> /// <param name="dayOfWeek">ISO-8601 day of week to return</param> /// <returns>The date corresponding to the given week year / week of week year / day of week.</returns> internal YearMonthDayCalendar GetYearMonthDayCalendarFromWeekYearWeekAndDayOfWeek(int weekYear, int weekOfWeekYear, IsoDayOfWeek dayOfWeek) { return weekYearCalculator.GetYearMonthDay(weekYear, weekOfWeekYear, dayOfWeek).WithCalendarOrdinal(Ordinal); }
public void Previous_InvalidArgument(IsoDayOfWeek targetDayOfWeek) { LocalDate start = new LocalDate(2011, 1, 1); Assert.Throws <ArgumentOutOfRangeException>(() => start.Previous(targetDayOfWeek)); }
public void GetLocalDate_Invalid( CalendarWeekRule bclRule, DayOfWeek firstDayOfWeek, int weekYear, int week, IsoDayOfWeek dayOfWeek) { var nodaRule = WeekYearRules.FromCalendarWeekRule(bclRule, firstDayOfWeek); Assert.Throws<ArgumentOutOfRangeException>(() => nodaRule.GetLocalDate(weekYear, week, dayOfWeek)); }