Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

DateTime.Now is incorrectly returning UtcNow in .NET 4.6 version

Discussion in 'Experimental Scripting Previews' started by CptDustmite, Nov 20, 2017.

  1. CptDustmite

    CptDustmite

    Joined:
    Jun 17, 2014
    Posts:
    61
    This bug was already reported here: https://issuetracker.unity3d.com/is...tc-time-regardless-of-your-current-local-time

    Datetime.Now returns UTC times instead of local user times.

    I'd like to know if there is any estimated time of fix for this, as it's a major bug for us and breaks all our features relying on local user time. We're too far in using .NET to go back now. Quite surprised that it was reported back in August and not addressed yet, and that so few have voted on the issue.

    Is there any workaround that people have tried which have worked for them? I'm thinking if you grab the lat/long off the device using LocationServices, then use TimeZoneInfo to get the UTC offset, you can essentially calculate the local time, HOWEVER this requires extra location permissions on device which is really not ideal, and additionally this local time needs to be calculated before any other code is initialised.

    Advice is appreciated.
     
    TapSlots and ajon542 like this.
  2. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    790
    That is probably on purpose because there are many people abusing DateTime.Now.
    Everytime you call it, it calls into system methods to convert the time to local time, which takes a while.
    With getting UTC time the method (the getter) is a lot faster, and you can still add your "utc to local" offset.
    So it's likely a performance thing.

    You can likely use the system language or culture info or something to get the right timezone as well instead of having to go by long/lat, but I'm not so sure about that.
     
  3. Jes28

    Jes28

    Joined:
    Sep 3, 2012
    Posts:
    621
  4. z000z

    z000z

    Joined:
    Dec 31, 2014
    Posts:
    91
    The problem is that the TimeZone of the device always reports as UTC so you can't find out what the correct TimeZone is to even do it manually. This also makes the .ToLocalTime() fail since the local time has been set to UTC.
     
  5. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    255
    We are looking into supporting TimeZoneInfo across as many platforms as possible. Some platforms do not expose any information other other Local and UTC. Rough guess is that this bug will get fixed in the 2018.1 time frame. Not sure if it will be backported to older releases. It will depend how invasive the changes are needed.
     
  6. Slot-Galaxy

    Slot-Galaxy

    Joined:
    Sep 30, 2016
    Posts:
    1
    Unfortunately this makes Unity's .Net 4.6 completely unusable. We just went through a migration for 4.6 upgraded a lot of 3rd party libraries and began using some of the new syntax only to find that our iOS notifications all schedule on UTC time. Now we could hack around this using our own native notifications plugin, or offset the timing knowing that DateTime.Now is messed up. However since iOS Notification Services is a Unity first party supported system, and it broke with this change, my trust has been broken. I cannot trust that there aren't other places that Unity is using DateTime.Now internally that are causing issues that haven't surfaced to us.

    I expect that the official answer will be to hide behind the "Experimental" tag on .Net 4.6, but it seems like with every Unity upgrade something fundamental breaks.
     
  7. Mikeysee

    Mikeysee

    Joined:
    Oct 14, 2013
    Posts:
    155
    It definately seems like something as fundamental as DateTime.Now should stay the same in 4.6 as in 3.5, pretty crazy that this was even allowed to do out the door as it was bound to break tons of stuff.
     
    TapSlots and ajon542 like this.
  8. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    255
    [
    The intention is not to hide behind anything. The .NET 4.6 upgrade is marked Experimental because it is not as stable as the old, default version. It is not the default upgrade path currently. We are fixing issues as they are reported, prioritizing things like crashes first.

    I mentioned that we are back porting fixes to released versions when possible, so it's likely this will get a fix in 2017.2 and 2017.3 patch/dot releases if the changes required aren't too risky. I am just comfortable saying it will definitely be addressed in 2018.1.
     
  9. mkderoy

    mkderoy

    Unity Technologies

    Joined:
    Oct 25, 2016
    Posts:
    22
    For those currently stuck on this, I've provided some code for a workaround for the time being.
    https://github.com/mderoy/MikeSnippets/blob/master/TimeZoneInfoWorkaround/TimeZoneInfoWorkaround.cs

    The workaround will only work on IL2CPP. Make sure you do not call the GetTimeZoneData icall when running against mono or you will throw an exception since the mono runtime no longer has that icall (You could implement your own GetTimeZoneData function in C/C++ or use an existing time library and pinvoke into it for a solution that works on mono as well). The example code uses our old .NET3.5 TimeZone icall to create a Custom TimeZoneInfo for the user's local time, then shows you how to set the Class Library's local time via reflection. This will fix DateTime.Now, and DateTime.UtcNow.ToLocalTime() for you until we've issued a fix. For simple time conversions, you can just use the custom timezone directly instead of using reflection. Again I'd like to emphasize that we're doing all we can to get modern .NET into the hands of Unity developers. We're tracking upstream mono's libraries, which for the most part should mean faster bug fixes and more stable libraries for you going forward.

    edit: change the loop to for(int year = 1973; year <= 2037; year++) for android
     
    Last edited: Dec 2, 2017
    Fillmore, MechEthan and dadude123 like this.
  10. MechEthan

    MechEthan

    Joined:
    Mar 23, 2016
    Posts:
    166
    Hmm... to me, "Experimental" (or "Alpha") means "don't use for anything that has a ship date or needs to actually function any time soon, because you don't know what's broken nor can you know when it will get fixed." Heck, something with that label might even get cancelled.

    So, while I fully agree with your frustrations on the matter, I just don't see it as an issue of trust, or breaking an implicit agreement, or whatever you want to call it.

    That all being said, it's thanks to people & teams like yours that this stuff gets found and prioritized correctly, and it sucks that you have to pay for it in time & money.
     
    joncham and dadude123 like this.
  11. Fillmore

    Fillmore

    Joined:
    Feb 14, 2018
    Posts:
    6
    I'm trying to use this, as Unity 2018.1.0b8 is still broken (Bug report submitted).

    I wanted to see the main method and all the print outs. So I did this:
    private void Awake()
    {
    Unity.CustomTimeZoneExample.Main(new string[1]);
    }

    MissingMethodException: Attempted to access a missing method.
    Unity.CustomTimeZoneExample.Main (System.String[] args) (at Assets/Scripts/Objects/TimeZoneInfoWorkaround.cs:46)
    Main.Awake () (at Assets/Scripts/Main.cs:37)

    Using the Unity Editor OSX.
     
  12. mkderoy

    mkderoy

    Unity Technologies

    Joined:
    Oct 25, 2016
    Posts:
    22
    @Fillmore The workaround will only work for il2cpp players.
    In any case the bug has been fixed, and will come to 2018.1 and 2017.3 within a few beta/patch releases.
     
  13. gboulard

    gboulard

    Joined:
    Nov 28, 2012
    Posts:
    21
  14. mkderoy

    mkderoy

    Unity Technologies

    Joined:
    Oct 25, 2016
    Posts:
    22
  15. gboulard

    gboulard

    Joined:
    Nov 28, 2012
    Posts:
    21
    couldn't find a ticket, except the forum link above.
    so i made one (on its way). will bump when i got the ticket entry.

    i made an empty project (last beta 2018.1.0b9), add a gameobject with a simple script that calls DateTime.Now in Awake function.
    switch to .Net 4.6 in the player settings and made a WebGL version.

    and got the exception raised.

    Thx for your time.
     

    Attached Files:

    mkderoy likes this.
  16. gboulard

    gboulard

    Joined:
    Nov 28, 2012
    Posts:
    21
unityunity