feat: enhance date parsing for shift start time in CommuteTracker

This commit is contained in:
Achintha Isuru
2026-02-02 15:30:15 -05:00
parent 3c51a4b34c
commit 8c1e67cf00

View File

@@ -68,11 +68,18 @@ class _CommuteTrackerState extends State<CommuteTracker> {
final now = DateTime.now(); final now = DateTime.now();
DateTime shiftStart; DateTime shiftStart;
try { try {
// Try parsing startTime as full datetime first
shiftStart = DateTime.parse(widget.shift!.startTime); shiftStart = DateTime.parse(widget.shift!.startTime);
} catch (_) { } catch (_) {
shiftStart = DateTime.parse( try {
'${widget.shift!.date} ${widget.shift!.startTime}', // Try parsing date as full datetime
); shiftStart = DateTime.parse(widget.shift!.date);
} catch (_) {
// Fall back to combining date and time
shiftStart = DateTime.parse(
'${widget.shift!.date} ${widget.shift!.startTime}',
);
}
} }
final hoursUntilShift = shiftStart.difference(now).inHours; final hoursUntilShift = shiftStart.difference(now).inHours;
final inCommuteWindow = hoursUntilShift <= 24 && hoursUntilShift >= 0; final inCommuteWindow = hoursUntilShift <= 24 && hoursUntilShift >= 0;
@@ -104,9 +111,21 @@ class _CommuteTrackerState extends State<CommuteTracker> {
int _getMinutesUntilShift() { int _getMinutesUntilShift() {
if (widget.shift == null) return 0; if (widget.shift == null) return 0;
final now = DateTime.now(); final now = DateTime.now();
final shiftStart = DateTime.parse( DateTime shiftStart;
'${widget.shift!.date} ${widget.shift!.startTime}', try {
); // Try parsing startTime as full datetime first
shiftStart = DateTime.parse(widget.shift!.startTime);
} catch (_) {
try {
// Try parsing date as full datetime
shiftStart = DateTime.parse(widget.shift!.date);
} catch (_) {
// Fall back to combining date and time
shiftStart = DateTime.parse(
'${widget.shift!.date} ${widget.shift!.startTime}',
);
}
}
return shiftStart.difference(now).inMinutes; return shiftStart.difference(now).inMinutes;
} }