finalize time parsing, reorg all date/time parsing to new file

This commit is contained in:
Ean Milligan (Bastion) 2023-03-26 03:55:33 -04:00
parent 06b35d5a2c
commit 2b62cd0e72
4 changed files with 137 additions and 98 deletions

View File

@ -0,0 +1,132 @@
export const monthsLong: Array<string> = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
export const monthsShort: Array<string> = monthsLong.map(month => month.slice(0, 3));
export const tzMap: Map<string, string> = new Map([
['CDT', '-05:00'],
['CST', '-06:00'],
['PST', '-08:00'],
['IST', '+05:30'],
['GMT', '+00:00'],
['EAT', '+03:00'],
['CET', '+01:00'],
['WAT', '+01:00'],
['CAT', '+02:00'],
['EET', '+02:00'],
['CEST', '+02:00'],
['SAST', '+02:00'],
['HST', '-10:00'],
['HDT', '-09:00'],
['AKST', '-09:00'],
['AKDT', '-08:00'],
['AST', '-04:00'],
['EST', '-05:00'],
['MST', '-07:00'],
['MDT', '-06:00'],
['EDT', '-04:00'],
['PDT', '-07:00'],
['ADT', '-03:00'],
['NST', '-03:30'],
['NDT', '-02:30'],
['AEST', '+10:00'],
['AEDT', '+11:00'],
['NZST', '+12:00'],
['NZDT', '+13:00'],
['EEST', '+03:00'],
['HKT', '+08:00'],
['WIB', '+07:00'],
['WIT', '+09:00'],
['IDT', '+03:00'],
['PKT', '+05:00'],
['WITA', '+08:00'],
['KST', '+09:00'],
['JST', '+09:00'],
['WET', '+00:00'],
['WEST', '+01:00'],
['ACST', '+09:30'],
['ACDT', '+10:30'],
['AWST', '+08:00'],
['UTC', '+00:00'],
['BST', '+01:00'],
['MSK', '+03:00'],
['MET', '+01:00'],
['MEST', '+02:00'],
['CHST', '+10:00'],
['SST', '-11:00'],
]);
export const shorthandUSTZ: Array<string> = ['ET', 'CT', 'MT', 'PT'];
// Takes user input Time and makes it actually usable
const parseEventTime = (preParsedEventTime: string): [string, string, string] => {
let parsedEventTimePeriod = '';
// Get AM or PM out of the rawTime
if (preParsedEventTime.endsWith('AM') || preParsedEventTime.endsWith('PM')) {
parsedEventTimePeriod = preParsedEventTime.slice(-2);
preParsedEventTime = preParsedEventTime.slice(0, -2).trim();
}
let parsedEventTimeHours: string;
let parsedEventTimeMinutes: string;
// Get Hours and Minutes out of rawTime
if (preParsedEventTime.length > 2) {
parsedEventTimeMinutes = preParsedEventTime.slice(-2);
parsedEventTimeHours = preParsedEventTime.slice(0, -2).trim();
} else {
parsedEventTimeHours = preParsedEventTime.trim();
parsedEventTimeMinutes = '00'
}
// Determine if we need to remove the time period
if (parseInt(parsedEventTimeHours) > 12) {
parsedEventTimePeriod = '';
}
return [parsedEventTimeHours, parsedEventTimeMinutes, parsedEventTimePeriod];
}
// Takes user input Time Zone and makes it actually usable
const parseEventTimeZone = (preParsedEventTimeZone: string): string => {
if (shorthandUSTZ.includes(preParsedEventTimeZone)) {
// Handle shorthand US timezones, adding S for standard time and D for Daylight Savings
const today = new Date();
const jan = new Date(today.getFullYear(), 0, 1);
const jul = new Date(today.getFullYear(), 6, 1);
if (today.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset())) {
preParsedEventTimeZone = `${preParsedEventTimeZone.slice(0, 1)}DT`;
} else {
preParsedEventTimeZone = `${preParsedEventTimeZone.slice(0, 1)}ST`;
}
}
if (tzMap.has(preParsedEventTimeZone)) {
// TZ is proper abbreviation, use our map to convert
return tzMap.get(preParsedEventTimeZone) || 'how did we get here?';
} else {
// Determine if user put in UTC4, which needs to be UTC+4
let addPlusSign = false;
if (!preParsedEventTimeZone.includes('+') || !preParsedEventTimeZone.includes('-')) {
addPlusSign = true;
}
// Determine if we need to prepend UTC/GMT, handle adding the + into the string
if (!preParsedEventTimeZone.startsWith('UTC') || preParsedEventTimeZone.startsWith('GMT')) {
preParsedEventTimeZone = `UTC${addPlusSign && '+'}${preParsedEventTimeZone}`;
} else if (addPlusSign) {
preParsedEventTimeZone = `${preParsedEventTimeZone.slice(0, 3)}+${preParsedEventTimeZone.slice(3)}`;
}
return preParsedEventTimeZone;
}
};
// Takes user input Date and makes it actually usable
const parseEventDate = (preParsedEventDate: string): [string, string, string] => {
return ['','',''];
}
// Take full raw Date/Time input and convert it to a proper Date
export const getDateFromRawInput = (rawEventTime: string, rawEventTimeZone: string, rawEventDate: string): Date => {
// Verify/Set Time
const [parsedEventTimeHours, parsedEventTimeMinutes, parsedEventTimePeriod] = parseEventTime(rawEventTime.replaceAll(':', '').toUpperCase())
// Verify/Set Time Zone
const parsedEventTimeZone = parseEventTimeZone(rawEventTimeZone.replaceAll(' ', '').trim().toUpperCase());
// Verify/Set Date
const [parsedEventYear, parsedEventMonth, parsedEventDay] = parseEventDate(rawEventDate.trim().toUpperCase());
return new Date(`${parsedEventMonth} ${parsedEventDay}, ${parsedEventYear} ${parsedEventTimeHours}:${parsedEventTimeMinutes} ${parsedEventTimePeriod} ${parsedEventTimeZone}`);
};

View File

@ -72,7 +72,7 @@ const execute = async (bot: Bot, interaction: Interaction) => {
type: MessageComponentTypes.InputText,
customId: eventDateId,
label: 'Start Date:',
placeholder: 'Enter date as "MONTH/DAY" or "Month, Day"',
placeholder: 'Enter date as "MONTH/DAY/YEAR" or "Month, Day, Year"',
style: TextStyles.Short,
minLength: 1,
maxLength: 20,

View File

@ -1,8 +1,9 @@
import { Bot, Interaction, InteractionResponseTypes, MessageComponentTypes, TextStyles } from '../../../deps.ts';
import { somethingWentWrong } from '../../commandUtils.ts';
import { eventTimeId, eventTimeZoneId, eventDateId, eventDescriptionId } from './step1-gameSelection.ts';
import { getFinalActivity, tzMap, idSeparator, pathIdxSeparator } from './utils.ts';
import { getFinalActivity, idSeparator, pathIdxSeparator } from './utils.ts';
import { Activities, Activity } from './activities.ts';
import { getDateFromRawInput } from './dateTimeUtils.ts';
export const customId = 'finalize';
@ -49,48 +50,8 @@ const execute = async (bot: Bot, interaction: Interaction) => {
return;
}
// Verify/Set Time
let preParsedEventTime = rawEventTime.replaceAll(':', '').toUpperCase();
let parsedEventTimePeriod = '';
// Get AM or PM out of the rawTime
if (preParsedEventTime.endsWith('AM') || preParsedEventTime.endsWith('PM')) {
parsedEventTimePeriod = preParsedEventTime.slice(-2);
preParsedEventTime = preParsedEventTime.slice(0, -2).trim();
}
let parsedEventTimeHours: string;
let parsedEventTimeMinutes: string;
// Get Hours and Minutes out of rawTime
if (preParsedEventTime.length > 2) {
parsedEventTimeMinutes = preParsedEventTime.slice(-2);
parsedEventTimeHours = preParsedEventTime.slice(0, -2).trim();
} else {
parsedEventTimeHours = preParsedEventTime.trim();
parsedEventTimeMinutes = '00'
}
// Determine if we need to remove the time period
if (parseInt(parsedEventTimeHours) > 12) {
parsedEventTimePeriod = '';
}
// Verify/Set Time Zone
let preParsedEventTimeZone = rawEventTimeZone.replaceAll(' ', '').trim().toUpperCase();
let parsedEventTimeZone = '';
if (tzMap.has(preParsedEventTimeZone)) {
parsedEventTimeZone = tzMap.get(preParsedEventTimeZone) || 'UTC+0';
} else {
let addPlusSign = false;
if (!preParsedEventTimeZone.includes('+') || !preParsedEventTimeZone.includes('-')) {
addPlusSign = true;
}
if (!preParsedEventTimeZone.startsWith('UTC') || preParsedEventTimeZone.startsWith('GMT')) {
preParsedEventTimeZone = `UTC${addPlusSign && '+'}${preParsedEventTimeZone}`;
} else if (addPlusSign) {
preParsedEventTimeZone = `${preParsedEventTimeZone.slice(0, 3)}+${preParsedEventTimeZone.slice(3)}`;
}
parsedEventTimeZone = preParsedEventTimeZone;
}
// Verify/Set Date
// Get Date Object from user input
const eventDateTime = getDateFromRawInput(rawEventTime, rawEventTimeZone, rawEventDate);
somethingWentWrong(bot, interaction, `TESTING@${rawEventTime}_${rawEventTimeZone}_${rawEventDate}`);
} else {

View File

@ -9,60 +9,6 @@ export const idSeparator = '@';
export const pathIdxSeparator = '|';
export const pathIdxEnder = '&';
export const selfDestructMessage = (currentTime: number) => `**Please note:** This message will self destruct <t:${Math.floor((currentTime + tokenTimeoutMS) / 1000)}:R> due to limits imposed by the Discord API.`;
export const monthsLong: Array<string> = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
export const monthsShort: Array<string> = monthsLong.map(month => month.slice(0, 3));
export const tzMap: Map<string, string> = new Map([
['CDT', '-05:00'],
['CST', '-06:00'],
['PST', '-08:00'],
['IST', '+05:30'],
['GMT', '+00:00'],
['EAT', '+03:00'],
['CET', '+01:00'],
['WAT', '+01:00'],
['CAT', '+02:00'],
['EET', '+02:00'],
['CEST', '+02:00'],
['SAST', '+02:00'],
['HST', '-10:00'],
['HDT', '-09:00'],
['AKST', '-09:00'],
['AKDT', '-08:00'],
['AST', '-04:00'],
['EST', '-05:00'],
['MST', '-07:00'],
['MDT', '-06:00'],
['EDT', '-04:00'],
['PDT', '-07:00'],
['ADT', '-03:00'],
['NST', '-03:30'],
['NDT', '-02:30'],
['AEST', '+10:00'],
['AEDT', '+11:00'],
['NZST', '+12:00'],
['NZDT', '+13:00'],
['EEST', '+03:00'],
['HKT', '+08:00'],
['WIB', '+07:00'],
['WIT', '+09:00'],
['IDT', '+03:00'],
['PKT', '+05:00'],
['WITA', '+08:00'],
['KST', '+09:00'],
['JST', '+09:00'],
['WET', '+00:00'],
['WEST', '+01:00'],
['ACST', '+09:30'],
['ACDT', '+10:30'],
['AWST', '+08:00'],
['UTC', '+00:00'],
['BST', '+01:00'],
['MSK', '+03:00'],
['MET', '+01:00'],
['MEST', '+02:00'],
['CHST', '+10:00'],
['SST', '-11:00'],
]);
export const tokenMap: Map<string, {
token: string;