.NET PCL exception when converting time from UTC to specified TimeZone

I am developing a project in Xamarin Studio using C #. Its a .net PCL project and my profile is 78. My problem is that I cannot convert DateTime from UTC to the specified timezone. I am using below code to convert DateTime from UTC to specified local TimeZone.

   DateTime dateTime = (TimeZoneInfo.ConvertTime (DateTime.SpecifyKind (DateTime.UtcNow, DateTimeKind.Utc), profile.TimeZone));

      

I am getting below exception

The Kind property of the dateTime parameter is DateTimeKind.Utc, but the sourceTimeZone parameter is not TimeZoneInfo.Utc.

There is TimeZoneInfo.ConvertTime

no parameter to specify in PCL TimeZoneInfo sourceTimeZone

. It only has 2 overloads with the options below.

ConvertTime (DateTime, TimeZoneInfo) and ConvertTime (DateTimeOffset, TimeZoneInfo)

TimeZoneInfo exists only to indicate the purpose of TimeZoneInfo.

It also has no methods TimeZoneInfo.ConvertTimeFromUtc, TimeZoneInfo.ConvertTimeToUtc

.

Please help me fix this.

+2


source to share


1 answer


To add a comment to Hans:

It's all by design. Time zone conversion requires a database operating system that keeps track of time zone rules around the world. Available on a desktop class computer that is not available on limited devices such as a phone. Without a database, you can only know about UTC and the timezone the device is configured for. You cannot use PCL if required, using a commercial web service so that conversion would be a workaround for you.



Check out Noda Time . It is a date / time library for .NET that has its own timezone data so it doesn't need to rely on the OS. It also supports PCL.

+5


source







All Articles