๐ง๐ถ๐บ๐ฒ ๐ ๐ฎ๐๐ต ๐๐ ๐๐ฎ๐ฟ๐ฑ๐ฒ๐ฟ ๐ง๐ต๐ฎ๐ป ๐๐ ๐๐ผ๐ผ๐ธ๐
Adding durations sounds simple. 2:45 plus 1:30 seems easy. Then a user logs 11:45 PM to 7:15 AM. Your code says they worked negative 16 hours. Time math is tricky.
Avoid these 6 common bugs:
The Decimal Trap. 1:30 is not 1.30. It is 1.5 hours. Do not do math on HH:MM strings. Convert everything to minutes first.
The Midnight Wrap. Shifts often cross midnight. If the end time is smaller than the start time, you have a wrap. Add 24 hours to the result.
The AM/PM Mix-up. 12:00 PM is noon. 12:00 AM is midnight. Standard parsers often fail here. Store time in 24h format internally.
The DST Shift. Daylight Savings Time changes day length. Local timestamps cause 1 hour errors. Use UTC or epoch seconds for math.
The Unit Mismatch. Some tools use milliseconds. Others use seconds. Mixing them creates huge errors. Pick one unit. Stick to it.
The Payroll Gap. Payroll needs decimals like 8.25. 8:15 is 8.25 hours. Set a clear rounding rule.
Stop doing math on strings or local times. Use this pipeline:
- Parse input to one base unit.
- Do all math in this unit.
- Format for humans at the end.
This system stops surprises.