<TimeSpan_LegacyFormatMode> Element

Determines whether the runtime preserves legacy behavior in formatting operations with System.TimeSpan values.

<configuration> Element
  <runtime> Element
    <TimeSpan_LegacyFormatMode> Element
<TimeSpan_LegacyFormatMode  
   enabled="true|false"/>

The following sections describe attributes, child elements, and parent elements.

Attributes

Attribute

Description

enabled

Required attribute.

Specifies whether the runtime uses legacy formatting behavior with System.TimeSpan values.

enabled Attribute

Value

Description

false

The runtime does not restore legacy formatting behavior.

true

The runtime restores legacy formatting behavior.

Child Elements

None.

Parent Elements

Element

Description

configuration

The root element in every configuration file used by the common language runtime and .NET Framework applications.

runtime

Contains information about runtime initialization options.

Starting with the .NET Framework 4, the System.TimeSpan structure implements the IFormattable interface and supports formatting operations with standard and custom format strings. If a parsing method encounters an unsupported format specifier or format string, it throws a FormatException.

In previous versions of the .NET Framework, the TimeSpan structure did not implement IFormattable and did not support format strings. However, many developers mistakenly assumed that TimeSpan did support a set of format strings and used them in composite formatting operations with methods such as String.Format. Ordinarily, if a type implements IFormattable and supports format strings, calls to formatting methods with unsupported format strings usually throw a FormatException. However, because TimeSpan did not implement IFormattable, the runtime ignored the format string and instead called the TimeSpan.ToString() method. This means that, although the format strings had no effect on the formatting operation, their presence did not result in a FormatException.

For cases in which legacy code passes a composite formatting method and an invalid format string, and that code cannot be recompiled, you can use the <TimeSpan_LegacyFormatMode> element to restore the legacy TimeSpan behavior. When you set the enabled attribute of this element to true, the composite formatting method results in a call to TimeSpan.ToString() rather than TimeSpan.ToString(String, IFormatProvider), and a FormatException is not thrown.

The following example instantiates a TimeSpan object and attempts to format it with the String.Format(String, Object) method by using an unsupported standard format string.

using System;

public class Example
{
   public static void Main()
   {
      TimeSpan interval = new TimeSpan(12, 30, 45);
      string output;
      try {
         output = String.Format("{0:r}", interval);
      }
      catch (FormatException) {
         output = "Invalid Format";
      }
      Console.WriteLine(output);
   }
}

When you run the example on the .NET Framework 3.5 or on an earlier version, it displays the following output:

12:30:45

This differs markedly from the output if you run the example on the .NET Framework 4 or later version:

Invalid Format

However, if you add the following configuration file to the example's directory and then run the example on the .NET Framework 4 or later version, the output is identical to that produced by the example when it is run on .NET Framework 3.5.

<?xml version ="1.0"?>
<configuration>
   <runtime>
      <TimeSpan_LegacyFormatMode enabled="true"/>
   </runtime>
</configuration>
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft