Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(flags): respect project timezone in schedule date picker (#61274)

Ben Lead26f8d95b7ac
diff --git a/frontend/src/lib/dayjs.ts b/frontend/src/lib/dayjs.ts
index c5b7c766e3be..0a1d8376447f 100644
--- a/frontend/src/lib/dayjs.ts
+++ b/frontend/src/lib/dayjs.ts
@@ -48,6 +48,16 @@ export function dayjsUtcToTimezone(
return datetime
}
+/** Current moment expressed as the given timezone's wall clock, returned as a naive (offset-free) Dayjs
+ * so it stays comparable to naive picked datetimes. Falls back to browser-local time on an invalid timezone. */
+export function dayjsNowInTimezone(timezone: string): Dayjs {
+ try {
+ return dayjs(dayjs().tz(timezone).format('YYYY-MM-DDTHH:mm:ss.SSS'))
+ } catch {
+ return dayjs()
+ }
+}
+
/** Parse local datetime string using Day.js, taking into account time zone conversion edge cases. */
export function dayjsLocalToTimezone(
isoString: string,
diff --git a/frontend/src/lib/lemon-ui/LemonCalendar/LemonCalendarSelect.test.tsx b/frontend/src/lib/lemon-ui/LemonCalendar/LemonCalendarSelect.test.tsx
index 777d6f649253..f5a9e72937bb 100644
--- a/frontend/src/lib/lemon-ui/LemonCalendar/LemonCalendarSelect.test.tsx
+++ b/frontend/src/lib/lemon-ui/LemonCalendar/LemonCalendarSelect.test.tsx
@@ -160,6 +160,53 @@ describe('LemonCalendarSelect', () => {
expect(onChange).toHaveBeenLastCalledWith(dayjs('2023-01-10T20:22:00.000Z'))
})
+ test('upcoming selection uses selectionPeriodTimezone for the boundary', async () => {
+ // 17:22 UTC is 12:22 in New York, so a same-day 2pm slot is still upcoming there.
+ const { onChange, clickOnDate, clickOnTime } = renderLemonCalendarSelect(null, {
+ granularity: 'minute',
+ selectionPeriod: 'upcoming',
+ selectionPeriodTimezone: 'America/New_York',
+ })
+
+ await clickOnDate('10')
+ expect(onChange).toHaveBeenCalledWith(dayjs('2023-01-10T12:22:00.000Z'))
+
+ await clickOnTime({ unit: 'h', value: '2' })
+ expect(onChange).toHaveBeenLastCalledWith(dayjs('2023-01-10T14:22:00.000Z'))
+ })
+
+ test('upcoming selection resolves the day boundary in selectionPeriodTimezone', async () => {
+ // 04:00 UTC on Jan 11 is still 23:00 on Jan 10 in New York, so "today" is Jan 10 there, not Jan 11 UTC.
+ jest.setSystemTime(new Date('2023-01-11 04:00:00'))
+ const { onChange, clickOnDate } = renderLemonCalendarSelect(null, {
+ granularity: 'minute',
+ selectionPeriod: 'upcoming',
+ selectionPeriodTimezone: 'America/New_York',
+ })
+
+ // Jan 10 is today in New York, so it stays selectable (UTC-local would wrongly block it as past).
+ await clickOnDate('10')
+ expect(onChange).toHaveBeenCalledWith(dayjs('2023-01-10T23:00:00.000Z'))
+ })
+
+ test('past selection resolves the day boundary in selectionPeriodTimezone', async () => {
+ // 20:00 UTC on Jan 10 is already 05:00 on Jan 11 in Tokyo, so "today" is Jan 11 there, not Jan 10 UTC.
+ jest.setSystemTime(new Date('2023-01-10 20:00:00'))
+ const { onChange, clickOnDate } = renderLemonCalendarSelect(null, {
+ granularity: 'minute',
+ selectionPeriod: 'past',
+ selectionPeriodTimezone: 'Asia/Tokyo',
+ })
+
+ // Jan 11 is today in Tokyo, so it stays selectable in past mode (UTC-local would wrongly block it as future).
+ await clickOnDate('11')
+ expect(onChange).toHaveBeenCalledWith(dayjs('2023-01-11T05:00:00.000Z'))
+
+ // Jan 12 is genuinely in the future in Tokyo and remains disabled.
+ await clickOnDate('12')
+ expect(onChange).toHaveBeenLastCalledWith(dayjs('2023-01-11T05:00:00.000Z'))
+ })
+
test('allow only upcoming selection after a limit (one day in the future)', async () => {
const { onChange, clickOnDate, clickOnTime } = renderLemonCalendarSelect(null, {
granularity: 'minute',
diff --git a/frontend/src/lib/lemon-ui/LemonCalendar/LemonCalendarSelect.tsx b/frontend/src/lib/lemon-ui/LemonCalendar/LemonCalendarSelect.tsx
index 22119464ee6d..ed62a94e4900 100644
--- a/frontend/src/lib/lemon-ui/LemonCalendar/LemonCalendarSelect.tsx
+++ b/frontend/src/lib/lemon-ui/LemonCalendar/LemonCalendarSelect.tsx
@@ -3,7 +3,7 @@ import { useRef, useState } from 'react'
import { IconX } from '@posthog/icons'
-import { dayjs } from 'lib/dayjs'
+import { dayjs, dayjsNowInTimezone } from 'lib/dayjs'
import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { LemonButton, LemonButtonProps, LemonButtonWithSideActionProps, SideAction } from 'lib/lemon-ui/LemonButton'
import {
@@ -103,6 +103,8 @@ export interface LemonCalendarSelectProps {
granularity?: LemonCalendarProps['granularity']
selectionPeriod?: 'past' | 'upcoming'
selectionPeriodLimit?: dayjs.Dayjs | null
+ /** Timezone used to determine which past/future dates are selectable (defaults to browser local). */
+ selectionPeriodTimezone?: string
showTimeToggle?: boolean
onToggleTime?: (value: boolean) => void
/** Use 24-hour format instead of 12-hour with AM/PM */
@@ -117,6 +119,7 @@ export function LemonCalendarSelect({
granularity = 'day',
selectionPeriod,
selectionPeriodLimit,
+ selectionPeriodTimezone,
showTimeToggle,
onToggleTime,
use24HourFormat = false,
@@ -124,7 +127,8 @@ export function LemonCalendarSelect({
const calendarRef = useRef<HTMLDivElement | null>(null)
const [selectValue, setSelectValue] = useState<dayjs.Dayjs | null>(value ? value.startOf(granularity) : null)
- const now = dayjs()
+ // Evaluate "now" as the timezone's wall clock (naive local Dayjs) so it's comparable to picked dates.
+ const now = selectionPeriodTimezone ? dayjsNowInTimezone(selectionPeriodTimezone) : dayjs()
const today = now.startOf('day')
const scrollToTime = (date: dayjs.Dayjs, skipAnimation: boolean): void => {
diff --git a/frontend/src/scenes/feature-flags/FeatureFlagSchedule.tsx b/frontend/src/scenes/feature-flags/FeatureFlagSchedule.tsx
index e1d4887b0913..602529b3dc63 100644
--- a/frontend/src/scenes/feature-flags/FeatureFlagSchedule.tsx
+++ b/frontend/src/scenes/feature-flags/FeatureFlagSchedule.tsx
@@ -454,6 +454,8 @@ export default function FeatureFlagSchedule(): JSX.Element {
saveEdit,
} = useActions(featureFlagScheduleEditLogic({ id: featureFlag.id ?? 'new' }))
const { aggregationLabel } = useValues(groupsModel)
+ const { currentTeam } = useValues(teamLogic)
+ const scheduleTimezone = currentTeam?.timezone || 'UTC'
const aggregationGroupTypeIndex = featureFlag.filters.aggregation_group_type_index
const scheduleFilters = { ...schedulePayload.filters, aggregation_group_type_index: aggregationGroupTypeIndex }
@@ -525,6 +527,7 @@ export default function FeatureFlagSchedule(): JSX.Element {
}}
placeholder="Select date"
selectionPeriod="upcoming"
+ selectionPeriodTimezone={scheduleTimezone}
granularity={repeatsValue === 'cron' ? 'day' : 'minute'}
format={repeatsValue === 'cron' ? 'MMMM D, YYYY' : undefined}
clearable
@@ -670,6 +673,7 @@ export default function FeatureFlagSchedule(): JSX.Element {
onChange={(value) => setEndDate(value)}
placeholder="Never"
selectionPeriod="upcoming"
+ selectionPeriodTimezone={scheduleTimezone}
granularity="day"
clearable
/>
@@ -1038,6 +1042,7 @@ export default function FeatureFlagSchedule(): JSX.Element {
}}
placeholder="Select date"
selectionPeriod="upcoming"
+ selectionPeriodTimezone={scheduleTimezone}
granularity={editRepeatsValue === 'cron' ? 'day' : 'minute'}
format={editRepeatsValue === 'cron' ? 'MMMM D, YYYY' : undefined}
clearable
@@ -1089,6 +1094,7 @@ export default function FeatureFlagSchedule(): JSX.Element {
onChange={(value) => setEditEndDate(value)}
placeholder="Never"
selectionPeriod="upcoming"
+ selectionPeriodTimezone={scheduleTimezone}
granularity="day"
clearable
/>