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());