parseSexagesimal function Protocol

double? parseSexagesimal(
  1. String text
)

Parses a sexagesimal value such as -12:30:45.5, 12 30 or 5;30.

Up to three components (whole, minutes, seconds) are accepted; the sign applies to the whole value. Returns null if text is not valid.

Implementation

double? parseSexagesimal(String text) {
  var rest = text.trim();
  var negative = false;
  if (rest.startsWith('-')) {
    negative = true;
    rest = rest.substring(1);
  } else if (rest.startsWith('+')) {
    rest = rest.substring(1);
  }
  final parts =
      rest.split(_sexagesimalSeparator).where((p) => p.isNotEmpty).toList();
  if (parts.isEmpty || parts.length > 3) return null;
  var result = 0.0;
  var divisor = 1.0;
  for (final part in parts) {
    if (part.startsWith('-') || part.startsWith('+')) return null;
    final component = double.tryParse(part);
    if (component == null || !component.isFinite) return null;
    result += component / divisor;
    divisor *= 60;
  }
  return negative ? -result : result;
}