From 66fc1d081c71b8babc4e549b2a99a8f7b1ea59bc Mon Sep 17 00:00:00 2001 From: walon Date: Sat, 16 Mar 2024 12:47:06 +0800 Subject: [PATCH] =?UTF-8?q?[refactor]=20DDateTime::UnixTimeOfCurrentContex?= =?UTF-8?q?t=E7=A7=BB=E5=88=B0DataExtensions=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.Bson/BsonDataVisitor.cs | 2 +- .../DataVisitors/ToLiteralVisitorBase.cs | 2 +- src/Luban.Core/Datas/DDateTime.cs | 4 +--- src/Luban.Core/Utils/DataExtensions.cs | 16 ++++++++++++++++ .../Binary/BinaryDataVisitor.cs | 2 +- .../Json/JsonDataVisitor.cs | 2 +- .../Xml/XmlDataVisitor.cs | 2 +- .../Yaml/YamlDataVisitor.cs | 2 +- src/Luban.MsgPack/MsgPackDataVisitor.cs | 2 +- .../DataVisitors/ProtobufBinDataVisitor.cs | 2 +- 10 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 src/Luban.Core/Utils/DataExtensions.cs diff --git a/src/Luban.Bson/BsonDataVisitor.cs b/src/Luban.Bson/BsonDataVisitor.cs index e789ee01..bf79a018 100644 --- a/src/Luban.Bson/BsonDataVisitor.cs +++ b/src/Luban.Bson/BsonDataVisitor.cs @@ -58,7 +58,7 @@ public void Accept(DString type, BsonDataWriter x) public virtual void Accept(DDateTime type, BsonDataWriter x) { - x.WriteValue(type.UnixTimeOfCurrentContext); + x.WriteValue(type.UnixTimeOfCurrentContext()); } public virtual void Accept(DBean type, BsonDataWriter x) diff --git a/src/Luban.Core/DataVisitors/ToLiteralVisitorBase.cs b/src/Luban.Core/DataVisitors/ToLiteralVisitorBase.cs index 3ed96891..12c58e3d 100644 --- a/src/Luban.Core/DataVisitors/ToLiteralVisitorBase.cs +++ b/src/Luban.Core/DataVisitors/ToLiteralVisitorBase.cs @@ -52,7 +52,7 @@ public virtual string Accept(DString type) public virtual string Accept(DDateTime type) { - return type.UnixTimeOfCurrentContext.ToString(); + return type.UnixTimeOfCurrentContext().ToString(); } public abstract string Accept(DBean type); diff --git a/src/Luban.Core/Datas/DDateTime.cs b/src/Luban.Core/Datas/DDateTime.cs index 6f1fdaf4..453b9e76 100644 --- a/src/Luban.Core/Datas/DDateTime.cs +++ b/src/Luban.Core/Datas/DDateTime.cs @@ -16,8 +16,6 @@ public DDateTime(DateTime time) this.Time = time; } - public long UnixTimeOfCurrentContext => GetUnixTime(GenerationContext.Current.TimeZone); - public override bool Equals(object obj) { return obj is DDateTime d && Time == d.Time; @@ -45,7 +43,7 @@ public string ToFormatString() public long GetUnixTime(TimeZoneInfo asTimeZone) { var destDateTime = TimeZoneInfo.ConvertTime(Time, asTimeZone, TimeZoneInfo.Utc); - return (long)new DateTimeOffset(destDateTime).ToUnixTimeSeconds(); + return new DateTimeOffset(destDateTime).ToUnixTimeSeconds(); } public override void Apply(IDataActionVisitor visitor, T x) diff --git a/src/Luban.Core/Utils/DataExtensions.cs b/src/Luban.Core/Utils/DataExtensions.cs new file mode 100644 index 00000000..a63c3faa --- /dev/null +++ b/src/Luban.Core/Utils/DataExtensions.cs @@ -0,0 +1,16 @@ +using Luban.Datas; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Utils; + +public static class DataExtensions +{ + public static long UnixTimeOfCurrentContext(this DDateTime dateTime) + { + return dateTime.GetUnixTime(GenerationContext.Current.TimeZone); + } +} diff --git a/src/Luban.DataTarget.Builtin/Binary/BinaryDataVisitor.cs b/src/Luban.DataTarget.Builtin/Binary/BinaryDataVisitor.cs index 2632bb5a..234408ca 100644 --- a/src/Luban.DataTarget.Builtin/Binary/BinaryDataVisitor.cs +++ b/src/Luban.DataTarget.Builtin/Binary/BinaryDataVisitor.cs @@ -57,7 +57,7 @@ public void Accept(DString type, ByteBuf x) public void Accept(DDateTime type, ByteBuf x) { - x.WriteLong(type.UnixTimeOfCurrentContext); + x.WriteLong(type.UnixTimeOfCurrentContext()); } public void Accept(DBean type, ByteBuf x) diff --git a/src/Luban.DataTarget.Builtin/Json/JsonDataVisitor.cs b/src/Luban.DataTarget.Builtin/Json/JsonDataVisitor.cs index 8ec2ec87..cf035ac9 100644 --- a/src/Luban.DataTarget.Builtin/Json/JsonDataVisitor.cs +++ b/src/Luban.DataTarget.Builtin/Json/JsonDataVisitor.cs @@ -59,7 +59,7 @@ public void Accept(DString type, Utf8JsonWriter x) public virtual void Accept(DDateTime type, Utf8JsonWriter x) { - x.WriteNumberValue(type.UnixTimeOfCurrentContext); + x.WriteNumberValue(type.UnixTimeOfCurrentContext()); } public virtual void Accept(DBean type, Utf8JsonWriter x) diff --git a/src/Luban.DataTarget.Builtin/Xml/XmlDataVisitor.cs b/src/Luban.DataTarget.Builtin/Xml/XmlDataVisitor.cs index 9e4280aa..d44b9c2a 100644 --- a/src/Luban.DataTarget.Builtin/Xml/XmlDataVisitor.cs +++ b/src/Luban.DataTarget.Builtin/Xml/XmlDataVisitor.cs @@ -60,7 +60,7 @@ public void Accept(DString type, XmlWriter w) public void Accept(DDateTime type, XmlWriter w) { - w.WriteValue(type.UnixTimeOfCurrentContext); + w.WriteValue(type.UnixTimeOfCurrentContext()); } public void Accept(DBean type, XmlWriter w) diff --git a/src/Luban.DataTarget.Builtin/Yaml/YamlDataVisitor.cs b/src/Luban.DataTarget.Builtin/Yaml/YamlDataVisitor.cs index 3b79b0c6..7e926725 100644 --- a/src/Luban.DataTarget.Builtin/Yaml/YamlDataVisitor.cs +++ b/src/Luban.DataTarget.Builtin/Yaml/YamlDataVisitor.cs @@ -70,7 +70,7 @@ public YamlNode Accept(DString type) public YamlNode Accept(DDateTime type) { - return ToPlainNode(type.UnixTimeOfCurrentContext.ToString()); + return ToPlainNode(type.UnixTimeOfCurrentContext().ToString()); } public YamlNode Accept(DBean type) diff --git a/src/Luban.MsgPack/MsgPackDataVisitor.cs b/src/Luban.MsgPack/MsgPackDataVisitor.cs index 8ebf20ee..fae4b20e 100644 --- a/src/Luban.MsgPack/MsgPackDataVisitor.cs +++ b/src/Luban.MsgPack/MsgPackDataVisitor.cs @@ -111,7 +111,7 @@ public void Accept(DString type, ref MessagePackWriter writer) public void Accept(DDateTime type, ref MessagePackWriter writer) { - writer.Write(type.UnixTimeOfCurrentContext); + writer.Write(type.UnixTimeOfCurrentContext()); } public void Accept(DBean type, ref MessagePackWriter writer) diff --git a/src/Luban.Protobuf/DataVisitors/ProtobufBinDataVisitor.cs b/src/Luban.Protobuf/DataVisitors/ProtobufBinDataVisitor.cs index adc073ae..83921340 100644 --- a/src/Luban.Protobuf/DataVisitors/ProtobufBinDataVisitor.cs +++ b/src/Luban.Protobuf/DataVisitors/ProtobufBinDataVisitor.cs @@ -54,7 +54,7 @@ public void Accept(DEnum type, CodedOutputStream x) public void Accept(DDateTime type, CodedOutputStream x) { - x.WriteInt64(type.UnixTimeOfCurrentContext); + x.WriteInt64(type.UnixTimeOfCurrentContext()); } public void Accept(DString type, CodedOutputStream x)