如何查找应用夏令时的时区和返回时间?

发布于 2024-09-09 02:18:58 字数 1134 浏览 11 评论 0原文

我正在尝试查找应用了夏令时的时区和返回时间?

目前我可以:

  1. 查找时区
  2. 获取 utc 偏移量
  3. 计算本地时间 确定
  4. 时区是否使用夏令时
  5. 获取夏令时的规则
  6. 确定当前时间是否使用夏令时

但是我在应用规则时遇到问题,这里是代码:

仅供参考

System.DateTime.Now.ToUniversalTime().Add(timeDiffUtcClient) returns = 2010/07/10 09:25:45 AM

 DateTime localDate = System.DateTime.Now.ToUniversalTime();
// Get the venue time zone info
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
TimeSpan timeDiffUtcClient = tz.BaseUtcOffset;
localDate = System.DateTime.Now.ToUniversalTime().Add(timeDiffUtcClient);


if (tz.SupportsDaylightSavingTime && tz.IsDaylightSavingTime(localDate))
{
    localDate = localDate.Subtract(tz.GetAdjustmentRules().Single(r => localDate >= r.DateStart && localDate <= r.DateEnd).DaylightDelta);
}
DateTimeOffset utcDate = localDate.ToUniversalTime();


return localDate;

最终值 localDate 为 {2010/07/10 08:20:40 AM}

应该是 {2010/07/10 09:20:40 AM}

由于某种原因休息了 1 小时。

I'm trying to Find a timezone and return time with DaylightSavingTime applied?

Currenty I can:

  1. find the time zone
  2. get utc offset
  3. calculate the local time based on that
  4. determine if the time zone uses DaylightSavingTime
  5. get the rules for DaylightSavingTime
  6. determine if the current time uses DaylightSavingTime

However I'm having issues applying the rules, here's the code:

fyi

System.DateTime.Now.ToUniversalTime().Add(timeDiffUtcClient)
returns = 2010/07/10 09:25:45 AM

 DateTime localDate = System.DateTime.Now.ToUniversalTime();
// Get the venue time zone info
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
TimeSpan timeDiffUtcClient = tz.BaseUtcOffset;
localDate = System.DateTime.Now.ToUniversalTime().Add(timeDiffUtcClient);


if (tz.SupportsDaylightSavingTime && tz.IsDaylightSavingTime(localDate))
{
    localDate = localDate.Subtract(tz.GetAdjustmentRules().Single(r => localDate >= r.DateStart && localDate <= r.DateEnd).DaylightDelta);
}
DateTimeOffset utcDate = localDate.ToUniversalTime();


return localDate;

The final value localDate of is {2010/07/10 08:20:40 AM}

It should be {2010/07/10 09:20:40 AM}

It's 1 hour off for some reason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鸠书 2024-09-16 02:18:58

好的,我修复了它:

 public static DateTime GetLocalTime(string TimeZoneName)
    {
        DateTime localDate = System.DateTime.Now.ToUniversalTime();

        // Get the venue time zone info
        TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName);
        TimeSpan timeDiffUtcClient = tz.BaseUtcOffset;
        localDate = System.DateTime.Now.ToUniversalTime().Add(timeDiffUtcClient);
        //DateTimeOffset localDate = new DateTimeOffset(venueTime, tz.BaseUtcOffset);

        if (tz.SupportsDaylightSavingTime && tz.IsDaylightSavingTime(localDate))
        {
            TimeZoneInfo.AdjustmentRule[] rules = tz.GetAdjustmentRules();
            foreach (var adjustmentRule in rules)
            {
                if (adjustmentRule.DateStart <= localDate && adjustmentRule.DateEnd >= localDate)
                {
                    localDate = localDate.Add(adjustmentRule.DaylightDelta);
                }
            }
            //localDate = localDate.Subtract(tz.GetAdjustmentRules().Single(r => localDate >= r.DateStart && localDate <= r.DateEnd).DaylightDelta);
        }
        DateTimeOffset utcDate = localDate.ToUniversalTime();


        return localDate;
    }

要测试它,您可以这样做:

Hashtable list = new Hashtable();
        foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones())
        {
            string name = tzi.DisplayName;
            DateTime localtime = TimeZoneHelper.GetLocalTime(tzi.Id);
            list.Add(name, localtime);
        }

然后在最后的“列表”上快速观看,然后访问 worldtimeserver.com 并确认一些城市。

ok, I fixed it:

 public static DateTime GetLocalTime(string TimeZoneName)
    {
        DateTime localDate = System.DateTime.Now.ToUniversalTime();

        // Get the venue time zone info
        TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName);
        TimeSpan timeDiffUtcClient = tz.BaseUtcOffset;
        localDate = System.DateTime.Now.ToUniversalTime().Add(timeDiffUtcClient);
        //DateTimeOffset localDate = new DateTimeOffset(venueTime, tz.BaseUtcOffset);

        if (tz.SupportsDaylightSavingTime && tz.IsDaylightSavingTime(localDate))
        {
            TimeZoneInfo.AdjustmentRule[] rules = tz.GetAdjustmentRules();
            foreach (var adjustmentRule in rules)
            {
                if (adjustmentRule.DateStart <= localDate && adjustmentRule.DateEnd >= localDate)
                {
                    localDate = localDate.Add(adjustmentRule.DaylightDelta);
                }
            }
            //localDate = localDate.Subtract(tz.GetAdjustmentRules().Single(r => localDate >= r.DateStart && localDate <= r.DateEnd).DaylightDelta);
        }
        DateTimeOffset utcDate = localDate.ToUniversalTime();


        return localDate;
    }

To test it you can do this:

Hashtable list = new Hashtable();
        foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones())
        {
            string name = tzi.DisplayName;
            DateTime localtime = TimeZoneHelper.GetLocalTime(tzi.Id);
            list.Add(name, localtime);
        }

then do a quickwatch on "list" at the end and go to worldtimeserver.com and confirm a few cities.

初雪 2024-09-16 02:18:58

我在这里加入有点晚了,但我不确定为什么你要手动完成这一切。你的整个功能不能被替换为:

public static DateTime GetLocalTime(string TimeZoneName)
{
  return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName));
}

I'm jumping in a bit late here, but I'm not sure why you're doing this all manually. Could not your entire function be replaced by:

public static DateTime GetLocalTime(string TimeZoneName)
{
  return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName));
}
放低过去 2024-09-16 02:18:58

您想忽略夏令时。

public static DateTime GetLocalTime(string TimeZoneName)
{
    DateTime localDate = System.DateTime.Now.ToUniversalTime();

    // Get the venue time zone info
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName);
    // create a timezone without daylight savings
    var userTimeZone = TimeZoneInfo.CreateCustomTimeZone(tz.StandardName, tz.BaseUtcOffset, tz.DisplayName, tz.StandardName);
    DateTime dateTimeInUtc = TimeZoneInfo.ConvertTime(localDate, userTimeZone, TimeZoneInfo.Utc);

    return dateTimeInUtc;
}

You want to ignore daylight savings.

public static DateTime GetLocalTime(string TimeZoneName)
{
    DateTime localDate = System.DateTime.Now.ToUniversalTime();

    // Get the venue time zone info
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName);
    // create a timezone without daylight savings
    var userTimeZone = TimeZoneInfo.CreateCustomTimeZone(tz.StandardName, tz.BaseUtcOffset, tz.DisplayName, tz.StandardName);
    DateTime dateTimeInUtc = TimeZoneInfo.ConvertTime(localDate, userTimeZone, TimeZoneInfo.Utc);

    return dateTimeInUtc;
}
╭⌒浅淡时光〆 2024-09-16 02:18:58

要正确转换 DateTime 与 TimeZone 和夏令时,请使用 TimeZoneInfo.ConvertTime 方法。

TimeZoneInfo.ConvertTime(myDateTime, toTimeZone)
TimeZoneInfo.ConvertTime(myDateTime, fromTimeZone, toTimeZone)

To correctly convert DateTime with TimeZone and Daylight Saving Time, use the TimeZoneInfo.ConvertTime method.

TimeZoneInfo.ConvertTime(myDateTime, toTimeZone)
TimeZoneInfo.ConvertTime(myDateTime, fromTimeZone, toTimeZone)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文