Reputation: 3929
This is a follow on from a question answered yesterday..
Convert 12hr Time String to DateTime object
Those times in the xml feed are EST (who does that?) but our timezone is BST.
so 10:30PM is 02:30AM UTC or 03:30AM BST
However, TryParseExact yields 10:30PM in local time (as to be expected given that there is not timezone information)
So the question; how can I parse that time as 02:30AM UTC rather than 10:30PM BST?
Upvotes: 0
Views: 1376
Reputation: 1500515
However, TryParseExact yields 10:30PM in local time
No, it doesn't. Not unless you tell it to. By default, and unless there's any indication of the offset in the pattern, the parse methods will return DateTime
values with a Kind
of Unspecified
- which is entirely appropriate as no information has been specified. If you just convert it to a string, it will assume it's actually a local time, but that's not what the value itself says. You need to understand the three kinds of DateTime
- it's a broken model IMO, but that's what we've got in the BCL.
You can pass that to the appropriate TimeZoneInfo
to apply a specific time zone and get an appropriate DateTimeOffset
, although it's then up to you to remember the actual time zone involved. (An offset isn't the same as a time zone.)
Alternatively, you could use my Noda Time project, which differentiates between the different logical types rather more clearly. You'd parse as a LocalTime
, then decide which LocalDate
to join that with in order to produce LocalDateTime
, which you could then convert to a ZonedDateTime
using the "America/Los_Angeles" time zone (or the Windows equivalent; the choice is yours). In performing that conversion, you'd specify what you'd want to happen if the given local time was invalid or ambiguous due to daylight saving transitions.
Upvotes: 3