DateTime.TryParse中的奇怪行为

盖威克

我正在尝试使用以下内容解析DateTime字符串“ 5-5-5-5”

DateTime.TryParse("5-5-5-5", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out result);

我希望这可以转换为

5/5/2005 5:00:00 AM 

但是相反,它被转换为

5/5/2005 10:30:00 AM.

看起来像它将提供的日期时间解释为GMT。因为我已经指定了AssumeLocal标志,所以这有点违反直觉这是DateTime班上的错误吗?

高拉夫·古普塔(Gaurav Gupta)

我理解您的观点并调试点网框架代码以了解后台发生的情况。

TryParse方法将忽略DateTimeStyles.AssumeLocal标志,并且仅AdjustToUniversal处理标志。

if ((styles & DateTimeStyles.AdjustToUniversal) != 0) {
            return (AdjustTimeZoneToUniversal(ref result));
        }
        return (AdjustTimeZoneToLocal(ref result, bTimeOnly));

另一方面,TryParseExact方法已正确实现,并且具有处理DateTimeStyles.AssumeLocal标志所需的所有逻辑查看此实现中如何处理其他情况。

            // If AssumeLocal or AssumeLocal is used, there will always be a kind specified. As in the
            // case when a time zone is present, it will default to being local unless AdjustToUniversal
            // is present. These comparisons determine whether setting the kind is sufficient, or if a
            // time zone adjustment is required. For consistentcy with the rest of parsing, it is desirable
            // to fall through to the Adjust methods below, so that there is consist handling of boundary
            // cases like wrapping around on time-only dates and temporarily allowing an adjusted date
            // to exceed DateTime.MaxValue
            if ((styles & DateTimeStyles.AssumeLocal) != 0) {
                if ((styles & DateTimeStyles.AdjustToUniversal) != 0) {
                    result.flags |= ParseFlags.TimeZoneUsed;
                    result.timeZoneOffset = TimeZoneInfo.GetLocalUtcOffset(result.parsedDate, TimeZoneInfoOptions.NoThrowOnInvalidTime);
                }
                else {
                    result.parsedDate = DateTime.SpecifyKind(result.parsedDate, DateTimeKind.Local);
                    return true;
                }
            }
            else if ((styles & DateTimeStyles.AssumeUniversal) != 0) {
                if ((styles & DateTimeStyles.AdjustToUniversal) != 0) {
                    result.parsedDate = DateTime.SpecifyKind(result.parsedDate, DateTimeKind.Utc);
                    return true;
                }
                else {
                    result.flags |= ParseFlags.TimeZoneUsed;
                    result.timeZoneOffset = TimeSpan.Zero;
                }
            }
            else {
                // No time zone and no Assume flags, so DateTimeKind.Unspecified is fine
                Contract.Assert(result.parsedDate.Kind == DateTimeKind.Unspecified, "result.parsedDate.Kind == DateTimeKind.Unspecified");
                return true;
            }

附带说明,调试Dot Net框架代码很有趣。如果您有兴趣,请从这里开始

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

DateTime和DateTimeZone的奇怪行为

来自分类Dev

PHP - strtotime() 和 datetime 奇怪的行为

来自分类Dev

Enum.TryParse奇怪的行为

来自分类Dev

DateTime.TryParse()的问题

来自分类Dev

子类化datetime.timedelta时的奇怪行为

来自分类Dev

比较DateTime对象和JODA时的奇怪行为

来自分类Dev

子类化datetime.timedelta时的奇怪行为

来自分类Dev

不导入datetime mod,python的行为奇怪吗?

来自分类Dev

DateTime.TryParse无法解析DateTime.MinValue

来自分类Dev

从DateTime.TryParse获取自定义dateTime格式

来自分类Dev

DateTime.TryParse->“鬼滴答声”

来自分类Dev

在isDate()或Datetime.TryParse中指定CultureInfo

来自分类Dev

DateTime.TryParse返回错误(IMO)答案

来自分类Dev

DateTime.TryParse仅在yyyy失败

来自分类Dev

分配 DateTime.TryParse 的结果或 null

来自分类Dev

带有DateTime的奇怪FormatException

来自分类Dev

GHCi中的奇怪行为

来自分类Dev

PHP中的奇怪行为

来自分类Dev

NSViewController中的奇怪行为

来自分类Dev

PHP中的奇怪行为

来自分类Dev

$ scope中的奇怪行为。

来自分类Dev

Laravel 中的奇怪行为

来自分类Dev

Python datetime怪异行为

来自分类Dev

php DateTime diff方法行为

来自分类Dev

PHP DateTime对象的异常(?)行为

来自分类Dev

从datetime对象中减去datetime.datetime.now()

来自分类Dev

从datetime对象中减去datetime.datetime.now()

来自分类Dev

PHP中的新DateTime()与新DateTime('NOW')

来自分类Dev

将varchar中的datetime转换为datetime