Merge pull request #302 from giraugh/fix/i-hate-mondays

Fix missing days
This commit is contained in:
Ewan Breakey 2023-08-02 23:13:20 +10:00 committed by GitHub
commit 62cdb0b745
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View file

@ -7,10 +7,10 @@ import { Temporal } from '@js-temporal/polyfill'
*/
export const calculateColumns = (dates: Temporal.ZonedDateTime[]): (Temporal.PlainDate | null)[] => {
// Dedupe dates by date and sort
const sortedDates = [...new Map(dates.map(d => {
const sortedDates = Array.from(new Map(dates.map(d => {
const plain = d.toPlainDate()
return [plain.toString(), plain]
})).values()]
})).values())
.sort(Temporal.PlainDate.compare)
// Partition by distance

View file

@ -10,7 +10,7 @@ export const convertTimesToDates = (times: string[], timezone: string): Temporal
return times.map(time => isSpecificDates ?
parseSpecificDate(time).withTimeZone(timezone)
: parseWeekdayDate(time).withTimeZone(timezone)
: parseWeekdayDate(time, timezone).withTimeZone(timezone)
)
}
@ -32,7 +32,7 @@ export const parseSpecificDate = (str: string): Temporal.ZonedDateTime => {
}
// Parse from UTC `HHmm-d` format into a ZonedDateTime in UTC based on the current date
const parseWeekdayDate = (str: string): Temporal.ZonedDateTime => {
const parseWeekdayDate = (str: string, timezone: string): Temporal.ZonedDateTime => {
if (str.length !== 6) {
throw new Error('String must be in HHmm-d format')
}
@ -43,9 +43,20 @@ const parseWeekdayDate = (str: string): Temporal.ZonedDateTime => {
// Construct PlainDateTime from today
const today = Temporal.Now.zonedDateTimeISO('UTC').round('day')
const currentDayOfWeek = today.dayOfWeek
return today.with({
hour, minute,
day: today.day + (dayOfWeek - currentDayOfWeek), // Set day of week
const dayDelta = dayOfWeek - today.dayOfWeek
const resultDay = today.add({ days: dayDelta })
let resultDate = resultDay.with({
hour, minute
})
// If resulting day (in target timezone) is in the next week, move it back to this week
// TODO: change data representation instead
const dayInTz = resultDate.withTimeZone(timezone)
const todayInTz = today.withTimeZone(timezone)
if (dayInTz.weekOfYear > todayInTz.weekOfYear) {
resultDate = resultDate.subtract({ days: 7 })
}
return resultDate
}