Untitled
Anonymous
plain_text
06/27/2024 9:17 AM
3.3 KB
16
Indexable
import 'package:intl/intl.dart';
List<String> getPossibleSlots({
required String duration,
required String startTime,
required String endTime,
required String dateRange,
required String bufferTimeBefore,
required String bufferTimeAfter,
}) {
// Parse time format
final timeFormatter = DateFormat('hh:mm a');
// Parse duration and buffer times
final durationInMinutes = int.parse(duration.split(' ')[0]);
final bufferBeforeInMinutes = int.parse(bufferTimeBefore.split(' ')[0]);
final bufferAfterInMinutes = int.parse(bufferTimeAfter.split(' ')[0]);
// Parse start and end times as DateTime objects
final startDate = timeFormatter.parse(startTime);
final endDate = timeFormatter.parse(endTime);
// Calculate total available time in minutes (excluding buffer after)
final totalAvailableMinutes = endDate.difference(startDate).inMinutes - bufferAfterInMinutes;
// Check if there's enough time for at least one slot
if (totalAvailableMinutes - durationInMinutes - bufferBeforeInMinutes < 0) {
return [];
}
// List to store possible slots
final slots = <String>[];
// Current slot time
var currentSlot = startDate;
// Loop until current slot exceeds or meets end time (considering buffer after)
while (currentSlot.isBefore(endDate) || currentSlot == endDate) {
// Check if slot end time (including buffer after) would exceed end time
final slotEndTime = currentSlot.add(Duration(minutes: durationInMinutes + bufferAfterInMinutes));
if (slotEndTime.isAfter(endDate)) {
break;
}
// Add current slot time to list
slots.add(timeFormatter.format(currentSlot));
// Calculate next slot start time with buffer and duration
currentSlot = currentSlot.add(Duration(minutes: durationInMinutes + bufferBeforeInMinutes + bufferAfterInMinutes));
}
return slots;
}
void main() {
// Sample input arguments
final duration = "15 minutes";
final startTime = "09:00 AM";
final endTime = "11:00 AM";
final dateRange = "30 days"; // Not used in this function currently
final bufferTimeBefore = "0 minutes";
final bufferTimeAfter = "0 minutes";
// Call the function with arguments
final possibleSlots = getPossibleSlots(
duration: duration,
startTime: startTime,
endTime: endTime,
dateRange: dateRange,
bufferTimeBefore: bufferTimeBefore,
bufferTimeAfter: bufferTimeAfter,
);
// Print the output list
print(possibleSlots);
}
Editor is loading...
Leave a Comment