Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find current moscow time using c#

Tags:

timezone

c#

.net

I'm trying to get local Moscow time in my .NET application.

Here is how far I went:

private static Dictionary<TimeZoneEnum, string> timeZoneDict = new Dictionary<TimeZoneEnum, string>()
{
    {TimeZoneEnum.RussiaStandardTime, "Russian Standard Time"}
};

public static DateTime GetDateTimeNowForTimeZone(TimeZoneEnum timezone)
{
    DateTime result = TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(timeZoneDict[timezone]));
    return result;
}

Method GetDateTimeNowForTimeZone gives me GMT+3 time but now in russia there is GMT+4 time. Is there anyu way to get robust solution for this issue in .net framework ?

like image 316
gruber Avatar asked Dec 19 '22 20:12

gruber


2 Answers

You need to patch your system. On my system (Windows 7 with all updates installed) it gives the correct time:

DateTime.UtcNow.Dump();
TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Russian Standard Time")).Dump();

output:

17/12/2013 09:11:08
17/12/2013 13:11:08

Microsoft has released an update a while ago to accomodate Russian DST law changes, you obviously don't have it.

*Dump() is an extension method provided by LINQPad. Equivalent of Console.WriteLine(<...>.ToString())

like image 81
DarkWanderer Avatar answered Dec 22 '22 11:12

DarkWanderer


I think this issue will get resolved with a patch by Microsoft. They update the timezones periodically.

As far as I know for now all you can do is +1 the time.

Of course you could also go down the path of some web time service and request the time from it.

like image 43
Alex Avatar answered Dec 22 '22 11:12

Alex