基于给出不正确值的刻度构造 DatetimeOffset 对象
Constructing DatetimeOffset object based on Ticks giving incorrect value
我的数据库中存储了 DatetimeOffset 值 table :
2002-08-27 07:49:20.3190000 -11:00
现在这就是我在 binary file
中写入此值的方式:
DateTimeOffset d = "2002-08-27 07:49:20.3190000 -11:00";
BinaryWriter w ;
w.Write(d.Ticks);
现在,当我尝试使用二进制 reader 读取此值时,它会根据 Ticks
生成错误,如下所示:
BinaryReader r;
var dateTime = DateTime.FromBinary(r.ReadInt64());
return new DateTimeOffset(dateTime);
输出: 8/27/2002 7:49:20 AM +05:30
这是怎么回事?
如何根据 Ticks 正确构造精确的 DatetimeOffset 对象?
您需要同时存储 UTC 刻度和时区偏移量:
DateTimeOffset d = "2002-08-27 07:49:20.3190000 -11:00";
BinaryWriter w;
w.Write(d.Ticks);
w.Write(d.Offset.Ticks);
BinaryReader r;
var dateTime = new DateTime(r.ReadInt64());
var offset = new TimeSpan(r.ReadInt64());
return new DateTimeOffset(dateTime, offset);
我的数据库中存储了 DatetimeOffset 值 table :
2002-08-27 07:49:20.3190000 -11:00
现在这就是我在 binary file
中写入此值的方式:
DateTimeOffset d = "2002-08-27 07:49:20.3190000 -11:00";
BinaryWriter w ;
w.Write(d.Ticks);
现在,当我尝试使用二进制 reader 读取此值时,它会根据 Ticks
生成错误,如下所示:
BinaryReader r;
var dateTime = DateTime.FromBinary(r.ReadInt64());
return new DateTimeOffset(dateTime);
输出: 8/27/2002 7:49:20 AM +05:30
这是怎么回事?
如何根据 Ticks 正确构造精确的 DatetimeOffset 对象?
您需要同时存储 UTC 刻度和时区偏移量:
DateTimeOffset d = "2002-08-27 07:49:20.3190000 -11:00";
BinaryWriter w;
w.Write(d.Ticks);
w.Write(d.Offset.Ticks);
BinaryReader r;
var dateTime = new DateTime(r.ReadInt64());
var offset = new TimeSpan(r.ReadInt64());
return new DateTimeOffset(dateTime, offset);