| 1 | import 'package:timezone/timezone.dart' as tz; |
| 2 | import 'package:timezone/data/latest.dart' as tz; |
| 3 | import 'package:intl/intl.dart'; |
| 4 | |
| 5 | /// Calculates the duration of a flight between two timezones |
| 6 | /// |
| 7 | /// Parameters: |
| 8 | /// - departureTime: Departure time string (supports ISO 8601 or custom format) |
| 9 | /// - departureTimezone: IANA timezone for departure (e.g., "America/Chicago") |
| 10 | /// - arrivalTime: Arrival time string |
| 11 | /// - arrivalTimezone: IANA timezone for arrival (e.g., "America/Los_Angeles") |
| 12 | /// - dateFormat: Optional custom format for parsing times |
| 13 | /// |
| 14 | /// Returns Duration representing the flight time |
| 15 | Duration calculateFlightDuration({ |
| 16 | required String departureTime, |
| 17 | required String departureTimezone, |
| 18 | required String arrivalTime, |
| 19 | required String arrivalTimezone, |
| 20 | String? dateFormat, |
| 21 | }) { |
| 22 | // Initialize timezone database |
| 23 | try { |
| 24 | tz.initializeTimeZones(); |
| 25 | } catch (e) { |
| 26 | throw TimeZoneInitException('Failed to initialize timezone database: $e |
| 27 | } |
| 28 | try { |
| 29 | // Parse times using appropriate method |
| 30 | final parsedDeparture = _parseDateTime(departureTime |
| 31 | final parsedArrival = _parseDateTime(arrivalTime |
| 32 | |
| 33 | // Get timezone locations |
| 34 | final departureLocation = tz.getLocation |
| 35 | final arrivalLocation = tz.getLocation |
| 36 | |
| 37 | // Convert to TZDateTime for each timezone |
| 38 | final tzDeparture = tz.TZDateTime |
| 39 | final tzArrival = tz.TZDateTime |
| 40 | |
| 41 | // Calculate duration |
| 42 | final duration = tzArrival.difference |
| 43 | |
| 44 | |
| 45 | return duration; |
| 46 | } on tz.LocationNotFoundException catch (e) { |
| 47 | throw TimeZoneNotFoundException('Invalid timezone identifier provided: $e |
| 48 | } catch (e) { |
| 49 | rethrow; // Preserve specific exception types |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// Helper function to parse datetime strings in various formats |
| 54 | DateTime _parseDateTime(String dateStr |
| 55 | try { |
| 56 | // First try parsing as ISO 8601 |
| 57 | return DateTime.parse |
| 58 | } catch (_) { |
| 59 | // If ISO parsing fails and format is provided, try using the format |
| 60 | if (format != null) { |
| 61 | try { |
| 62 | return DateFormat(format).parse |
| 63 | } catch (e) { |
| 64 | throw DateParseException('Could not parse "$dateStr |
| 65 | 'Please check the time format and try again.'); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // If no format provided, try common formats |
| 70 | final commonFormats = [ |
| 71 | "yyyy-MM-dd HH:mm", |
| 72 | "yyyy-MM-dd hh:mm a", |
| 73 | "MM/dd/yyyy HH:mm", |
| 74 | "dd-MM-yyyy HH:mm", |
| 75 | ]; |
| 76 | |
| 77 | for (final fmt in commonFormats) { |
| 78 | try { |
| 79 | return DateFormat(fmt).parse |
| 80 | } catch (_) { |
| 81 | continue; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | throw DateParseException('Could not parse "$dateStr |
| 86 | '(e.g., "2024-12-28T10:07:00Z") or specify a custom format.'); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// Custom exception for invalid flight times |
| 91 | class FlightTimeException implements Exception |
| 92 | final String message; |
| 93 | FlightTimeException(this.message); |
| 94 | |
| 95 | @override |
| 96 | String toString() => message |
| 97 | } |
| 98 | |
| 99 | /// Other exception classes |
| 100 | class DateParseException implements Exception |
| 101 | final String message; |
| 102 | DateParseException(this.message); |
| 103 | |
| 104 | @override |
| 105 | String toString() => message |
| 106 | } |
| 107 | |
| 108 | class TimeZoneInitException implements Exception |
| 109 | final String message; |
| 110 | TimeZoneInitException(this.message); |
| 111 | |
| 112 | @override |
| 113 | String toString() => message |
| 114 | } |
| 115 | |
| 116 | class TimeZoneNotFoundException implements Exception |
| 117 | final String message; |
| 118 | TimeZoneNotFoundException(this.message); |
| 119 | |
| 120 | @override |
| 121 | String toString() => message |
| 122 | } |