Fix incorrect month wrapping when parsing weekdays

This commit is contained in:
Ewan Breakey 2023-08-02 22:54:01 +10:00
parent 909715bdf9
commit 1d0f08d3f0
2 changed files with 23 additions and 10 deletions

View file

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

View file

@ -8,9 +8,11 @@ import { Temporal } from '@js-temporal/polyfill'
export const convertTimesToDates = (times: string[], timezone: string): Temporal.ZonedDateTime[] => { export const convertTimesToDates = (times: string[], timezone: string): Temporal.ZonedDateTime[] => {
const isSpecificDates = times[0].length === 13 const isSpecificDates = times[0].length === 13
console.log(times)
return times.map(time => isSpecificDates ? return times.map(time => isSpecificDates ?
parseSpecificDate(time).withTimeZone(timezone) parseSpecificDate(time).withTimeZone(timezone)
: parseWeekdayDate(time).withTimeZone(timezone) : parseWeekdayDate(time, timezone).withTimeZone(timezone)
) )
} }
@ -32,7 +34,7 @@ export const parseSpecificDate = (str: string): Temporal.ZonedDateTime => {
} }
// Parse from UTC `HHmm-d` format into a ZonedDateTime in UTC based on the current date // 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) { if (str.length !== 6) {
throw new Error('String must be in HHmm-d format') throw new Error('String must be in HHmm-d format')
} }
@ -45,10 +47,21 @@ const parseWeekdayDate = (str: string): Temporal.ZonedDateTime => {
} }
// Construct PlainDateTime from today // Construct PlainDateTime from today
const today = Temporal.Now.zonedDateTimeISO('UTC').round('day') const today = Temporal.Now.zonedDateTimeISO('Utc').round('day')
const currentDayOfWeek = today.dayOfWeek const dayDelta = dayOfWeek - today.dayOfWeek
return today.with({ const resultDay = today.add({ days: dayDelta })
hour, minute,
day: today.day + (dayOfWeek - currentDayOfWeek), // Set day of week 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
} }