Skip to content

Commit

Permalink
Timezone bugfix (#545)
Browse files Browse the repository at this point in the history
Fixing bug affecting calendar and support page thanks to @paulbou who generously 
helped us in identifying these issues!!!! 🤗

Bumping project to next patch version
Starting to fix issue affecting timezone
Alter /nous-soutenir page
Revising README
Updating deps
Revising support page
Handling timezone distinct from Europe/Paris
Fixing error affecting support page
  • Loading branch information
thierrymarianne authored Mar 25, 2023
1 parent 6fd3546 commit a60bb09
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 86 deletions.
130 changes: 80 additions & 50 deletions components/calendar-month/calendar-month.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<template>
<div class="calendar-month">
<div
class="calendar-month"
>
<div
class="calendar-month__buttons"
:style="pickItemIcon"
Expand Down Expand Up @@ -47,10 +49,13 @@
</th>
</tr>
</thead>
<tbody class="calendar-month__day-numbers">
<tbody
class="calendar-month__day-numbers"
>
<tr
v-for="(rowNumber, rowIndex) in dayRows()"
:key="rowIndex"
:data-key="rowIndex"
class="calendar-month__day-row"
>
<td
Expand Down Expand Up @@ -196,24 +201,72 @@ class CalendarMonth extends mixins(ApiMixin) {
return this.getNextMonth(this.month, this.year)
}
dateOfFirstVisibleDay () {
let firstVisibleDayCandidate = this.nameOfFirstDayOfMonth()
get daysAfterSelectedMonth (): number {
let dateOfLastVisibleDay
const dateCandidate = this.setTimezone(new Date(this.year, this.month, 1))
try {
dateOfLastVisibleDay = this.dateOfLastVisibleDay()
} catch (e) {
return 0
}
this.guardAgainstMissingMonthOrYear(firstVisibleDayCandidate)
let daysAfterSelectedMonth = 0
if (dateOfLastVisibleDay.getMonth() !== this.month) {
daysAfterSelectedMonth = dateOfLastVisibleDay.getDate()
}
while (firstVisibleDayCandidate !== 'Lun.') {
dateCandidate.setDate(dateCandidate.getDate() - 1)
firstVisibleDayCandidate = this.whichDayOfWeek(dateCandidate.getDay())
return daysAfterSelectedMonth
}
get daysBeforeSelectedMonth (): number {
let dateOfFirstVisibleDay
try {
dateOfFirstVisibleDay = this.dateOfFirstVisibleDay
} catch (e) {
return 0
}
return dateCandidate
let daysBeforeSelectedMonth = 0
if (dateOfFirstVisibleDay.getMonth() !== this.month) {
daysBeforeSelectedMonth = this.totalDaysInPreviousMonth() - dateOfFirstVisibleDay.getDate() + 1
}
return daysBeforeSelectedMonth
}
get totalDaysInMonth (): number {
return this.setTimezone(new Date(this.year, this.month + 1, 0)).getDate()
}
get dateOfFirstVisibleDay (): Date {
const firstDayOfMonth = structuredClone(this.now())
firstDayOfMonth.setDate(1)
let dateOfFirstVisibleDay = this.findDateOfFirstVisibleDay(firstDayOfMonth)
if (this.whichDateHasBeenPicked && (
dateOfFirstVisibleDay.getDay() !== this.whichDateHasBeenPicked.getDay() ||
dateOfFirstVisibleDay.getMonth() !== this.whichDateHasBeenPicked.getMonth() ||
dateOfFirstVisibleDay.getFullYear() !== this.whichDateHasBeenPicked.getFullYear()
)) {
const dateOfFirstVisibleDayCandidate = structuredClone(this.whichDateHasBeenPicked)
dateOfFirstVisibleDayCandidate.setDate(1)
dateOfFirstVisibleDayCandidate.setHours(0)
dateOfFirstVisibleDayCandidate.setMinutes(0)
dateOfFirstVisibleDay = this.findDateOfFirstVisibleDay(dateOfFirstVisibleDayCandidate)
}
return dateOfFirstVisibleDay
}
get nameOfFirstDayOfMonth () : string {
return this.daysOfWeek[this.setTimezone(new Date(this.year, this.month, 1)).getDay()]
}
dateOfLastVisibleDay () {
let lastVisibleDayCandidate = this.nameOfLastDayOfMonth()
const dateCandidate = this.setTimezone(new Date(this.year, this.month, this.totalDaysInMonth()))
const dateCandidate = this.setTimezone(new Date(this.year, this.month, this.totalDaysInMonth))
this.guardAgainstMissingMonthOrYear(lastVisibleDayCandidate)
Expand All @@ -231,7 +284,7 @@ class CalendarMonth extends mixins(ApiMixin) {
return (new Array(7))
.fill('', 0, 7)
.map((_, index) => {
const dayOfWeek = this.setTimezone(new Date(this.dateOfFirstVisibleDay().getTime()))
const dayOfWeek = this.setTimezone(new Date(this.dateOfFirstVisibleDay.getTime()))
dayOfWeek.setDate(dayOfWeek.getDate() + index + shift)
return dayOfWeek
Expand All @@ -254,56 +307,33 @@ class CalendarMonth extends mixins(ApiMixin) {
this.pickDate(this.nextMonth)
}
nameOfFirstDayOfMonth () {
return this.daysOfWeek[this.setTimezone(new Date(this.year, this.month, 1)).getDay()]
findDateOfFirstVisibleDay (date: Date): Date {
let firstVisibleDayCandidate = this.nameOfFirstDayOfMonth
const dateCandidate = this.setTimezone(date)
this.guardAgainstMissingMonthOrYear(firstVisibleDayCandidate)
while (firstVisibleDayCandidate !== 'Lun.') {
dateCandidate.setDate(dateCandidate.getDate() - 1)
firstVisibleDayCandidate = this.whichDayOfWeek(dateCandidate.getDay())
}
return dateCandidate
}
nameOfLastDayOfMonth () {
const lastDayNumberOfMonth = this.totalDaysInMonth()
const lastDayNumberOfMonth = this.totalDaysInMonth
return this.whichDayOfWeek(this.setTimezone(new Date(this.year, this.month, lastDayNumberOfMonth)).getDay())
}
totalDaysInMonth (): number {
return this.setTimezone(new Date(this.year, this.month + 1, 0)).getDate()
}
totalDaysInPreviousMonth (): number {
return this.setTimezone(new Date(this.year, this.month, 0)).getDate()
}
dayRows () {
let dateOfFirstVisibleDay
try {
dateOfFirstVisibleDay = this.dateOfFirstVisibleDay()
} catch (e) {
return
}
let daysBeforeSelectedMonth = 0
if (dateOfFirstVisibleDay.getMonth() !== this.month) {
daysBeforeSelectedMonth = this.totalDaysInPreviousMonth() - dateOfFirstVisibleDay.getDate() + 1
}
let dateOfLastVisibleDay
try {
dateOfLastVisibleDay = this.dateOfLastVisibleDay()
} catch (e) {
return
}
let daysAfterSelectedMonth = 0
if (dateOfLastVisibleDay.getMonth() !== this.month) {
daysAfterSelectedMonth = dateOfLastVisibleDay.getDate()
}
return (
daysBeforeSelectedMonth +
this.totalDaysInMonth() +
daysAfterSelectedMonth
) / 7
return Math.floor((this.daysBeforeSelectedMonth + this.totalDaysInMonth + this.daysAfterSelectedMonth) / 7)
}
getNextItemClasses () {
Expand Down
1 change: 1 addition & 0 deletions layouts/error.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<Highlights
v-if="false"
:error="error"
:error-message-provider="errorMessageProvider"
>
Expand Down
19 changes: 13 additions & 6 deletions mixins/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Component, Vue } from 'nuxt-property-decorator'
import Time from '../modules/time'
import Errors from '../modules/errors'

const clientTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone

export const formatDate = (date: Date): string => {
return Time.formatDate(date)
}
Expand All @@ -16,16 +18,17 @@ export const isValidDate = (day: string): boolean => {
return Time.formatDate(new Date(day)) === day
}

export const setTimezone = (date: Date, timezone = 'Europe/Paris'): Date => {
export const setTimezone = (date: Date, timezone = clientTimezone): Date => {
return new Date(date.toLocaleString('en-US', { timeZone: timezone }))
}

export const getMinDate = () => {
return new Date(Date.parse('01 Jan 2018 00:00:00 GMT'))
return setTimezone(new Date(Date.parse('01 Jan 2018 00:00:00 GMT')))
}

export const now = (timezone = 'Europe/Paris'): Date => {
return setTimezone(new Date(), timezone)
export const now = (timezone = clientTimezone): Date => {
const rightNow = new Date()
return setTimezone(rightNow, timezone)
}

@Component
Expand Down Expand Up @@ -117,15 +120,19 @@ export default class DateMixin extends Vue {
return this.setTimezone(new Date(year, month - 1, 1))
}

setTimezone (date: Date, timezone = 'Europe/Paris'): Date {
setTimezone (date: Date, timezone = clientTimezone): Date {
return setTimezone(date, timezone)
}

now (timezone = 'Europe/Paris'): Date {
now (timezone = clientTimezone): Date {
return now(timezone)
}

whichDayOfWeek (dayNumber: number): string {
return this.daysOfWeek[dayNumber]
}

get whichDateHasBeenPicked (): Date|null {
return this.$store.getters['date-picker/datePicker']()
}
}
60 changes: 30 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pages/highlight/_day.vue
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ export default class Highlights extends mixins(SourcesMixin) {
'homepage',
'contact',
'sources',
'support',
'not-found'
].find(route => route === ctx.route.name)) {
return true
Expand Down Expand Up @@ -716,6 +717,7 @@ export default class Highlights extends mixins(SourcesMixin) {
'contact',
'legal-notice',
'sources',
'support',
'not-found'
].every(r => r !== this.$route.name) || (
this.visitingCuratedHighlightsRoute &&
Expand Down

0 comments on commit a60bb09

Please sign in to comment.