0

I am migrating php code into Python and came across datetime. My code:

date_raw = datetime.datetime.strptime(data["Campaign_Start_Date"], '%Y-%m-%d')
date_new = date_raw.strftime("%Y-%m-%d"+"T"+"%H:%M:%S GMT")
print(date_new)

# 2020-09-14T00:00:00 GMT

My desired output is: 2020-09-14T00:00:00-04:00 So I need to append GMT to the end of the string, but can't find a way to have a proper format back.

martineau
  • 119,623
  • 25
  • 170
  • 301
Chique_Code
  • 1,422
  • 3
  • 23
  • 49
  • Erm, it seems you just did it…? I mean, you appended `GMT` to the end. – Błotosmętek May 04 '20 at 18:16
  • 2
    your desired output seems to have a time zone offset relative to UTC ("-04:00"), however you parse a datetime string with '%Y-%m-%d'. where should the time zone info come from? – FObersteiner May 04 '20 at 18:17
  • What is the value of `data["Campaign_Start_Date"]` that is being passed to `strptime()`? – martineau May 04 '20 at 18:18
  • @martineau `2020-09-14` – Chique_Code May 04 '20 at 18:19
  • @Błotosmętek It has to be this: `2020-09-14T00:00:00-04:00` without `GMT` in the end. – Chique_Code May 04 '20 at 18:20
  • Can't you just use `date_new = date_raw.strftime("%Y-%m-%d"+"T"+"%H:%M:%S-04:00")`? – martineau May 04 '20 at 18:22
  • @MrFuppes The value of data["Campaign_Start_Date"] is `2020-09-14` I need to somehow convert it so it looks exactly like this: `2020-09-14T00:00:00-04:00` In my understanding -04:00 is Time from GMT. – Chique_Code May 04 '20 at 18:23
  • @martineau I thought about this option, but I wonder what is the proper way to do this without hardcoding... – Chique_Code May 04 '20 at 18:24
  • @Chique Please include the input in the question. I'm having a hard time following these comments. – wjandrea May 04 '20 at 18:25
  • 1
    -04:00 is Time from GMT might correct, but only _your_ local time zone. Do you want to hardcode it? – martineau May 04 '20 at 18:25
  • 2
    So why did you write " I need to append GMT to the end of the string" when in fact you **don't want** to append it? – Błotosmętek May 04 '20 at 18:25
  • @Błotosmętek Whatever this `-04:00` is it needs to be appended to the end of the string. `-04:00` is Time from GMT. – Chique_Code May 04 '20 at 18:29
  • does your input data hold any info on the timezone in which it was recorded? (note: use timestamps in UTC whenever you can, saves you a lot of work ;-)) – FObersteiner May 04 '20 at 18:39
  • @Chique `-04:00` is a UTC/GMT offset. Eastern Time is currently on UTC-4 under daylight savings, and will be on Sept 14 as well. Based on your profile you're in NJ which is in ET. So do you want to use UTC-4, Eastern Time, or local time? – wjandrea May 04 '20 at 18:41
  • @MrFuppes The API returns the date without any info on the timezone. – Chique_Code May 04 '20 at 18:45

1 Answers1

2

strptime doesn't automagically know about the timezone from a time formatted as '%Y-%m-%d', you will have to include that, e.g.

from datetime import datetime
import pytz

# parse the string
datestring = '2020-05-04'
date = datetime.strptime(datestring, '%Y-%m-%d')

# add a timezone info
tz = pytz.timezone('US/Eastern')
date_est = tz.localize(date)
# datetime.datetime(2020, 5, 4, 0, 0, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)

print(date_est.isoformat())
# 2020-05-04T00:00:00-04:00
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • This is still effectively hardcoding the timezone into the code. – martineau May 04 '20 at 18:30
  • @martineau, sure - you have to define it somewhere I guess. Unless you take e.g. the setting of the machine you run the script on but that sounds pretty unpredictable to me... – FObersteiner May 04 '20 at 18:33
  • @MrFuppes This works perfectly! Thank you. I accepted the answer. – Chique_Code May 04 '20 at 18:46
  • @Chique_Code: I'd say if you're sure about the timezone of the input data, you should be fine hardcoding it. However, an API should either strictly use UTC or provide the timezone info (UTC offset at least). – FObersteiner May 04 '20 at 18:51