Realm Date Storage vs. Local UI Timezone Conversion: Need for Explicit Timezone Persistence?
0 reputation · 17 Feb 2023, 10:08 UTC
0 reputation · 17 Feb 2023, 10:08 UTC
The goal is to decide whether Realm should persist an explicit timezone identifier together with the UTC instant so that applications can reconstruct the original local time without performing manual conversion on read.
Constraints include maintaining backward compatibility with existing Realms, keeping storage overhead minimal, and ensuring that synchronized Realms continue to converge on the same instant across devices with different zone settings.
Uncertainty remains about the impact on query indexes, migration complexity, and whether the identifier should be optional or required for new schemas.
What are the migration implications for adding a timezone field to existing synchronized Realms? How would query performance be affected if timezone metadata is stored alongside the UTC timestamp? Should the timezone identifier be optional, and how should Realm treat legacy values that lack this field?
29275 reputation · 17 Feb 2023, 20:18 UTC
Realm’s Date type stores an absolute point in time as UTC epoch milliseconds. No timezone is persisted. For displaying the same instant in the device’s current timezone, no explicit timezone persistence is needed. Persist an explicit timezone identifier only if you must reconstruct the original local wall-clock time the user entered, e.g., a meeting that should always show as 9:00 in Europe regardless of where the device is later used.
Backward compatibility and minimal storage overhead favor keeping Date alone and doing conversion in the UI. Adding a timezone field adds per-object storage and migration work, and is only justified when original local time must be preserved across devices and locale changes.
One missing diagnostic detail that changes the recommendation: do your user stories require preserving the original local wall-clock time for future-dated events across devices, or is displaying the instant adjusted to the current device timezone sufficient?
Use comments to ask for clarification. Post a solution as an answer.
29,275 reputation · 17 Feb 2023, 16:58 UTC
The answer correctly notes that synchronized Realms converge on the same UTC instant. However, if some clients persist an explicit timezone identifier while others rely solely on the device's current zone, the displayed local time for the same event can diverge after a device travels or changes its system zone. This is not a data-corruption issue—it is a UX inconsistency that appears when the app renders the instant without a stored original zone.
If you adopt an optional timeZoneIdentifier field, enforce a consistent write policy: either all clients write the zone at creation time, or none do. A mixed fleet will show the meeting as 09:00 on a client that stored "Europe/Berlin" but as 10:00 on a client that fell back to the current device zone after a DST transition.