Limits of Magento's automatic daylight‑saving adjustments for custom datetime attributes
0 reputation · 30 Mar 2020, 22:59 UTC
0 reputation · 30 Mar 2020, 22:59 UTC
Goal: Ensure that custom datetime attributes in Magento automatically reflect daylight‑saving time changes according to the store’s configured timezone, so that storefront displays remain accurate after DST transitions without manual intervention.
Constraint: Magento persists all timestamps in UTC and converts them for display using the store’s timezone setting, but this conversion applies only to core datetime fields. Custom attributes that are not explicitly marked as timezone‑aware are stored as raw values and are not adjusted when DST shifts, potentially causing an off‑by‑one hour error in the displayed time.
Uncertainty: It is unclear whether Magento provides a built‑in attribute type, backend flag, or configuration option that forces timezone‑aware conversion for custom datetime attributes, or if developers must implement their own conversion logic (e.g., using Magento’s DateTimeZone helpers) to achieve automatic DST adjustment.
Questions:
35275 reputation · 31 Mar 2020, 04:22 UTC
Magento does not provide a native attribute flag or a configuration switch (such as an "is_timezone_aware" checkbox) that automatically enables DST-aware conversion for custom EAV datetime attributes. While core datetime fields are processed through the framework's timezone conversion layers, custom attributes are treated as raw data. If you store a value and render it without explicit conversion, it will not adjust for Daylight Saving Time (DST) transitions, leading to the common "off-by-one-hour" error.
Magento persists timestamps in UTC. The discrepancy occurs because the automatic conversion from UTC to the store's configured timezone is tied to specific rendering paths (like core UI components) rather than the attribute definition itself. If a custom attribute is rendered via a raw template or a custom API response, it bypasses the TimezoneInterface, displaying the UTC value or a fixed offset that does not account for the IANA timezone rules governing DST.
To ensure custom attributes respect DST without modifying core code, you must implement the conversion logic using Magento's built-in timezone services. This ensures that PHP's internal timezone database handles the transition rules automatically.
\Magento\Framework\Stdlib\DateTime\TimezoneInterface::convertConfigTimeToUtc().TimezoneInterface::date() or formatDate() methods to convert the UTC value back to the store's configured timezone during rendering.To verify that your implementation is DST-aware, perform the following tests:
Note: There are no official patches or first-party extensions that add this behavior globally to all custom attributes; the supported architectural pattern is the use of the TimezoneInterface.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.