0

I have a dictionary of timestamps which are strings which I first converted to datetime objects. I need to do the sql equivalent of timestamp at time zone 'UTC' at time zone 'America/New_York' Here I have a for loop

 for x in dataj:
     x['event_time'] = datetime.strptime(x['event_time'].split('.')[0], "%Y-%m-%d %H:%M:%S")
     x['event_time'] = x['event_time'].replace(tzinfo=timezone('America/New_York'))

I get this error

 TypeError: timezone() argument 1 must be datetime.timedelta, not str

dataj looks like so:

P(dataj[0])
{'$insert_id': '14ae91db-4b9e-4898-88dd-62fc9f99dcb4',
 '$schema': 12,
 'adid': None,
 'event_time': '2019-12-01 00:00:19.251000'
 }
spak
  • 253
  • 1
  • 2
  • 12

2 Answers2

0

This answer shows how to add the needed timezones to the datetime object. You need to use pytz.

Here is a segment that is similar to your post.

import datetime
import pytz
x = '2019-12-01 00:00:19.251000'
as_datetime = datetime.datetime.strptime(x.split('.')[0], "%Y-%m-%d %H:%M:%S")
as_datetime # datetime.datetime(2019, 12, 1, 0, 0, 19)

utcmoment = as_datetime.replace(tzinfo=pytz.utc)
as_nytz = utcmoment.astimezone(pytz.timezone('America/New_York'))
as_nytz # datetime.datetime(2019, 11, 30, 19, 0, 19, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>)
rajah9
  • 11,645
  • 5
  • 44
  • 57
0

Without knowing all background...

from datetime import datetime, timedelta
from pytz import timezone
import pytz

dataj = {'$insert_id': '14ae91db-4b9e-4898-88dd-62fc9f99dcb4','$schema': 12, 'adid': None,'event_time': '2019-12-01 00:00:19.251000'}

print(dataj)

for schema, value in dataj.items():
    if schema == 'event_time':
        correct_time = datetime.strptime(dataj["event_time"].split('.')[0], "%Y-%m-%d %H:%M:%S")
        dataj['event_time'] = correct_time.replace(tzinfo=timezone('America/New_York'))

print(dataj)
Hillborn
  • 94
  • 8