parseRFC3339DateTime static method

DateTime parseRFC3339DateTime(
  1. String dateTimeString
)

Parse a datetime string in RFC3339 format and return a corresponding DateTime object. The method checks if the input string contains a timezone offset. If it does, it splits the string into date and offset parts, parses the date part, and returns it. If there's no offset, it parses the input string as a UTC time.

Implementation

static DateTime parseRFC3339DateTime(String dateTimeString) {
  // Check if the input string contains a timezone offset
  if (dateTimeString.contains('+')) {
    // Split the string into the date and offset parts
    final parts = dateTimeString.split('+');
    if (parts.length != 2) {
      throw BuError(
        DefaultErrorType.invalidArgument,
        "Invalid RFC3339 format: $dateTimeString",
      );
    }
    try {
      final datePart = DateTime.parse(parts[0]);
      return datePart;
    } on FormatException catch (e) {
      throw BuError(
        DefaultErrorType.invalidArgument,
        "Invalid RFC3339 format: $dateTimeString",
        details: {"error": e.toString()},
      );
    }
  } else {
    try {
      return DateTime.parse(dateTimeString).toUtc();
    } on FormatException catch (e) {
      throw BuError(
        DefaultErrorType.invalidArgument,
        "Invalid RFC3339 format: $dateTimeString",
        details: {"error": e.toString()},
      );
    }
  }
}