Home > OS >  How to Export current formatted time from several time zones to text file
How to Export current formatted time from several time zones to text file

Time:10-12

I have little Python programming experience, but need to export the current time in a certain 12-hour format from several different time zones to a single text file. I have found several solutions, but need to integrate all three components into one solution.

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())

The last sequence doesn't seem to work in Python 3.7, but if possible would like it to work using (unfortunately) a Python 2.7 installation.

Ultimately, I'd like these results:

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

Much thanks for any assistance.

CodePudding user response:

There are a couple of problems with your code:

  1. You are calling datetime.datetime.now four times which will give you the date-times at the moments when those statements will be executed i.e. you will get date-times at four different moments. You need to get the date-time at the current moment only once and then convert the same to the corresponding date-time in the required timezones.
  2. I recommend you use with open(...) as variable block which enables your code to close the file automatically as soon as exiting from the block.

Working code tested using 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')

Data written in current_time.txt from a sample run:

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

Working code tested using 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')
  • Related