public static TimeSpanWithAccuracy?FromString(string?text, IFormatProvider?formatProvider = null) { if (string.IsNullOrWhiteSpace(text)) { return(null); } string trimmed = text.Trim(); Regex timeRegex = CreateTimeRegexFor(formatProvider); Match match = timeRegex.Match(trimmed); if (match.Success) { string accuracySymbol = match.Groups["AccuracySymbol"].Value; string seconds = match.Groups["Seconds"].Value; string milliseconds = match.Groups["Milliseconds"].Value; TimeSpan timeValue = TimeSpan.FromSeconds(int.Parse(seconds)) + TimeSpan.FromMilliseconds(int.Parse(milliseconds)); TimeAccuracy accuracy = GetAccuracyForSymbol(accuracySymbol); return(new TimeSpanWithAccuracy(timeValue, accuracy)); } throw new FormatException($"Time value '{text}' is invalid. Use format: s.mmm."); }
public TimeSpanWithAccuracy ElapsedSince(RecordedTime other) { Guard.NotNull(other, nameof(other)); TimeSpan softTimeElapsed = SoftwareTimeInUtc - other.SoftwareTimeInUtc; if (HardwareSynchronizedTime != null && other.HardwareSynchronizedTime != null) { TimeSpan hardTimeElapsed = HardwareSynchronizedTime.Value - other.HardwareSynchronizedTime.Value; // When device restarts, it takes at least 4 seconds before it is reconnected. double measureDelta = Math.Abs(softTimeElapsed.TotalSeconds - hardTimeElapsed.TotalSeconds); bool isValidSensorTime = measureDelta < 4 && hardTimeElapsed >= TimeSpan.Zero; if (isValidSensorTime) { TimeAccuracy effectiveAccuracy = Combine(Accuracy, other.Accuracy, true); return(new TimeSpanWithAccuracy(hardTimeElapsed, effectiveAccuracy)); } } TimeAccuracy nonHighAccuracy = Combine(Accuracy, other.Accuracy, false); return(new TimeSpanWithAccuracy(softTimeElapsed, nonHighAccuracy)); }
public RecordedTime Add(TimeSpanWithAccuracy offset) { TimeSpan? hardwareTime = offset.Accuracy == TimeAccuracy.HighPrecision ? HardwareSynchronizedTime + offset.TimeValue : null; TimeAccuracy effectiveAccuracy = Combine(Accuracy, offset.Accuracy, true); DateTime softwareTimeInUtc = SoftwareTimeInUtc + offset.TimeValue; return(new RecordedTime(hardwareTime, softwareTimeInUtc, effectiveAccuracy)); }
public void FormatTimeAsDash_WhenTimeIsNullRegardlessOfAccuracy(TimeAccuracy accuracy) { var sut = new SegmentTimesFormatter(accuracy); var formattedTime = sut.Format(null); Assert.Equal(TimeFormatConstants.DASH, formattedTime); }
[InlineData("-1.00:02:01.999", TimeAccuracy.Hundredths, "−24:02:01.99")] // Actual:<0:01.99> [Fail] public void NegativesTestRegularTimeFormatter(string timespanText, TimeAccuracy timeAccuracy, string expectedTime) { var formatter = new RegularTimeFormatter(timeAccuracy); var time = TimeSpan.Parse(timespanText); var formattedTime = formatter.Format(time); Assert.Equal(expectedTime, formattedTime); }
public void FormatTimeAsDash_WhenTimeIsNullRegardlessOfAccuracy(TimeAccuracy accuracy) { var sut = new PossibleTimeSaveFormatter { Accuracy = accuracy }; var formattedTime = sut.Format(null); Assert.Equal(TimeFormatConstants.DASH, formattedTime); }
public void ReturnZeroWithCorrectAmountOfDecimals_WhenZeroWithAccuracyIsExpected( NullFormat nullFormat, TimeAccuracy accuracy, string expectedConversion) { var sut = new GeneralTimeFormatter { NullFormat = nullFormat, Accuracy = accuracy }; var formattedTime = sut.Format(null); Assert.Equal(expectedConversion, formattedTime); }
private static TimeAccuracy Combine(TimeAccuracy first, TimeAccuracy second, bool allowHighResolutionMeasure) { if (first == TimeAccuracy.UserEdited || second == TimeAccuracy.UserEdited) { return(TimeAccuracy.UserEdited); } if (first == TimeAccuracy.LowPrecision || second == TimeAccuracy.LowPrecision) { return(TimeAccuracy.LowPrecision); } return(allowHighResolutionMeasure ? TimeAccuracy.HighPrecision : TimeAccuracy.LowPrecision); }
public void TestDeltaSplitTimeFormatter(string timespanText, TimeAccuracy timeAccuracy, bool dropDecimals, string expected) { var formatter = new DeltaSplitTimeFormatter(timeAccuracy, dropDecimals: dropDecimals); TimeSpan?time = null; if (timespanText != null) { time = TimeSpan.Parse(timespanText); } string formatted = formatter.Format(time); Assert.AreEqual(expected, formatted); }
[DataRow("-1.00:02:01.999", TimeAccuracy.Hundredths, "−24:02:01.99")] // Actual:<0:01.99> [Fail] public void NegativesTestRegularTimeFormatter(string timespanText, TimeAccuracy timeAccuracy, string expected) { var formatter = new RegularTimeFormatter(timeAccuracy); TimeSpan?time = null; if (timespanText != null) { time = TimeSpan.Parse(timespanText); } string formatted = formatter.Format(time); Assert.AreEqual(expected, formatted); }
public void TestPreciseDeltaFormatter(string timespanText, TimeAccuracy timeAccuracy, string expected) { var formatter = new PreciseDeltaFormatter(timeAccuracy); TimeSpan?time = null; if (timespanText != null) { time = TimeSpan.Parse(timespanText); } string formatted = formatter.Format(time); Assert.AreEqual(expected, formatted); }
public void FormatTimeCorrectly_WhenDecimalsDropped(string timespanText, TimeAccuracy accuracy, bool dropDecimals, string expectedTime) { var sut = new PossibleTimeSaveFormatter { DropDecimals = dropDecimals, Accuracy = accuracy }; var time = TimeSpan.Parse(timespanText); var formattedTime = sut.Format(time); Assert.Equal(expectedTime, formattedTime); }
public void TestRegularSumOfBestTimeFormatter(string timespanText, TimeAccuracy timeAccuracy, string expected) { var formatter = new RegularSumOfBestTimeFormatter(); formatter.Accuracy = timeAccuracy; TimeSpan?time = null; if (timespanText != null) { time = TimeSpan.Parse(timespanText); } string formatted = formatter.Format(time); Assert.AreEqual(expected, formatted); }
public static string Format(double Percentage, TimeAccuracy timeAccuracy) { if (timeAccuracy == TimeAccuracy.Seconds) { return(string.Format("{0}%", (Math.Truncate(Percentage)).ToString())); } else if (timeAccuracy == TimeAccuracy.Tenths) { double tmp = Math.Truncate(Percentage * 10); return(string.Format("{0}%", (tmp / 10).ToString("0.0"))); } else { double tmp = Math.Truncate(Percentage * 100); return(string.Format("{0}%", (tmp / 100).ToString("0.00"))); } }
public void TestPossibleTimeSaveFormatter(string timespanText, TimeAccuracy accuracy, string expected) { var formatter = new PossibleTimeSaveFormatter(); formatter.Accuracy = accuracy; TimeSpan?time = null; if (timespanText != null) { time = TimeSpan.Parse(timespanText); } string formatted = formatter.Format(time); Assert.AreEqual(expected, formatted); }
public void TestTimeAccuracy(string timespanText, string expected, TimeAccuracy accuracy) { var formatter = new GeneralTimeFormatter(); formatter.DigitsFormat = DigitsFormat.SingleDigitMinutes; formatter.Accuracy = accuracy; TimeSpan?time = null; if (timespanText != null) { time = TimeSpan.Parse(timespanText); } string formatted = formatter.Format(time); Assert.AreEqual(expected, formatted); }
public RecordedTime(TimeSpan?hardwareSynchronizedTime, DateTime softwareTimeInUtc, TimeAccuracy?accuracy = null) { if (accuracy == null) { Accuracy = hardwareSynchronizedTime == null ? TimeAccuracy.LowPrecision : TimeAccuracy.HighPrecision; } else if (accuracy == TimeAccuracy.HighPrecision && hardwareSynchronizedTime == null) { Accuracy = TimeAccuracy.LowPrecision; } else { Accuracy = accuracy.Value; } HardwareSynchronizedTime = Accuracy == TimeAccuracy.UserEdited ? null : hardwareSynchronizedTime; SoftwareTimeInUtc = GetTimeValueRoundedToWholeMilliseconds(softwareTimeInUtc); }
public static string ConvertFromSeconds(ulong totalSeconds, TimeAccuracy timeAccuracy) { int hours = (int)(totalSeconds / 3600); int minutes = (int)((totalSeconds % 3600) / 60); int seconds = (int)(totalSeconds % 60); switch (timeAccuracy) { case TimeAccuracy.Hours: return(string.Format("{0:D2}h", hours)); case TimeAccuracy.HoursAndMinutes: return(string.Format("{0}h {1:D2}m", hours, minutes)); case TimeAccuracy.HoursAndMinutesAndSeconds: return(string.Format("{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds)); default: return("Zły format"); } }
public void DeltaComponentFormatterFormatsTimeCorrectly_WhenTimeIsValid(string timespanText, TimeAccuracy timeAccuracy, bool dropDecimals, string expectedDelta) { var sut = new DeltaComponentFormatter(timeAccuracy, dropDecimals: dropDecimals); var time = TimeSpan.Parse(timespanText); var formattedTime = sut.Format(time); Assert.Equal(expectedDelta, formattedTime); }
public void DeltaTimeFormatterFormatsTimeInGivenAccuracy_WhenTimeIsValid(string timespanText, TimeAccuracy timeAccuracy, bool dropDecimals, string expectedDelta) { var sut = new DeltaTimeFormatter { Accuracy = timeAccuracy, DropDecimals = dropDecimals }; var time = TimeSpan.Parse(timespanText); var formattedDelta = sut.Format(time); Assert.Equal(expectedDelta, formattedDelta); }
public void PreciseDeltaFormatterFormatsTimeCorrectly_WhenTimeIsValid(string timespanText, TimeAccuracy timeAccuracy, string expectedDelta) { var sut = new PreciseDeltaFormatter(timeAccuracy); var time = TimeSpan.Parse(timespanText); var formattedTime = sut.Format(time); Assert.Equal(expectedDelta, formattedTime); }
public DeltaSplitTimeFormatter(TimeAccuracy accuracy, bool dropDecimals) { Accuracy = accuracy; DropDecimals = dropDecimals; }
public void FormatsTimeCorrectly_WhenDeterminedAccuracyIsSupplied(string timespanText, TimeAccuracy accuracy, string expectedTime) { var sut = new GeneralTimeFormatter { DigitsFormat = DigitsFormat.SingleDigitMinutes, Accuracy = accuracy }; var time = TimeSpan.Parse(timespanText); var formattedTime = sut.Format(time); Assert.Equal(expectedTime, formattedTime); }
public RegularTimeFormatter(TimeAccuracy accuracy = TimeAccuracy.Seconds) { Accuracy = accuracy; }
public void RunPredictionFormatterFormatsTimeAsDash_WhenTimeIsNullRegardlessOfAccuracy(TimeAccuracy timeAccuracy) { var sut = new RunPredictionFormatter(timeAccuracy); var formattedTime = sut.Format(null); Assert.Equal(TimeFormatConstants.DASH, formattedTime); }
public void FormatTimeCorrectly_WhenTimeIsValidAndAccuracyIsSupplied(string timespanText, TimeAccuracy accuracy, string expectedTime) { var sut = new PossibleTimeSaveFormatter { Accuracy = accuracy }; var time = TimeSpan.Parse(timespanText); var formattedTime = sut.Format(time); Assert.Equal(expectedTime, formattedTime); }
public RunPredictionFormatter(TimeAccuracy accuracy) { Accuracy = accuracy; }
public SegmentTimesFormatter(TimeAccuracy accuracy) { Accuracy = accuracy; }
public PreciseDeltaFormatter(TimeAccuracy accuracy) { Accuracy = accuracy; }
public RegularSplitTimeFormatter(TimeAccuracy accuracy) { Accuracy = accuracy; }
public DiscordComponentDeltaFormatter(TimeAccuracy accuracy, bool dropDecimals) { Accuracy = accuracy; DropDecimals = dropDecimals; }
protected void UpdateTimeFormat() { if (Settings.DigitsFormat == "1") { CurrentTimeFormat = TimeFormat.Seconds; } else if (Settings.DigitsFormat == "00:01") { CurrentTimeFormat = TimeFormat.Minutes; } else if (Settings.DigitsFormat == "0:00:01") { CurrentTimeFormat = TimeFormat.Hours; } else { CurrentTimeFormat = TimeFormat.TenHours; } if (Settings.AlternateTimeFormat == "1") { AlternateTimeFormat = TimeFormat.Seconds; } else if (Settings.AlternateTimeFormat == "00:01") { AlternateTimeFormat = TimeFormat.Minutes; } else if (Settings.AlternateTimeFormat == "0:00:01") { AlternateTimeFormat = TimeFormat.Hours; } else { CurrentTimeFormat = TimeFormat.TenHours; } if (Settings.Accuracy == ".23") { CurrentAccuracy = TimeAccuracy.Hundredths; } else if (Settings.Accuracy == ".2") { CurrentAccuracy = TimeAccuracy.Tenths; } else { CurrentAccuracy = TimeAccuracy.Seconds; } if (Settings.AlternateAccuracy == ".23") { AlternateAccuracy = TimeAccuracy.Hundredths; } else if (Settings.AlternateAccuracy == ".2") { AlternateAccuracy = TimeAccuracy.Tenths; } else { AlternateAccuracy = TimeAccuracy.Seconds; } }