3

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.

enter image description here

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.

Siddharth
  • 4,142
  • 9
  • 44
  • 90

6 Answers6

4

Using the code below you can get local TimeZoneInfo object.

TimeZoneInfo infos = TimeZoneInfo.Local;
Deepak Arora
  • 440
  • 2
  • 11
  • using this, I am getting "Local" in output, I want "Asia/Kolkata" in output. – Siddharth May 15 '16 at 10:56
  • use infos.DisplayName property to get the display name from TimeZoneInfo object. – Deepak Arora May 15 '16 at 10:58
  • 1
    Close. Use `Id`, not `DisplayName` – Matt Johnson-Pint May 15 '16 at 16:12
  • @MattJohnson, it is not displaying Asia/Kolkata like that. – Siddharth May 16 '16 at 15:23
  • @Siddharth - `TimeZoneInfo.Local.Id` is indeed the correct API to use. What does it return in Unity? There is [an open bug related to `TimeZoneInfo` in Unity](https://issuetracker.unity3d.com/issues/win-system-dot-timezoneinfo-does-not-return-any-time-zones) - though about a different part of the problem. Still, this may be related. See also [this question](http://stackoverflow.com/q/35043022/634824). – Matt Johnson-Pint May 16 '16 at 15:49
  • @MattJohnson, I am getting "Local" word in output. Let me check above links. – Siddharth May 16 '16 at 16:21
  • @MattJohnson, I want "Asia/Kolkata" in output and they are passing as argument that thing. I want as return statement. – Siddharth May 16 '16 at 16:23
  • @MattJohnson, Using linked answer, how can I figure our which time zone is working and which one not because I want to run my game in all time zones. – Siddharth May 16 '16 at 16:28
  • I didn't add an answer of my own because I don't work with Unity, so I don't know for certain. My understanding is that there's a bug in Unity and this is broken. I don't know much more than that. Sorry. – Matt Johnson-Pint May 16 '16 at 16:33
  • @MattJohnson, its okay, you try to help me that is really big thing for me :) – Siddharth May 16 '16 at 16:37
  • See also [this Unity forum thread](http://answers.unity3d.com/questions/726640/some-problem-on-timezoneinfolocal.html) where another user states that he belives TimeZoneInfo class is broken in Unity 5.1+. – Matt Johnson-Pint May 16 '16 at 23:45
3

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
Ali Nadalizadeh
  • 2,726
  • 3
  • 22
  • 24
1

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#?

Community
  • 1
  • 1
1

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

Muhammad Hassan
  • 475
  • 2
  • 14
  • In this FindSystemTimeZoneById("India Standard Time") not working, I already given exception in my question. – Siddharth May 15 '16 at 11:00
  • Above code contains some syntax error so it can't able to run – Siddharth May 15 '16 at 11:02
  • Ok By searching I found a link solve your problem by implementing an helper function you can see this link : http://stackoverflow.com/questions/5996320/net-timezoneinfo-from-olson-time-zone – Muhammad Hassan May 15 '16 at 11:17
  • Try to use this `TimeZoneInfo infos = System.TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"); Console.WriteLine(infos);` You will find this properties that can help you `infos.infos.Id` `infos.DisplayName` `infos.BaseUtcOffset` – Muhammad Hassan May 15 '16 at 12:14
  • @Muhmmad Hassan, TimeZoneInfo infos = System.TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"); is generating exception for me. I have already mentioned in question. – Siddharth May 15 '16 at 12:48
0

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);
    }
}
이성수
  • 1
  • 1
0

Update your player settings to use .NET 4.6

Nolemonpledge
  • 139
  • 1
  • 2
  • 11