为什么 UtcDateTime 函数没有将偏移量添加到 UTC 日期?

JSON

对于即时 DateTime 跟踪,我使用的是DateTimeOffset数据类型。以下函数将用户对应的 TimeZone ID 偏移量添加到 UTC DateTime 属性DateTimeOffset

根据文档UtcDateTime将对DateTimeOffset. 下面的代码虽然没有。为什么转换没有发生?

添加 TimeSpan 偏移量的函数,

public static DateTimeOffset GetUtcDateTime (DateTime sourceDateTime, string timeZoneId) {
 TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById (timeZoneId);
 TimeSpan offset = timeZone.GetUtcOffset (sourceDateTime);
 DateTimeOffset utcTime = new DateTimeOffset (sourceDateTime, offset);
 return utcTime;
 }

在这里我试图转换,

DateTimeOffset utcDate = (DateTime.UtcNow);
DateTime fromUtc = utcDate.DateTime;
DateTimeOffset UtcDate = StaticHandlers.GetUtcDateTime (fromUtc, "America/Los_Angeles");
Console.WriteLine ("UTC now is {0} and UTC Date LA is {1} and UtcDateTime LA is {2}", utcDate, UtcDate, utcDate.UtcDateTime);

输出是,

UTC 现在是 5/8/18 6:43:37 AM +00:00 和 UTC Date LA 是 5/8/18 6:43:37 AM -07:00 UtcDateTime LA 是 5/8/18 6:43 :37 上午

更新,

我想保留 UTC 和用户偏移量以进行跟踪。在这种情况下,夏令时很重要。下面的例子显示了我在说什么。

DateTime currentDateTime = DateTime.Now;
DateTime beforeDST_LA = new DateTime (2018, 3, 11, 0, 0, 0);
DateTime afterDST_LA = new DateTime (2018, 3, 12, 0, 0, 0);
TimeSpan offsetCurrent = tzi.GetUtcOffset (currentDateTime);
TimeSpan offsetBeforeDST = tzi.GetUtcOffset (beforeDST_LA);
TimeSpan offsetAfterDST = tzi.GetUtcOffset (afterDST_LA);
Console.WriteLine ("Current offset is {0} before DST is {1} and After DST is {2}", offsetCurrent, offsetBeforeDST, offsetAfterDST);

DST 之前的当前偏移量为 -07:00:00,DST 之后为 -08:00:00,DST 之后为 -07:00:00

马特·约翰逊-品脱

首先,我不会调用您的 function GetUtcDateTime,因为它不是这样做的。它试图DateTimeOffset为特定时间的特定时区获取 ,因此将其称为GetDateTimeOffset.

您在代码中缺少的主要概念是DateTime具有.Kind设置DateTimeKind值的属性代码中的几个地方考虑​​了这种类型:

  • GetUtcOffset在确定偏移量之前将转换UtcLocal种类到提供的区域。

  • new DateTimeOffset 如果种类和偏移量冲突(如果您提供偏移量),(构造函数)将出错。

  • 当您将 a 分配DateTime给 a 时DateTimeOffset,隐式转换正在评估种类。

  • 当您.DateTime调用DateTimeOffset,种类将始终Unspecified- 无论偏移量如何。

如果您将所有这些都考虑在内,您就会意识到在调用 之前需要自己检查种类GetUtcOffset如果不是, Unspecified那么您需要在获取偏移量之前将其转换为指定的时区。

public static DateTimeOffset GetDateTimeOffset(DateTime sourceDateTime, string timeZoneId)
{
    TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);

    // here, is where you need to convert
    if (sourceDateTime.Kind != DateTimeKind.Unspecified)
        sourceDateTime = TimeZoneInfo.ConvertTime(sourceDateTime, timeZone);

    TimeSpan offset = timeZone.GetUtcOffset(sourceDateTime);
    return new DateTimeOffset(sourceDateTime, offset);
}

既然已经解决了这个问题,请转到下一组问题,这就是您所说的问题。

DateTimeOffset utcDate = (DateTime.UtcNow);
DateTime fromUtc = utcDate.DateTime;

在第 1 行中,从DateTimeto的隐式转换DateTimeOffset将偏移量设置为00:00- 因为DateTime.UtcNowhas .Kind == DateTimeKind.Utc

在第 2 行,对.DateTime属性的调用设置了fromUtc.Kind == DateTimeKind.Unspecified从本质上讲,你已经剥离了那种。

所以不是这样,只需DateTime.UtcNow直接传递到函数中即可。这种类型将持续存在,并且一切都会起作用 - 现在Kind已经识别并且转换正在函数内部发生。

综上所述,如果您的原始值都是DateTimeOffset(例如,DateTimeOffset.UtcNow),那么您根本不需要该功能。直接打电话就行TimeZoneInfo.ConvertTimeDateTimeOffset

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将UTC偏移量添加到当前日期

将日期时间偏移量添加到 UTC 时间戳

为什么moment.utc(...)。diff(moment.utc(...))受源日期偏移量影响

在Javascript中为UTC日期添加偏移量

将具有UTC偏移量的字符串转换为日期时间对象

Java日历:为什么要逆转UTC偏移量?

根据日期格式获取UTC日期偏移量

续集,Postgres。保存日期,包括 UTC 偏移量

如何设置日期时间的UTC偏移量?

使用偏移量比较将 UTC 时间转换为本地日期时间的 XSL

如何将日期,时间和UTC偏移量转换为本地DateTime

Python ValueError:两个日期都必须具有与.loc相同的UTC偏移量

通过添加/减去本地偏移量来计算日期时间[将任何日期时间转换为UTC时间格式]

为什么TZ = UTC-8会产生UTC + 8的日期?

将本地时间转换为 UTC 并添加到 UTC 日期时间时刻

将UTC日期转换为本地日期

为什么 Oracle 隐式地将 SYS_EXTRACT_UTC 添加到索引中?

为什么JS承担字符串日期没有时间为UTC,如果0填充和地方如果不加垫?

Python UTC日期时间对象的ISO格式不包含Z(Zulu或零偏移量)

根据 UTC 日期按天分组和聚合,调整为指定的时区偏移量

如何在DateTimeOffset中保留日期并将偏移量重置为零(UTC)?

Laravel 5.3-碳日期-UTC偏移量获取时区名称

SQL 使用 UTC 偏移量和夏令時轉換日期

乔达(Joda)时间更改日期调整时的UTC偏移量

AIF转换的有效UTCDateTime类型字符串格式是什么?

红宝石日期宝石utc偏移

向 Boomi 中没有的日期字符串添加偏移量 - Java/“Groovy”脚本映射函数

UTCDateTime :: toDateTime()方法returnig 1970日期时间

在UTC中将HOUR_OF_DAY设置为0时,为什么GregorianCalendar更改日期?