-1

I have nested json file which has time zone which is in UTC format I am capturing that and putting it into a column and then trying to convert that to cst by creating a column for CST but it is not converting can anybody help am posting the code below

def extract_json_data(fpath):
    print("Extracting " + fpath)
    f = open(fpath, 'r')
    json_data = json.loads(f.read())
    data = json_data['data']
    dt = datetime.datetime.strptime(json_data['time'], "%Y-%m-%dT%H:%M:%SZ")
    dt_cst = dt.astimezone(timezone('US/Central'))
    _ = [row.update({'time_UTC': dt.strftime("%Y-%m-%dT%H:%M:%SZ"),
                     'time_CST': dt_cst.strftime("%Y-%m-%dT%H:%M:%S CST")}) for row in data]

image showing that time not being converted

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
user13073332
  • 21
  • 1
  • 7
  • You might get better help with this question if you had spelled `pytz` correctly, and included it in your code sample. – Mark Ransom May 16 '20 at 16:50
  • Your format string `"%Y-%m-%dT%H:%M:%SZ"` parses a literal `Z` at the end, I think you want to parse the timezone (Z; UTC), so use `"%Y-%m-%dT%H:%M:%S%z"` - see my answer below. – FObersteiner May 16 '20 at 18:55

2 Answers2

1

Use a format string to parse the timezone, so that the datetime object you work with is timezone-aware:

from datetime import datetime

# the string actually has timezone information: Z (UTC)
timestring = "2019-01-01T00:00:00Z"

# wrong:
dt = datetime.strptime(timestring, "%Y-%m-%dT%H:%M:%SZ")
# dt is naive:
# datetime.datetime(2019, 1, 1, 0, 0)

# right:
dt = datetime.strptime(timestring, "%Y-%m-%dT%H:%M:%S%z")
# dt now knows it's in UTC:
# datetime.datetime(2019, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)

Now you can change the time of your datetime object to a different timezone:

import pytz
tz = pytz.timezone('US/Central')
dt_cst = dt.astimezone(tz)
# datetime.datetime(2018, 12, 31, 18, 0, tzinfo=<DstTzInfo 'US/Central' CST-1 day, 18:00:00 STD>)

A more convenient solution would be to skip pytz and use dateutil instead:

import dateutil
timestring = "2019-01-01T00:00:00Z"
dt = dateutil.parser.parse(timestring)

# dt
# datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
0

Here's a way to do that:

import datetime
from dateutil import tz

# create a time-zone object representing UTC. 
from_zone = tz.gettz('UTC')

# Create another time zone object, representing the target time zone. 
# note that according to the tz package documentation 
# (https://dateutil.readthedocs.io/en/stable/tz.html#dateutil.tz.gettz), 
# they have Windows-specific time-zone names support. 
to_zone = tz.gettz('America/Chicago')

# This is just a sample dictionary, so I cam extract the 'time' 
# field like you do in your code. It's really not needed here. 
json_data = {'time': "2020-05-16T08:17:42Z"} # an example for a datetime

# Create a datetime object, representing the UTC time. 
utc = datetime.datetime.strptime(json_data['time'], "%Y-%m-%dT%H:%M:%SZ")

# now replace the timezone field of the newly created datetime object, 
# so it would be UTC. 
utc = utc.replace(tzinfo=from_zone)

# Convert time zone from UTC to central
central = utc.astimezone(to_zone)

print(central)

you'll get:

2020-05-16 03:17:42-05:00

Roy2012
  • 11,755
  • 2
  • 22
  • 35
  • Hello Roy is it possible for you to explain your code line by line please as I tried it but did not work I might be missing something I will explain mine if you can just point out where I am going wrong am reading my JSON file and then getting time out of it and putting into a column and then trying to convert that colum time into cst and storing it in another coloum – user13073332 May 16 '20 at 14:15
  • @user13073332. Added some notes. You say it's not working - what happens if you run my code as is? – Roy2012 May 16 '20 at 16:46
  • your example timestring "2020-05-16T08:17:42Z" has a timezone at the end, so why use `replace()`? simply use the correct format string for `strptime`.... – FObersteiner May 16 '20 at 18:51