DateTime.FromBinary(Int64) DateTime.FromBinary(Int64) DateTime.FromBinary(Int64) DateTime.FromBinary(Int64) Method

定义

反序列化一个 64 位二进制值,并重新创建序列化的 DateTime 初始对象。Deserializes a 64-bit binary value and recreates an original serialized DateTime object.

public:
 static DateTime FromBinary(long dateData);
public static DateTime FromBinary (long dateData);
static member FromBinary : int64 -> DateTime
Public Shared Function FromBinary (dateData As Long) As DateTime

参数

dateData
Int64 Int64 Int64 Int64

64 位带符号整数,它对 2 位字段的 Kind 属性以及 62 位字段的 Ticks 属性进行了编码。A 64-bit signed integer that encodes the Kind property in a 2-bit field and the Ticks property in a 62-bit field.

返回

一个对象,它等效于由 DateTime 方法序列化的 ToBinary() 对象。An object that is equivalent to the DateTime object that was serialized by the ToBinary() method.

异常

dateData 小于 MinValue 或大于 MaxValuedateData is less than MinValue or greater than MaxValue.

注解

使用方法将当前DateTime对象的值转换为二进制值。 ToBinaryUse 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方法返回的值与提供给该ToBinary方法的原始DateTime值不同。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属性具有值Local的结构来表示。A 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. ToBinary方法生成的DateTime二进制表示形式还原本地值时,该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 then 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:00For example, the transition from standard time to daylight saving time occurs in the Pacific Time zone of the United States 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 is local time, Coordinated Universal Time (UTC), or neither, concatenated to a private Ticks field, which contains the number of 100-nanosecond ticks that specify a date and time. 可以通过属性访问Ticks的计时周期数,可以通过Kind属性访问 theKind 字段。The number of ticks can be accessed with the Ticks property and theKind 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 needed to serialize only the Ticks data in the DateTime. 从 .NET Framework 2.0 开始,还必须序列化类型数据。Starting with the .NET Framework 2.0, you must also serialize the Kind data.

适用于

另请参阅