parseXsDateTime static method

int parseXsDateTime(
  1. String value
)

Implementation

static int parseXsDateTime(String value) {
  const String pattern =
      '(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt](\\d\\d):(\\d\\d):(\\d\\d)([\\.,](\\d+))?([Zz]|((\\+|\\-)(\\d?\\d):?(\\d\\d)))?';
  final List<Match> matchList = RegExp(pattern).allMatches(value).toList();
  if (matchList.isEmpty) {
    throw ParserException('Invalid date/time format: $value');
  }
  final Match match = matchList[0];
  int timezoneShift;
  if (match.group(9) == null) {
    // No time zone specified.
    timezoneShift = 0;
  } else if (match.group(9) == 'Z' || match.group(9) == 'z') {
    timezoneShift = 0;
  } else {
    timezoneShift =
        int.parse(match.group(12)!) * 60 + int.parse(match.group(13)!);
    if ('-' == match.group(11)) timezoneShift *= -1;
  }

  //todo UTCではなくGMT?
  final DateTime dateTime = DateTime.utc(
      int.parse(match.group(1)!),
      int.parse(match.group(2)!),
      int.parse(match.group(3)!),
      int.parse(match.group(4)!),
      int.parse(match.group(5)!),
      int.parse(match.group(6)!));
  if (match.group(8)?.isNotEmpty == true) {
    //todo ここ実装再検討
  }

  int time = dateTime.millisecondsSinceEpoch;
  if (timezoneShift != 0) {
    time -= timezoneShift * 60000;
  }

  return time;
}