2010-08-17 15 views
31

Say przekonwertować kilka sekund do obiektu TimeSpan tak:Format TimeSpan większa niż 24 godziny

Dim sec = 1254234568 
Dim t As TimeSpan = TimeSpan.FromSeconds(sec) 

Jak mogę sformatować obiekt TimeSpan w formacie jak poniżej:

>105hr 56mn 47sec 

Czy ma wbudowaną funkcję lub czy muszę napisać niestandardową funkcję?

Odpowiedz

47

Cóż, najprostszą rzeczą do zrobienia jest samodzielne sformatowanie, np.

return string.Format("{0}hr {1}mn {2}sec", 
        (int) span.TotalHours, 
        span.Minutes, 
        span.Seconds); 

W VB:

Public Shared Function FormatTimeSpan(span As TimeSpan) As String 
    Return String.Format("{0}hr {1}mn {2}sec", _ 
         CInt(Math.Truncate(span.TotalHours)), _ 
         span.Minutes, _ 
         span.Seconds) 
End Function 

Nie wiem, czy którykolwiek z formatowania w .NET 4 TimeSpan byłoby to prostsze.

+1

Możesz chcieć podać kod VB, ponieważ kod w pytaniu wygląda tak :-) – Joey

+2

Czy czegoś brakuje?Denerwuję się, gdy moja odpowiedź nie zgadza się z Jonem Skeetem :-) –

+3

'((int) span.TotalMinutes)% 60' może być zastąpione przez' span.Minutes'. To samo z sekundami. –

3

string.Format("{0}hr {1}mn {2}sec", (int) t.TotalHours, t.Minutes, t.Seconds);

+1

To jest tylko dla TimeSpan <24 godz. I t, Sekundy powinny być t.Seconds –

+2

Powinieneś zmienić to na '{0: f0} ..., t.TotalHours, ...' – SLaks

+0

@Angkor: Dobra uwaga. Zmieniono, aby poradzić sobie z> 24 godzinami –

1

może trzeba obliczyć godzin. Zakres godzin w funkcji TimeSpan.ToString wynosi tylko 0-23.

Najgorsze, co musisz zrobić, to formatowanie surowych napisów a la Jon Skeet.

0

Spróbuj Funkcja:

Public Shared Function GetTimeSpanString(ByVal ts As TimeSpan) As String 
     Dim output As New StringBuilder() 

     Dim needsComma As Boolean = False 

     If ts = Nothing Then 

      Return "00:00:00" 

     End If 

     If ts.TotalHours >= 1 Then 
      output.AppendFormat("{0} hr", Math.Truncate(ts.TotalHours)) 
      If ts.TotalHours > 1 Then 
       output.Append("s") 
      End If 
      needsComma = True 
     End If 

     If ts.Minutes > 0 Then 
      If needsComma Then 
       output.Append(", ") 
      End If 
      output.AppendFormat("{0} m", ts.Minutes) 
      'If ts.Minutes > 1 Then 
      ' output.Append("s") 
      'End If 
      needsComma = True 
     End If 

     Return output.ToString() 

End Function  

Convert A Timespan To Hours And Minutes

6

Microsoft nie (obecnie) mają prosty skrót format string do tego. Najprostsze opcje zostały już udostępnione.

string.Format("{0}hr {1:mm}mn {1:ss}sec", (int)t.TotalHours, t); 

Jednak nadmiernie dokładny rozwiązaniem jest wdrożenie własnego ICustomFormatter dla TimeSpan. Nie polecałbym tego, chyba że używasz tego tak często, że oszczędziłoby ci to czasu na dłuższą metę. Jednak są chwile, w których piszesz klasę, w której pisanie własnego numeru jest właściwe, więc napisałem to jako przykład.

/// <summary> 
/// Custom string formatter for TimeSpan that allows easy retrieval of Total segments. 
/// </summary> 
/// <example> 
/// TimeSpan myTimeSpan = new TimeSpan(27, 13, 5); 
/// string.Format("{0:th,###}h {0:mm}m {0:ss}s", myTimeSpan) -> "27h 13m 05s" 
/// string.Format("{0:TH}", myTimeSpan) -> "27.2180555555556" 
/// 
/// NOTE: myTimeSpan.ToString("TH") does not work. See Remarks. 
/// </example> 
/// <remarks> 
/// Due to a quirk of .NET Framework (up through version 4.5.1), 
/// <code>TimeSpan.ToString(format, new TimeSpanFormatter())</code> will not work; it will always call 
/// TimeSpanFormat.FormatCustomized() which takes a DateTimeFormatInfo rather than an 
/// IFormatProvider/ICustomFormatter. DateTimeFormatInfo, unfortunately, is a sealed class. 
/// </remarks> 
public class TimeSpanFormatter : IFormatProvider, ICustomFormatter 
{ 
    /// <summary> 
    /// Used to create a wrapper format string with the specified format. 
    /// </summary> 
    private const string DefaultFormat = "{{0:{0}}}"; 

    /// <remarks> 
    /// IFormatProvider.GetFormat implementation. 
    /// </remarks> 
    public object GetFormat(Type formatType) 
    { 
     // Determine whether custom formatting object is requested. 
     if (formatType == typeof(ICustomFormatter)) 
     { 
      return this; 
     } 

     return null; 
    } 

    /// <summary> 
    /// Determines whether the specified format is looking for a total, and formats it accordingly. 
    /// If not, returns the default format for the given <para>format</para> of a TimeSpan. 
    /// </summary> 
    /// <returns> 
    /// The formatted string for the given TimeSpan. 
    /// </returns> 
    /// <remarks> 
    /// ICustomFormatter.Format implementation. 
    /// </remarks> 
    public string Format(string format, object arg, IFormatProvider formatProvider) 
    { 
     // only apply our format if there is a format and if the argument is a TimeSpan 
     if (string.IsNullOrWhiteSpace(format) || 
      formatProvider != this || // this should always be true, but just in case... 
      !(arg is TimeSpan) || 
      arg == null) 
     { 
      // return the default for whatever our format and argument are 
      return GetDefault(format, arg); 
     } 

     TimeSpan span = (TimeSpan)arg; 

     string[] formatSegments = format.Split(new char[] { ',' }, 2); 
     string tsFormat = formatSegments[0]; 

     // Get inner formatting which will be applied to the int or double value of the requested total. 
     // Default number format is just to return the number plainly. 
     string numberFormat = "{0}"; 
     if (formatSegments.Length > 1) 
     { 
      numberFormat = string.Format(DefaultFormat, formatSegments[1]); 
     } 

     // We only handle two-character formats, and only when those characters' capitalization match 
     // (e.g. 'TH' and 'th', but not 'tH'). Feel free to change this to suit your needs. 
     if (tsFormat.Length != 2 || 
      char.IsUpper(tsFormat[0]) != char.IsUpper(tsFormat[1])) 
     { 
      return GetDefault(format, arg); 
     } 

     // get the specified time segment from the TimeSpan as a double 
     double valAsDouble; 
     switch (char.ToLower(tsFormat[1])) 
     { 
      case 'd': 
       valAsDouble = span.TotalDays; 
       break; 
      case 'h': 
       valAsDouble = span.TotalHours; 
       break; 
      case 'm': 
       valAsDouble = span.TotalMinutes; 
       break; 
      case 's': 
       valAsDouble = span.TotalSeconds; 
       break; 
      case 'f': 
       valAsDouble = span.TotalMilliseconds; 
       break; 
      default: 
       return GetDefault(format, arg); 
     } 

     // figure out if we want a double or an integer 
     switch (tsFormat[0]) 
     { 
      case 'T': 
       // format Total as double 
       return string.Format(numberFormat, valAsDouble); 

      case 't': 
       // format Total as int (rounded down) 
       return string.Format(numberFormat, (int)valAsDouble); 

      default: 
       return GetDefault(format, arg); 
     } 
    } 

    /// <summary> 
    /// Returns the formatted value when we don't know what to do with their specified format. 
    /// </summary> 
    private string GetDefault(string format, object arg) 
    { 
     return string.Format(string.Format(DefaultFormat, format), arg); 
    } 
} 

Note, jak w uwagach w kodzie, TimeSpan.ToString(format, myTimeSpanFormatter) nie będzie działać z powodu dziwactwo Framework, więc zawsze będziesz musiał użyć string.Format (format, myTimeSpanFormatter) aby skorzystać z tej klasa. Zobacz How to create and use a custom IFormatProvider for DateTime?.


EDIT: Jeśli naprawdę, a mam na myśli naprawdę, żeby to się pracować z TimeSpan.ToString(string, TimeSpanFormatter), można dodać następujące powyższej TimeSpanFormatter klasy:

/// <remarks> 
/// Update this as needed. 
/// </remarks> 
internal static string[] GetRecognizedFormats() 
{ 
    return new string[] { "td", "th", "tm", "ts", "tf", "TD", "TH", "TM", "TS", "TF" }; 
} 

I dodaj następującą klasę gdzieś w tej samej przestrzeni nazw:

public static class TimeSpanFormatterExtensions 
{ 
    private static readonly string CustomFormatsRegex = string.Format(@"([^\\])?({0})(?:,{{([^(\\}})]+)}})?", string.Join("|", TimeSpanFormatter.GetRecognizedFormats())); 

    public static string ToString(this TimeSpan timeSpan, string format, ICustomFormatter formatter) 
    { 
     if (formatter == null) 
     { 
      throw new ArgumentNullException(); 
     } 

     TimeSpanFormatter tsFormatter = (TimeSpanFormatter)formatter; 

     format = Regex.Replace(format, CustomFormatsRegex, new MatchEvaluator(m => MatchReplacer(m, timeSpan, tsFormatter))); 
     return timeSpan.ToString(format); 
    } 

    private static string MatchReplacer(Match m, TimeSpan timeSpan, TimeSpanFormatter formatter) 
    { 
     // the matched non-'\' char before the stuff we actually care about 
     string firstChar = m.Groups[1].Success ? m.Groups[1].Value : string.Empty; 

     string input; 
     if (m.Groups[3].Success) 
     { 
      // has additional formatting 
      input = string.Format("{0},{1}", m.Groups[2].Value, m.Groups[3].Value); 
     } 
     else 
     { 
      input = m.Groups[2].Value; 
     } 

     string replacement = formatter.Format(input, timeSpan, formatter); 
     if (string.IsNullOrEmpty(replacement)) 
     { 
      return firstChar; 
     } 

     return string.Format("{0}\\{1}", firstChar, string.Join("\\", replacement.ToCharArray())); 
    } 
} 

Po tym, można użyć

ICustomFormatter formatter = new TimeSpanFormatter(); 
string myStr = myTimeSpan.ToString(@"TH,{000.00}h\:tm\m\:ss\s", formatter); 

gdzie {000.00} jest jednak chcesz, int lub podwójne totalhours być sformatowany. Zwróć uwagę na otaczające nawiasy klamrowe, których nie powinno tam być w przypadku string.Format(). Należy również zauważyć, że formattermusi być zadeklarowany (lub odlany) jako ICustomFormatter zamiast TimeSpanFormatter.

Nadmierne? Tak. Niesamowite? Uhhh ....

+0

Beautiful over engineering. Zdejmuję czapkę. – BanksySan

+1

Twoja krótka i słodka odpowiedź bierze pod uwagę wiodące zera, piękne! Przez jakiś czas utkwiło mu na przykład "47: 33: 4" .. lepiej niż sam Skeet! – Murphybro2

+2

Naprawdę kocham to rozwiązanie. Niestety nie będzie można jej użyć w dwukierunkowym scenariuszu wiążącym, ponieważ nie ma implementacji 'TimeSpan.Parse', która zajęłaby instancję' ICustomFormatter'. – modiX

0

Możesz rozważyć użycie typu Noda Time o numerze .

Na przykład:

Duration d = Duration.FromSeconds(sec); 

Albo

Duration d = Duration.FromTimeSpan(ts); 

Następnie można po prostu sformatować go jako ciąg znaków, na przykład:

string result = d.ToString("H'hr' m'mn' s'sec'", CultureInfo.InvariantCulture); 

Alternatywnie, można użyć pattern-based API zamiast :

DurationPattern p = DurationPattern.CreateWithInvariantCulture("H'hr' m'mn' s'sec'"); 
string result = p.Format(d); 

Zaletą interfejsu API wzorca jest to, że wystarczy raz utworzyć wzór. Jeśli masz wiele wartości do przeanalizowania lub sformatowania, może to przynieść znaczne korzyści.

0

Moje rozwiązanie to:

string text = Math.Floor(timeUsed.TotalHours) + "h " + ((int)timeUsed.TotalMinutes) % 60 + "min"; 
1

Możesz spróbować:

TimeSpan ts = TimeSpan.FromSeconds(1254234568); 
Console.WriteLine($"{((int)ts.TotalHours).ToString("d2")}hr {ts.Minutes.ToString("d2")}mm {ts.Seconds.ToString("d2")}sec"); 
+0

'toString (" d2 ")' z połączeniem z zaakceptowaną odpowiedzią otrzymuje najlepszy wynik –

Powiązane problemy