| 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 | extension DateTimeTimezoneFormatter on DateTime |
| 6 | /// Converts DateTime to a specified timezone and returns formatted time string |
| 7 | /// |
| 8 | /// Parameters: |
| 9 | /// - timezone: IANA timezone identifier (e.g., "America/New_York", "Asia/Tokyo") |
| 10 | /// - format: Optional custom format string. Defaults to "h:mm a" (e.g., "10:30 AM") |
| 11 | /// |
| 12 | /// Returns formatted time string in the specified timezone |
| 13 | /// Throws [TimeZoneInitException] if timezones are not initialized |
| 14 | /// Throws [TimeZoneNotFoundException] if timezone is invalid |
| 15 | String toTimezoneString(String |
| 16 | // Ensure timezone database is initialized |
| 17 | try { |
| 18 | tz.initializeTimeZones(); |
| 19 | } catch (e) { |
| 20 | throw TimeZoneInitException('Failed to initialize timezone database: $e |
| 21 | } |
| 22 | |
| 23 | try { |
| 24 | // Get timezone location |
| 25 | final location = tz.getLocation |
| 26 | // Convert DateTime to TZDateTime |
| 27 | final tzDateTime = tz.TZDateTime |
| 28 | |
| 29 | // Create formatter with specified or default format |
| 30 | final formatter = DateFormat(format |
| 31 | |
| 32 | return formatter.format(tzDateTime); |
| 33 | } on tz.LocationNotFoundException catch (e) { |
| 34 | throw TimeZoneNotFoundException('Invalid timezone identifier: $timezone |
| 35 | } catch (e) { |
| 36 | throw Exception('Error converting time to timezone $timezone |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | String getTimezoneDifference(String timezone1 |
| 42 | try { |
| 43 | tz.initializeTimeZones(); |
| 44 | } catch (e) { |
| 45 | throw TimeZoneInitException('Failed to initialize timezone database: $e |
| 46 | } |
| 47 | |
| 48 | // Create DateTime objects in both timezones |
| 49 | final location1 = tz.getLocation |
| 50 | final location2 = tz.getLocation |
| 51 | |
| 52 | // Get the offset from UTC for each timezone |
| 53 | final offset1 = location1.currentTimeZone. |
| 54 | final offset2 = location2.currentTimeZone. |
| 55 | |
| 56 | // Calculate difference in minutes |
| 57 | final differenceInMinutes = (offset1 - offset2) ~ |
| 58 | |
| 59 | // Calculate hours and remaining minutes |
| 60 | final hours = differenceInMinutes ~/ |
| 61 | final minutes = differenceInMinutes.abs |
| 62 | |
| 63 | // Build the string |
| 64 | final StringBuffer result = |
| 65 | |
| 66 | if (hours != 0) { |
| 67 | result.write('$hours |
| 68 | if (hours.abs() != |
| 69 | } |
| 70 | |
| 71 | // Add minutes part if there are minutes |
| 72 | if (minutes != 0) { |
| 73 | if (hours != 0) result.write |
| 74 | result.write('$minutes |
| 75 | if (minutes != 1) result.write |
| 76 | } |
| 77 | |
| 78 | // Handle case where difference is 0 |
| 79 | if (result.isEmpty) { |
| 80 | return 'same timezone'; |
| 81 | } |
| 82 | |
| 83 | return result.toString(); |
| 84 | } |
| 85 | |
| 86 | class TimeZoneInitException implements Exception |
| 87 | final String message; |
| 88 | TimeZoneInitException(this.message); |
| 89 | |
| 90 | @override |
| 91 | String toString() => message |
| 92 | } |
| 93 | |
| 94 | /// Custom exception for invalid timezone identifiers |
| 95 | class TimeZoneNotFoundException implements Exception |
| 96 | final String message; |
| 97 | TimeZoneNotFoundException(this.message); |
| 98 | |
| 99 | @override |
| 100 | String toString() => message |
| 101 | } |