From 77c54f7d42668e99a59db2340e7810fa7263bd31 Mon Sep 17 00:00:00 2001 From: Pranshu Gupta Date: Tue, 15 Oct 2024 01:26:31 +0530 Subject: [PATCH 1/2] Added try-catch --- lib/utils/time_conversion.dart | 52 +++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/lib/utils/time_conversion.dart b/lib/utils/time_conversion.dart index 10e0b16a3..103998d89 100644 --- a/lib/utils/time_conversion.dart +++ b/lib/utils/time_conversion.dart @@ -19,12 +19,18 @@ String combineDateTime(String date, String time) { /// /// **returns**: /// * `Map`: A map containing the separate date and time strings. +/// * Returns an empty map if the input is invalid. Map splitDateTimeUTC(String dateTimeStr) { - final DateTime dateTime = DateTime.parse(dateTimeStr); - return { - 'date': DateFormat('yyyy-MM-dd').format(dateTime), - 'time': DateFormat("HH:mm:ss.SSS'Z'").format(dateTime), - }; + try { + final DateTime dateTime = DateTime.parse(dateTimeStr); + return { + 'date': DateFormat('yyyy-MM-dd').format(dateTime), + 'time': DateFormat("HH:mm:ss.SSS'Z'").format(dateTime), + }; + } catch (e) { + print('Timezone Error parsing UTC date time: $e $dateTimeStr'); + return {}; + } } /// Splits the given local date and time string into separate date and time strings. @@ -34,12 +40,18 @@ Map splitDateTimeUTC(String dateTimeStr) { /// /// **returns**: /// * `Map`: A map containing the separate date and time strings. +/// * Returns an empty map if the input is invalid. Map splitDateTimeLocal(String dateTimeStr) { - final DateTime dateTime = DateTime.parse(dateTimeStr); - return { - 'date': DateFormat('yyyy-MM-dd').format(dateTime), - 'time': DateFormat('HH:mm').format(dateTime), - }; + try { + final DateTime dateTime = DateTime.parse(dateTimeStr); + return { + 'date': DateFormat('yyyy-MM-dd').format(dateTime), + 'time': DateFormat('HH:mm').format(dateTime), + }; + } catch (e) { + print('Timezone Error parsing local date time: $e $dateTimeStr'); + return {}; + } } /// Converts the given UTC time to local time. @@ -49,9 +61,15 @@ Map splitDateTimeLocal(String dateTimeStr) { /// /// **returns**: /// * `String`: The converted local time string. +/// * Returns an empty string if the input is invalid. String convertUTCToLocal(String utcTime) { - final DateTime dateTime = DateTime.parse(utcTime).toLocal(); - return DateFormat('yyyy-MM-ddTHH:mm:ss.SSS').format(dateTime); + try { + final DateTime dateTime = DateTime.parse(utcTime).toLocal(); + return DateFormat('yyyy-MM-ddTHH:mm:ss.SSS').format(dateTime); + } catch (e) { + print('Timezone Error converting UTC to local: $e $utcTime'); + return ''; + } } /// Converts the given local time to UTC time. @@ -61,9 +79,15 @@ String convertUTCToLocal(String utcTime) { /// /// **returns**: /// * `String`: The converted UTC time string. +/// * Returns an empty string if the input is invalid. String convertLocalToUTC(String localTime) { - final DateTime dateTime = DateTime.parse(localTime).toUtc(); - return DateFormat("yyyy-MM-ddTHH:mm:ss.SSS'Z'").format(dateTime); + try { + final DateTime dateTime = DateTime.parse(localTime).toUtc(); + return DateFormat("yyyy-MM-ddTHH:mm:ss.SSS'Z'").format(dateTime); + } catch (e) { + print('Timezone Error converting local to UTC: $e $localTime'); + return ''; + } } /// Traverses a nested map and converts date and time fields to the desired format. From b99c3236148f8d5b23fe1660fc0656cb0b66a662 Mon Sep 17 00:00:00 2001 From: Pranshu Gupta Date: Tue, 15 Oct 2024 01:37:24 +0530 Subject: [PATCH 2/2] Update time_conversion_test.dart --- .../events/time_conversion_test.dart | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/widget_tests/after_auth_screens/events/time_conversion_test.dart b/test/widget_tests/after_auth_screens/events/time_conversion_test.dart index 7b5eb3ec8..435375942 100644 --- a/test/widget_tests/after_auth_screens/events/time_conversion_test.dart +++ b/test/widget_tests/after_auth_screens/events/time_conversion_test.dart @@ -24,12 +24,22 @@ void main() { expect(result['time'], '14:30:00.000Z'); }); + test('splitDateTimeUTC returns empty map for invalid input', () { + final result = splitDateTimeUTC('invalid-datetime'); + expect(result, isEmpty); + }); + test('splitDateTimeLocal splits local datetime correctly', () { final result = splitDateTimeLocal('2023-05-01T14:30:00.000'); expect(result['date'], '2023-05-01'); expect(result['time'], '14:30'); }); + test('splitDateTimeLocal returns empty map for invalid input', () { + final result = splitDateTimeLocal('invalid-datetime'); + expect(result, isEmpty); + }); + test('convertUTCToLocal converts UTC to local time', () { const utcTime = '2023-05-01T14:30:00.000Z'; final localTime = convertUTCToLocal(utcTime); @@ -40,6 +50,11 @@ void main() { ); }); + test('convertUTCToLocal returns empty string for invalid input', () { + final localTime = convertUTCToLocal('invalid-datetime'); + expect(localTime, isEmpty); + }); + test('convertLocalToUTC converts local to UTC time', () { const localTime = '2023-05-01T14:30:00.000'; final utcTime = convertLocalToUTC(localTime); @@ -50,6 +65,11 @@ void main() { ); }); + test('convertLocalToUTC returns empty string for invalid input', () { + final utcTime = convertLocalToUTC('invalid-datetime'); + expect(utcTime, isEmpty); + }); + group('traverseAndConvertDates', () { test('converts direct fields', () { final testObj = { @@ -75,6 +95,19 @@ void main() { expect(testObj['startTime'], matches(r'^\d{2}:\d{2}$')); }); + test('handles invalid date/time in traverseAndConvertDates', () { + final testObj = { + 'createdAt': 'invalid-datetime', + 'startDate': 'invalid-date', + 'startTime': 'invalid-time', + 'name': 'Test', + }; + traverseAndConvertDates(testObj, convertUTCToLocal, splitDateTimeLocal); + expect(testObj['createdAt'], isEmpty); + expect(testObj['startDate'], isEmpty); + expect(testObj['startTime'], isEmpty); + }); + test('converts nested objects', () { final testObj = { 'user': {