Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add timezone support on date fields #10896

Merged
merged 31 commits into from
Feb 10, 2025

Conversation

paulpopus
Copy link
Contributor

@paulpopus paulpopus commented Jan 30, 2025

Adds support for timezone selection on date fields.

Summary

New admin.timezones config:

{
  // ...
  admin: {
    // ...
    timezones: {
      supportedTimezones: ({ defaultTimezones }) => [
        ...defaultTimezones,
        { label: '(GMT-6) Monterrey, Nuevo Leon', value: 'America/Monterrey' },
      ],
      defaultTimezone: 'America/Monterrey',
    },
  }
}

New timezone property on date fields:

{
  type: 'date',
  name: 'date',
  timezone: true,
}

Configuration

All date fields now accept timezone: true to enable this feature, which will inject a new field into the configuration using the date field's name to construct the name for the timezone column. So publishingDate will have publishingDate_tz as an accompanying column. This new field is inserted during config sanitisation.

Dates continue to be stored in UTC, this will help maintain dates without needing a migration and it makes it easier for data to be manipulated as needed. Mongodb also has a restriction around storing dates only as UTC.

All timezones are stored by their IANA names so it's compatible with browser APIs. There is a newly generated type for SupportedTimezones which is reused across fields.

We handle timezone calculations via a new package @date-fns/tz which we will be using in the future for handling timezone aware scheduled publishing/unpublishing and more.

UI

Dark mode
image

Light mode
image

jacobsfletch
jacobsfletch previously approved these changes Feb 4, 2025
@paulpopus paulpopus disabled auto-merge February 4, 2025 18:30
@DanRibbens
Copy link
Contributor

@jacobsfletch did you pull the branch down and test this when you reviewed? If not I plan to do this later today before we merge.

@JarrodMFlesch
Copy link
Contributor

JarrodMFlesch commented Feb 7, 2025

I think we may want to use _tz instead of _timezone as the field suffix here. Shorter names are generally better for PG, and we are adding this one automatically. TZ is also fairly common when talking about dates.

Copy link
Member

@jacobsfletch jacobsfletch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few last changes to make here:

  • The supportedTimezones array exported from Payload should be renamed defaultTimezones
  • This should be typed with a new Timezone[] type
  • The supportedTimezones property should accept a function that threads defaultTimezones as an argument
  • Need to type supportedTimezones to ensure values are strictly an IANA timezone

packages/payload/src/config/types.ts Outdated Show resolved Hide resolved
@paulpopus
Copy link
Contributor Author

Added a few more tests to ensure UTC values are set as expected between the summer and winter times (daylight savings). Also tests for the below

Timezone selector is now clearable.

If date field is required and timezone is not provided but enabled it will now throw an error as well.

@jacobsfletch jacobsfletch merged commit 430ebd4 into main Feb 10, 2025
71 checks passed
@jacobsfletch jacobsfletch deleted the feat/timezone-support-on-date-fields branch February 10, 2025 20:02
Copy link
Contributor

🚀 This is included in version v3.23.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants