FixedDateTimeFormatter constructor

FixedDateTimeFormatter(
  1. String pattern, {
  2. bool isUtc = true,
})

Creates a new FixedDateTimeFormatter with the provided pattern.

The pattern interprets the characters mentioned in FixedDateTimeFormatter to represent fields of a DateTime value. Other characters are not special. If isUtc is set to false, the DateTime is constructed with respect to the local timezone.

There must at most be one sequence of each special character to ensure a single source of truth when constructing the DateTime, so a pattern of "CCCC-MM-DD, CC" is invalid, because it has two separate C sequences.

Implementation

FixedDateTimeFormatter(this.pattern, {this.isUtc = true}) {
  int? currentCharacter;
  var start = 0;
  for (var i = 0; i < pattern.length; i++) {
    var formatCharacter = pattern.codeUnitAt(i);
    if (currentCharacter != formatCharacter) {
      _blocks.saveBlock(currentCharacter, start, i);
      if (_validFormatCharacters.contains(formatCharacter)) {
        var hasSeenBefore = _blocks.formatCharacters.indexOf(formatCharacter);
        if (hasSeenBefore > -1) {
          throw FormatException(
              "Pattern contains more than one '$formatCharacter' block.\n"
              'Previous occurrence at index ${_blocks.starts[hasSeenBefore]}',
              pattern,
              i);
        } else {
          start = i;
          currentCharacter = formatCharacter;
        }
      } else {
        currentCharacter = null;
      }
    }
  }
  _blocks.saveBlock(currentCharacter, start, pattern.length);
}