parseWithNumericTz function

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

Parse a dateTime string with a numeric TZ like +300

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 parseWithNumericTz(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 && matchStr.group(2) != null) {
      final offsetStr = matchStr.group(2)!;
      final offsetInt = int.parse(matchStr.group(2)!);
      final duration = Duration(
        hours: int.parse(offsetStr.substring(1, 3)),
        minutes: int.parse(offsetStr.substring(3)),
      );

      return (offsetInt < 0) ? tmp.add(duration) : tmp.subtract(duration);
    }

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