Timezone Management Guide for Global Services
Managing timezones correctly is crucial for global applications. This guide covers timezone concepts, implementation strategies, and best practices for handling time across different regions.
1. Timezone Fundamentals
Understanding Timezones
Key Concepts
const timezoneBasics = {
utc: {
description: 'Coordinated Universal Time - Global time standard',
usage: 'Server storage, API communications',
abbreviation: 'UTC or GMT'
},
localTime: {
description: 'Time in user\'s specific timezone',
usage: 'User interface display',
considerations: 'Daylight saving time, timezone changes'
},
offset: {
description: 'Hours difference from UTC',
example: 'UTC+9 (Japan), UTC-5 (New York)',
variability: 'Changes with daylight saving time'
}
};
Common Timezone Operations
JavaScript Implementation
class TimezoneManager {
constructor() {
this.commonTimezones = {
'America/New_York': 'Eastern Time',
'America/Los_Angeles': 'Pacific Time',
'Europe/London': 'British Time',
'Europe/Paris': 'Central European Time',
'Asia/Tokyo': 'Japan Standard Time',
'Asia/Shanghai': 'China Standard Time',
'Australia/Sydney': 'Australian Eastern Time'
};
}
getCurrentTimezone() {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
}
convertToTimezone(date, timezone) {
return new Date(date.toLocaleString('en-US', { timeZone: timezone }));
}
formatForTimezone(date, timezone, options = {}) {
const defaultOptions = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
timeZoneName: 'short'
};
return date.toLocaleString('en-US', {
...defaultOptions,
...options,
timeZone: timezone
});
}
getTimezoneOffset(timezone, date = new Date()) {
const utc = new Date(date.toLocaleString('en-US', { timeZone: 'UTC' }));
const local = new Date(date.toLocaleString('en-US', { timeZone: timezone }));
return (local.getTime() - utc.getTime()) / (1000 * 60 * 60);
}
}
2. Database Storage Strategies
UTC Storage Best Practices
Storage Recommendations
-- Always store in UTC
CREATE TABLE events (
id SERIAL PRIMARY KEY,
event_name VARCHAR(255),
event_time TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
user_timezone VARCHAR(50),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Query for user's timezone
SELECT
event_name,
event_time AT TIME ZONE 'UTC' AT TIME ZONE 'America/New_York' as local_time
FROM events;
API Design Patterns
Timezone-Aware API
class TimezoneAPI {
scheduleEvent(eventData) {
// Accept local time with timezone
const { dateTime, timezone, ...otherData } = eventData;
// Convert to UTC for storage
const utcDateTime = this.convertToUTC(dateTime, timezone);
return {
...otherData,
scheduledTime: utcDateTime,
userTimezone: timezone
};
}
getUserEvents(userId, userTimezone) {
const events = this.getEventsFromDB(userId);
return events.map(event => ({
...event,
localTime: this.convertToUserTimezone(
event.scheduledTime,
userTimezone
),
displayTime: this.formatForDisplay(
event.scheduledTime,
userTimezone
)
}));
}
convertToUTC(dateTime, fromTimezone) {
const date = new Date(dateTime);
const utcTime = new Date(date.toLocaleString('en-US', {
timeZone: 'UTC'
}));
return utcTime.toISOString();
}
}