Prompt
How do I handle time zones in a calendar API?
Latest observation
Handling time zones in a calendar API usually comes down to one rule: store and exchange timestamps in a canonical form, but always preserve the user’s intended local time and time zone.
Core principles
-
Use UTC internally
- Store event instants in UTC for reliable comparisons, sorting, and conflict checks.
- Example:
2026-11-23T15:00:00Z
-
Keep the original time zone
- If an event is “9:00 AM in New York,” store:
- the local wall time:
09:00 - the time zone:
America/New_York
- the local wall time:
- This matters for DST and future recurrences.
- If an event is “9:00 AM in New York,” store:
-
Never rely on a numeric offset alone
-05:00is not enough because offsets change with daylight saving time.- Prefer IANA time zone IDs like
America/New_York,Europe/Berlin.
-
Be clear about whether a datetime is “floating” or anchored
- Anchored datetime: has a time zone and represents a real instant.
- Floating datetime: no time zone, interpreted in the viewer’s or organizer’s local zone.
- Calendars often use floating times for things like “every day at 8 AM local time.”
Recommended data model
For an event, store something like:
start_local:2026-11-23T09:00:00time_zone:America/New_Yorkstart_utc:2026-11-23T14:00:00Zend_local:2026-11-23T10:00:00end_utc:2026-11-23T15:00:00Z
You can compute UTC from local + zone, or store both if that fits your system. If you store both, ensure they stay in sync.
API design tips
1. Accept and return explicit time zone info
Example request:
{
"title": "Team meeting",
"start": "2026-11-23T09:00:00",
"end": "2026-11-23T10:00:00",
"timeZone": "America/New_York"
}
Or if you use full ISO 8601 with offset:
{
"start": "2026-11-23T09:00:00-05:00",
"end": "2026-11-23T10:00:00-05:00"
}
Best practice: include the IANA zone too when the event should follow DST rules over time.
2. Choose one canonical representation for storage
Common options:
- Store UTC timestamps + time zone ID
- Store local datetime + time zone ID and derive UTC as needed
For calendar apps, the first is often simpler for querying, but the second is often necessary for recurring events. Many systems store both.
3. Handle recurrence carefully
If an event recurs “every Monday at 9 AM America/New_York,” DST changes mean the UTC time changes across the year.
- Recurrence should usually be defined in local time with a zone.
- Expand occurrences using the time zone rules for each date.
4. Support all-day events properly
All-day events are often date-only, not datetime:
{
"startDate": "2026-11-23",
"endDate": "2026-11-24",
"allDay": true
}
Treat them as calendar dates, not times, to avoid timezone drift.
5. Be explicit in API responses
Include:
- the original local datetime
- the time zone
- the UTC instant
- maybe the displayed offset at that date
Example:
{
"id": "evt_123",
"title": "Team meeting",
"start": "2026-11-23T09:00:00-05:00",
"timeZone": "America/New_York",
"startUtc": "2026-11-23T14:00:00Z"
}
Common pitfalls
- Using server local time instead of the event’s intended zone
- Storing only offsets and breaking DST behavior
- Converting recurring events to UTC too early
- Mixing all-day events with timed events
- Assuming every user is in one time zone
- Ignoring ambiguous or invalid local times
- Example: during DST spring forward,
2026-03-08T02:30may not exist in some zones. - During fall back,
01:30may occur twice.
- Example: during DST spring forward,
Practical rule of thumb
- For one-off events: store UTC + IANA zone.
- For recurring events: store local time + IANA zone, generate each occurrence carefully.
- For all-day events: store date-only values.
If you want, I can also show:
- a sample database schema,
- how to handle DST edge cases,
- or examples in JavaScript, Python, or Java.