Skip to content

[looking for feedback] Reworking temporal DataTypes #14295

Description

@ephys

DataTypes.DATE is a bit of a mess:

  • Some dialects include a timezone, others do not.
  • It doesn't provide a way to choose whether the type should include a time zone.
  • When it does store the time zone, the time zone is specified as a UTC Offset, instead of a time zone name.
  • Finally, DATE means DATEONLY in SQL, not DATETIME.

Moreover, DataTypes.DATE was designed for the legacy Date class, and includes workarounds for timeZone support.

I propose that once Temporal reaches stage 4, we deprecate that DataType and replace it with the following proposal:

ℹ️ This proposal has been last updated on 22-Dec-2022. See edit history of this post to view older versions.


Sequelize DataType JS Types pg mysql/mariadb mssql DB2 Oracle
DataTypes.DATETIME.ZONED ZonedDateTime1 ❌ ❌ ❌ ❌ ❌
DataTypes.DATETIME.OFFSET Instant, string timestamp tz ❌ datetimeoffset ❌ timestamp with time zone
DataTypes.DATETIME.PLAIN PlainDateTime, string timestamp datetime datetime2 timestamp timestamp
DataTypes.TIME PlainTime, string time time time time ❌
DataTypes.DATEONLY PlainDate, string date date date date date

Temporal.{PlainMonthDay, PlainYearMonth, TimeZone, Calendar} don't have an associated JS nor DB DataType.

As indicated in #14213, we're not currently interested in supporting postgres's time tz type, as they themself recommend against using it.

Footnotes

  1. For now, I trying to use ZonedDateTime with one of these types should emit an error explaining that no DB type matching ZonedDateTime is available yet, and that the user should
    split it into two attributes: a plain datetime & a timezone, or store it as a string. ↩

Activity

  1. added
    RFC
    on Mar 30, 2022
  2. self-assigned this
    on Mar 30, 2022
  3. mytharcher commented on Jun 17, 2022

    @mytharcher

    My proposal for the naming:

    • DateTypes.DATETIME: Always means "Date with timezone". Because it will work fine in most cases by default.
    • DataTypes.DATETIME.PLAIN: Always means "Date without timezone". It could only accepts Temporal.PlainDateTime.

    Many developers arguing in other issues for demanding of date/time without timezone, because they didn't find out the best practice of usage of time.

    In my opinion, when we want to use a date/time field in database, it should always be with timezone. And physically, time is always with timezone. The cases when people are not mentioning timezone, mostly they only consider the local time. But use time with timezone in these cases are also acceptable and no bad effects.

    The base reason why should we use date/time type of fields in database, is Just because we need them to be calculated with other values (comparing, manipulating, etc.). If people only need time for literally expressing, the timezone will not be strictly necessary. In this case, just use plain time or even string will be ok. But if we use time for calculating, we need the exact value (with timezone).

    Another thing is, always use UTC formatted in any transferring is the best practice. Also for storing to database. So in PG it works great by default. It will be the clients' stuff to transform stored time to local timezone in view. And by default Date in browser parsing an UTC time string works right.

    Think about it, if we use time with timezone in a project, whether if it is a local one or global, it always work fine. But if use time without timezone, it only works for local projects.

  4. crazyscience commented on Aug 2, 2022

    @crazyscience

    But use time with timezone in these cases are also acceptable and no bad effects.

    Super not true. When you have a table with 30 billion rows and 4 datetime columns in each row, the addition of useless data (in this case, timezone info) definitely has an impact. Maybe not noticeable when someone's building yet another todo app. But ORMs should be built for performance application and production loads, not hobbyists building a blog.

    Additionally, timezone boundaries move constantly. Storing the timezone data in the database as if it's going to be accurate for any length of time is seriously misguided. Production applications should default to avoid timezones and use them when they are needed specifically.

    https://www.youtube.com/watch?v=-5wpm-gesOY

  5. crazyscience commented on Aug 2, 2022

    @crazyscience

    My proposal for the naming:

    • DateTypes.DATETIME: Always means "Date without timezone". Because it will work fine in most cases by default.
    • DataTypes.DATETIME.IM_A_MASOCHIST: Always means "Date with timezone". 😄
  6. mytharcher commented on Aug 3, 2022

    @mytharcher

    @crazyscience

    You may have a check on those DBs docs, whether the DATE type with timezone or without, it will not affect size of storing.

    You can always use UTC time to store into DB, the timezone will be calculated or not on Sequelize or client side. Unless you just need local time, and no need to convert, you just store local time. The size of each has no difference.

    From another aspect, performance optimization should be the App developers' responsibility. As for framework/library developer, should just provide the options. You can also use the DataTypes.DATETIME.PLAIN for no conversion needed fields. It all by your own.

  7. crazyscience commented on Aug 10, 2022

    @crazyscience

    @mytharcher while I concede that the storage does indeed appear to be unaffected, I still would maintain that a framework has a lot of sway in how programming patterns are adopted and therefor still think that the default behavior should reflect best practices.

    Per the postgres link you sent above, directly below the table of storage sizes, they have a remark that says that according to the SQL standards, datetime columns must not include timezone data unless explicitly stated. I still agree with the SQL standards on this one. Timezones in the database are a bit of a foot 🔫.

  8. mytharcher commented on Aug 24, 2022

    @mytharcher

    @crazyscience

    I think we could make clear some things below:

    1. In fact, I don't really care about if the timezone info is stored in DB.
    2. What I insisted is to use UTC/GMT time to store into DB.
    3. The timezone converting should be handled in ORM (if not handled by DB) or client side.

    In these conditions, DateTypes.DATETIME with timezone by default (defined in ORM) would be the easiest way for most user.

  9. ephys commented on Sep 17, 2022

    @ephys
    MemberAuthor

    The reason I originally went for DataTypes.DATETIME (unzoned) + DataTypes.DATETIME.ZONED is that we currently have DataTypes.TIME (unzoned) & have a PR for DataTypes.TIME.ZONED.

    I think mixing up which one is zoned by default and which one isn't would lead to confusion.

    However, it makes sense for DataTypes.TIME to be unzoned by default, because, as the pg docs indicate, the usefulness of DataTypes.TIME.ZONED is questionable. That is not true for DataTypes.DATETIME.ZONED.

    Whether we make zoned or unzoned the default is inevitably going to lead to people using it without thinking about whether they want it to be zoned or not. This is mitigated by the fact that we will only accept ZonedDateTime & PlainDateTime respectively, so they will eventually have to think about which type they really need, but maybe we should go the route of no default here.

    • DataTypes.DATETIME: error, please use DataTypes.DATETIME.ZONED, or DataTypes.DATETIME.PLAIN
    • DataTypes.DATETIME.PLAIN
    • DataTypes.DATETIME.ZONED

    or just

    • DataTypes.ZONEDDATETIME
    • DataTypes.PLAINDATETIME (this uppercase convention makes this somewhat difficult to read)
  10. WikiRik commented on Sep 18, 2022

    @WikiRik
    Member

    Yeah, I don't think DataTypes.ZONEDDATETIME is a good solution. I think it should be fine to have no default and force people to specify DataTypes.DATETIME.ZONED.

  11. ephys commented on Sep 27, 2022

    @ephys
    Author
  12. ephys commented on Dec 22, 2022

    @ephys
    MemberAuthor

    Updated the proposal (in the original post) with the following changes:

    I still have one pending question:

    • Should DataTypes.DATETIME.OFFSET accept Date? While Date is not compatible with DataTypes.DATETIME.PLAIN, it works fine with DataTypes.DATETIME.OFFSET. I think we can support it without any issues
  13. added
    type: feature
    and removed
    RFC
    on Dec 22, 2022
  14. changed the title [-][RFC] Consider deprecating & replacing `DataTypes.DATE`[/-] [+]Reworking temporal DataTypes[/+] on Dec 22, 2022
  15. changed the title [-]Reworking temporal DataTypes[/-] [+][looking for feedback] Reworking temporal DataTypes[/+] on Dec 22, 2022
  16. pinned this issue on Dec 22, 2022
  17. ephys commented on Jun 14, 2023

    @ephys
    MemberAuthor

    As mentioned in #4900, we can also map the INTERVAL data type to Temporal.Duration

  18. unpinned this issue on Mar 19, 2024
  19. added theissue type on Mar 9, 2025
  20. removed
    type: feature
    on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

  • Status
    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions