Skip to content

Instantly share code, notes, and snippets.

@nafeu
Last active October 27, 2022 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nafeu/c56a2188666a3af462537d16a81623f7 to your computer and use it in GitHub Desktop.
Save nafeu/c56a2188666a3af462537d16a81623f7 to your computer and use it in GitHub Desktop.
Timezone Message Script
/*
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