DateTime.ToBinary DateTime.ToBinary DateTime.ToBinary DateTime.ToBinary Method

定义

将当前 DateTime 对象序列化为一个 64 位二进制值,该值随后可用于重新创建 DateTime 对象。Serializes the current DateTime object to a 64-bit binary value that subsequently can be used to recreate the DateTime object.

public:
 long ToBinary();
public long ToBinary ();
member this.ToBinary : unit -> int64
Public Function ToBinary () As Long

返回

64 位有符号整数,它对 KindTicks 属性进行了编码。A 64-bit signed integer that encodes the Kind and Ticks properties.

注解

使用ToBinary方法将当前值转换DateTime为二进制值的对象。Use the ToBinary method to convert the value of the current DateTime object to a binary value. 随后,使用二进制值和FromBinary方法来重新创建原始DateTime对象。Subsequently, use the binary value and the FromBinary method to recreate the original DateTime object.

重要

在某些情况下,DateTime返回值FromBinary方法不是与原始DateTime值提供给ToBinary方法。In some cases, the DateTime value returned by the FromBinary method is not identical to the original DateTime value supplied to the ToBinary method. 有关详细信息,请参阅下一部分中,"本地时间调整"。For more information, see the next section, "Local Time Adjustment".

本地时间调整Local Time Adjustment

表示本地时间,即协调通用时间调整为本地时区DateTime结构,其Kind属性具有值LocalA local time, which is a Coordinated Universal Time adjusted to the local time zone, is represented by a DateTime structure whose Kind property has the value Local. 还原本地时DateTime值由生成的二进制表示形式ToBinary方法,FromBinary方法可能会调整重新创建的值,以使是否不等于原始值。When restoring a local DateTime value from the binary representation that is produced by the ToBinary method, the FromBinary method may adjust the recreated value so that it is not equal to the original value. 在下列条件下,可能会出现这种情况:This can occur under the following conditions:

  • 如果本地DateTime由一个时区中序列化对象ToBinary方法,然后反序列化中通过不同的时区FromBinary方法中,表示所生成的本地时间DateTime自动调整对象为第二个时区。If a local DateTime object is serialized in one time zone by the ToBinary method, and then deserialized in a different time zone by the FromBinary method, the local time represented by the resulting DateTime object is automatically adjusted to the second time zone.

    例如,考虑DateTime对象,表示本地时间的下午 3 点For example, consider a DateTime object that represents a local time of 3 P.M. 在美国执行应用程序使用太平洋时区ToBinary方法以将其转换DateTime为二进制值的对象。An application that is executing in the U.S. Pacific Time zone uses the ToBinary method to convert that DateTime object to a binary value. 在美国执行的另一个应用程序使用东部标准时间时区FromBinary方法将转换为一个新的二进制值DateTime对象。Another application that is executing in the U.S. Eastern Time zone uses the FromBinary method to convert the binary value to a new DateTime object. 新值DateTime对象是下午 6 时,这表示时间作为原始的下午 3 点到相同的点The value of the new DateTime object is 6 P.M., which represents the same point in time as the original 3 P.M. 值,但将会调整为美国东部时区中的本地时间。value, but is adjusted to local time in the Eastern Time zone.

  • 如果本地的二进制表示形式DateTime值表示本地时区的系统中的无效时间FromBinary是调用,该时间会调整,这样就有效。If the binary representation of a local DateTime value represents an invalid time in the local time zone of the system on which FromBinary is called, the time is adjusted so that it is valid.

    例如,从标准时间到夏时制的转换发生在美国在 2010 年 3 月 14 日凌晨 2:00,此时时间前进一小时,上午 3 点到太平洋时区For example, the transition from standard time to daylight saving time occurs in the U.S. Pacific Time zone on March 14, 2010, at 2:00 A.M., when the time advances by one hour, to 3:00 A.M. 此小时间隔为无效的时间,即,不存在该时区的时间间隔。This hour interval is an invalid time, that is, a time interval that does not exist in this time zone. 下面的示例演示时的时间内此范围转换为二进制值通过ToBinary方法,然后通过还原FromBinary方法中,原始值调整为有效的时间。The following example shows that when a time that falls within this range is converted to a binary value by the ToBinary method and is then restored by the FromBinary method, the original value is adjusted to become a valid time. 您可以确定是否特定日期和时间值也可能受到修改将其传递到TimeZoneInfo.IsInvalidTime方法,该示例作为演示。You can determine whether a particular date and time value may be subject to modification by passing it to the TimeZoneInfo.IsInvalidTime method, as the example illustrates.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          DateTime localDate = new DateTime(2010, 3, 14, 2, 30, 0, DateTimeKind.Local);
          long binLocal = localDate.ToBinary();
          if (TimeZoneInfo.Local.IsInvalidTime(localDate))
             Console.WriteLine("{0} is an invalid time in the {1} zone.", 
                               localDate, 
                               TimeZoneInfo.Local.StandardName);
    
          DateTime localDate2 = DateTime.FromBinary(binLocal);
          Console.WriteLine("{0} = {1}: {2}", 
                            localDate, localDate2, localDate.Equals(localDate2));
       }
    }
    // The example displays the following output:
    //    3/14/2010 2:30:00 AM is an invalid time in the Pacific Standard Time zone.
    //    3/14/2010 2:30:00 AM = 3/14/2010 3:30:00 AM: False
    
    Module Example
       Public Sub Main()
          Dim localDate As Date = DateTime.SpecifyKind(#03/14/2010 2:30AM#, DateTimeKind.Local)
          Dim binLocal As Long = localDate.ToBinary()
          If TimeZoneInfo.Local.IsInvalidTime(localDate) Then
             Console.WriteLine("{0} is an invalid time in the {1} zone.", _
                               localDate, _
                               TimeZoneInfo.Local.StandardName)
          End If
          Dim localDate2 As Date = DateTime.FromBinary(binLocal)
          Console.WriteLine("{0} = {1}: {2}", _
                            localDate, localDate2, localDate.Equals(localDate2))
       End Sub
    End Module
    ' The example displays the following output:
    '    3/14/2010 2:30:00 AM is an invalid time in the Pacific Standard Time zone.
    '    3/14/2010 2:30:00 AM = 3/14/2010 3:30:00 AM: False
    

版本的注意事项Version Considerations

从.NET Framework 2.0 版中,开始DateTime结构组成的私有类型字段,指示指定的时间值基于本地时间、 协调世界时 (UTC),还是两者皆未和一个专用的计时周期字段,其中包含指定的日期和时间的 100 纳秒为刻度数。Starting with the .NET Framework version 2.0, a DateTime structure consists of a private Kind field, which indicates whether the specified time value is based on local time, Coordinated Universal Time (UTC), or neither, and a private Ticks field, which contains the number of 100-nanosecond ticks that specify a date and time. 计时周期数字段可以访问与Ticks属性和类型字段都可以访问与Kind属性。The Ticks field can be accessed with the Ticks property and the Kind field can be accessed with the Kind property.

在.NET Framework 2.0 中,如果您序列化之前DateTime对象而使用序列化接口,如不是手动System.Runtime.Serialization.ISerializable,只需序列化中的计时周期数据DateTime结构。Prior to the .NET Framework 2.0, if you serialized a DateTime object manually instead of using a serialization interface such as System.Runtime.Serialization.ISerializable, you only needed to serialize the Ticks data in the DateTime structure. 从 2.0 版开始,必须还序列化类型的数据。Starting with version 2.0, you must also serialize the Kind data.

适用于

另请参阅