如何将当前格式化时间从多个时区导出到文本文件
How to Export current formatted time from several time zones to text file
我没有什么 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
非常感谢您的帮助。
您的代码有几个问题:
- 您正在调用
datetime.datetime.now
四次,这将为您提供执行这些语句时的日期时间,即您将在四个不同的时刻获得日期时间。您只需获取当前时刻的日期时间一次,然后将其转换为所需时区中相应的日期时间。
- 我建议您使用
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')
我没有什么 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
非常感谢您的帮助。
您的代码有几个问题:
- 您正在调用
datetime.datetime.now
四次,这将为您提供执行这些语句时的日期时间,即您将在四个不同的时刻获得日期时间。您只需获取当前时刻的日期时间一次,然后将其转换为所需时区中相应的日期时间。 - 我建议您使用
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')