简体   繁体   English

.NET:使用GMT偏移从日期获取UTC DateTime

[英].NET: Getting UTC DateTime from date with GMT offset

I have four integers: 我有四个整数:

  • Day of month (1 - 31) 每月的日子(1 - 31)
  • Month of year (1 - 12) 一年中的一个月(1 - 12)
  • Year
  • Hour of day (0 - 23) 一天中的小时(0-23)

These integers represent a date and time that a user on my web page has selected. 这些整数表示我的网页上的用户选择的日期和时间。 They could be anywhere on earth. 他们可能在地球上的任何地方。

Thankfully, I have the GMT offset of their location on earth. 值得庆幸的是,我在地球上的位置有GMT偏移量。 It's a decimal. 这是一个小数。

How do I take these four integers, plus GMT offset decimal, and get a DateTime in UTC that represents them? 如何获取这四个整数,加上GMT偏移小数,并获得代表它们的UTC DateTime时间?

To answer this question, fill in the method body of this function: 要回答这个问题,请填写此函数的方法体:

public static DateTime UtcDateTime(int day, int month, int year, int hour, decimal gmtOffset) { // todo } public static DateTime UtcDateTime(int day,int month,int year,int hour,decimal gmtOffset){// todo}

I would recommend using DateTime in conjunction with TimeZoneInfo . 我建议将DateTimeTimeZoneInfo结合使用。 You can store UTC time on your server, and convert time to every user using his own TimeZoneInfo . 您可以在服务器上存储UTC时间,并使用自己的TimeZoneInfo将时间转换为每个用户。 TimeZoneInfo can be set by user, or you can somehow extract it from Windows settings, if it is possible. TimeZoneInfo可以由用户设置,或者如果可能的话,您可以以某种方式从Windows设置中提取它。 We ask users to set their TimeZoneInfo (via drop-down list) on their settings page. 我们要求用户在其设置页面上设置TimeZoneInfo (通过下拉列表)。

Update 更新

While the Microsoft states in MSDN that we should use DateTimeOffset rather than DateTime there are some problems using DateTimeOffset . 虽然Microsoft在MSDN中声明我们应该使用DateTimeOffset而不是DateTime但使用DateTimeOffset会遇到一些问题。 I failed trying to serialize/deserialize DataTimeOffset instances transferring them via WCF. 我没有尝试序列化/反序列化通过WCF传输它们的DataTimeOffset实例。 And DateTime/TimeZoneInfo works fine. 而DateTime / TimeZoneInfo工作正常。 I like DateTimeOffset a lot, but it still isn't suitable for real apps in some ways. 我很喜欢DateTimeOffset,但它在某些方面仍然不适合真正的应用程序。

And another one downside of DateTimeOffset is that it doesn't contain all the information you need to convert time, because it contains only an offset. DateTimeOffset的另一个缺点是它不包含转换时间所需的所有信息,因为它只包含一个偏移量。 But there can be several actual TimeZones with the same offset, so you can't restore time zone given only offset. 但是可能有几个具有相同偏移量的实际时区,因此您无法仅在给定偏移的情况下恢复时区。 And time zone is not just an offset, it is the set of rules defining time conversion, winter/summer time etc 时区不仅仅是一个偏移,它是定义时间转换,冬季/夏季时间等的一套规则

Update 更新

Here you are: 这个给你:

private static DateTime ToUTC(int day, int month, int year, int hour, decimal utcOffset)
{
    TimeSpan offset = TimeSpan.FromMinutes((double)(utcOffset * 60)); // time zone offset is always aligned to minutes
    return new DateTimeOffset(year, month, day, hour, 0, 0, offset).ToUniversalTime().DateTime;
}

You should use DateTimeOffset. 您应该使用DateTimeOffset。

Convert the decimal into the precision you require then construct as below (using FromSeconds as an example): 将小数转换为您需要的精度,然后如下构造(使用FromSeconds作为示例):

new DateTimeOffset(year,month,day,hour,0,0,TimeSpan.FromSeconds(offset)); new DateTimeOffset(year,month,day,hour,0,0,TimeSpan.FromSeconds(offset));

http://msdn.microsoft.com/en-us/library/system.datetimeoffset(v=VS.90).aspx http://msdn.microsoft.com/en-us/library/system.datetimeoffset(v=VS.90).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM