跳到主要内容
uencoder

如何Convert 之间 Timezones

转换 a date and time from one timezone to another, accounting for UTC offsets and DST.

问题

你有 时间在 one timezone 和 need equivalent 时间在 另一个 timezone.

前置条件

  • Understanding 的 UTC 和 timezone offsets
  • Knowledge 的 DST

步骤

  1. 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. 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. 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

预期结果

正确本地时间在 目标 timezone, accounting 为 DST 和 offset differences.

常见错误

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.

相关工具

参考链接