MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MD / parseTime

Function parseTime

plugins/schedule.js:46–76  ·  view source on GitHub ↗

* Parse time input to a future Date * Supports: 10m | 2h | 1h30m | 14:30 | 10:30am

(input)

Source from the content-addressed store, hash-verified

44 * Supports: 10m | 2h | 1h30m | 14:30 | 10:30am
45 */
46function parseTime(input) {
47 const now = new Date();
48 // e.g. 10m, 2h, 1h30m
49 const relativeMatch = input.match(/^(?:(\d+)h)?(?:(\d+)m)?$/i);
50 if (relativeMatch && (relativeMatch[1] || relativeMatch[2])) {
51 const hours = parseInt(relativeMatch[1] || '0', 10);
52 const minutes = parseInt(relativeMatch[2] || '0', 10);
53 if (hours === 0 && minutes === 0)
54 return null;
55 return new Date(now.getTime() + (hours * 60 + minutes) * 60 * 1000);
56 }
57 // e.g. 14:30, 10:30am, 9:00pm
58 const clockMatch = input.match(/^(\d{1,2}):(\d{2})(am|pm)?$/i);
59 if (clockMatch) {
60 let hour = parseInt(clockMatch[1], 10);
61 const minute = parseInt(clockMatch[2], 10);
62 const meridiem = clockMatch[3]?.toLowerCase();
63 if (meridiem === 'pm' && hour < 12)
64 hour += 12;
65 if (meridiem === 'am' && hour === 12)
66 hour = 0;
67 const target = new Date(now);
68 target.setHours(hour, minute, 0, 0);
69 // If already passed, schedule for tomorrow
70 if (target.getTime() <= now.getTime()) {
71 target.setDate(target.getDate() + 1);
72 }
73 return target;
74 }
75 return null;
76}
77function formatTimeLeft(ms) {
78 if (ms <= 0)
79 return 'now';

Callers 1

handlerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected