|
| 1 | +import {setTimeout} from 'node:timers/promises' |
| 2 | +import {isMainThread, parentPort, Worker} from 'node:worker_threads' |
| 3 | + |
| 4 | +interface TimeRemaining { |
| 5 | + days: number |
| 6 | + hours: number |
| 7 | + minutes: number |
| 8 | + seconds: number |
| 9 | +} |
| 10 | + |
| 11 | +function getTimeRemaining(from: Date, to: Date): TimeRemaining { |
| 12 | + const epochFrom: number = from.valueOf() |
| 13 | + const epochTo: number = to.valueOf() |
| 14 | + |
| 15 | + let conversion: number |
| 16 | + let epochRemainingTime: number = epochTo - epochFrom |
| 17 | + |
| 18 | + if (epochRemainingTime <= 0) |
| 19 | + return { |
| 20 | + days: 0, |
| 21 | + hours: 0, |
| 22 | + minutes: 0, |
| 23 | + seconds: 0, |
| 24 | + } |
| 25 | + |
| 26 | + conversion = 24 * 60 * 60 * 1000 |
| 27 | + const days: number = Math.floor(epochRemainingTime / conversion) |
| 28 | + epochRemainingTime -= days * conversion |
| 29 | + |
| 30 | + conversion = 60 * 60 * 1000 |
| 31 | + const hours: number = Math.floor(epochRemainingTime / conversion) |
| 32 | + epochRemainingTime -= hours * conversion |
| 33 | + |
| 34 | + conversion = 60 * 1000 |
| 35 | + const minutes: number = Math.floor(epochRemainingTime / conversion) |
| 36 | + epochRemainingTime -= minutes * conversion |
| 37 | + |
| 38 | + conversion = 1000 |
| 39 | + const seconds: number = Math.floor(epochRemainingTime / conversion) |
| 40 | + epochRemainingTime -= seconds * conversion |
| 41 | + |
| 42 | + return { |
| 43 | + days: Math.max(0, days), |
| 44 | + hours: Math.max(0, hours), |
| 45 | + minutes: Math.max(0, minutes), |
| 46 | + seconds: Math.max(0, seconds), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +async function main() { |
| 51 | + let startDate: Date = new Date() // Current date |
| 52 | + const endDate: Date = new Date(2024, 10, 27, 23, 59, 0, 0) |
| 53 | + let remainingTime: TimeRemaining = getTimeRemaining(startDate, endDate) |
| 54 | + |
| 55 | + if (!isMainThread) parentPort?.postMessage('clearConsole') |
| 56 | + |
| 57 | + console.log( |
| 58 | + `> Time remaining for ${endDate.toUTCString()}: ` + |
| 59 | + `[ ${remainingTime.days.toString().padStart(6, '0')} days | ` + |
| 60 | + `${remainingTime.hours.toString().padStart(2, '0')} hours | ` + |
| 61 | + `${remainingTime.minutes.toString().padStart(2, '0')} minutes | ` + |
| 62 | + `${remainingTime.seconds.toString().padStart(2, '0')} seconds ].` |
| 63 | + ) |
| 64 | + |
| 65 | + while ( |
| 66 | + remainingTime.days || |
| 67 | + remainingTime.hours || |
| 68 | + remainingTime.minutes || |
| 69 | + remainingTime.seconds |
| 70 | + ) { |
| 71 | + startDate = new Date() // Current date |
| 72 | + remainingTime = await setTimeout( |
| 73 | + 1 * 1000, |
| 74 | + getTimeRemaining(startDate, endDate) |
| 75 | + ) |
| 76 | + |
| 77 | + if (!isMainThread) parentPort?.postMessage('clearConsole') |
| 78 | + |
| 79 | + console.log( |
| 80 | + `> Time remaining for ${endDate.toUTCString()}: ` + |
| 81 | + `[ ${remainingTime.days.toString().padStart(6, '0')} days | ` + |
| 82 | + `${remainingTime.hours.toString().padStart(2, '0')} hours | ` + |
| 83 | + `${remainingTime.minutes |
| 84 | + .toString() |
| 85 | + .padStart(2, '0')} minutes | ` + |
| 86 | + `${remainingTime.seconds |
| 87 | + .toString() |
| 88 | + .padStart(2, '0')} seconds ].` |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + console.log('\n> The day has come!') |
| 93 | +} |
| 94 | + |
| 95 | +if (isMainThread) { |
| 96 | + const worker: Worker = new Worker(__filename) |
| 97 | + |
| 98 | + worker.on('message', (message) => { |
| 99 | + if (message === 'clearConsole') console.clear() |
| 100 | + }) |
| 101 | +} else { |
| 102 | + main() |
| 103 | +} |
0 commit comments