.NET PCL例外,而将时间从UTC到指定的时区 [英] .NET PCL exception while converting time from UTC to specified TimeZone

查看:185
本文介绍了.NET PCL例外,而将时间从UTC到指定的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用C#在Xamarin Studio中的一个项目。它是一种.NET PCL项目,我的个人资料是78。我的问题是,我无法为DateTime转换UTC到指定的时区。我使用低于code转换日期时间从UTC到指定的本地时区。

I am developing a project in Xamarin Studio using C#. Its a .net PCL project and my profile is 78. My problem is, i am unable to convert a DateTime from UTC to 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

dateTime参数的Kind属性为DateTimeKind.Utc,但   在sourceTimeZone参数不等于TimeZoneInfo.Utc。

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

在PCL TimeZoneInfo.ConvertTime 没有指定的的TimeZoneInfo sourceTimeZone 的参数。它只有2以下参数重载。

In PCL TimeZoneInfo.ConvertTime doesn't have a parameter for specifying the TimeZoneInfo sourceTimeZone. It has only 2 overloads with below parameters.

ConvertTime(日期时间,的TimeZoneInfo)及ConvertTime(的DateTimeOffset,   的TimeZoneInfo)

ConvertTime(DateTime, TimeZoneInfo) & ConvertTime(DateTimeOffset, TimeZoneInfo)

的TimeZoneInfo只存在指定目的地的TimeZoneInfo。

TimeZoneInfo exist only to specify destination TimeZoneInfo.

此外,它不具有 TimeZoneInfo.ConvertTimeFromUtc,TimeZoneInfo.ConvertTimeToUtc 方法。

请人帮我解决这个问题。

Please someone help me to fix this.

推荐答案

要添加到汉斯的评论:

这是都通过设计。时区的转换需要一个操作系统,保持在世界各地的时区的规则轨道的数据库。可在桌面级的机器,而不是像手机有限的设备可用。如果没有数据库,你只能知道一些有关UTC和器件配置该时区。您不能使用PCL,如果这是一个要求,使用商用的Web服务进行转换对你将是一个解决办法。

This is all entirely by design. Timezone conversions requires an operating system with a database that keeps track of the timezone rules across the world. Available on a desktop class machine, not available on limited devices like a phone. Without the database, you can only know something about UTC and the timezone for which the device was configured. You cannot use PCL if this is a requirement, using a commercial web service to make the conversion for you would be a workaround.

看一看野田佳彦时间。这是一个日期/时间库.NET有它自己的时区的数据,因此不必依赖操作系统。它还支持PCLS。

Have a look at Noda Time. This is a Date/Time library for .NET which has its own time zone data so it doesn't have to rely on the OS. It also supports PCLs.

这篇关于.NET PCL例外,而将时间从UTC到指定的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆