1923f272d5c9f0e5a1ca5e1698bcc5c7196423d5c6a43272d5d0c19cb6e65a2fd15dd4f3bab8246383729d715e4f979b8c3f2b4e911f89f316c53d0c85338e 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const parseTime = (time) => {
  2. const values = (time || "").split(":");
  3. if (values.length >= 2) {
  4. let hours = Number.parseInt(values[0], 10);
  5. const minutes = Number.parseInt(values[1], 10);
  6. const timeUpper = time.toUpperCase();
  7. if (timeUpper.includes("AM") && hours === 12) {
  8. hours = 0;
  9. } else if (timeUpper.includes("PM") && hours !== 12) {
  10. hours += 12;
  11. }
  12. return {
  13. hours,
  14. minutes
  15. };
  16. }
  17. return null;
  18. };
  19. const compareTime = (time1, time2) => {
  20. const value1 = parseTime(time1);
  21. if (!value1)
  22. return -1;
  23. const value2 = parseTime(time2);
  24. if (!value2)
  25. return -1;
  26. const minutes1 = value1.minutes + value1.hours * 60;
  27. const minutes2 = value2.minutes + value2.hours * 60;
  28. if (minutes1 === minutes2) {
  29. return 0;
  30. }
  31. return minutes1 > minutes2 ? 1 : -1;
  32. };
  33. const padTime = (time) => {
  34. return `${time}`.padStart(2, "0");
  35. };
  36. const formatTime = (time) => {
  37. return `${padTime(time.hours)}:${padTime(time.minutes)}`;
  38. };
  39. const nextTime = (time, step) => {
  40. const timeValue = parseTime(time);
  41. if (!timeValue)
  42. return "";
  43. const stepValue = parseTime(step);
  44. if (!stepValue)
  45. return "";
  46. const next = {
  47. hours: timeValue.hours,
  48. minutes: timeValue.minutes
  49. };
  50. next.minutes += stepValue.minutes;
  51. next.hours += stepValue.hours;
  52. next.hours += Math.floor(next.minutes / 60);
  53. next.minutes = next.minutes % 60;
  54. return formatTime(next);
  55. };
  56. export { compareTime, formatTime, nextTime, padTime, parseTime };
  57. //# sourceMappingURL=utils.mjs.map