| 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 between two dates in different timezones |
| 6 | /// |
| 7 | /// Parameters: |
| 8 | /// - date1: First date string (supports multiple formats including ISO 8601) |
| 9 | /// - timezone1: IANA timezone for first date |
| 10 | /// - date2: Second date string |
| 11 | /// - timezone2: IANA timezone for second date |
| 12 | /// - format: Optional custom format for non-ISO dates |
| 13 | Duration calculateTimezoneDifference({ |
| 14 | required String date1, |
| 15 | required String timezone1, |
| 16 | required String date2, |
| 17 | required String timezone2, |
| 18 | String? format, |
| 19 | }) { |
| 20 | // Initialize timezone database |
| 21 | try { |
| 22 | tz.initializeTimeZones(); |
| 23 | } catch (e) { |
| 24 | throw TimeZoneInitException('Failed to initialize timezone database: $e |
| 25 | } |
| 26 | |
| 27 | try { |
| 28 | // Parse dates using appropriate method |
| 29 | final parsedDate1 = _parseDateTime(date1 |
| 30 | final parsedDate2 = _parseDateTime(date2 |
| 31 | |
| 32 | // Get timezone locations |
| 33 | final location1 = tz.getLocation |
| 34 | final location2 = tz.getLocation |
| 35 | |
| 36 | // Convert to TZDateTime for each timezone |
| 37 | final tzDateTime1 = tz.TZDateTime |
| 38 | final tzDateTime2 = tz.TZDateTime |
| 39 | |
| 40 | // Calculate difference |
| 41 | return tzDateTime2.difference(tzDateTime1); |
| 42 | } on tz.LocationNotFoundException catch (e) { |
| 43 | throw TimeZoneNotFoundException('Invalid timezone identifier: $e |
| 44 | } catch (e) { |
| 45 | throw Exception('Error calculating timezone difference: $e |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Helper function to parse datetime strings in various formats |
| 50 | DateTime _parseDateTime(String dateStr |
| 51 | try { |
| 52 | // First try parsing as ISO 8601 |
| 53 | return DateTime.parse |
| 54 | } catch (_) { |
| 55 | // If ISO parsing fails and format is provided, try using the format |
| 56 | if (format != null) { |
| 57 | try { |
| 58 | return DateFormat(format).parse |
| 59 | } catch (e) { |
| 60 | throw DateParseException('Could not parse "$dateStr |
| 61 | 'Error: $e'); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // If no format provided, try common formats |
| 66 | final commonFormats = [ |
| 67 | "yyyy-MM-dd HH:mm", |
| 68 | "yyyy-MM-dd hh:mm a", |
| 69 | "MM/dd/yyyy HH:mm", |
| 70 | "dd-MM-yyyy HH:mm", |
| 71 | "yyyy/MM/dd HH:mm", |
| 72 | ]; |
| 73 | |
| 74 | for (final fmt in commonFormats) { |
| 75 | try { |
| 76 | return DateFormat(fmt).parse |
| 77 | } catch (_) { |
| 78 | continue; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | throw DateParseException('Could not parse "$dateStr |
| 83 | 'or specify a custom format.'); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// Custom exception classes (same as before) |
| 88 | class DateParseException implements Exception |
| 89 | final String message; |
| 90 | DateParseException(this.message); |
| 91 | |
| 92 | @override |
| 93 | String toString() => message |
| 94 | } |
| 95 | |
| 96 | class TimeZoneInitException implements Exception |
| 97 | final String message; |
| 98 | TimeZoneInitException(this.message); |
| 99 | |
| 100 | @override |
| 101 | String toString() => message |
| 102 | } |
| 103 | |
| 104 | class TimeZoneNotFoundException implements Exception |
| 105 | final String message; |
| 106 | TimeZoneNotFoundException(this.message); |
| 107 | |
| 108 | @override |
| 109 | String toString() => message |
| 110 | } |
| 111 | |
| 112 | // Example usage with different date formats |
| 113 | void main() { |
| 114 | try { |
| 115 | // Example 1: Using ISO 8601 format |
| 116 | final duration1 = calculateTimezoneDifference(date1 |
| 117 | print('Duration 1: ${duration1. |
| 118 | |
| 119 | // Example 2: Using custom format |
| 120 | final duration2 = calculateTimezoneDifference(date1 |
| 121 | print('Duration 2: ${duration2. |
| 122 | |
| 123 | // Example 3: Using common format without specifying format string |
| 124 | final duration3 = calculateTimezoneDifference(date1 |
| 125 | print('Duration 3: ${duration3. |
| 126 | } on DateParseException catch (e) { |
| 127 | print('Date parsing error: $e' |
| 128 | } on TimeZoneNotFoundException catch (e) { |
| 129 | print('Timezone error: $e' |
| 130 | } on TimeZoneInitException catch (e) { |
| 131 | print('Initialization error: $e' |
| 132 | } catch (e) { |
| 133 | print('Unexpected error: $e' |
| 134 | } |
| 135 | } |