/// <summary> /// Convert TimeSpan to time string. /// </summary> private static string _ConvertTimeSpanToTimeString(TimeSpan timeSpan, EUnitTime smallestUnitTime, bool isShowZeroValue, string keyDay, string keyDays, string keyHour, string keyHours, string keyMinute, string keyMinutes, string keySecond, string keySeconds) { string time = string.Empty; if (smallestUnitTime <= EUnitTime.Day) { if (timeSpan.Days >= 2) { time = timeSpan.Days + " " + MyLocalizationManager.Instance.LoadKey(keyDays); } else if (timeSpan.Days >= 1) { time = "1 " + MyLocalizationManager.Instance.LoadKey(keyDay); } else if (isShowZeroValue) { time = "0 " + MyLocalizationManager.Instance.LoadKey(keyDay); } } if (smallestUnitTime <= EUnitTime.Hour) { if (timeSpan.Hours >= 2) { time += (time.Length > 0 ? " " : string.Empty) + timeSpan.Hours + " " + MyLocalizationManager.Instance.LoadKey(keyHours); } else if (timeSpan.Hours >= 1) { time += (time.Length > 0 ? " " : string.Empty) + "1 " + MyLocalizationManager.Instance.LoadKey(keyHour); } else if (isShowZeroValue) { time += (time.Length > 0 ? " " : string.Empty) + "0 " + MyLocalizationManager.Instance.LoadKey(keyHour); } } if (smallestUnitTime <= EUnitTime.Minute) { if (timeSpan.Minutes >= 2) { time += (time.Length > 0 ? " " : string.Empty) + timeSpan.Minutes + " " + MyLocalizationManager.Instance.LoadKey(keyMinutes); } else if (timeSpan.Minutes >= 1) { time += (time.Length > 0 ? " " : string.Empty) + "1 " + MyLocalizationManager.Instance.LoadKey(keyMinute); } else if (isShowZeroValue) { time += (time.Length > 0 ? " " : string.Empty) + "0 " + MyLocalizationManager.Instance.LoadKey(keyMinute); } } if (smallestUnitTime <= EUnitTime.Second) { if (timeSpan.Seconds >= 2) { time += (time.Length > 0 ? " " : string.Empty) + timeSpan.Seconds + " " + MyLocalizationManager.Instance.LoadKey(keySeconds); } else if (timeSpan.Seconds >= 1) { time += (time.Length > 0 ? " " : string.Empty) + "1 " + MyLocalizationManager.Instance.LoadKey(keySecond); } else if (isShowZeroValue) { time += (time.Length > 0 ? " " : string.Empty) + "0 " + MyLocalizationManager.Instance.LoadKey(keySecond); } } return(time); }
/// <summary> /// Convert TimeSpan to time string. /// </summary> /// <param name="highestUnitTime">highest unit of time can be shown</param> /// <param name="maxUnitQuantity">maximum number of units can be shown</param> /// <param name="isShowZeroValue">always show even it is zero value</param> public static string ConvertTimeSpanToFullTimeString(TimeSpan timeSpan, EUnitTime highestUnitTime = EUnitTime.Second, int maxUnitQuantity = 2, bool isShowZeroValue = false, string keyDay = "_TEXT_DAY", string keyDays = "_TEXT_DAYS", string keyHour = "_TEXT_HOUR", string keyHours = "_TEXT_HOURS", string keyMinute = "_TEXT_MINUTE", string keyMinutes = "_TEXT_MINUTES", string keySecond = "_TEXT_SECOND", string keySeconds = "_TEXT_SECONDS") { return(_ConvertTimeSpanToTimeString(timeSpan, highestUnitTime, maxUnitQuantity, isShowZeroValue, keyDay, keyDays, keyHour, keyHours, keyMinute, keyMinutes, keySecond, keySeconds)); }
/// <summary> /// Convert TimeSpan to time string. /// </summary> /// <param name="smallestUnitTime">smallest unit of time can be shown</param> /// <param name="isShowZeroValue">always show even it is zero value</param> public static string ConvertTimeSpanToShortTimeString(TimeSpan timeSpan, EUnitTime smallestUnitTime = EUnitTime.Second, bool isShowZeroValue = false, string keyDay = "_TEXT_SHORT_DAY", string keyDays = "_TEXT_SHORT_DAYS", string keyHour = "_TEXT_SHORT_HOUR", string keyHours = "_TEXT_SHORT_HOURS", string keyMinute = "_TEXT_SHORT_MINUTE", string keyMinutes = "_TEXT_SHORT_MINUTES", string keySecond = "_TEXT_SHORT_SECOND", string keySeconds = "_TEXT_SHORT_SECONDS") { return(_ConvertTimeSpanToTimeString(timeSpan, smallestUnitTime, isShowZeroValue, keyDay, keyDays, keyHour, keyHours, keyMinute, keyMinutes, keySecond, keySeconds)); }