import React, { useState } from ‘react’;
import { Button } from ‘@/components/ui/button’;
import { Input } from ‘@/components/ui/input’;
import { Card, CardHeader, CardTitle, CardContent } from ‘@/components/ui/card’;
import { Separator } from ‘@/components/ui/separator’;
const TimeCalculator = () => {
const [input, setInput] = useState(‘’);
const [total, setTotal] = useState({ formatted: ‘’, decimal: ‘’ });
const parseTimeRange = (range) => {
const [start, end] = range.split(‘-‘).map(t => t.trim());
// Convert time to minutes
const getMinutes = (time) => {
const [hours, minutes] = time.split(':').map(Number);
return hours * 60 + minutes;
};
const startMinutes = getMinutes(start);
const endMinutes = getMinutes(end);
// Handle times past midnight
return endMinutes < startMinutes ?
(24 * 60 - startMinutes) + endMinutes :
endMinutes - startMinutes; };
const calculateTotal = () => {
try {
// Split input into lines and filter empty ones
const ranges = input.split(‘\n’).filter(line => line.trim());
// Calculate total minutes
const totalMinutes = ranges.reduce((sum, range) => {
return sum + parseTimeRange(range);
}, 0);
// Convert to hours and minutes format
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
const formatted = `${hours}h${minutes ? ` ${minutes}m` : ''}`;
// Convert to exact decimal hours (no rounding)
const exactDecimal = totalMinutes / 60;
setTotal({
formatted,
decimal: `${exactDecimal}h`
});
} catch (error) {
setTotal({ formatted: 'Invalid input format', decimal: '' });
} };
return (
);
};
export default TimeCalculator;