タイムゾーンを変換する方法
日時をあるタイムゾーンから別のタイムゾーンへ、UTC オフセットと夏時間を考慮して変換します。
问题
あるタイムゾーンの時刻を、別のタイムゾーンの相当する時刻にする必要があります。
前提条件
- UTC とタイムゾーンオフセットの理解
- 夏時間(DST)に関する知識
ステップ
-
1. Parse the source time with its timezone
Identify the source timezone (e.g., America/New_York, Asia/Tokyo, UTC). Parse the date and time in that timezone. Do not assume the system's local timezone matches the source.
入力2023-10-14 10:00 America/New_York输出parsed with timezone -
2. Convert to UTC first
Convert the source time to UTC as an intermediate step. This avoids directly computing offsets between two timezones, which is error-prone due to DST rules that differ by timezone and date. UTC is the universal pivot.
入力10:00 EDT → UTC输出14:00 UTC -
3. Convert from UTC to the target timezone
Convert the UTC time to the target timezone, applying the target's UTC offset and DST rules for that specific date. Use the IANA timezone database (via a library) for accurate, up-to-date offsets and DST transitions.
入力14:00 UTC → Asia/Tokyo输出23:00 JST
期待される結果
夏時間とオフセットの違いを考慮した、目的のタイムゾーンでの正しい現地時刻。
よくあるエラー
- Using fixed offsets instead of timezone names
- Fixed offsets (like UTC-5) do not account for DST. Always use IANA timezone names (America/New_York) with a timezone-aware library.
- System timezone assumed as source
- Explicitly specify the source timezone. The system's local timezone may differ, especially on servers.
トラブルシューティング指引
- If the result is off by one hour, check for a DST transition on that date.
- Use the IANA timezone database, which is updated several times per year for rule changes.
セキュリティ上の制限
- Timezone conversion is a pure calculation with no security implications.
関連ツール
タイムゾーンコンバーター
Convert a time or Unix timestamp across multiple time zones (UTC, New York, London, Tokyo, Shanghai, Los Angeles, Paris)。無料、プライベート、100%クライアントサイド。 ツール
タイムスタンプコンバーター
Convert between Unix timestamps and human-readable dates with UTC and local time。無料、プライベート、100%クライアントサイド。 ツール
時間差計算ツール
Calculate difference between two dates。無料、プライベート、100%クライアントサイド。 ツール