Skip to content

Instantly share code, notes, and snippets.

@pengowray
Created August 4, 2019 11:23
Show Gist options
  • Save pengowray/59e08fe5b9c1c92708c2daddcf4392ce to your computer and use it in GitHub Desktop.
Save pengowray/59e08fe5b9c1c92708c2daddcf4392ce to your computer and use it in GitHub Desktop.
This is the general idea. This class combines everything every other TimeFormatter does in about the same space as some of the longer ones. That means all the formatters can be rewritten just as a call to this with the right selection of parameters, and not require any of their own lengthy if-then-else logic.
using System;
using System.Globalization;
namespace LiveSplit.TimeFormatters {
public class GeneralTimeFormatter : ITimeFormatter {
public TimeAccuracy Accuracy;
/// <summary>
/// Only shows decimals if they're non-zero
/// </summary>
public bool AutomaticPrecision = false;
/// <summary>
/// Drop decimals if over 1 minute
/// </summary>
public bool DropDecimals = false;
///TODO: add enum: BigMinutes (for issue #1336)
public TimeFormat TimeFormat = TimeFormat.Seconds;
/// <summary>
/// e.g. "1d 23:59:10" instead of "47:59:10"
/// </summary>
public bool AllowDays = false;
/// <summary>
/// Include a "+" for positive times (excluding zero)
/// </summary>
public bool ShowPlus = false;
//TODO: enum-ize
//TODO: Maybe should also have a "-:--" (replace 0's with dashes for null)
/// <summary>
/// If false, null values show a zero time value; if true nulls show as a zero value.
/// </summary>
public bool DashNullValue = true;
static readonly private CultureInfo ic = CultureInfo.InvariantCulture;
public string Format(TimeSpan? timeNullable) {
//Dictionary<TimeAccuracy, string> AccuracyFormats = new Dictionary<TimeAccuracy, string>();
//string[] AccuracyFormats = new string[Accuracy.Length]
if (!timeNullable.HasValue) {
if (DashNullValue) {
return TimeFormatConstants.DASH;
} else {
timeNullable = TimeSpan.Zero;
}
}
TimeSpan time = timeNullable.Value;
string minusString = time < TimeSpan.Zero ? TimeFormatConstants.MINUS : (ShowPlus ? "+" : "");
//TODO: time = TimeSpan.Zero - time;
//TODO: escape minus string to be used in a format? shouldn't be needed?
string decimalFormat = "";
if (DropDecimals && time.TotalMinutes >= 1 ) {
decimalFormat = "";
} else if (Accuracy == TimeAccuracy.Seconds) {
decimalFormat = "";
} else if (Accuracy == TimeAccuracy.Tenths) {
decimalFormat = ".f";
} else if (Accuracy == TimeAccuracy.Hundredths) {
decimalFormat = ".ff";
}
if (AutomaticPrecision) {
decimalFormat.Replace('f', 'F');
}
if (time.TotalDays >= 1) {
if (AllowDays) {
return minusString + time.ToString(@"%d\d hh\:mm\:ss" + decimalFormat, ic);
} else {
return minusString + (int)time.TotalHours + time.ToString(@"\:mm\:ss" + decimalFormat, ic);
}
} else if (TimeFormat == TimeFormat.TenHours) {
return minusString + time.ToString(@"hh\:mm\:ss" + decimalFormat, ic);
} else if (time.TotalHours >= 1 || TimeFormat == TimeFormat.Hours) {
//if (TimeFormat == TimeFormat.BigMinutes) return minusString + ((int)time.TotalMinutes) + time.ToString(@"\:ss\" + decimalFormat, ic);
return minusString + time.ToString(@"h\:mm\:ss" + decimalFormat, ic);
} else if (TimeFormat == TimeFormat.Minutes) {
return minusString + time.ToString(@"mm\:ss\", ic);
} else if (time.TotalMinutes >= 1) {
return minusString + time.ToString(@"m\:ss" + decimalFormat, ic);
} else {
return minusString + time.ToString(@"s" + decimalFormat, ic);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment