feat: Implement UTC conversion for order date and time serialization in order use cases

This commit is contained in:
Achintha Isuru
2026-03-19 23:34:29 -04:00
parent 207831eb3e
commit 4cd83a9281
9 changed files with 186 additions and 135 deletions

View File

@@ -68,3 +68,45 @@ String formatTime(String timeStr) {
}
}
}
/// Converts a local date + local HH:MM time string to a UTC HH:MM string.
///
/// Combines [localDate] with the hours and minutes from [localTime] (e.g.
/// "09:00") to create a full local [DateTime], converts it to UTC, then
/// extracts the HH:MM portion.
///
/// Example: March 19, "21:00" in UTC-5 → "02:00" (next day UTC).
String toUtcTimeHHmm(DateTime localDate, String localTime) {
final List<String> parts = localTime.split(':');
final DateTime localDt = DateTime(
localDate.year,
localDate.month,
localDate.day,
int.parse(parts[0]),
int.parse(parts[1]),
);
final DateTime utcDt = localDt.toUtc();
return '${utcDt.hour.toString().padLeft(2, '0')}:'
'${utcDt.minute.toString().padLeft(2, '0')}';
}
/// Converts a local date + local HH:MM time string to a UTC YYYY-MM-DD string.
///
/// This accounts for date-boundary crossings: a shift at 11 PM on March 19
/// in UTC-5 is actually March 20 in UTC.
///
/// Example: March 19, "23:00" in UTC-5 → "2026-03-20".
String toUtcDateIso(DateTime localDate, String localTime) {
final List<String> parts = localTime.split(':');
final DateTime localDt = DateTime(
localDate.year,
localDate.month,
localDate.day,
int.parse(parts[0]),
int.parse(parts[1]),
);
final DateTime utcDt = localDt.toUtc();
return '${utcDt.year.toString().padLeft(4, '0')}-'
'${utcDt.month.toString().padLeft(2, '0')}-'
'${utcDt.day.toString().padLeft(2, '0')}';
}