Skip to main content
 首页 » 编程设计

Python:更正时间戳中的本地时间

2025年05月04日8cloudgamer

使用 https://gist.github.com/jordan-brough/4007432我有一个时间戳可以在 sublime 中工作,但时间差了 7 个小时。我怎样才能解决这个问题?我需要设置时区吗?

import sublime, sublime_plugin 
from datetime import datetime 
 
class TimestampCommand(sublime_plugin.TextCommand): 
 def run(self, edit): 
   stamp = datetime.utcnow().strftime("%m/%d/%y %H:%M %p - ") 
   for r in self.view.sel(): 
    if r.empty(): 
      self.view.insert (edit, r.a, stamp) 
    else: 
      self.view.replace(edit, r,   stamp) 

请您参考如下方法:

尝试将 datetime.utcnow() 替换为 datetime.now()

UTC = 协调世界时,时间偏移量为 0