From f6b8b32123341e0ba7c990cb18523be406a302a6 Mon Sep 17 00:00:00 2001 From: walon Date: Mon, 15 Nov 2021 23:26:17 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D.net=206=E7=9A=84TimeZone=E7=9B=B8=E5=85=B3=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E5=AF=BC=E8=87=B4datetime=E8=AE=A1=E7=AE=97=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98=20=E3=80=90=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E3=80=91=E6=96=B0=E5=A2=9Edatetime=E7=9A=84=E7=AD=89?= =?UTF-8?q?=E4=BB=B7=E7=B1=BB=E5=9E=8B=E5=90=8Dtime=20=E3=80=90=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E3=80=91Luban.Server=E7=9A=84Dockerfile=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E6=8B=B7=E5=A4=87localtime=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.ClientServer/Program.cs | 5 ++++- src/Luban.Common/Source/Utils/TimeUtil.cs | 13 +++++++++++++ src/Luban.Job.Cfg/Source/Datas/DDateTime.cs | 4 +++- src/Luban.Job.Cfg/Source/Utils/DataUtil.cs | 2 ++ src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs | 1 + src/Luban.Server/Properties/launchSettings.json | 4 ++++ src/Luban.Server/Scripts/Dockerfile | 1 - src/Luban.Server/Source/Program.cs | 5 +++++ 8 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 src/Luban.Common/Source/Utils/TimeUtil.cs diff --git a/src/Luban.ClientServer/Program.cs b/src/Luban.ClientServer/Program.cs index 6f3a47d..379ac35 100644 --- a/src/Luban.ClientServer/Program.cs +++ b/src/Luban.ClientServer/Program.cs @@ -38,6 +38,9 @@ namespace Luban.ClientServer [Option('t', "template_search_path", Required = false, HelpText = "string template search path.")] public string TemplateSearchPath { get; set; } + + [Option("timezone", Required = false, HelpText = "default timezone")] + public string DefaultTimeZone { get; set; } = "Asia/Shanghai"; } private static void PrintUsage(string err) @@ -170,7 +173,7 @@ Options: StringTemplateUtil.AddTemplateSearchPath(options.TemplateSearchPath); } StringTemplateUtil.AddTemplateSearchPath(FileUtil.GetPathRelateApplicationDirectory("Templates")); - + TimeZoneUtil.DefaultTimeZone = TimeZoneInfo.FindSystemTimeZoneById(options.DefaultTimeZone); GenServer.Ins.Start(true, options.Port, ProtocolStub.Factories); GenServer.Ins.RegisterJob("cfg", new Luban.Job.Cfg.JobController()); diff --git a/src/Luban.Common/Source/Utils/TimeUtil.cs b/src/Luban.Common/Source/Utils/TimeUtil.cs new file mode 100644 index 0000000..739b759 --- /dev/null +++ b/src/Luban.Common/Source/Utils/TimeUtil.cs @@ -0,0 +1,13 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Common.Utils +{ + public static class TimeZoneUtil + { + public static TimeZoneInfo DefaultTimeZone { get; set; } = TimeZoneInfo.Utc; + } +} diff --git a/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs b/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs index b6f8ba7..2f1bbf3 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs @@ -1,3 +1,4 @@ +using Luban.Common.Utils; using Luban.Job.Cfg.DataVisitors; using Luban.Job.Cfg.Utils; using System; @@ -15,10 +16,11 @@ namespace Luban.Job.Cfg.Datas public DDateTime(DateTime time) { + this.Time = time; // time.Kind == DateTimeKind.Unspecified // DateTimeOffset把它当作Local处理 - this._localTime = (int)new DateTimeOffset(time).ToUnixTimeSeconds(); + this._localTime = (int)new DateTimeOffset(TimeZoneInfo.ConvertTime(time, TimeZoneUtil.DefaultTimeZone, TimeZoneInfo.Utc)).ToUnixTimeSeconds(); } public override bool Equals(object obj) diff --git a/src/Luban.Job.Cfg/Source/Utils/DataUtil.cs b/src/Luban.Job.Cfg/Source/Utils/DataUtil.cs index 1b4f3f3..bcbca91 100644 --- a/src/Luban.Job.Cfg/Source/Utils/DataUtil.cs +++ b/src/Luban.Job.Cfg/Source/Utils/DataUtil.cs @@ -1,3 +1,4 @@ +using Luban.Common.Utils; using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.TypeVisitors; @@ -61,6 +62,7 @@ namespace Luban.Job.Cfg.Utils public static DDateTime CreateDateTime(string x) { DateTime dateTime = DateTime.ParseExact(x, dateTimeFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None); + //return new DDateTime(TimeZoneInfo.ConvertTime(dateTime, TimeZoneUtil.DefaultTimeZone, TimeZoneInfo.Utc)); return new DDateTime(dateTime); } diff --git a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs index b27624e..9ad7906 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -213,6 +213,7 @@ namespace Luban.Job.Common.Defs case "vector2": return TVector2.Create(nullable, tags); case "vector3": return TVector3.Create(nullable, tags); case "vector4": return TVector4.Create(nullable, tags); + case "time": case "datetime": return SupportDatetimeType ? TDateTime.Create(nullable, tags) : throw new NotSupportedException($"鍙湁閰嶇疆鏀寔datetime鏁版嵁绫诲瀷"); default: { diff --git a/src/Luban.Server/Properties/launchSettings.json b/src/Luban.Server/Properties/launchSettings.json index 59ed2f6..f5ddc6e 100644 --- a/src/Luban.Server/Properties/launchSettings.json +++ b/src/Luban.Server/Properties/launchSettings.json @@ -6,6 +6,10 @@ "TestTemplate": { "commandName": "Project", "commandLineArgs": "-t D:\\workspace\\luban_examples\\Projects\\CustomTemplates" + }, + "timezone-utc": { + "commandName": "Project", + "commandLineArgs": "--timezone \"Etc/UTC\"" } } } \ No newline at end of file diff --git a/src/Luban.Server/Scripts/Dockerfile b/src/Luban.Server/Scripts/Dockerfile index e0ad82f..5eac597 100644 --- a/src/Luban.Server/Scripts/Dockerfile +++ b/src/Luban.Server/Scripts/Dockerfile @@ -41,7 +41,6 @@ COPY nuget.config ./nuget.config RUN dotnet publish -c Release -o out FROM mcr.microsoft.com/dotnet/runtime:6.0 AS runtime -RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone WORKDIR /app COPY --from=build /app/Luban.Server/out ./ EXPOSE 8899/tcp diff --git a/src/Luban.Server/Source/Program.cs b/src/Luban.Server/Source/Program.cs index 04e34f8..b81d945 100644 --- a/src/Luban.Server/Source/Program.cs +++ b/src/Luban.Server/Source/Program.cs @@ -20,6 +20,9 @@ namespace Luban.Server [Option('t', "template_search_path", Required = false, HelpText = "additional template search path")] public string TemplateSearchPath { get; set; } + + [Option("timezone", Required = false, HelpText = "default timezone")] + public string DefaultTimeZone { get; set; } = "Asia/Shanghai"; } private static CommandLineOptions ParseOptions(String[] args) @@ -56,6 +59,8 @@ namespace Luban.Server System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + TimeZoneUtil.DefaultTimeZone = TimeZoneInfo.FindSystemTimeZoneById(options.DefaultTimeZone); + GenServer.Ins.Start(false, options.Port, ProtocolStub.Factories); GenServer.Ins.RegisterJob("cfg", new Luban.Job.Cfg.JobController());