gpt4 book ai didi

c# - 在 IIS 或 web.config 中设置不同的时区

转载 作者:行者123 更新时间:2023-11-30 19:56:26 24 4
gpt4 key购买 nike

我在很多地方都在记录时间

If Request.DynamicSettings.AirlineSettings.AirlineGeneralSettings.TimeLogEnabled Then
StartTime = DateTime.Now
LogTime(Reflection.MethodBase.GetCurrentMethod.DeclaringType.FullName, Reflection.MethodBase.GetCurrentMethod.Name, StartTime, DateTime.Now, "AB-SCR(I)", 0,)
End If

所有我用过的地方

DateTime.Now

我现在面临一个问题,我目前在海湾服务器中托管它,格林威治标准时间 +4:00我需要在 Gmt +3Gmt 为另一个国家举办同一个项目对于此托管,我需要使用该国家/地区的本地时间记录时间。

有什么方法可以做到这一点,而不必修改我的每一行代码。

我看过这篇文章timzone with asp.net但由于我的服务已经启动,我有很多代码需要更改,我正在寻找更简单的解决方案。

谢谢。

最佳答案

一些事情:

  1. 您不能在 IIS 配置或 web.config 中更改时区。这不是 IIS 问题,而是您的应用程序代码中的问题。

  2. DateTime.Now 永远不应在服务器端应用程序中使用,例如 ASP.Net 网络应用程序。阅读The case against DateTime.Now .

  3. 如果您只是计算运行时间,请不要使用 DateTime。相反,使用 System.Diagnostics.Stopwatch .

    Stopwatch sw = Stopwatch.StartNew();
    // ...do some work ...
    sw.Stop();
    TimeSpan elapsed = sw.Elapsed; // how long it took will be in the Elapsed property
  4. 如果您确实想要特定时区的当前时间,则需要知道时区标识符。 (GMT+4 和 GMT+3 不是时区,而是时区偏移量,请参阅 the timezone tag wiki 中的“Time Zone != Offset”。)您可以使用以下命令查看 Windows 时区列表TimeZoneInfo.GetSystemTimeZones(),或通过在命令行上调用 tzutil/l

    然后在你的应用程序中:

    string tz = "Arabian Standard Time";
    DateTime now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);

    您可能应该重构您的代码,以便在您的 LogTime 方法中完成此操作。那么您将只有一个地方可以为您的应用程序设置时区。

关于c# - 在 IIS 或 web.config 中设置不同的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33839556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com