public static Ticker BuildTicker(CountdownItem item) { DateTime eventDate = new DateTime(item.EventDateTime.Ticks, DateTimeKind.Local); Ticker myTicker = new Ticker(); LocalDateTime fromDate = new LocalDateTime(); LocalDateTime toDate = new LocalDateTime(); //Period timePeriod = null; PeriodBuilder pendingPeriod = new PeriodBuilder(); //TimeSpan timeElapsed = timePeriod; switch (item.Type) { case "Countdown": if (eventDate > DateTime.Now) { // Future Date //timeElapsed = new TimeSpan(eventDate.Subtract(DateTime.Now).Ticks); fromDate = GetLocalDateTimeFromDateTime(DateTime.Now); toDate = GetLocalDateTimeFromDateTime(eventDate); myTicker.Phrase = "Until " + item.EventName; } else { // Past Date //timeElapsed = new TimeSpan(DateTime.Now.Subtract(eventDate).Ticks); //timePeriod = Period.Between(GetLocalDateTimeFromDateTime(DateTime.Now), GetLocalDateTimeFromDateTime(eventDate)); fromDate = GetLocalDateTimeFromDateTime(DateTime.Now); toDate = GetLocalDateTimeFromDateTime(eventDate); myTicker.Phrase = "Since " + item.EventName; } break; case "Anniversary": if (eventDate > DateTime.Now) { // Future Date //timeElapsed = new TimeSpan(eventDate.AddYears(1).Subtract(DateTime.Now).Ticks); //timePeriod = Period.Between(GetLocalDateTimeFromDateTime(DateTime.Now), GetLocalDateTimeFromDateTime(eventDate.AddYears(1))); fromDate = GetLocalDateTimeFromDateTime(DateTime.Now); toDate = GetLocalDateTimeFromDateTime(eventDate.AddYears(1)); myTicker.Phrase = "Until " + item.EventName + " 1st Anniversary"; } else { // Past Date int n = 1; while (eventDate.AddYears(n) < DateTime.Now) { n++; } //timeElapsed = new TimeSpan(eventDate.AddYears(n).Subtract(DateTime.Now).Ticks); //timePeriod = Period.Between(GetLocalDateTimeFromDateTime(DateTime.Now), GetLocalDateTimeFromDateTime(eventDate.AddYears(n))); fromDate = GetLocalDateTimeFromDateTime(DateTime.Now); toDate = GetLocalDateTimeFromDateTime(eventDate.AddYears(n)); myTicker.Phrase = "Until " + item.EventName + " " + n + "th Anniversary"; } break; } //timePeriod. //timePeriod = Period.Between(fromDate, toDate); //timePeriod. // (item.YearFlag)?PeriodUnits.Years|: "" // (item.YearFlag)?PeriodUnits.Months:| // (item.YearFlag)?PeriodUnits.Weeks:| // (item.YearFlag)?PeriodUnits.Days:| // (item.YearFlag)?PeriodUnits.Hours:| // (item.YearFlag)?PeriodUnits.Minutes:| // (item.YearFlag)?PeriodUnits.Seconds:| // PeriodUnits.None // ); //timePeriod = pendingPeriod = new PeriodBuilder(Period.Between(fromDate, toDate, ((item.YearFlag) ? PeriodUnits.Years : PeriodUnits.None) | ((item.MonthFlag) ? PeriodUnits.Months : PeriodUnits.None) | ((item.WeekFlag) ? PeriodUnits.Weeks : PeriodUnits.None) | ((item.DayFlag) ? PeriodUnits.Days : PeriodUnits.None) | ((item.HourFlag) ? PeriodUnits.Hours : PeriodUnits.None) | ((item.MinuteFlag) ? PeriodUnits.Minutes : PeriodUnits.None) | ((item.SecondFlag || item.HeartbeatFlag) ? PeriodUnits.Seconds : PeriodUnits.None) | PeriodUnits.None )); if (item.YearFlag) { ////int yy = Period.Between(fromDate, toDate, PeriodUnits.Years).Years; myTicker.Year = pendingPeriod.Years.ToString("00"); //pendingPeriod.Years = 0; //fromDate.PlusYears(1); } if (item.MonthFlag) { //pendingPeriod = new PeriodBuilder(Period.Between(fromDate, toDate, PeriodUnits.Months)); myTicker.Month = pendingPeriod.Months.ToString("00"); //pendingPeriod.Months = 0; } if (item.WeekFlag) { //pendingPeriod = new PeriodBuilder(Period.Between(fromDate, toDate, PeriodUnits.Years)); myTicker.Week = pendingPeriod.Weeks.ToString("00"); //pendingPeriod.Weeks = 0; } if (item.DayFlag) { //pendingPeriod = new PeriodBuilder(Period.Between(fromDate, toDate, PeriodUnits.Years)); myTicker.Day = pendingPeriod.Days.ToString("00"); //pendingPeriod.Days = 0; } if (item.HourFlag) { //pendingPeriod = new PeriodBuilder(Period.Between(fromDate, toDate, PeriodUnits.Years)); myTicker.Hour = pendingPeriod.Hours.ToString("00"); //pendingPeriod.Hours = 0; } if (item.MinuteFlag) { //pendingPeriod = new PeriodBuilder(Period.Between(fromDate, toDate, PeriodUnits.Years)); myTicker.Minute = pendingPeriod.Minutes.ToString("00"); //pendingPeriod.Minutes = 0; } if (item.SecondFlag) { //pendingPeriod = new PeriodBuilder(Period.Between(fromDate, toDate, PeriodUnits.Years)); myTicker.Second = pendingPeriod.Seconds.ToString("00"); } if (item.HeartbeatFlag) { //pendingPeriod = new PeriodBuilder(Period.Between(fromDate, toDate, PeriodUnits.Years)); myTicker.Heartbeat = (pendingPeriod.Seconds * item.HeartbeatCount/60).ToString("00"); } myTicker.YearFlag = item.YearFlag; myTicker.MonthFlag = item.MonthFlag; myTicker.WeekFlag = item.WeekFlag; myTicker.DayFlag = item.DayFlag; myTicker.HourFlag = item.HourFlag; myTicker.MinuteFlag = item.MinuteFlag; myTicker.SecondFlag = item.SecondFlag; myTicker.HeartbeatFlag = item.HeartbeatFlag; return myTicker; }
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(); }