none
Bug with String.Format and TimeSpan in .NET 4?

    Question

  • We noticed this debugging an exception in a service last night. The following works in .NET 3.5:

      class Program
      {
        static void Main(string[] args)
        {
          TimeSpan ts = new TimeSpan(12, 30, 30);
          try
          {
            Console.WriteLine(Environment.Version);
            Console.WriteLine(String.Format("{0:hh:mm:ss}", ts));
          }
          catch (Exception ex)
          {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
          }
          Console.WriteLine("Press any key...");
          Console.ReadKey();
        }
      }
    

    However it fails when built for .NET 4...

    Input string was not in a correct format.
      at System.Globalization.TimeSpanFormat.FormatCustomized(TimeSpan value, Strin
    g format, DateTimeFormatInfo dtfi)
      at System.Globalization.TimeSpanFormat.Format(TimeSpan value, String format,
    IFormatProvider formatProvider)
      at System.TimeSpan.ToString(String format, IFormatProvider formatProvider)
      at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String fo
    rmat, Object[] args)
      at System.String.Format(IFormatProvider provider, String format, Object[] arg
    s)
      at System.String.Format(String format, Object arg0)
      at StringFormat40.Program.Main(String[] args) in C:\Mark Personal\VSProjects\
    StringFormat40\StringFormat40\Program.cs:line 16
    

    Examining the stack in both cases, it looks like in .NET 4.0 the String.Format method ends up deferring to the new TimeSpan format code, which is not tolerant of unescaped colons or periods. What makes it a candidate for a bug is that the behavior is inconsistent with DateTime's formatting, which accepts both escaped and unescaped colons and periods.

    Just wanted to pass this along.

    Thursday, May 06, 2010 3:05 PM

Answers

All replies

  • In .NET 4.0 we have use following code which will also work in lower versions

     class Program
      {
        static void Main(string[] args)
        {
          TimeSpan ts = new TimeSpan(12, 30, 30);
          try
          {
            Console.WriteLine(Environment.Version);
            Console.WriteLine(String.Format("{0:hh\\:mm\\:ss}", ts));
    
          }
          catch (Exception ex)
          {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
          }
          Console.WriteLine("Press any key...");
          Console.ReadKey();
        }
      }

    More details is available on following link

    http://msdn.microsoft.com/en-us/library/ee372287.aspx 


    Gaurav Khanna
    Thursday, May 06, 2010 5:44 PM
  • In .NET 4.0 we have use following code which will also work in lower versions


    It will but it will not do any actual formatting in older versions. Since TimeSpan is not IFormattable in older versions of .NET framework.


    Please, give old MSDN back!
    • Edited by alabax Thursday, May 13, 2010 9:50 AM I've got it
    Thursday, May 13, 2010 6:52 AM
  • Mark/alabax,

    There is a configuration switch to restore the old behavior of TimeSpan, see: http://msdn.microsoft.com/en-us/library/ee803802.aspx.

    Regards

    David

     

     


    Base Class Library Team (BCL) | My Blog: http://davesbox.com
    • Marked as answer by SamAgain Monday, May 17, 2010 10:19 AM
    Friday, May 14, 2010 9:24 PM