Access Current System Time Zone

大城市里の小女人 提交于 2020-06-27 12:49:06

问题


Basically I can able to detect list of system time zones using following code:

foreach(TimeZoneInfo info in tz)
    Debug.Log("time zone id : " + info.Id + " display name : " + info.DisplayName);

Running this code, I have following output in console.

In this list, I want to know, which one is my current system is following? Because I want specific information about that time zone. As like in following code, I was written clear "Asia/Kolkata".

TimeZoneInfo infos = System.TimeZoneInfo.FindSystemTimeZoneById("Asia/Kolkata");

By running which code, I can able to get "Asia/Kolkata" as output?

EDIT: I was already using NodaTime here and code running for this purpose.

  // Create a NodaTime DateTimeZoneSource object for IANA time zone names
    var dateTimeZoneSource = TzdbDateTimeZoneSource.Default;

    // Get the Windows time zone by name (or use TimeZoneInfo.Local)
    var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");

    // Convert between Windows and IANA time zone names
    var tzdbTimeZoneInfo = dateTimeZoneSource.MapTimeZoneId(timeZoneInfo);

    Console.WriteLine(tzdbTimeZoneInfo);

But in this, I am getting exception regarding,

Exception of type 'System.TimeZoneNotFoundException' was thrown.

I am using Mac system and editor as Mono Develop - Unity and my target devices are iPhones.


回答1:


Using the code below you can get local TimeZoneInfo object.

TimeZoneInfo infos = TimeZoneInfo.Local;



回答2:


maybe help u this links :

here https://msdn.microsoft.com/en-us/library/gg154758.aspx

and

here

List of Timezone ID's for use with FindTimeZoneById() in C#?




回答3:


Use this

TimeZoneInfo infos = TimeZoneInfo.FindSystemTimeZoneById(
             { "Asia/Kolkata", "India Standard Time" }); 

You can see this link for TimeZoneIds

http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html




回答4:


Set your .NET API compatibility to 4.6. Here are the test results in my iOS device:

TimeZoneInfo.Local.Id
Editor: Local
iOS Device: Local

TimeZoneInfo.Local.DisplayName
Editor: Local
iOS Device: (GMT+03:30) Local Time

TimeZoneInfo.Local.StandardName
Editor: +0330
iOS Device: +0330

TimeZoneInfo.Local.DaylightName
Editor: +0430
iOS Device: +0430

TimeZoneInfo.Local.BaseUtcOffset.Hours + ":" + TimeZoneInfo.Local.BaseUtcOffset.Minutes);
Editor: 3:30
iOS Device: 3:30



回答5:


On window Unity5.1.3 editor, TimeZoneInfo.GetSystemTimeZones() return empty list. TimeZoneInfo.FindSystemTimeZoneById() and TimeZoneInfo.CreateCustomTimeZone() both raise System.TimeZoneNotFoundException. but it work well in android device.

private static DateTime ToSeoulTime(DateTime dateUtc, bool ignoreUnspecified = false)
{
    if (ignoreUnspecified == false && dateUtc.Kind == DateTimeKind.Unspecified)
    {
        if (UnityEngine.Application.isEditor)
        {
            throw new Exception("dateUtc.Kind == DateTimeKind.Unspecified");
        }
        else
        {
            UnityEngine.Debug.LogWarning("dateUtc.Kind == DateTimeKind.Unspecified");
        }
    }
    try
    {
        var seoulTimezone = TimeZoneInfo.FindSystemTimeZoneById("Korea Standard Time");
        return TimeZoneInfo.ConvertTime(dateUtc, seoulTimezone);
    }
    catch (System.TimeZoneNotFoundException e)
    {
        return new DateTime(dateUtc.AddHours(9).Ticks, DateTimeKind.Unspecified);
    }
}



回答6:


Update your player settings to use .NET 4.6



来源:https://stackoverflow.com/questions/37237223/access-current-system-time-zone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!