Example model for working with Dates, Timezones and Daylight Savings Time

Created on 29 March 2023, over 1 year ago
Updated 3 August 2023, over 1 year ago

Motivation

Custom Date fields store their values as UTC in the database, without timezone or DST (Daylight Savings) calculations applied. When working with Date fields, there are a few tricks to get the expected results. Attached to this issue is an example model showing steps for calculating timezone offsets and applying formatting. When working with Date Ranges, this example could be extended to loop through the formatting steps for Start and End times.


For this model to work, I added a custom date field (unpublish_date) to the Article content type. The flow will trigger when updating existing Article content. There may be an easier way to achieve formatted dates, this is just how I was able to solve for my needs.

🌱 Plan
Status

Fixed

Version

1.1

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States sgroundwater

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sgroundwater
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    This is very nice @sgroundwater, thanks for sharing this.

    With all that gained knowledge, I wonder if you make suggestions on how to enhance the Tamper: Unix timestamp to Date such that it would accept a timezone field as an additional config argument. If provided, it should then assume that the given timestamp is UTC and that the date should be re-calculated for the given timezone. Just came across that though as e.g. some areas in India have timezones like +4:30 where the calculation would have to be extended to cover that too ;-)

  • πŸ‡ΊπŸ‡ΈUnited States sgroundwater

    @jurgenhass - thanks for the improvement suggestions. (My solution is just a "hack" and is far from optimal or user friendly.)
    It looks like there may already be be some work in this direction of your suggestion. I'm reviewing this patch as an option.

    https://www.drupal.org/project/tamper/issues/3268276 ✨ Time offset tamper Needs work

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    Your solution is certainly more than a hack, it is really solving the problem and once that stage is being reached, it's often time to review and continue improvement.

    The link to that issue in the tamper module is a good match. It looks like they are approaching it with mathematical steps there too, but it could be done with date and time tools more efficiently, I guess. Will post a comment over there and link back to here, so that they get an alternative idea.

  • πŸ‡ΊπŸ‡ΈUnited States sgroundwater

    RE " but it could be done with date and time tools more efficiently " Yes, I agree.

    I just did a quick review test of the patch linked above. While not perfect, it does work. The problem with this patch is that it does not account for DST changes. The date calculation is hard-coded with for a specific number of hours, it does not change based on the time of the year. This patch was really designed for use with Feeds, where the DST offset might not be a showstopper. I am going to take some time to see if I can build on this patch to suite our needs.


  • πŸ‡ΊπŸ‡ΈUnited States sgroundwater

    So a few updates to report. Over in the Tamper module, I just posted an updated patch for a TimeZone Tamper (TimeOffset).
    With this patch, a new tamper "TAMPER : TIME_OFFSET" is available. This patch allows you to set a UTC time into any timezone you choose. So you can do things like show a date in two different zones. The patch is failing, probably because of a lack of tests. I'll push on to clean things up.

    One additional tip, when working dates, there are three places to be mindful of. System Regional Timezone, a field display's timezone, or a user's local timezone. This took me a bit of time confirming how these settings affect things. (ECA ig grabbing the UTC value from the database.) Dates in the database are stored as UTC without a timezone. For my sites, I'm setting the Regional Timezone as "New York", there is no timezone on my date field, and my local timezone is set to "New York".

    https://www.drupal.org/project/tamper/issues/3268276 ✨ Time offset tamper Needs work

  • Status changed to Postponed: needs info over 1 year ago
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    @sgroundwater has there been any update on this?

  • Status changed to Fixed over 1 year ago
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024