【问题标题】:timezone conversion in wp8 C#wp8 C#中的时区转换
【发布时间】:2014-05-27 13:57:50
【问题描述】:

我想知道没有办法将时区的日期和时间转换为另一个时区! 我有很多关于时区转换的例子,而且都在非 WP SDK 上。令人惊讶的是,WP SDK 的命名空间 System 的 TimeZoneInfo 类没有 FindSystemTimeZoneById() 的方法。

这里的Converting time between Timezones 和How to convert a datetime to specific timezone in c#? 是在.net 上转换时区的非常好的例子,除了WP SDK。 假设一个场景,我在西澳大利亚标准时间有一段时间,现在我需要将该时间转换为东部标准时间。通常我会这样做:

string timeZoneStringOne = "W. Australia Standard Time";
TimeZoneInfo timeZoneIDOne = TimeZoneInfo.FindSystemTimeZoneById(timeZoneStringOne);

string timeZoneStringTwo = "Eastern Standard Time";
TimeZoneInfo timeZoneIDTwo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneStringTwo);

DateTime dt = new DateTime(2010, 02, 08, 05, 00, 00);

DateTimeOffset dtOffset1 = new DateTimeOffset(dt, timeZoneIDOne.GetUtcOffset(dt));
Console.WriteLine(dtOffset1);

DateTimeOffset dtOffset2 = TimeZoneInfo.ConvertTime(dtOffset1, timeZoneIDTwo);
Console.WriteLine(dtOffset2);

DateTimeOffset dtOffset3 = TimeZoneInfo.ConvertTime(dtOffset2, timeZoneIDOne);
Console.WriteLine(dtOffset3);

Console.ReadKey();

/*
  Output :
  2/8/2010 5:00:00 AM +08:00
  2/7/2010 4:00:00 PM -05:00
  2/8/2010 5:00:00 AM +08:00
*/

但是如何在 windows phone 8 上做到这一点???

【问题讨论】:

  • 这需要一个时区信息数据库。你有一个放在桌面上,它对你的裤袋来说太重了。所以没什么了不起的。使用网络服务或保留自己的网络服务是一种方法。

标签: c# .net datetime windows-phone-8 timezone


【解决方案1】:

不幸的是,TimeZoneInfo 对象在 WP8 和 WinRT 上被削弱了,因为没有 FindSystemTimeZoneById,甚至没有 Id 属性。

恕我直言,最好的解决方案是改用Noda Time。当编译为可移植类库 (PCL) 时,它可以在 WP8 上正常运行。 PCL 是最新 NuGet 包中的默认构建,因此您应该可以轻松使用它。

但是,您需要使用 IANA time zones 而不是 Microsoft 的,因为 BCL 时区提供程序在便携式版本中不可用。

// Get the input value
LocalDateTime ldt1 = new LocalDateTime(2010, 2, 8, 5, 0, 0);
DateTimeZone tz1 = DateTimeZoneProviders.Tzdb["Australia/Perth"];
ZonedDateTime zdt1 = ldt1.InZoneLeniently(tz1);

// Convert to the target time zone
DateTimeZone tz2 = DateTimeZoneProviders.Tzdb["America/New_York"];
ZonedDateTime zdt2 = zdt1.WithZone(tz2);

// If you need a DateTimeOffset, you can get one easily
DateTimeOffset dto = zdt2.ToDateTimeOffset();

【讨论】:

    【解决方案2】:

    最近我在不使用任何其他 Lib 的情况下获得了另一个解决方案。就我而言,它解决了我的问题。就我而言,一个时区是固定的(例如,我的时区是 UTC -4 并且需要将其转换为系统本地时间),因此我可以将其转换为 UTC 时间和然后将其转换为我系统的本地时区,在这种特殊情况下,使用 DateTime 的 AddHours() 方法将其转换为 wp sdk。这里是:

    string GetLocalDateTime(DateTime targetDateTime)
    {
        int fromTimezone = -3;
        int localTimezone;
    
        if (TimeZoneInfo.Local.BaseUtcOffset.Minutes != 0)
            localTimezone = Convert.ToInt16(TimeZoneInfo.Local.BaseUtcOffset.Hours.ToString() + (TimeZoneInfo.Local.BaseUtcOffset.Minutes / 60).ToString());
        else
            localTimezone = TimeZoneInfo.Local.BaseUtcOffset.Hours;
    
        DateTime Sdt = targetDateTime;
        DateTime UTCDateTime = targetDateTime.AddHours(-(fromTimezone));
        DateTime localDateTime = UTCDateTime.AddHours(+(localTimezone));
    
        return localDateTime.ToLongDateString() + " " + localDateTime.ToShortTimeString();
    }
    

    【讨论】:

    • 您可以通过将BaseUtcOffset 保留为TimeSpan 来消除所有逻辑和字符串操作。但即便如此,您也没有考虑当地时区的 DST。
    • 另外,您可以简单地在DateTime 上使用ToLocalTime,然后将其调整为UTC。或者更好的是,只需从源值创建一个DateTimeOffset,然后在其上使用ToLocalTime。我会将此作为答案,除了您最初的问题是关于在两个 named 时区之间转换,而不是在固定输入和本地区域之间转换。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2011-08-15
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 2017-03-05
    相关资源
    最近更新 更多