Reputation: 14417
I have a set of DateTime
text representations in ISO 8601 format, some of them have time zone specified. By default, DateTime.Parse()
adjusts them to the local time zone (or to UTC with a special option), but in both cases the original time zone is missing. However, I need to detect which of DateTime
strings were specified with the time zone and get its value for further processing.
Any ideas on how to do that?
UPD sample inputs:
2015-06-26T22:57:09Z
2015-06-26T22:57:09
2015-06-26T22:57:09+01:00
Upvotes: 3
Views: 5387
Reputation: 98848
Let me try to clear a few things..
First of all both DateTime
and DateTimeOffset
are timezone awareness. A DateTime
might know it's UTC
or Local
, but still, it can't know what local really means. A DateTimeOffset
is somewhat better, it keeps a UTC time with an UTC offset. But still, these are not enough information to determine a timezone because different timezones can have same offsets.
DateTime.Parse
generally returns a DateTime
with Kind
as Unspecified
. It returns;
Local
when your string has time zone information.UTC
when your string has time zone information and using AdjustToUniversal
style or your string has Z or GMT designator and using RoundtripKind
style. That's why DateTime.Parse("2015-06-26T22:57:09")
returns Unspecified
but both DateTime.Parse("2015-06-26T22:57:09Z")
and DateTime.Parse("2015-06-26T22:57:09+01:00")
returns Local
as a Kind
. That's why no matter which you used, you will not get a real time zone information.
I would suggest you to used NodaTime
instead. It has a ZonedDateTime
structure as defined;
A
LocalDateTime
in a specific time zone and with a particular offset to distinguish between otherwise-ambiguous instants.
This structure is would be better for your case.
Upvotes: 3