You’ll want to use the TimeZoneInfo class.
You can get the TimeZoneInfo for the configuration of the current system or for a certain zone using its identifier like this
TimeZoneInfo ctz = TimeZoneInfo.Local;
TimeZoneInfo mstz = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
The better idea is don’t bother. Either use UTC for anything internal, or use local time for anything the player sees. Unless you intend to tie a lot of features to time of day (which is likely to get maddening), it’s probably better not to.
using UnityEngine;
using System.Collections;
using System;
public class test : MonoBehaviour {
void Start () {
TimeZoneInfo mstz = TimeZoneInfo.FindSystemTimeZoneById ("Mountain Standard Time");
if (mstz.IsDaylightSavingTime (DateTime.Now) ) {
Debug.Log ("now");
}
else {
Debug.Log ("not now");
}
}
}
The device has the timezones, I looked it up and found all the files, with equal name.
When I run the code from the stack overflow answer (with Visuals Studio, independent from Unity) , I get all the Time Zones as a result.
Don’t know where the mistake is…
EDIT:
using System;
namespace TimeZoneIds
{
class Program
{
static void Main(string[] args)
{
DateTime date1 = new DateTime(2015, 7, 1, 12, 0, 0);
TimeZoneInfo active_Timezone = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
if (active_Timezone.IsDaylightSavingTime(date1.Date))
{
Console.WriteLine("DST");
}
else
{
Console.WriteLine("no DST");
}
}
}
}
This code works in my standalone compiler, but not in Unity. Always the same Error.
fails to execute. Even wrapping this with a try get causes the code to simply halt - crazy!
void Start ()
{
try
{
foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones()) // Script just stops here.
Debug.Log(z.Id);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
I did a bit of googling and it looks like this has been a common problem in a number of versions of Mono. I couldn’t nail down a bug report referring to Unity’s version of Mono but I did find that Xamarin has the TimeZoneInfo listed in their docs as a .net version 4 class, despite it being listed as available in .net 3.5 by Microsoft. My only guess is that the version of Mono used by Unity does not support this class.
I checked around a bit on GitHub for an open source alternative without any luck. I’m sure something is out there but I don’t have a lot of time to look. Afraid I haven’t got any more ideas. Sorry!
ok … i have the same problem ive been looking for a solution but this whole class is not getting any information about timezones. Since it is the only class that can search for information on different offsets and timezone regions we are unable to determine for exemple if its daylight saving time in a given region. i already filed a bug about it on unity.
Possible solutions (that i still didnt manage to achive since im a new programmer) : this .net API that has the same timezoneinfo functionality (but for some cthulluonic reason its failing to load the timezone information).
case you are developping to android or ios you could try to use their respective classes to solve timezones and then send it back to unity (i cant even do that.)
those are the solutions i found. Oh there is a tird one, trying to build a class yourself and get timezone data from this page in here .