【问题标题】:How to Export current formatted time from several time zones to text file如何将当前格式化时间从多个时区导出到文本文件
【发布时间】:2021-11-30 01:39:55
【问题描述】:

我没有多少 Python 编程经验,但需要将当前时间以某种 12 小时格式从几个不同的时区导出到单个文本文件中。我找到了几种解决方案,但需要将所有三个组件集成到一个解决方案中。

from datetime import datetime
"{:%A, %B %d %Y %I:%M:%S %p %Z}".format(datetime.now())

import datetime
import pytz
my_date = datetime.datetime.now(pytz.timezone('UTC'))
print(my_date)
my_date = datetime.datetime.now(pytz.timezone('America/New_York'))
print(my_date)
my_date = datetime.datetime.now(pytz.timezone('EST'))
print(my_date)
my_date = datetime.datetime.now(pytz.timezone('Pacific/Port_Moresby'))
print(my_date)

import datetime
open("C:\temp\current_time.txt", "w").write("UTC Time: ")
open("C:\temp\current_time.txt", "a").write(datetime.datetime.now().ctime()+ '\n')
open("C:\temp\current_time.txt", "a").write("EST Time: ")
open("C:\temp\current_time.txt", "a").write(datetime.datetime.now().ctime()+ '\n')
open("C:\temp\current_time.txt", "a").write("New York Time: ")
open("C:\temp\current_time.txt", "a").write(datetime.datetime.now().ctime()+ '\n')
open("C:\temp\current_time.txt", "a").write("Port Moresby Time: ")
open("C:\temp\current_time.txt", "a").write(datetime.datetime.now().ctime())

最后一个序列似乎在 Python 3.7 中不起作用,但如果可能的话,希望它使用(不幸的是)Python 2.7 安装来工作。

最终,我想要这些结果:

UTC Time: Monday, October 11, 2021 09:05:17
EST Time: Monday, October 11, 2021 04:05:17 AM
New York Time: Monday, October 11, 2021 05:05:17 AM
Port Moresby Time: Monday, October 11, 2021 07:05:17 PM

非常感谢您的帮助。

【问题讨论】:

  • 请注意,%Z 不会给您time zone name,而只是一个缩写(由于模棱两可,可能有点没用)。

标签: python python-2.7 datetime format timezone


【解决方案1】:

您的代码有几个问题:

  1. 您正在调用datetime.datetime.now 四次,这将为您提供执行这些语句时的日期时间,即您将获得四个不同时刻的日期时间。您只需获取当前时刻的日期时间一次,然后将其转换为所需时区中相应的日期时间。
  2. 我建议您使用with open(...) as variable 块,它可以让您的代码在退出块后自动关闭文件。

使用 Python 3.9 测试的工作代码:

from datetime import datetime
import pytz

now = datetime.now()

now_utc = now.astimezone(pytz.timezone('UTC'))
now_est = now.astimezone(pytz.timezone('EST'))
now_new_york = now.astimezone(pytz.timezone('America/New_York'))
now_port_moresby = now.astimezone(pytz.timezone('Pacific/Port_Moresby'))

format = "%A, %B %d, %Y %I:%M:%S %p"

with open("current_time.txt", "w") as file:
    file.write(f'UTC Time: {now_utc.strftime(format)}\n')
    file.write(f'EST Time: {now_est.strftime(format)}\n')
    file.write(f'New York Time: {now_new_york.strftime(format)}\n')
    file.write(f'Port Moresby Time: {now_port_moresby.strftime(format)}\n')

样本运行中写入 current_time.txt 的数据:

UTC Time: Monday, October 11, 2021 12:25:50 PM
EST Time: Monday, October 11, 2021 07:25:50 AM
New York Time: Monday, October 11, 2021 08:25:50 AM
Port Moresby Time: Monday, October 11, 2021 10:25:50 PM

使用 Python 2.7 测试的工作代码

from datetime import datetime
import pytz

now_utc = datetime.utcnow()

tz_utc = pytz.timezone('UTC')

now_est = tz_utc.localize(now_utc).astimezone(pytz.timezone('EST'))
now_new_york = tz_utc.localize(now_utc).astimezone(pytz.timezone('America/New_York'))
now_port_moresby = tz_utc.localize(now_utc).astimezone(pytz.timezone('Pacific/Port_Moresby'))

format = "%A, %B %d, %Y %I:%M:%S %p"

with open("current_time.txt", "w") as file:
    file.write('UTC Time: ' + now_utc.strftime(format) + '\n')
    file.write('EST Time: ' + now_est.strftime(format) + '\n')
    file.write('New York Time: '+ now_new_york.strftime(format) + '\n')
    file.write('Port Moresby Time: ' + now_port_moresby.strftime(format) + '\n')

【讨论】:

  • 顺便说一句。 EST is obsolete
  • 在测试你的答案时,'now_utc = now.astimezone(pytz.timezone('UTC'))' 行会产生:运行时错误/回溯(最近一次调用):/ File " ",第 1 行,在 /ValueError: astimezone() 不能应用于天真的日期时间
  • 另外,“美国/纽约”会在“东部标准时间”和“东部夏令时间”之间自动调整吗?非常感谢。
  • @noni - 在你的问题中,你写过,最后一个序列似乎在 Python 3.7 中不起作用。该声明使我对所需版本感到困惑。现在,我也为 Python 2.7 添加了代码。
  • 我很抱歉最初没有澄清我的安装使用的 Python 版本。我在安装时测试了您的代码,一切似乎都有效。我将“current_time.txt”更改为“c:\temp\current_time.txt”并以回车结束命令,但得到一个“Parsing error SyntaxError: invalid syntax (line 2)”,而且没有找到结果文本文件。非常感谢您的专业知识。
猜你喜欢
  • 2011-05-27
  • 2019-05-01
  • 1970-01-01
  • 2014-07-26
  • 2011-12-06
  • 2017-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多