Home > front end >  Python time zone conversion from UTC to EST
Python time zone conversion from UTC to EST

Time:09-17

I have the below list in python.

[['File_1','2021-09-09 07:05:10'],['File_2','2021-09-08 08:05:11']]

The above timestamp is a string in UTC timezone. I would like to convert this to EST timezone.

I tried using pytz package using

datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S').astimezone(pytz.timezone('US/Eastern')) 

but it gives me result like

['File_1',datetime.datetime(2021,09,03,4,20,5)].

The format of date time is not as expected.

CodePudding user response:

you need to set UTC first, then convert to the desired tz:

from datetime import datetime, timezone
from zoneinfo import ZoneInfo # Python 3.9  standard lib

l = [['File_1','2021-09-09 07:05:10'],['File_2','2021-09-08 08:05:11']]

out = [[i[0], datetime.fromisoformat(i[1]) # Python 3.7 
                .replace(tzinfo=timezone.utc)
                .astimezone(ZoneInfo("America/New_York"))] for i in l]
       
print(out)
# [['File_1', datetime.datetime(2021, 9, 9, 3, 5, 10, tzinfo=zoneinfo.ZoneInfo(key='America/New_York'))], ['File_2', datetime.datetime(2021, 9, 8, 4, 5, 11, tzinfo=zoneinfo.ZoneInfo(key='America/New_York'))]]

# with pytz:
import pytz

outp = [[i[0], datetime.fromisoformat(i[1]) # Python 3.7 
                .replace(tzinfo=timezone.utc)
                .astimezone(pytz.timezone("America/New_York"))] for i in l]
print(outp)
# [['File_1', datetime.datetime(2021, 9, 9, 3, 5, 10, tzinfo=<DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>)], ['File_2', datetime.datetime(2021, 9, 8, 4, 5, 11, tzinfo=<DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>)]]

If you want a string instead of datetime object, use strftime or even simpler: .isoformat().

CodePudding user response:

You can use the below snippet to get your output in the same format as your input

from datetime import datetime
import pytz
from pytz import timezone

timestamp = '2021-09-09 07:05:10'
datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S').astimezone(pytz.timezone('US/Eastern')).strftime("%Y-%m-%d %H:%M:%S")

# Output
'2021-09-09 03:05:10'

If you wish to display the timezone in the format, you can use

datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S').astimezone(pytz.timezone('US/Eastern')).strftime("%Y-%m-%d %H:%M:%S %Z%z")

# Output
'2021-09-09 03:05:10 EDT-0400'

CodePudding user response:

Here's one way to convert it to EST

1st. Declare the list, and identify the date you want to convert as string

list = [['File_1','2021-09-09 07:05:10'],['File_2','2021-09-08 08:05:11']]
date_to_convert = list[1][1]

2nd. Import the needed libraries

import pytz
from datetime import datetime,timezone

3rd. Convert the string to date time

d = datetime.fromisoformat(date_to_convert)

4th. Declare datetime timezone as utc.

d = date_to_string.replace(tzinfo=timezone.utc)
print(d.isoformat())
# the output for the above: '2021-09-08T08:05:11 00:00'

5th. Set the format and convert to EST

fmt = '%Y-%m-%d %H:%M:%S'
est_date = d.astimezone(pytz.timezone('US/Eastern')).strftime(fmt)
print(est_date)
# the output for the above: '2021-09-08 04:05:11'

CodePudding user response:

USE THIS WAY REMBER UPERCASE AND LOWERCASE ALSO MATTER

import time

timer = time.strftime("%Y-%m-%d -- %H:%M:%S %p")
print(timer)
  • Related