Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added try-catch #2592

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions lib/utils/time_conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ String combineDateTime(String date, String time) {
///
/// **returns**:
/// * `Map<String, String>`: A map containing the separate date and time strings.
/// * Returns an empty map if the input is invalid.
Map<String, String> 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.
Expand All @@ -34,12 +40,18 @@ Map<String, String> splitDateTimeUTC(String dateTimeStr) {
///
/// **returns**:
/// * `Map<String, String>`: A map containing the separate date and time strings.
/// * Returns an empty map if the input is invalid.
Map<String, String> 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.
Expand All @@ -49,9 +61,15 @@ Map<String, String> 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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 = {
Expand All @@ -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': {
Expand Down
Loading