dateTimeFromJson function

DateTime? dateTimeFromJson(
  1. String? json
)

Converts a dot-separated string to a DateTime object. The expected format is "YYYY.MM.DD.HH.MM.SS".

Implementation

DateTime? dateTimeFromJson(String? json) {
  try {
    final parts = (json ?? '').split('.'); // Split the string into components

    // Ensure the correct number of components are provided
    if (parts.length != 6) {
      return null; // Return null if the format is incorrect
    }

    // Parse each component to an integer
    final years = int.parse(parts[0]); // Parse year
    final months = int.parse(parts[1]); // Parse month
    final days = int.parse(parts[2]); // Parse day
    final hours = int.parse(parts[3]); // Parse hour
    final minutes = int.parse(parts[4]); // Parse minute
    final seconds = int.parse(parts[5]); // Parse second

    // Create and return a DateTime object
    return DateTime(
      years,
      months,
      days,
      hours,
      minutes,
      seconds,
    );
  } catch (e) {
    debugPrint(e.toString()); // Log any errors during parsing
    return null; // Return null if an error occurs
  }
}