Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Determine if date is Daylight Saving Time or not

Discussion in 'Scripting' started by S_P_S, Jul 22, 2015.

  1. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    Hey,

    the title actually says everything.

    I want to determine if a date with information about the time is daylight saving time or not, depending on the location.

    How do do this?
    Maybe here is the right way? :
    https://msdn.microsoft.com/en-us/library/bb384268(v=vs.110).aspx

    Maybe you can give me some ideas how you would deal with the topic.

    Regards
     
  2. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    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
    Code (csharp):
    1. TimeZoneInfo ctz = TimeZoneInfo.Local;
    2. TimeZoneInfo mstz = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
    Then use the method IsDaylightSavingTime like this
    Code (csharp):
    1. ctz.IsDaylightSavingTime(DateTime.Now);
     
  3. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    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.
     
  4. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    Thanks for the answers!

    @eisenpony
    Where can I find a list of the TimeZone ID's for:
    Code (CSharp):
    1. TimeZoneInfo mstz = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
    Regards
     
  5. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
  6. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    Thanks for the link!

    I tried this:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5.  
    6. public class test : MonoBehaviour {
    7.  
    8.     void Start () {
    9.  
    10.         TimeZoneInfo mstz = TimeZoneInfo.FindSystemTimeZoneById ("Mountain Standard Time");
    11.        
    12.         if (mstz.IsDaylightSavingTime (DateTime.Now) ) {
    13.            
    14.             Debug.Log ("now");
    15.         }
    16.         else {
    17.            
    18.             Debug.Log ("not now");
    19.            
    20.         }
    21.     }
    22. }
    23.  
    But I always get the Error:
     
  7. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    Does somebody worked with the TimeZoneInfo.FindSystemTimeZoneById method and can tell me where my mistake is?

    Regards
     
  8. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    the device you are on might not have that zone. Did you try running the code from the stack overflow answer? It should list the available zones.
     
  9. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    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:
    Code (CSharp):
    1. using System;
    2.  
    3. namespace TimeZoneIds
    4. {
    5.     class Program
    6.     {
    7.  
    8.      
    9.  
    10.         static void Main(string[] args)
    11.         {
    12.             DateTime date1 = new DateTime(2015, 7, 1, 12, 0, 0);
    13.  
    14.             TimeZoneInfo active_Timezone = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
    15.  
    16.             if (active_Timezone.IsDaylightSavingTime(date1.Date))
    17.             {
    18.  
    19.                 Console.WriteLine("DST");
    20.             }
    21.             else
    22.             {
    23.  
    24.                 Console.WriteLine("no DST");
    25.  
    26.             }
    27.         }
    28.     }
    29. }
    This code works in my standalone compiler, but not in Unity. Always the same Error.

    Regards
     
    Last edited: Jul 27, 2015
  10. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    I tried this myself and found that
    Code (csharp):
    1. TimeZoneInfo.GetSystemTimeZones()
    fails to execute. Even wrapping this with a try get causes the code to simply halt - crazy!
    Code (csharp):
    1. void Start ()
    2. {
    3.   try
    4.   {
    5.     foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones()) // Script just stops here.
    6.       Debug.Log(z.Id);
    7.   }
    8.   catch (Exception e)
    9.   {
    10.     Debug.LogError(e);
    11.   }
    12. }
    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!
     
  11. Lancemaker_

    Lancemaker_

    Joined:
    Nov 14, 2011
    Posts:
    34
    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 .