Skip to content

Commit

Permalink
Added try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
pranshugupta54 authored Oct 14, 2024
1 parent bfda1a5 commit 77c54f7
Showing 1 changed file with 38 additions and 14 deletions.
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 {};

Check warning on line 32 in lib/utils/time_conversion.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/time_conversion.dart#L31-L32

Added lines #L31 - L32 were not covered by tests
}
}

/// 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 {};

Check warning on line 53 in lib/utils/time_conversion.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/time_conversion.dart#L52-L53

Added lines #L52 - L53 were not covered by tests
}
}

/// 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');

Check warning on line 70 in lib/utils/time_conversion.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/time_conversion.dart#L70

Added line #L70 was not covered by tests
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');

Check warning on line 88 in lib/utils/time_conversion.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/time_conversion.dart#L88

Added line #L88 was not covered by tests
return '';
}
}

/// Traverses a nested map and converts date and time fields to the desired format.
Expand Down

0 comments on commit 77c54f7

Please sign in to comment.