Getting the original timezone of a DateTime value when parsing

I have a set of DateTime

ISO 8601 text views , some of which have a timezone. By default it DateTime.Parse()

sets them to the local timezone (or UTC using a custom parameter), but in both cases the original timezone is missing. However, I need to determine which string from DateTime

was specified in the time zone and get its value for further processing.

Any ideas on how to do this?

UPD Sample Inputs:

2015-06-26T22: 57: 09Z
2015-06-26T22: 57: 09
2015-06-26T22: 57: 09 + 01: 00

+3


source to share


1 answer


Let me try to clarify a few things.

First of all, both DateTime

and DateTimeOffset

are time zone indicators. A DateTime

may know this UTC

or Local

, but nevertheless he may not know what local really means. A DateTimeOffset

slightly better, it supports UTC time with UTC offset . However, this is not enough information to determine the time zone, because different time zones can have the same offsets.

DateTime.Parse

usually returns DateTime

with Kind

like Unspecified

. It returns:

  • Local

    when your string contains timezone information.
  • UTC

    when your string contains timezone information and uses the style AdjustToUniversal

    or , your string has a Z or GMT pointer and uses the style RoundtripKind

    .


This is why it DateTime.Parse("2015-06-26T22:57:09")

returns Unspecified

, but both DateTime.Parse("2015-06-26T22:57:09Z")

also DateTime.Parse("2015-06-26T22:57:09+01:00")

return Local

as Kind

. This is why no matter what you use, you will not get real-time information.

I would suggest you instead NodaTime

. It has a structure ZonedDateTime

as defined;

A in a specific time zone and with a specific offset to distinguish ambiguous points from each other.LocalDateTime

This structure will be better for your case.

+3


source







All Articles