private static List <(LocalDateTime start, LocalDateTime end)> GetSessions(string s) { List <(LocalDateTime start, LocalDateTime end)> list = new(); if (string.IsNullOrEmpty(s)) { return(list); } string[] days = s.Split(';'); foreach (string day in days) { string[] parts = day.Split(':'); Debug.Assert(parts.Length == 2); LocalDate date = DatePattern.Parse(parts[0]).Value; IEnumerable <string> sessions = parts[1].Split(',').Distinct(); // the sessions may be repeated(?) foreach (string session in sessions.Where(x => x != "CLOSED")) { (LocalTime startTime, LocalTime endTime) = GetSessionTime(session); LocalDateTime start = date.At(startTime); LocalDateTime end = date.At(endTime); if (end < start) { start = start.PlusDays(-1); //end = end.PlusDays(1); ?? } if (list.Any() && (start <= list.Last().end || end <= start)) // ensure consecutive, non-overlapping periods { throw new InvalidDataException("Invalid period."); } list.Add((start, end)); } } return(list);
public void At_StartWithoutValue() { // Arrange LocalDate date = Fixture.Create <LocalDate>(); LocalTime?time = Fixture.Create <LocalTime>(x => x != LocalTime.Midnight); // Act var interval = date.At(null, time); // Assert interval.StartDateTime.HasValue.Should().BeFalse(); // TODO: Add LocalDateTimeAssertions! interval.EndDateTime.Should().Be(date.At(time.Value)); }
public void At_NullableStartTimeIsEqualToNullableEndTime() { // Arrange LocalDate date = Fixture.Create <LocalDate>(); LocalTime?time = Fixture.Create <LocalTime>(); // Act var interval = date.At(time, time); // Assert interval.StartDateTime.Should().Be(date.At(time.Value)); interval.EndDateTime.Should().Be(date.NextDay().At(time.Value)); }
public void At_EndWithoutValue() { // Arrange LocalDate date = Fixture.Create <LocalDate>(); LocalTime?time = Fixture.Create <LocalTime>(); // Act var interval = date.At(time, null); // Assert interval.StartDateTime.Should().Be(date.At(time.Value)); interval.EndDateTime.HasValue.Should().BeFalse(); // TODO: Add LocalDateTimeAssertions! }
public void At_NullableStartTimeIsGreaterThanNullableEndTime() { // Arrange LocalDate date = Fixture.Create <LocalDate>(); (LocalTime? startTime, LocalTime? endTime) = Fixture.Create <LocalTime>((x, y) => x > y); // Act var interval = date.At(startTime, endTime); // Assert interval.StartDateTime.Should().Be(date.At(startTime.Value)); interval.EndDateTime.Should().Be(date.NextDay().At(endTime.Value)); }
public async Task <IActionResult> OnPost([FromQuery] int year, [FromQuery] int month, [FromQuery] int day, [FromQuery] string id) { if (!ModelState.IsValid) { ErrorMessage = "入力に誤りがあります。"; return(await PageResult(year, month, day, id)); } var startTime = LocalTimePattern.Create("HH:mm", CultureInfo.CurrentCulture).Parse(StartTime !); if (!startTime.Success) { ErrorMessage = "入力に誤りがあります。"; return(await PageResult(year, month, day, id)); } var date = new LocalDate(year, month, day); var result = await _service.UpdateAppointmentSlotAsync(id, date.At(startTime.Value), Period.FromMinutes(DurationMinutes !.Value), CountOfSlot !.Value); if (!result.Succeeded) { ErrorMessage = result.ErrorMessage; return(await PageResult(year, month, day, id)); } return(RedirectToPage("SlotDetails", new { year, month, day, id })); }
public async Task TestPriceTickTest() { var symbol = "AAPL"; var security = await new YahooSnapshot().GetAsync(symbol) ?? throw new Exception($"Invalid symbol: {symbol}."); var timeZoneName = security.ExchangeTimezoneName ?? throw new Exception($"TimeZone name not found."); var timeZone = timeZoneName.ToTimeZone(); var date = new LocalDate(2017, 1, 3); var instant = date.At(new LocalTime(16, 0)).InZoneStrictly(timeZone).ToInstant(); var ticks = await YahooHistory .FromDate(instant) .Frequency(Frequency.Daily) .GetPricesAsync(symbol); if (ticks == null) { throw new Exception("Invalid symbol"); } var tick = ticks.First(); Assert.Equal(115.800003m, tick.Open); Assert.Equal(116.330002m, tick.High); Assert.Equal(114.760002m, tick.Low); Assert.Equal(116.150002m, tick.Close); Assert.Equal(28_781_900, tick.Volume); }
public void At() { var date = new LocalDate(2012, 6, 19, CalendarSystem.Julian); var offset = Offset.FromHours(5); var time = new LocalTime(14, 15, 12).PlusNanoseconds(123456789); Assert.AreEqual(new OffsetDate(date, offset).At(time), date.At(time).WithOffset(offset)); }
public void At() { LocalDate date = new LocalDate(2010, 6, 16); LocalTime time = new LocalTime(16, 20); LocalDateTime dateTime = Snippet.For(date.At(time)); Assert.AreEqual(new LocalDateTime(2010, 6, 16, 16, 20, 0), dateTime); }
public void CombinationWithTime() { // Test all three approaches in the same test - they're logically equivalent. var calendar = CalendarSystem.Julian; LocalDate date = new LocalDate(2014, 3, 28, calendar); LocalTime time = new LocalTime(20, 17, 30); LocalDateTime expected = new LocalDateTime(2014, 3, 28, 20, 17, 30, calendar); Assert.AreEqual(expected, date + time); Assert.AreEqual(expected, date.At(time)); Assert.AreEqual(expected, time.On(date)); }
public void CombinationWithTime() { // Test all three approaches in the same test - they're logically equivalent. var calendar = CalendarSystem.GetJulianCalendar(4); LocalDate date = new LocalDate(2014, 3, 28, calendar); LocalTime time = new LocalTime(20, 17, 30); LocalDateTime expected = new LocalDateTime(2014, 3, 28, 20, 17, 30, calendar); Assert.AreEqual(expected, date + time); Assert.AreEqual(expected, date.At(time)); Assert.AreEqual(expected, time.On(date)); }
/// <summary> /// Returns a <see cref="LocalDateTime"/> pair representing the start and end date time of an interval /// that starts on <paramref name="date"/> at <paramref name="startTime"/> and ends the next time it is <paramref name="endTime"/>. /// </summary> /// <param name="date">The local date.</param> /// <param name="startTime">The start time.</param> /// <param name="endTime">The end time. If <paramref name="endTime"/> is less than or equal to <paramref name="startTime"/>, <paramref name="endTime"/> is considered to be on the following day.</param> /// <returns>The start and end date time of the interval.</returns> public static NullableDateTimeInterval At(this LocalDate date, LocalTime?startTime, LocalTime?endTime) { var startDateTime = startTime.HasValue ? date.At(startTime.Value) : default(LocalDateTime? ); var endDate = endTime <= startTime || endTime.HasValue && endTime.Value == LocalTime.Midnight ? date.NextDay() : date; var endDateTime = endTime.HasValue ? endDate.At(endTime.Value) : default(LocalDateTime? ); return(new NullableDateTimeInterval { StartDateTime = startDateTime, EndDateTime = endDateTime }); }
public async Task TestDividend() { var symbol = "AAPL"; var tz = "America/New_York".ToTimeZone(); var date = new LocalDate(2020, 2, 7); var instant = date.At(new LocalTime(16, 0)).InZoneStrictly(tz).ToInstant(); IList <DividendTick>?list = await YahooHistory .FromDate(instant) .GetDividendsAsync(symbol); var dividend = list?[0].Dividend; Assert.Equal(0.77m, dividend); }
/// <summary> /// Get the ShiftInstance for the specified day /// </summary> /// <param name="day">Date</param> /// <returns>Shift instance</returns> public ShiftInstance GetShiftInstanceForDay(LocalDate day) { ShiftInstance instance = null; Rotation shiftRotation = Rotation; int dayInRotation = GetDayInRotation(day); // shift or off shift TimePeriod period = shiftRotation.GetPeriods()[dayInRotation - 1]; if (period.IsWorkingPeriod()) { LocalDateTime startDateTime = day.At(period.StartTime); instance = new ShiftInstance((Shift)period, startDateTime, this); } return(instance); }
public void MoreGranularDeconstruction() { var date = new LocalDate(2017, 10, 15); var time = new LocalTime(21, 30, 15); var offset = Offset.FromHours(-2); var value = new OffsetDateTime(date.At(time), offset); var(actualDate, actualTime, actualOffset) = value; Assert.Multiple(() => { Assert.AreEqual(date, actualDate); Assert.AreEqual(time, actualTime); Assert.AreEqual(offset, actualOffset); }); }
public async Task TestFrequencyWeekly() { var timeZone = DateTimeZoneProviders.Tzdb.GetZoneOrNull("America/New_York"); var startDate = new LocalDate(2019, 1, 7); var zdt = startDate.At(new LocalTime(16, 0, 0)).InZoneStrictly(timeZone !); var yahooQuotes = new YahooQuotesBuilder(Logger) .SetPriceHistoryFrequency(Frequency.Weekly) .HistoryStarting(zdt.ToInstant()) .Build(); var security = await yahooQuotes.GetAsync("AAPL", HistoryFlags.PriceHistory); var ticks = security !.PriceHistory.Value; Assert.Equal(startDate, ticks[0].Date); // previous Monday Assert.Equal(39.20, ticks[1].Close, 2); }
public async Task TestSplit() { var symbol = "AAPL"; var tz = "America/New_York".ToTimeZone(); var date = new LocalDate(2014, 6, 9); var instant = date.At(new LocalTime(16, 0)).InZoneStrictly(tz).ToInstant(); IList <SplitTick>?splits = await YahooHistory .FromDate(instant) .GetSplitsAsync(symbol); if (splits == null) { throw new NullReferenceException("Invalid symbol"); } Assert.Equal(1, splits[0].BeforeSplit); Assert.Equal(7, splits[0].AfterSplit); }
public async Task TestFrequencyMonthly() { var timeZone = DateTimeZoneProviders.Tzdb.GetZoneOrNull("America/New_York"); var startDate = new LocalDate(2019, 2, 1); var zdt = startDate.At(new LocalTime(16, 0, 0)).InZoneStrictly(timeZone !); var yahooQuotes = new YahooQuotesBuilder(Logger) .SetPriceHistoryFrequency(Frequency.Monthly) .HistoryStarting(zdt.ToInstant()) .Build(); var security = await yahooQuotes.GetAsync("AAPL", HistoryFlags.PriceHistory); var ticks = security !.PriceHistory.Value; foreach (var tick in ticks) { Write($"{tick.Date} {tick.Close}"); } Assert.Equal(startDate, ticks[0].Date); // next start of month! Assert.Equal(47.49, ticks[1].Close, 2); }
public LocalInterval ToLocalInterval(LocalDate date) => new LocalInterval(date.At(Start), date.At(End));
// TODO: Test /// <summary> /// Returns a <see cref="DateTimeInterval"/> that starts on <paramref name="date"/> at <paramref name="timeInterval"/>'s start and ends the next time it is <paramref name="timeInterval"/>'s end. /// </summary> /// <param name="date">The local date.</param> /// <param name="timeInterval">The time interval.</param> /// <returns>The date time interval.</returns> public static DateTimeInterval At(this LocalDate date, TimeInterval timeInterval) => date.At(timeInterval.Start, timeInterval.End);
/// <summary> /// Returns a <see cref="DateTimeInterval"/> that starts on <paramref name="date"/> at <paramref name="startTime"/> and ends the next time it is <paramref name="endTime"/>. /// </summary> /// <param name="date">The local date.</param> /// <param name="startTime">The start time.</param> /// <param name="endTime">The end time. If <paramref name="endTime"/> is less than or equal to <paramref name="startTime"/>, <paramref name="endTime"/> is considered to be on the following day.</param> /// <returns>The date time interval.</returns> public static DateTimeInterval At(this LocalDate date, LocalTime startTime, LocalTime endTime) { var endDate = startTime < endTime ? date : date.NextDay(); return(new DateTimeInterval(date.At(startTime), endDate.At(endTime))); }
public static LocalDateTime At(this LocalDate localDate, int hour, int minute, int second) => localDate.At(new LocalTime(hour, minute, second));
/// <summary> /// Combines the <see cref="LocalDate"/> with the time at the given hour, minute, second and millisecond. /// </summary> /// <param name="date">The date.</param> /// <param name="hour">The hour of day.</param> /// <param name="minute">The minute of the hour.</param> /// <param name="second">The second of the minute.</param> /// <param name="millisecond">The millisecond of the second.</param> /// <exception cref="System.ArgumentOutOfRangeException">The parameters do not form a valid time.</exception> /// <returns>The resulting date time.</returns> public static LocalDateTime At(this LocalDate date, int hour, int minute, int second = 0, int millisecond = 0) => date.At(new LocalTime(hour, minute, second, millisecond));
private void TestShiftInstances(WorkSchedule ws, LocalDate instanceReference) { Rotation rotation = ws.Teams[0].Rotation; // shift instances LocalDate startDate = instanceReference; LocalDate endDate = instanceReference.PlusDays(rotation.GetDuration().Days); long days = TimePeriod.DeltaDays(instanceReference, endDate) + 1; LocalDate day = startDate; for (long i = 0; i < days; i++) { List <ShiftInstance> instances = ws.GetShiftInstancesForDay(day); foreach (ShiftInstance instance in instances) { int isBefore = instance.StartDateTime.CompareTo(instance.GetEndTime()); Assert.IsTrue(isBefore < 0); Assert.IsTrue(instance.Shift != null); Assert.IsTrue(instance.Team != null); Shift shift = instance.Shift; LocalTime startTime = shift.StartTime; LocalTime endTime = shift.GetEnd(); Assert.IsTrue(shift.IsInShift(startTime)); Assert.IsTrue(shift.IsInShift(startTime.PlusSeconds(1))); Duration shiftDuration = instance.Shift.Duration; // midnight is special case if (!shiftDuration.Equals(Duration.FromHours(24))) { Assert.IsFalse(shift.IsInShift(startTime.PlusSeconds(-1))); } Assert.IsTrue(shift.IsInShift(endTime)); Assert.IsTrue(shift.IsInShift(endTime.PlusSeconds(-1))); if (!shiftDuration.Equals(Duration.FromHours(24))) { Assert.IsFalse(shift.IsInShift(endTime.PlusSeconds(1))); } LocalDateTime ldt = day.At(startTime); Assert.IsTrue(ws.GetShiftInstancesForTime(ldt).Count > 0); ldt = day.At(startTime.PlusSeconds(1)); Assert.IsTrue(ws.GetShiftInstancesForTime(ldt).Count > 0); ldt = day.At(startTime.PlusSeconds(-1)); foreach (ShiftInstance si in ws.GetShiftInstancesForTime(ldt)) { if (!shiftDuration.Equals(Duration.FromHours(24))) { Assert.IsFalse(shift.Name.Equals(si.Shift.Name)); } } ldt = day.At(endTime); Assert.IsTrue(ws.GetShiftInstancesForTime(ldt).Count > 0); ldt = day.At(endTime.PlusSeconds(-1)); Assert.IsTrue(ws.GetShiftInstancesForTime(ldt).Count > 0); ldt = day.At(endTime.PlusSeconds(1)); foreach (ShiftInstance si in ws.GetShiftInstancesForTime(ldt)) { if (!shiftDuration.Equals(Duration.FromHours(24))) { Assert.IsFalse(shift.Name.Equals(si.Shift.Name)); } } } day = day.PlusDays(1); } }
public void TestTeamWorkingTime2() { schedule = new WorkSchedule("4 Team Plan", "test schedule"); // Day shift #1, starts at 07:00 for 15.5 hours Shift crossover = schedule.CreateShift("Crossover", "Day shift #1 cross-over", new LocalTime(7, 0, 0), Duration.FromHours(15).Plus(Duration.FromMinutes(30))); // Day shift #2, starts at 07:00 for 14 hours Shift day = schedule.CreateShift("Day", "Day shift #2", new LocalTime(7, 0, 0), Duration.FromHours(14)); // Night shift, starts at 22:00 for 14 hours Shift night = schedule.CreateShift("Night", "Night shift", new LocalTime(22, 0, 0), Duration.FromHours(14)); // Team 4-day rotation Rotation rotation = new Rotation("4 Team", "4 Team"); rotation.AddSegment(day, 1, 0); rotation.AddSegment(crossover, 1, 0); rotation.AddSegment(night, 1, 1); Team team1 = schedule.CreateTeam("Team 1", "First team", rotation, referenceDate); // partial in Day 1 LocalTime am7 = new LocalTime(7, 0, 0); LocalDate testStart = referenceDate.PlusDays(rotation.GetDayCount()); LocalDateTime from = testStart.At(am7); LocalDateTime to = testStart.At(am7.PlusHours(1)); //------------------------------------------------------------------ // from first day in rotation for Team1 from = testStart.At(LocalTime.Midnight); to = testStart.At(LocalTime.MaxValue); Duration duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(14))); to = testStart.PlusDays(1).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(29).Plus(Duration.FromMinutes(30)))); to = testStart.PlusDays(2).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(31).Plus(Duration.FromMinutes(30)))); to = testStart.PlusDays(3).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(43).Plus(Duration.FromMinutes(30)))); to = testStart.PlusDays(4).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(57).Plus(Duration.FromMinutes(30)))); to = testStart.PlusDays(5).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(73))); to = testStart.PlusDays(6).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(75))); to = testStart.PlusDays(7).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(87))); // from third day in rotation for Team1 from = testStart.PlusDays(2).At(LocalTime.Midnight); to = testStart.PlusDays(2).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(2))); to = testStart.PlusDays(3).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(14))); to = testStart.PlusDays(4).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(28))); to = testStart.PlusDays(5).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(43).Plus(Duration.FromMinutes(30)))); to = testStart.PlusDays(6).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(45).Plus(Duration.FromMinutes(30)))); }
public void TestNonWorkingTime() { schedule = new WorkSchedule("Non Working Time", "Test non working time"); LocalDate date = new LocalDate(2017, 1, 1); LocalTime time = new LocalTime(7, 0, 0); NonWorkingPeriod period1 = schedule.CreateNonWorkingPeriod("Day1", "First test day", date.At(LocalTime.Midnight), Duration.FromHours(24)); NonWorkingPeriod period2 = schedule.CreateNonWorkingPeriod("Day2", "First test day", date.PlusDays(7).At(time), Duration.FromHours(24)); LocalDateTime from = date.At(time); LocalDateTime to = date.At(time.PlusHours(1)); // case #1 Duration duration = schedule.CalculateNonWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(1))); // case #2 from = date.Minus(Period.FromDays(1)).At(time); to = date.PlusDays(1).At(time); duration = schedule.CalculateNonWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(24))); // case #3 from = date.Minus(Period.FromDays(1)).At(time); to = date.Minus(Period.FromDays(1)).At(time.PlusHours(1)); duration = schedule.CalculateNonWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(0))); // case #4 from = date.PlusDays(1).At(time); to = date.PlusDays(1).At(time.PlusHours(1)); duration = schedule.CalculateNonWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(0))); // case #5 from = date.Minus(Period.FromDays(1)).At(time); to = date.At(time); duration = schedule.CalculateNonWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(7))); // case #6 from = date.At(time); to = date.PlusDays(1).At(time); duration = schedule.CalculateNonWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(17))); // case #7 from = date.At(LocalTime.Noon); to = date.PlusDays(7).At(LocalTime.Noon); duration = schedule.CalculateNonWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(17))); // case #8 from = date.Minus(Period.FromDays(1)).At(LocalTime.Noon); to = date.PlusDays(8).At(LocalTime.Noon); duration = schedule.CalculateNonWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(48))); // case #9 schedule.DeleteNonWorkingPeriod(period1); schedule.DeleteNonWorkingPeriod(period2); from = date.At(time); to = date.At(time.PlusHours(1)); // case #10 duration = schedule.CalculateNonWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(0))); Duration shiftDuration = Duration.FromHours(8); LocalTime shiftStart = new LocalTime(7, 0, 0); Shift shift = schedule.CreateShift("Work Shift1", "Working time shift", shiftStart, shiftDuration); Rotation rotation = new Rotation("Case 10", "Case10"); rotation.AddSegment(shift, 1, 1); LocalDate startRotation = new LocalDate(2017, 1, 1); Team team = schedule.CreateTeam("Team", "Team", rotation, startRotation); team.RotationStart = startRotation; period1 = schedule.CreateNonWorkingPeriod("Day1", "First test day", date.At(LocalTime.Midnight), Duration.FromHours(24)); LocalDate mark = date.PlusDays(rotation.GetDayCount()); from = mark.At(time.Minus(Period.FromHours(2))); to = mark.At(time.Minus(Period.FromHours(1))); // case #11 duration = schedule.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(0))); // case #12 from = date.At(shiftStart); to = date.At(time.PlusHours(8)); duration = schedule.CalculateNonWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(8))); }