public void Normalize_AllNegative() { var original = new PeriodBuilder { Hours = -1, Minutes = -119, Seconds = -150 }.Build(); var normalized = original.Normalize(); var expected = new PeriodBuilder { Hours = -3, Minutes = -1, Seconds = -30 }.Build(); Assert.AreEqual(expected, normalized); }
public void Plus_FullPeriod() { LocalDateTime start = new LocalDateTime(2011, 4, 2, 12, 15, 8); var period = new PeriodBuilder { Years = 1, Months = 2, Weeks = 3, Days = 4, Hours = 5, Minutes = 6, Seconds = 7, Milliseconds = 8, Ticks = 9 }.Build(); var actual = start.Plus(period); var expected = new LocalDateTime(2012, 6, 27, 17, 21, 15, 8, 9); Assert.AreEqual(expected, actual); }
public void Normalize_Nanoseconds() { var original = new PeriodBuilder { Ticks = 1, Nanoseconds = 150 }.Build(); var normalized = original.Normalize(); var expected = new PeriodBuilder { Nanoseconds = 250 }.Build(); Assert.AreEqual(expected, normalized); }
public void Normalize_MixedSigns_NegativeResult() { var original = new PeriodBuilder { Hours = 1, Minutes = -121 }.Build(); var normalized = original.Normalize(); var expected = new PeriodBuilder { Hours = -1, Minutes = -1 }.Build(); Assert.AreEqual(expected, normalized); }
public void Normalize_MultipleFields() { var original = new PeriodBuilder { Hours = 1, Minutes = 119, Seconds = 150 }.Build(); var normalized = original.Normalize(); var expected = new PeriodBuilder { Hours = 3, Minutes = 1, Seconds = 30 }.Build(); Assert.AreEqual(expected, normalized); }
public void Normalize_Seconds() { var original = new PeriodBuilder { Minutes = 1, Seconds = 150 }.Build(); var normalized = original.Normalize(); var expected = new PeriodBuilder { Minutes = 3, Seconds = 30 }.Build(); Assert.AreEqual(expected, normalized); }
public void Normalize_Ticks() { var original = new PeriodBuilder { Milliseconds = 1, Ticks = 15000 }.Build(); var normalized = original.Normalize(); var expected = new PeriodBuilder { Milliseconds = 2, Ticks = 0, Nanoseconds = 500000 }.Build(); Assert.AreEqual(expected, normalized); }
public void Normalize_Hours() { var original = new PeriodBuilder { Hours = 25, Days = 1 }.Build(); var normalized = original.Normalize(); var expected = new PeriodBuilder { Hours = 1, Days = 2 }.Build(); Assert.AreEqual(expected, normalized); }
public void Normalize_Weeks() { var original = new PeriodBuilder { Weeks = 2, Days = 5 }.Build(); var normalized = original.Normalize(); var expected = new PeriodBuilder { Days = 19 }.Build(); Assert.AreEqual(expected, normalized); }
public virtual DurationFormatter WithLocale(String localeName_0) { if (!localeName_0.Equals(this.localeName)) { PeriodFormatter newFormatter = formatter.WithLocale(localeName_0); PeriodBuilder newBuilder = builder.WithLocale(localeName_0); DateFormatter newFallback = (fallback == null) ? null : fallback .WithLocale(localeName_0); return(new BasicDurationFormatter(newFormatter, newBuilder, newFallback, fallbackLimit, localeName_0, timeZone)); } return(this); }
public void Minus_FullPeriod() { // Period deliberately chosen to require date rollover LocalDateTime start = new LocalDateTime(2011, 4, 2, 12, 15, 8); var period = new PeriodBuilder { Years = 1, Months = 2, Weeks = 3, Days = 4, Hours = 15, Minutes = 6, Seconds = 7, Milliseconds = 8, Ticks = 9, Nanoseconds = 11 }.Build(); var actual = start.Minus(period); var expected = new LocalDateTime(2010, 1, 7, 21, 9, 0).PlusNanoseconds(991999089L); Assert.AreEqual(expected, actual, $"{expected:yyyy-MM-dd HH:mm:ss.fffffffff} != {actual:yyyy-MM-dd HH:mm:ss.fffffffff}"); }
static void Main() { Period p1 = new PeriodBuilder { Years = 3, Months = 10 }.Build(); Period p2 = new PeriodBuilder { Years = 5, Months = -12 }.Build(); Period sum = p1 + p2; Period normalized = NormalizeYearsAndMonths(sum); Console.WriteLine($"{normalized.Years} years; {normalized.Months} months"); }
public void Interval() { // Note: PG interval has microsecond precision, another under that is lost. var expected = new PeriodBuilder { Years = 1, Months = 2, Weeks = 3, Days = 4, Hours = 5, Minutes = 6, Seconds = 7, Milliseconds = 8, Nanoseconds = 9000 }.Build().Normalize(); var timeSpan = new TimeSpan(445, (int)expected.Hours, (int)expected.Minutes, (int)expected.Seconds, (int)expected.Milliseconds).Add(TimeSpan.FromTicks(90)); using var conn = OpenConnection(); using (var cmd = new NpgsqlCommand("SELECT @p1, @p2, @p3, @p4", conn)) { cmd.Parameters.Add(new NpgsqlParameter("p1", NpgsqlDbType.Interval) { Value = expected }); cmd.Parameters.AddWithValue("p2", expected); cmd.Parameters.Add(new NpgsqlParameter("p3", NpgsqlDbType.Interval) { Value = timeSpan }); cmd.Parameters.AddWithValue("p4", timeSpan); using (var reader = cmd.ExecuteReader()) { reader.Read(); for (var i = 0; i < 2; i++) { Assert.That(reader.GetFieldType(i), Is.EqualTo(typeof(Period))); Assert.That(reader.GetFieldValue <Period>(i), Is.EqualTo(expected)); Assert.That(reader.GetValue(i), Is.EqualTo(expected)); Assert.That(() => reader.GetTimeSpan(i), Is.EqualTo(timeSpan)); Assert.That(reader.GetFieldValue <TimeSpan>(i), Is.EqualTo(timeSpan)); } for (var i = 2; i < 4; i++) { Assert.That(() => reader.GetTimeSpan(i), Is.EqualTo(timeSpan)); Assert.That(reader.GetFieldValue <TimeSpan>(i), Is.EqualTo(timeSpan)); } } } }
public void Build() { PeriodBuilder periodBuilder = new PeriodBuilder() { Years = 2, Months = 3, Days = 4 }; Period period = Snippet.For(periodBuilder.Build()); Assert.AreEqual(2, period.Years); Assert.AreEqual(3, period.Months); Assert.AreEqual(4, period.Days); }
public void BuildPeriod() { var period = Period.FromHours(5); period = Period.FromHours(5) + Period.FromMinutes(3); Period built = new PeriodBuilder() { Hours = 5, Minutes = 3 }.Build(); Assert.AreEqual(0, built.Seconds); Assert.AreEqual(period, built); }
public void BuilderPattern() { Period period = Period.FromHours(5); period = Period.FromHours(5) + Period.FromMinutes(3); Period built = new PeriodBuilder { Hours = 5, Minutes = 3 }.Build(); Assert.AreEqual(0, built.Seconds); Assert.AreEqual(period, built); var shyPeriod = new ShyPeriod.Builder { }.Build(); }
public void BuildSeriesShouldCreateMonthlyValueOneYearIntervalsTest() { // arrange var period = PeriodBuilder.ThisYear(); const SeriesResolution resolution = SeriesResolution.Month; // act var actual = SeriesValueListBuilder.BuildSeries(period.From, period.To, resolution); // assert Assert.AreEqual(DateTime.Now.Month, actual.Count); Assert.AreEqual(1, actual.First().Begin.Day); Assert.AreEqual(DateTime.Now.Day, actual.Last().End.Day); Data2Console(actual); }
public void BuildSeriesShouldCreateDailyValueIntervalsTest() { // arrange var period = PeriodBuilder.ThisWeek(); const SeriesResolution resolution = SeriesResolution.Day; // act var actual = SeriesValueListBuilder.BuildSeries(period.From, period.To, resolution); // assert Assert.AreEqual(Math.Round(period.Span.TotalDays + 0.5), actual.Count); Assert.AreEqual(0, actual[0].Begin.Minute); Assert.AreEqual(0, actual.Last().End.Minute); Data2Console(actual); }
/// <summary> /// Return a formatter based on this factory's current settings. /// </summary> /// /// <returns>a BasicDurationFormatter</returns> public virtual DurationFormatter GetFormatter() { if (f == null) { if (fallback != null) { fallback = fallback.WithLocale(localeName).WithTimeZone( timeZone); } formatter = GetPeriodFormatter().WithLocale(localeName); builder = GetPeriodBuilder().WithLocale(localeName).WithTimeZone( timeZone); f = CreateFormatter(); } return(f); }
public void Period_EqualStartAndEnd() { // Arrange var time = Fixture.Create <LocalTime>(); var interval = new TimeInterval(time, time); var expected = new PeriodBuilder { Days = 1 }.Build(); // Act var period = interval.Period; // Assert period.Should().Be(expected); }
public void BetweenYearMonth_SingleUnit(string startText, string endText, PeriodUnits units, int expectedValue) { var start = YearMonthPattern.Iso.Parse(startText).Value; var end = YearMonthPattern.Iso.Parse(endText).Value; var forward = Period.Between(start, end, units); var expectedForward = new PeriodBuilder { [units] = expectedValue }.Build(); Assert.AreEqual(expectedForward, forward); var backward = Period.Between(end, start, units); var expectedBackward = new PeriodBuilder { [units] = -expectedValue }.Build(); Assert.AreEqual(expectedBackward, backward); }
public static void CalculateKosmorDate(ZonedDateTime currentDate) { LocalDateTime localTime = new LocalDateTime(2004, 2, 1, 0, 0); ZonedDateTime startDate = new ZonedDateTime(localTime, DateTimeZone.Utc, Offset.Zero); int kosmorDay = 6; int kosmorYear = 3500; PeriodBuilder pb = new PeriodBuilder(); pb.Milliseconds = MILLISECONDS_IN_A_DAY; Period period = pb.Build(); ZonedDateTime date = currentDate.Minus(period.ToDuration()); while (startDate < currentDate) { startDate = startDate.Plus(period.ToDuration()); if (currentDate.Calendar.IsLeapYear(currentDate.Year)) { if (kosmorDay > 366) { kosmorDay = 1; kosmorYear++; } else { kosmorDay++; } } else if (kosmorDay > 365) { kosmorDay = 1; kosmorYear++; } else { kosmorDay++; } } String formattedKosmorDay = kosmorDay.ToString("#000"); kosmorDate = kosmorYear + "_" + formattedKosmorDay; }
public void SumTest() { decimal expected; decimal actual; using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext)) { target.SetValue(DateTime.Today.AddDays(-1), 50, 5, 2); target.SetValue(DateTime.Today.AddDays(-1).AddMinutes(120), 100, 5, 2); var period = PeriodBuilder.Yesterday(); var begin = period.From; var end = period.To; expected = 10000; // ((100 / 5) * 1000) - ((50 / 5) * 1000) actual = target.Sum(begin, end, Unit.kWh); } Assert.AreEqual(expected, actual); }
public void BetweenLocalDateTimes_NotInt64Representable() { LocalDateTime start = new LocalDateTime(-5000, 1, 1, 0, 1, 2, 123); LocalDateTime end = new LocalDateTime(9000, 1, 1, 1, 2, 3, 456); Assert.False((end.ToLocalInstant().TimeSinceLocalEpoch - start.ToLocalInstant().TimeSinceLocalEpoch).IsInt64Representable); Period expected = new PeriodBuilder { // 365.2425 * 14000 = 5113395 Hours = 5113395L * 24 + 1, Minutes = 1, Seconds = 1, Milliseconds = 333 }.Build(); Period actual = Period.Between(start, end, PeriodUnits.AllTimeUnits); Assert.AreEqual(expected, actual); }
/// <summary> /// Returns a new period with only the specified units. /// </summary> /// <param name="period">The period whose values should be used.</param> /// <param name="units">The units that should be taken from the given period.</param> /// <returns>A new period with only the specified units.</returns> public static Period OnlyWith(this Period period, PeriodUnits units = PeriodUnits.AllUnits) { var builder = new PeriodBuilder(); if (units.HasFlag(PeriodUnits.Years)) { builder.Years = period.Years; } if (units.HasFlag(PeriodUnits.Months)) { builder.Months = period.Months; } if (units.HasFlag(PeriodUnits.Weeks)) { builder.Weeks = period.Weeks; } if (units.HasFlag(PeriodUnits.Days)) { builder.Days = period.Days; } if (units.HasFlag(PeriodUnits.Hours)) { builder.Hours = period.Hours; } if (units.HasFlag(PeriodUnits.Minutes)) { builder.Minutes = period.Minutes; } if (units.HasFlag(PeriodUnits.Seconds)) { builder.Seconds = period.Seconds; } if (units.HasFlag(PeriodUnits.Milliseconds)) { builder.Milliseconds = period.Milliseconds; } if (units.HasFlag(PeriodUnits.Ticks)) { builder.Ticks = period.Ticks; } return(builder.Build()); }
public static Variable DurationVal(string input) { var durType = DataTypeEnum.DayTimeDuration; var ym = _YM.Match(input); if (ym.Success) { durType = DataTypeEnum.YearMonthDuration; } var durationVal = input; bool neg = false; if (input.StartsWith('-')) { durationVal = input.TrimStart('-'); neg = true; } var dur = PeriodPattern.NormalizingIso.Parse(durationVal); if (dur.Success) { var val = dur.Value; if (neg) { var negVal = new PeriodBuilder { Years = (val.Years > 0) ? val.Years * -1 : 0, Months = (val.Months > 0) ? val.Months * -1 : 0, Weeks = (val.Weeks > 0) ? val.Weeks * -1 : 0, Days = (val.Days > 0) ? val.Days * -1 : 0, Hours = (val.Hours > 0) ? val.Hours * -1 : 0, Minutes = (val.Minutes > 0) ? val.Minutes * -1 : 0, Seconds = (val.Seconds > 0) ? val.Seconds * -1 : 0, Milliseconds = (val.Milliseconds > 0) ? val.Milliseconds * -1 : 0, Ticks = (val.Ticks > 0) ? val.Ticks * -1 : 0 }.Build(); return(Variable.DurationType(negVal, durType)); } return(Variable.DurationType(dur.Value, durType)); } return(new Variable()); }
public override Period Create(ISpecimenContext context) { var days = Random.Next(7); var hoursAndMinutes = LocalTimeGenerator.Create(context); // Avoid zero periods if (days == 0 && hoursAndMinutes == LocalTime.Midnight) { return(Create(context)); } var periodBuilder = new PeriodBuilder { Days = days, Hours = hoursAndMinutes.Hour, Minutes = hoursAndMinutes.Minute }; return(periodBuilder.Build()); }
public void XmlSerialization() { var value = new PeriodBuilder { Years = 10, Days = 5, Hours = 3, Seconds = 20 }; TestHelper.AssertXmlRoundtrip(value, "<value>P10Y5DT3H20S</value>", new BuilderEqualityComparer()); var dateOnly = new PeriodBuilder { Years = 10, Days = 5 }; TestHelper.AssertXmlRoundtrip(dateOnly, "<value>P10Y5D</value>", new BuilderEqualityComparer()); var timeOnly = new PeriodBuilder { Hours = 5, Minutes = 1 }; TestHelper.AssertXmlRoundtrip(timeOnly, "<value>PT5H1M</value>", new BuilderEqualityComparer()); }
public void Period_StartAfterEnd() { // Arrange var start = new LocalTime(15, 17, 35); var end = new LocalTime(08, 59, 24); var interval = new TimeInterval(start, end); var expected = new PeriodBuilder { Hours = 17, Minutes = 41, Seconds = 49 }.Build(); // Act var period = interval.Period; // Assert period.Should().Be(expected); }
public static Variable YearMonthDurationFunction(List <Variable> parameters) { if (parameters.Count == 2) { foreach (var item in parameters) { if (item.ValueType != DataTypeEnum.Date && item.ValueType != DataTypeEnum.DateTime) { throw new FEELException($"Incorrect data types for year month duration function: {item}"); } } LocalDate leftDt = LocalDate.MinIsoValue; LocalDate rightDt = LocalDate.MinIsoValue;; if (parameters[0].ValueType == DataTypeEnum.DateTime) { leftDt = DateTimeToDate(parameters[0]); } if (parameters[0].ValueType == DataTypeEnum.Date) { leftDt = parameters[0].DateVal; } if (parameters[1].ValueType == DataTypeEnum.DateTime) { rightDt = DateTimeToDate(parameters[1]); } if (parameters[1].ValueType == DataTypeEnum.Date) { rightDt = parameters[1].DateVal; } var per = (rightDt - leftDt); var ym = new PeriodBuilder { Years = per.Years, Months = per.Months }.Build(); return(Variable.DurationType(ym, DataTypeEnum.YearMonthDuration)); } throw new FEELException("Failed retrieving a year month duration value to incorrect number of parameters"); }
public void ToString_MultipleUnits() { var period = new PeriodBuilder { Hours = 5, Minutes = 30 }.Build(); Assert.AreEqual("PT5H30M", period.ToString()); }
public void Normalize_Milliseconds() { var original = new PeriodBuilder { Seconds = 1, Milliseconds = 1500 }.Build(); var normalized = original.Normalize(); var expected = new PeriodBuilder { Seconds = 2, Milliseconds= 500 }.Build(); Assert.AreEqual(expected, normalized); }
public void ToBuilder_SingleUnit() { var builder = Period.FromHours(5).ToBuilder(); var expected = new PeriodBuilder { Hours = 5 }.Build(); Assert.AreEqual(expected, builder.Build()); }
public void HasDateComponent_SingleValued(PeriodUnits unit, bool hasDateComponent) { var period = new PeriodBuilder {[unit] = 1 }.Build(); Assert.AreEqual(hasDateComponent, period.HasDateComponent); }
public void ToBuilder_MultipleUnits() { var builder = (Period.FromHours(5) + Period.FromWeeks(2)).ToBuilder(); var expected = new PeriodBuilder { Hours = 5, Weeks = 2 }.Build(); Assert.AreEqual(expected, builder.Build()); }
public void XmlSerialization() { Duration value = new PeriodBuilder { Days = 5, Hours = 3, Minutes = 20, Seconds = 35, Ticks = 1234500 }.Build().ToDuration(); TestHelper.AssertXmlRoundtrip(value, "<value>5:03:20:35.12345</value>"); TestHelper.AssertXmlRoundtrip(-value, "<value>-5:03:20:35.12345</value>"); }
public void Normalize_Nanoseconds() { var original = new PeriodBuilder { Ticks = 1, Nanoseconds = 150 }.Build(); var normalized = original.Normalize(); var expected = new PeriodBuilder { Ticks = 2, Nanoseconds = 50}.Build(); Assert.AreEqual(expected, normalized); }
public void Normalize_ZeroResult() { var original = new PeriodBuilder { Years = 0 }.Build(); Assert.AreEqual(Period.Zero, original.Normalize()); }
public void Normalize_DoesntAffectMonthsAndYears() { var original = new PeriodBuilder { Years = 2, Months = 1, Days = 400 }.Build(); Assert.AreEqual(original, original.Normalize()); }
public void ToDuration_ValidAllAcceptableUnits() { Period period = new PeriodBuilder { Weeks = 1, Days = 2, Hours = 3, Minutes = 4, Seconds = 5, Milliseconds = 6, Ticks = 7 }.Build(); Assert.AreEqual( 1 * NodaConstants.TicksPerWeek + 2 * NodaConstants.TicksPerDay + 3 * NodaConstants.TicksPerHour + 4 * NodaConstants.TicksPerMinute + 5 * NodaConstants.TicksPerSecond + 6 * NodaConstants.TicksPerMillisecond + 7, period.ToDuration().Ticks); }
public void Between_LocalDateTime_AwkwardTimeOfDayWithMultipleUnits() { LocalDateTime start = new LocalDateTime(2014, 1, 1, 16, 0, 0); LocalDateTime end = new LocalDateTime(2015, 2, 3, 8, 0, 0); Period actual = Period.Between(start, end, PeriodUnits.YearMonthDay | PeriodUnits.AllTimeUnits); Period expected = new PeriodBuilder { Years = 1, Months = 1, Days = 1, Hours = 16 }.Build(); Assert.AreEqual(expected, actual); }