parseWithNamedTz function

DateTime parseWithNamedTz(
  1. String value,
  2. ParseInfo pi
)

Parse a DateTime string with a named TZ like PDT

Dart do not support parsing of Z on DateFormat and always will default to UTC this is a hacky way to move the date offset manually. this will not use DST or any other external information, just the offset

reference: https://github.com/dart-lang/intl/issues/19

Implementation

DateTime parseWithNamedTz(String value, ParseInfo pi) {
  final dateStr = (pi.fcb != null) ? pi.fcb!(value, pi) : value;
  try {
    final tmp = DateFormat(pi.format).parseUtc(dateStr);
    final matchStr = pi.rgx.matchAsPrefix(dateStr);

    if (matchStr != null) {
      final tzName = matchStr.group(2);
      final data = tzInfoDb[tzName];

      if (data != null) {
        final duration = Duration(minutes: data.offset.abs());
        return (data.offset < 0) ? tmp.add(duration) : tmp.subtract(duration);
      }
    }

    return tmp;
  } on FormatException {
    throw FormatException('(parseWithNamedTz) format error: [$dateStr] with [${pi.format}]');
  }
}