time_machine icon indicating copy to clipboard operation
time_machine copied to clipboard

Confusion on format vs parse

Open alevinetx opened this issue 6 years ago • 0 comments

I've been going in circles, digging in the source and docs with trying code variations, and I'm not sure what i'm missing.

Problem: I have a String for a date/time with a timezone attached eg "2019-12-01 08:10 CST"

I wanted to convert this in to a DateTime, but as there's no timezone support, I'm now trying to use TimeMachine to use ZonedDateTime.

Pattern: MM-dd-yyyy HH:mm x --> pattern is only capable of formatting, not parsing

If I change the pattern to: MM-dd-yyyy HH:mm z --> The specified time zone identifier is not recognized

The 2nd error makes sense, as I believe 'z' means the fuller text version, eg "America/Chicago". I failed to bookmark where I saw the formatting codes, and I'm unable to find it now (probably due to crossed eyes).

My current attempt looks like this:

    var usc = await Cultures.getCulture('en-US');
 ...
var eventPattern = ZonedDateTimePattern.createWithCulture("MM-dd-yyyy HH:mm z", usc);

I've tried createWithInvariantCulture, and many others I've forgotten.

I'm sure this is easy, but what is it I'm not seeing or understanding? I've also tried giving a DateTimeZoneProvider (var tzdb = await DateTimeZoneProviders.tzdb;), to alleviate what I understood the problem to be with the "formatter, not a parser", and that's a null DTZP.

Thank you!

alevinetx avatar Nov 12 '19 15:11 alevinetx