You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

不同时区转UTC时区转换方法及指定时区转换需求咨询

多指定时区转UTC时间的实现方案

前置说明

部分缩写时区存在歧义(比如CST可指代中国标准时间、美国中部时间、古巴标准时间等,IST可指代印度标准时间、以色列标准时间、爱尔兰标准时间等),以下默认采用行业最常用的偏移定义,如有特殊场景需自行调整偏移值。

对应时区与UTC的偏移规则(标准时,不含夏令时,如需夏令时需额外适配地区规则)

  • AFGHAN:阿富汗标准时间,偏移为UTC+4:30,转UTC=原时间 - 4小时30分钟
  • RUS02:俄罗斯2区时间(叶卡捷琳堡时间),偏移为UTC+5,转UTC=原时间 - 5小时
  • UT+103:即UTC+10:30,代表澳大利亚中部夏令时/巴布亚新几内亚部分地区时间,转UTC=原时间 - 10小时30分钟
  • UTC+1:中欧标准时间/西非标准时间,转UTC=原时间 - 1小时
  • UTC+10:澳州东部标准时间/关岛时间,转UTC=原时间 -10小时
  • UTC+43:即UTC+4:30,同阿富汗标准时间,转UTC=原时间 -4小时30分钟
  • WAT:西非时间,偏移为UTC+1,转UTC=原时间 -1小时
  • PST:太平洋标准时间(北美),偏移为UTC-8,转UTC=原时间 +8小时
  • IST:默认取印度标准时间,偏移为UTC+5:30,转UTC=原时间 -5小时30分钟
  • CST:默认取中国标准时间(北京时间),偏移为UTC+8,转UTC=原时间 -8小时;若为美国中部标准时间则偏移为UTC-6,转UTC=原时间+6小时,需根据场景确认

技术实现方案

核心逻辑

  1. 先将输入的时间字符串解析为对应时区的datetime对象
  2. 将该对象转换为UTC时区的datetime对象
  3. 按需求格式化输出即可

Python 实现示例(Python3.9+推荐用标准库zoneinfo,低版本可替换为pytz第三方库)

from zoneinfo import ZoneInfo
from datetime import datetime

# 定义时区映射,可按实际业务场景调整歧义时区取值
tz_mapping = {
    "AFGHAN": ZoneInfo("Asia/Kabul"),
    "RUS02": ZoneInfo("Asia/Yekaterinburg"),
    "UT+103": ZoneInfo("Pacific/Guadalcanal"), # 对应UTC+10:30
    "UTC+1": ZoneInfo("Etc/GMT-1"), # Etc/GMT±X的规则是X为偏移值,+1对应GMT-1
    "UTC+10": ZoneInfo("Etc/GMT-10"),
    "UTC+43": ZoneInfo("Asia/Kabul"),
    "WAT": ZoneInfo("Africa/Lagos"),
    "PST": ZoneInfo("America/Los_Angeles"),
    "IST": ZoneInfo("Asia/Kolkata"),
    "CST": ZoneInfo("Asia/Shanghai") # 若用美国中部时间改为"America/Chicago"
}

def convert_to_utc(dt_str: str, source_tz_key: str, dt_format: str = "%Y-%m-%d %H:%M:%S") -> str:
    # 解析原时间
    source_dt = datetime.strptime(dt_str, dt_format).replace(tzinfo=tz_mapping[source_tz_key])
    # 转UTC
    utc_dt = source_dt.astimezone(ZoneInfo("UTC"))
    return utc_dt.strftime(dt_format)

# 示例调用
print(convert_to_utc("2024-05-20 12:00:00", "CST"))

Java 实现示例(Java8+用内置java.time包,低版本可替换为Joda-Time第三方库)

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class TzConverter {
    private static final Map<String, ZoneId> TZ_MAPPING = new HashMap<>();
    static {
        TZ_MAPPING.put("AFGHAN", ZoneId.of("Asia/Kabul"));
        TZ_MAPPING.put("RUS02", ZoneId.of("Asia/Yekaterinburg"));
        TZ_MAPPING.put("UT+103", ZoneId.of("Pacific/Guadalcanal"));
        TZ_MAPPING.put("UTC+1", ZoneId.of("Etc/GMT-1"));
        TZ_MAPPING.put("UTC+10", ZoneId.of("Etc/GMT-10"));
        TZ_MAPPING.put("UTC+43", ZoneId.of("Asia/Kabul"));
        TZ_MAPPING.put("WAT", ZoneId.of("Africa/Lagos"));
        TZ_MAPPING.put("PST", ZoneId.of("America/Los_Angeles"));
        TZ_MAPPING.put("IST", ZoneId.of("Asia/Kolkata"));
        TZ_MAPPING.put("CST", ZoneId.of("Asia/Shanghai"));
    }

    public static String convertToUtc(String dtStr, String sourceTzKey, String formatPattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatPattern);
        ZonedDateTime sourceDt = ZonedDateTime.parse(dtStr, formatter.withZone(TZ_MAPPING.get(sourceTzKey)));
        ZonedDateTime utcDt = sourceDt.withZoneSameInstant(ZoneId.of("UTC"));
        return utcDt.format(formatter);
    }

    public static void main(String[] args) {
        System.out.println(convertToUtc("2024-05-20 12:00:00", "CST", "yyyy-MM-dd HH:mm:ss"));
    }
}

注意事项

  • 不要手动做时间加减,直接用编程语言内置的时区处理库,可自动处理夏令时、历史时区变更等特殊场景,避免手动计算出错
  • 歧义时区(IST、CST等)一定要先和业务方确认实际对应的地区,再选择正确的ZoneId
  • 时区数据库尽量保持更新,避免因地区时区规则变更导致转换错误

内容的提问来源于stack exchange,提问作者srikanth boda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 07:57:01