Fix incorrect month wrapping when parsing weekdays
This commit is contained in:
parent
909715bdf9
commit
1d0f08d3f0
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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')
|
||||||
}
|
}
|
||||||
|
|
@ -41,14 +43,25 @@ const parseWeekdayDate = (str: string): Temporal.ZonedDateTime => {
|
||||||
const [hour, minute] = [Number(str.substring(0, 2)), Number(str.substring(2, 4))]
|
const [hour, minute] = [Number(str.substring(0, 2)), Number(str.substring(2, 4))]
|
||||||
let dayOfWeek = Number(str.substring(5))
|
let dayOfWeek = Number(str.substring(5))
|
||||||
if (dayOfWeek === 0) {
|
if (dayOfWeek === 0) {
|
||||||
dayOfWeek = 7 // Sunday is 7 in ISO8601
|
dayOfWeek = 7 // Sunday is 7 in ISO8601
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue