System.TimeZone depreciated

Assets\Scripts\Systemz\GameRoute.cs(263,42): warning CS0618: ‘TimeZone’ is obsolete: ‘System.TimeZone has been deprecated. Please investigate the use of System.TimeZoneInfo instead.’

I was getting UNIX time like this

int currentOffset = (int)TimeZone.CurrentTimeZone.GetUtcOffset( System.DateTime.Now).TotalSeconds;

Anyone know a work around to do this with TimeZoneinfo?

Why mess with time zones at all? If you want UTC time, you can just get it directly via System.DateTime.UtcNow.

This has a simple code snippet for converting that to Unix Epoch time:

Thanks man
I’m actually not messing with time zones. That code just is lol.
Something I got when I googled unix time.

I just simply need the current unix time

I guess i was wrong. I am trying to grab the time offset for the player lol.
This below works for windows and web gl platform. It fails on Android though.

TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
                int currentOffset = (int)t.TotalSeconds;

I don’t understand how that is working on windows and not on android.
That code above is just getting UTC time offset.

What I need is to get the users time offset from their system.

I’m using CultureInfo.InvariantCulture in my game. The code sets a new background and lighting in the morning and resets the scene to night according to the computer time.

private void Update()
{
    DateTime nightTime = DateTime.ParseExact("22:00", "HH:mm", CultureInfo.InvariantCulture);
    DateTime dayTime = DateTime.Parse("06:00");
    DateTime now = DateTime.Now;

    if(now >= dayTime && now <= nightTime)
    {
        SetDayTime();
    }
}

I figured it out

TimeZoneInfo localZone = TimeZoneInfo.Local;
int currentOffset = (int)localZone.BaseUtcOffset.TotalSeconds;

I live in Missouri so this returns -21000 for me.

Or if you need display formats
localZone.StandardName will return (UTC-08:00) Pacific Time (US & Canada)
localZone.StandardName will return Pacific Standard Time.
localZone.BaseUtcOffset will return -06:00:00

1 Like