Skip to content

Instantly share code, notes, and snippets.

@tim-rohrer
Last active January 15, 2024 18:08
Show Gist options
  • Save tim-rohrer/5a18691f3ba206c6c6ce7a90514b0de0 to your computer and use it in GitHub Desktop.
Save tim-rohrer/5a18691f3ba206c6c6ce7a90514b0de0 to your computer and use it in GitHub Desktop.
Provides code that can be used to generate startDateUTC based on a provided date
/**
* These are snippets/functions of code used in another app to migrate meeting data from Central to
* an experimental database used by Central Query.
*/
import { DateTime } from "luxon"
enum Weekdays {
MONDAY = 1,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY,
}
const tz = formatTZ(meeting.timezone)
const formatTZ = (tz: string) => {
const parts = tz.split("/")
const newParts = parts.map((part) => {
const subParts = part.split("_")
const newSubParts = subParts.map((sp) => {
const ret = ["in", "of"].includes(sp)
? sp
: sp[0].toUpperCase() + sp.substring(1).toLowerCase()
return ret
})
return newSubParts.join("_")
})
return newParts.join("/")
}
const updated = dstAware(meeting.time, tz)
const dstAware = (time: string, tz: string) => {
const localTimeParts = time.split(":")
const now = DateTime.utc()
const date = {
year: now.year,
month: now.month,
day: now.day,
hour: Number(localTimeParts[0]),
minute: Number(localTimeParts[1]),
}
return DateTime.fromObject(date, { zone: tz })
}
const startDateUTC = nextOccurrence(weekday, updated).toJSDate()
const nextOccurrence = (dayOfWeek: Weekdays, dateTime: DateTime) => {
const adjustedDayOfWeek = dayOfWeek
const advance = (adjustedDayOfWeek + (7 - dateTime.get("weekday"))) % 7
const newOrdinalDate = dateTime.ordinal + advance
return dateTime.set({ ordinal: newOrdinalDate })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment