public ActionResult AddRehearsalPart(AdminHomeVM vm) { PeriodBuilder builder = new PeriodBuilder(); builder.Minutes = vm.NewRehearsalPart.IntLengthMinutes; vm.NewRehearsalPart.DurLength = builder.Build(); GetDAL get = new GetDAL(); get.OpenConnection(); foreach (var id in vm.NewRehearsalPart.ArrMemberNeededIDs) { Users tmpUser = get.GetUserByID(id); vm.NewRehearsalPart.LstMembers.Add(tmpUser); } get.CloseConnection(); InsertDAL insert = new InsertDAL(); insert.OpenConnection(); int newRehearsalPartID = insert.InsertRehearsalPart(vm.NewRehearsalPart); vm.NewRehearsalPart.IntRehearsalPartID = newRehearsalPartID; foreach (var m in vm.NewRehearsalPart.LstMembers) { AttendancePlanned ap = new AttendancePlanned(vm.NewRehearsalPart, m); insert.InsertAttendancePlanned(ap); } insert.CloseConnection(); return(RedirectToAction("Index")); }
public Data() { Id = Guid.NewGuid(); var milliseconds = SystemClock.Instance.GetCurrentInstant().ToUnixTimeMilliseconds(); Created = Instant.FromUnixTimeMilliseconds(milliseconds).InZone(provider.GetSystemDefault()); CreatedDateTime = Created.LocalDateTime; CreatedDate = CreatedDateTime.Date; CreatedTime = CreatedDateTime.TimeOfDay; CreatedOffsetDateTime = Created.ToOffsetDateTime(); CreatedOffsetTime = CreatedOffsetDateTime.ToOffsetTime(); Duration = Duration.FromHours(29.5); var b = new PeriodBuilder { Years = 1, Months = 10, Weeks = 3, Days = 4, Hours = 25, Minutes = 65, Seconds = 100, Milliseconds = 4, Nanoseconds = 3, Ticks = 3 }; Period = b.Build(); }
public void Indexer_Setter_ValidUnits() { var builder = new PeriodBuilder(); builder[PeriodUnits.Months] = 1; builder[PeriodUnits.Weeks] = 2; builder[PeriodUnits.Days] = 3; builder[PeriodUnits.Hours] = 4; builder[PeriodUnits.Minutes] = 5; builder[PeriodUnits.Seconds] = 6; builder[PeriodUnits.Milliseconds] = 7; builder[PeriodUnits.Ticks] = 8; var expected = new PeriodBuilder { Years = 0, Months = 1, Weeks = 2, Days = 3, Hours = 4, Minutes = 5, Seconds = 6, Milliseconds = 7, Ticks = 8 }.Build(); Assert.AreEqual(expected, builder.Build()); }
public EventSchedule(Event @event, int intMinutesWeekday, int intMinutesWeekend, string strMondayStart, string strTuesdayStart, string strWednesdayStart, string strThursdayStart, string strFridayStart, string strSaturdayStart, string strSundayStart) { Event = @event; PeriodBuilder periodBuilderWeekday = new PeriodBuilder { Minutes = intMinutesWeekday }; PeriodBuilder periodBuilderWeekend = new PeriodBuilder { Minutes = intMinutesWeekend }; PerWeekdayDuration = periodBuilderWeekday.Build(); PerWeekendDuration = periodBuilderWeekend.Build(); var culture = (CultureInfo)CultureInfo.InvariantCulture.Clone(); var pattern = LocalTimePattern.Create("h:mmtt", culture); TmeMondayStart = pattern.Parse(strMondayStart).Value; TmeTuesdayStart = pattern.Parse(strTuesdayStart).Value; TmeWednesdayStart = pattern.Parse(strWednesdayStart).Value; TmeThursdayStart = pattern.Parse(strThursdayStart).Value; TmeFridayStart = pattern.Parse(strFridayStart).Value; TmeSaturdayStart = pattern.Parse(strSaturdayStart).Value; TmeSundayStart = pattern.Parse(strSundayStart).Value; }
public static LocalDate PlusYears(int count) { var periodBuilder = new PeriodBuilder { Years = count }; return(AddToToday(periodBuilder.Build())); }
public static LocalDate MinusYears(int count) { var periodBuilder = new PeriodBuilder { Years = count }; return(SubtractFromToday(periodBuilder.Build())); }
public Period CalculateAge(Planet planet, Duration earthAge) { var years = earthAge.TotalSeconds / planet.YearDuration.TotalSeconds; var pb = new PeriodBuilder(); pb.Years = (int)Math.Round(years); pb.Days = 40; return(pb.Build()); }
public void ConstructionFromPeriod() { Period existingPeriod = Period.FromYears(5); PeriodBuilder periodBuilder = Snippet.For(new PeriodBuilder(existingPeriod)); periodBuilder.Months = 6; Period period = periodBuilder.Build(); Assert.AreEqual(5, period.Years); Assert.AreEqual(6, period.Months); }
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 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; }
/// <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 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()); }
private Period DateStep() { // ReSharper disable PossibleNullReferenceException PeriodBuilder step = (Step + Period.FromTicks(NodaConstants.TicksPerStandardDay >> 1)).Normalize().ToBuilder(); // ReSharper restore PossibleNullReferenceException Debug.Assert(step != null); step.Ticks = 0; step.Milliseconds = 0; step.Seconds = 0; step.Minutes = 0; step.Hours = 0; Period rounded = step.Build(); Debug.Assert(rounded != null); Debug.Assert(!rounded.HasTimeComponent); // ReSharper disable once AssignNullToNotNullAttribute return(rounded.IsZero() ? TimeHelpers.OneDayPeriod : rounded); }
// TODO(misc): Tidy this up a *lot*. public ParseResult <Period> Parse(string text) { if (text is null) { return(ParseResult <Period> .ArgumentNull("text")); } if (text.Length == 0) { return(ParseResult <Period> .ValueStringEmpty); } ValueCursor valueCursor = new ValueCursor(text); valueCursor.MoveNext(); if (valueCursor.Current != 'P') { return(ParseResult <Period> .MismatchedCharacter(valueCursor, 'P')); } bool inDate = true; PeriodBuilder builder = new PeriodBuilder(); PeriodUnits unitsSoFar = 0; while (valueCursor.MoveNext()) { if (inDate && valueCursor.Current == 'T') { inDate = false; continue; } bool negative = valueCursor.Current == '-'; var failure = valueCursor.ParseInt64 <Period>(out long unitValue); if (failure != null) { return(failure); } if (valueCursor.Length == valueCursor.Index) { return(ParseResult <Period> .EndOfString(valueCursor)); } // Various failure cases: // - Repeated unit (e.g. P1M2M) // - Time unit is in date part (e.g. P5M) // - Date unit is in time part (e.g. PT1D) // - Unit is in incorrect order (e.g. P5D1Y) // - Unit is invalid (e.g. P5J) // - Unit is missing (e.g. P5) PeriodUnits unit; switch (valueCursor.Current) { case 'Y': unit = PeriodUnits.Years; break; case 'M': unit = inDate ? PeriodUnits.Months : PeriodUnits.Minutes; break; case 'W': unit = PeriodUnits.Weeks; break; case 'D': unit = PeriodUnits.Days; break; case 'H': unit = PeriodUnits.Hours; break; case 'S': unit = PeriodUnits.Seconds; break; case ',': case '.': unit = PeriodUnits.Nanoseconds; break; // Special handling below default: return(InvalidUnit(valueCursor, valueCursor.Current)); } if ((unit & unitsSoFar) != 0) { return(RepeatedUnit(valueCursor, valueCursor.Current)); } // This handles putting months before years, for example. Less significant units // have higher integer representations. if (unit < unitsSoFar) { return(MisplacedUnit(valueCursor, valueCursor.Current)); } // The result of checking "there aren't any time units in this unit" should be // equal to "we're still in the date part". if ((unit & PeriodUnits.AllTimeUnits) == 0 != inDate) { return(MisplacedUnit(valueCursor, valueCursor.Current)); } // Seen a . or , which need special handling. if (unit == PeriodUnits.Nanoseconds) { // Check for already having seen seconds, e.g. PT5S0.5 if ((unitsSoFar & PeriodUnits.Seconds) != 0) { return(MisplacedUnit(valueCursor, valueCursor.Current)); } builder.Seconds = unitValue; if (!valueCursor.MoveNext()) { return(ParseResult <Period> .MissingNumber(valueCursor)); } // Can cope with at most 999999999 nanoseconds if (!valueCursor.ParseFraction(9, 9, out int totalNanoseconds, 1)) { return(ParseResult <Period> .MissingNumber(valueCursor)); } // Use whether or not the seconds value was negative (even if 0) // as the indication of whether this value is negative. if (negative) { totalNanoseconds = -totalNanoseconds; } builder.Milliseconds = (totalNanoseconds / NanosecondsPerMillisecond) % MillisecondsPerSecond; builder.Ticks = (totalNanoseconds / NanosecondsPerTick) % TicksPerMillisecond; builder.Nanoseconds = totalNanoseconds % NanosecondsPerTick; if (valueCursor.Current != 'S') { return(ParseResult <Period> .MismatchedCharacter(valueCursor, 'S')); } if (valueCursor.MoveNext()) { return(ParseResult <Period> .ExpectedEndOfString(valueCursor)); } return(ParseResult <Period> .ForValue(builder.Build())); } builder[unit] = unitValue; unitsSoFar |= unit; } if (unitsSoFar == 0) { return(ParseResult <Period> .ForInvalidValue(valueCursor, TextErrorMessages.EmptyPeriod)); } return(ParseResult <Period> .ForValue(builder.Build())); }
public ParseResult <Period> Parse(string text) { if (text is null) { return(ParseResult <Period> .ArgumentNull("text")); } if (text.Length == 0) { return(ParseResult <Period> .ValueStringEmpty); } ValueCursor valueCursor = new ValueCursor(text); valueCursor.MoveNext(); if (valueCursor.Current != 'P') { return(ParseResult <Period> .MismatchedCharacter(valueCursor, 'P')); } bool inDate = true; PeriodBuilder builder = new PeriodBuilder(); PeriodUnits unitsSoFar = 0; while (valueCursor.MoveNext()) { if (inDate && valueCursor.Current == 'T') { inDate = false; continue; } var failure = valueCursor.ParseInt64 <Period>(out long unitValue); if (failure != null) { return(failure); } if (valueCursor.Length == valueCursor.Index) { return(ParseResult <Period> .EndOfString(valueCursor)); } // Various failure cases: // - Repeated unit (e.g. P1M2M) // - Time unit is in date part (e.g. P5M) // - Date unit is in time part (e.g. PT1D) // - Unit is in incorrect order (e.g. P5D1Y) // - Unit is invalid (e.g. P5J) // - Unit is missing (e.g. P5) PeriodUnits unit; switch (valueCursor.Current) { case 'Y': unit = PeriodUnits.Years; break; case 'M': unit = inDate ? PeriodUnits.Months : PeriodUnits.Minutes; break; case 'W': unit = PeriodUnits.Weeks; break; case 'D': unit = PeriodUnits.Days; break; case 'H': unit = PeriodUnits.Hours; break; case 'S': unit = PeriodUnits.Seconds; break; case 's': unit = PeriodUnits.Milliseconds; break; case 't': unit = PeriodUnits.Ticks; break; case 'n': unit = PeriodUnits.Nanoseconds; break; default: return(InvalidUnit(valueCursor, valueCursor.Current)); } if ((unit & unitsSoFar) != 0) { return(RepeatedUnit(valueCursor, valueCursor.Current)); } // This handles putting months before years, for example. Less significant units // have higher integer representations. if (unit < unitsSoFar) { return(MisplacedUnit(valueCursor, valueCursor.Current)); } // The result of checking "there aren't any time units in this unit" should be // equal to "we're still in the date part". if ((unit & PeriodUnits.AllTimeUnits) == 0 != inDate) { return(MisplacedUnit(valueCursor, valueCursor.Current)); } builder[unit] = unitValue; unitsSoFar |= unit; } return(ParseResult <Period> .ForValue(builder.Build())); }
protected static Period RandomPeriod( [CanBeNull] Period minimum, [NotNull] Period maximum, bool hasDate = true, bool hasTime = true) { if (minimum != null) { minimum = minimum.Normalize(); } // ReSharper disable once AssignNullToNotNullAttribute maximum = maximum.Normalize(); // ReSharper disable once PossibleNullReferenceException Period diff = minimum == null ? maximum : (maximum - minimum).Normalize(); PeriodBuilder builder = new PeriodBuilder(); Assert.IsNotNull(diff); bool first = true; if (hasDate) { if (diff.Years != 0) { builder.Years = (long)(diff.Years * Random.NextDouble()); first = false; } if (first && diff.Months != 0) { builder.Months = (long)(diff.Months * Random.NextDouble()); first = false; } else if (!first) { builder.Months = Random.Next(MonthsPerYear); } if (first && diff.Weeks != 0) { builder.Weeks = (long)(diff.Weeks * Random.NextDouble()); first = false; } else if (!first) { builder.Weeks = Random.Next(WeeksPerMonth); } if (first && diff.Days != 0) { builder.Days = (long)(diff.Days * Random.NextDouble()); first = false; } else if (!first) { builder.Days = Random.Next(NodaConstants.DaysPerStandardWeek); } if (minimum != null) { builder.Years += minimum.Years; builder.Months += minimum.Months; builder.Weeks += minimum.Weeks; builder.Days += minimum.Days; } } else { first = !diff.HasDateComponent; } if (hasTime) { if (first && diff.Hours != 0) { builder.Hours = (long)(diff.Hours * Random.NextDouble()); first = false; } else if (!first) { builder.Hours = Random.Next(NodaConstants.HoursPerStandardDay); } if (first && diff.Minutes != 0) { builder.Minutes = (long)(diff.Minutes * Random.NextDouble()); first = false; } else if (!first) { builder.Minutes = Random.Next(NodaConstants.MinutesPerHour); } if (first && diff.Seconds != 0) { builder.Seconds = (long)(diff.Seconds * Random.NextDouble()); first = false; } else if (!first) { builder.Seconds = Random.Next(NodaConstants.SecondsPerMinute); } if (first && diff.Milliseconds != 0) { builder.Milliseconds = (long)(diff.Milliseconds * Random.NextDouble()); first = false; } else if (!first) { builder.Milliseconds = Random.Next(NodaConstants.MillisecondsPerSecond); } if (first && diff.Ticks != 0) { builder.Ticks = (long)(diff.Ticks * Random.NextDouble()); first = false; } else if (!first) { builder.Ticks = Random.Next((int)NodaConstants.TicksPerMillisecond); } if (minimum != null) { builder.Hours += minimum.Hours; builder.Minutes += minimum.Minutes; builder.Seconds += minimum.Seconds; builder.Milliseconds += minimum.Milliseconds; builder.Ticks += minimum.Ticks; } } return(builder.Build().Normalize()); }
public Data(PeriodBuilder builder) : this(builder.Build()) { }
protected static Period PeriodDivideApprox([NotNull] Period period, int divisor, bool hasTime = true) { PeriodBuilder builder = period.Normalize().ToBuilder(); if (!hasTime) { builder.Hours = 0; builder.Minutes = 0; builder.Seconds = 0; builder.Milliseconds = 0; builder.Ticks = 0; } if (builder.Years != 0) { if (builder.Years > divisor) { builder.Years /= divisor; return(builder.Build().Normalize()); } builder.Months += builder.Years * MonthsPerYear; builder.Years = 0; } if (builder.Months != 0) { if (builder.Months > divisor) { builder.Months /= divisor; return(builder.Build().Normalize()); } builder.Weeks += builder.Months * WeeksPerMonth; builder.Months = 0; } if (builder.Weeks != 0) { if (builder.Weeks > divisor) { builder.Weeks /= divisor; return(builder.Build().Normalize()); } builder.Days += builder.Weeks * NodaConstants.DaysPerStandardWeek; builder.Weeks = 0; } if (builder.Days != 0) { if (builder.Days > divisor) { builder.Days /= divisor; return(builder.Build().Normalize()); } builder.Hours += builder.Days * NodaConstants.HoursPerStandardDay; builder.Days = 0; } if (!hasTime) { builder.Days = 1; builder.Hours = 0; return(builder.Build().Normalize()); } if (builder.Hours != 0) { if (builder.Hours > divisor) { builder.Hours /= divisor; return(builder.Build().Normalize()); } builder.Minutes += builder.Hours * NodaConstants.MinutesPerHour; builder.Hours = 0; } if (builder.Minutes != 0) { if (builder.Minutes > divisor) { builder.Minutes /= divisor; return(builder.Build().Normalize()); } builder.Seconds += builder.Minutes * NodaConstants.SecondsPerMinute; builder.Minutes = 0; } if (builder.Seconds != 0) { if (builder.Seconds > divisor) { builder.Seconds /= divisor; return(builder.Build().Normalize()); } builder.Milliseconds += builder.Seconds * NodaConstants.MillisecondsPerSecond; builder.Seconds = 0; } if (builder.Milliseconds != 0) { if (builder.Milliseconds > divisor) { builder.Milliseconds /= divisor; return(builder.Build().Normalize()); } builder.Ticks += builder.Milliseconds * NodaConstants.TicksPerMillisecond; builder.Milliseconds = 0; } builder.Ticks /= divisor; return(builder.Build().Normalize()); }
public Period Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { var str = reader.ReadStringSegmentRaw(); var i = str.Offset; var periodBuilder = new PeriodBuilder(); if (str.Array[i++] != 'P') { Exceptions.ThrowInvalidPeriodFormat(str); } while (str.Array[i] != 'T' && i - str.Offset < str.Count) { var amount = NumberConverter.ReadInt32(str.Array, i, out var readCount); i += readCount; var unit = str.Array[i++]; switch (unit) { case (byte)'Y': periodBuilder.Years = amount; break; case (byte)'M': periodBuilder.Months = amount; break; case (byte)'W': periodBuilder.Weeks = amount; break; case (byte)'D': periodBuilder.Days = amount; break; default: Exceptions.ThrowInvalidPeriodFormat(str); break; } } if (str.Array[i++] == 'T') { while (i - str.Offset < str.Count) { var amount = NumberConverter.ReadInt64(str.Array, i, out var readCount); i += readCount; var unit = str.Array[i++]; switch (unit) { case (byte)'H': periodBuilder.Hours = amount; break; case (byte)'M': periodBuilder.Minutes = amount; break; case (byte)'S': periodBuilder.Seconds = amount; break; default: Exceptions.ThrowInvalidPeriodFormat(str); break; } } } return(periodBuilder.Build()); }