Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pranshugupta54 committed Oct 10, 2024
1 parent 59459a7 commit f1d1034
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lib/utils/time_conversion.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import 'package:intl/intl.dart';

/// Combines the given date and time strings into a single string.
///
/// **params**:
/// * `date`: The date string in a valid date format (e.g., 'YYYY-MM-DD').
/// * `time`: The time string in a valid time format (e.g., 'HH:MM:SS').
///
/// **returns**:
/// * `String`: A string that combines the `date` and `time`, separated by a space.
String combineDateTime(String date, String time) {
return '$date $time';
}

/// Splits the given UTC date and time string into separate date and time strings.
///
/// **params**:
/// * `dateTimeStr`: The UTC date and time string in a valid format.
///
/// **returns**:
/// * `Map<String, String>`: A map containing the separate date and time strings.
Map<String, String> splitDateTimeUTC(String dateTimeStr) {
final DateTime dateTime = DateTime.parse(dateTimeStr);
return {
Expand All @@ -12,6 +27,13 @@ Map<String, String> splitDateTimeUTC(String dateTimeStr) {
};
}

/// Splits the given local date and time string into separate date and time strings.
///
/// **params**:
/// * `dateTimeStr`: The local date and time string in a valid format.
///
/// **returns**:
/// * `Map<String, String>`: A map containing the separate date and time strings.
Map<String, String> splitDateTimeLocal(String dateTimeStr) {
final DateTime dateTime = DateTime.parse(dateTimeStr);
return {
Expand All @@ -20,16 +42,39 @@ Map<String, String> splitDateTimeLocal(String dateTimeStr) {
};
}

/// Converts the given UTC time to local time.
///
/// **params**:
/// * `utcTime`: The UTC time string in a valid format.
///
/// **returns**:
/// * `String`: The converted local time string.
String convertUTCToLocal(String utcTime) {
final DateTime dateTime = DateTime.parse(utcTime).toLocal();
return DateFormat('yyyy-MM-ddTHH:mm:ss.SSS').format(dateTime);
}

/// Converts the given local time to UTC time.
///
/// **params**:
/// * `localTime`: The local time string in a valid format.
///
/// **returns**:
/// * `String`: The converted UTC time string.
String convertLocalToUTC(String localTime) {
final DateTime dateTime = DateTime.parse(localTime).toUtc();
return DateFormat("yyyy-MM-ddTHH:mm:ss.SSS'Z'").format(dateTime);
}

/// Traverses a nested map and converts date and time fields to the desired format.
///
/// **params**:
/// * `obj`: The nested map to traverse and convert.
/// * `convertFn`: A function that converts a combined date and time string to the desired format.
/// * `splitFn`: A function that splits a converted date and time string into separate date and time strings.
///
/// **returns**:
/// None
void traverseAndConvertDates(
Map<String, dynamic> obj,
String Function(String) convertFn,
Expand Down Expand Up @@ -72,6 +117,7 @@ void traverseAndConvertDates(
});
}

/// Contains information about the date and time fields used for conversion.
const dateTimeFields = {
'directFields': [
'createdAt',
Expand Down

0 comments on commit f1d1034

Please sign in to comment.