Last active
October 27, 2022 17:53
-
-
Save nafeu/c56a2188666a3af462537d16a81623f7 to your computer and use it in GitHub Desktop.
Timezone Message Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage: | |
1. Make sure you have node installed on your machine (https://github.com/Schniz/fnm) | |
2. Run `npm install --global moment-timezone` | |
3. Replace ROOT_TIMEZONE with your local timezone | |
ie. const ROOT_TIMEZONE = 'America/Toronto' | |
4. Use `node timezone-message.js [MESSAGE] [TIME] [AM/PM]` | |
ie. `node timezone-message.js 'next pomodoro starting at:' 3:00 PM` | |
Example Output: | |
next pomodoro starting at: | |
12:00 PM β πΊπΈ Los Angeles | |
2:00 PM β πΊπΈ Chicago | |
3:00 PM β π¨π¦ Toronto | |
4:00 PM β π¦π· Buenos Aires | |
9:00 PM β π©πͺ Berlin | |
4:00 AM β π―π΅ Tokyo | |
*/ | |
const moment = require('moment-timezone') | |
const args = process.argv.slice(2); | |
const ROOT_TIMEZONE = 'America/Toronto'; | |
const LOCATIONS = [ | |
{ | |
timezone: 'America/Los_Angeles', | |
flag: 'πΊπΈ' | |
}, | |
{ | |
timezone: 'America/Chicago', | |
flag: 'πΊπΈ' | |
}, | |
{ | |
timezone: 'America/Toronto', | |
flag: 'π¨π¦' | |
}, | |
{ | |
timezone: 'America/Argentina/Buenos_Aires', | |
flag: 'π¦π·' | |
}, | |
{ | |
timezone: 'Europe/Berlin', | |
flag: 'π©πͺ' | |
}, | |
{ | |
timezone: 'Asia/Tokyo', | |
flag: 'π―π΅' | |
} | |
]; | |
const FORMAT = 'hh:mm A'; | |
const main = () => { | |
const message = args[0] || '' | |
const root = moment() | |
.tz(ROOT_TIMEZONE); | |
if (args[1]) { | |
const inputTime = args[1]; | |
const inputAmPm = args[2] || 'pm'; | |
let hour = Number(inputTime.split(":")[0]); | |
if (hour < 12 && inputAmPm.toLowerCase() === 'pm') { | |
hour += 12; | |
} | |
const minute = Number(inputTime.split(":")[1] || 0); | |
root | |
.hour(hour) | |
.minute(minute); | |
} | |
let output = '' | |
output += `${message}`; | |
LOCATIONS.forEach(({ timezone, flag }) => { | |
let timeString = root.clone().tz(timezone).format(FORMAT); | |
const city = timezone.split('/')[timezone.split('/').length - 1].replace('_', ' '); | |
if (timeString[0] === '0') { | |
timeString = ' ' + timeString.substring(1); | |
} | |
output += `\n${timeString} β ${flag} ${city}`; | |
}) | |
console.log(output); | |
} | |
if (args.length > 0) { | |
main(); | |
} else { | |
console.log(`Usage: '[MESSAGE]' (TIME) (AM/PM)`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment