【问题标题】:How to convert datetime.time from UTC to different timezone?如何将 datetime.time 从 UTC 转换为不同的时区?
【发布时间】:2013-05-12 07:16:37
【问题描述】:

我有一个保存时间的变量,它是 UTC 中的 datetime.time 类型,我希望它转换为其他时区。

我们可以转换 datetime.datetime 实例中的时区,如此 SO 链接 - How do I convert local time to UTC in Python? 所示。我无法弄清楚如何在 datetime.time 实例中转换时区。我不能使用 astimezone 因为 datetime.time 没有这个方法。

例如:

>>> t = d.datetime.now().time()
>>> t
datetime.time(12, 56, 44, 398402)
>>> 

我需要 UTC 格式的“t”。

【问题讨论】:

    标签: python datetime timezone


    【解决方案1】:

    有四种情况:

    1. 输入datetime.timetzinfo 设置(例如OP 提到UTC)
      1. 输出为非天真的时间
      2. 作为原始时间输出(tzinfo 未设置)
    2. 输入datetime.timetzinfo 未设置
      1. 输出为非天真的时间
      2. 作为原始时间输出(tzinfo 未设置)

    正确答案需要使用datetime.datetime.timetz()函数,因为datetime.time不能通过直接调用localize()astimezone()来构建为非朴素时间戳。

    from datetime import datetime, time
    import pytz
    
    def timetz_to_tz(t, tz_out):
        return datetime.combine(datetime.today(), t).astimezone(tz_out).timetz()
    
    def timetz_to_tz_naive(t, tz_out):
        return datetime.combine(datetime.today(), t).astimezone(tz_out).time()
    
    def time_to_tz(t, tz_out):
        return tz_out.localize(datetime.combine(datetime.today(), t)).timetz()
    
    def time_to_tz_naive(t, tz_in, tz_out):
        return tz_in.localize(datetime.combine(datetime.today(), t)).astimezone(tz_out).time()
    

    基于 OP 要求的示例:

    t = time(12, 56, 44, 398402)
    time_to_tz(t, pytz.utc) # assigning tzinfo= directly would not work correctly with other timezones
    
    datetime.time(12, 56, 44, 398402, tzinfo=<UTC>)
    

    如果需要简单的时间戳:

    time_to_tz_naive(t, pytz.utc, pytz.timezone('Europe/Berlin'))
    
    datetime.time(14, 56, 44, 398402)
    

    time() 实例已经设置了tzinfo 的情况比较容易,因为datetime.combine 会从传递的参数中提取tzinfo,所以我们只需转换为tz_out

    【讨论】:

      【解决方案2】:

      我会创建一个临时日期时间对象,转换 tz,然后再次提取时间。

      import datetime
      def time_to_utc(t):
          dt = datetime.datetime.combine(datetime.date.today(), t)
          utc_dt = datetime_to_utc(dt)
          return utc_dt.time()
      
      t = datetime.datetime.now().time()
      utc_t = time_to_utc(t)
      

      其中,datetime_to_utclinked question 中的任何建议。

      【讨论】:

      • 好的,是否无法使用 datetime.time 实例转换时区。谷歌搜索它没有找到任何东西。你的方法很好,但是太费时间了。有没有更好的办法?谢谢。
      【解决方案3】:

      使用pytz 与UTC 时区相互转换的简单方法:

      import datetime, pytz
      
      def time_to_utc(naive, timezone="Europe/Istanbul"):
          local = pytz.timezone(timezone)
          local_dt = local.localize(naive, is_dst=None)
          utc_dt = local_dt.astimezone(pytz.utc)
          return utc_dt
      
      def utc_to_time(naive, timezone="Europe/Istanbul"):
          return naive.replace(tzinfo=pytz.utc).astimezone(pytz.timezone(timezone))
      
      # type(naive) """DateTime"""
      # type(timezone) """String"""
      

      【讨论】:

      • 你可能想解释你的代码,天真应该是原生的,就像你当地的时区一样???
      • 其中naive 是一个没有时区的datetime 对象,timezone 是一个匹配pytz.timezone 名称之一的字符串。 The datetime documentation 简要解释了幼稚与有意识的 datetime 对象。
      猜你喜欢
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2019-10-30
      • 1970-01-01
      • 2016-02-06
      相关资源
      最近更新 更多