0

I am trying to convert a timestamp in the format of 2020-07-27T23:00:53+00:00 to milliseconds using the following line of code

utc_time = datetime.datetime.strptime('2020-07-27T23:00:53+00:00', '%Y-%m-%dT%H:%M:%S%z')
milsec = utc_time.timestamp() * 1000

It works fine on my local system but when I try to run the code on a remote system which is in the same timezone as I am, I get the error

time data '2020-07-27T23:00:53+00:00' does not match format '%Y-%m-%dT%H:%M:%S%z'.

Is there anything that I should be aware of here? Appreciate any help.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Marius
  • 25
  • 8
  • Does this answer your question? [Convert UTC time to python datetime](https://stackoverflow.com/questions/13662789/convert-utc-time-to-python-datetime) – sushanth Jul 28 '20 at 03:42
  • @Sushanth I don't have that error. They used "y" instead of "Y". Mine is "Y". Also, the code runs fine on my local system. – Marius Jul 28 '20 at 03:45
  • ``datetime.datetime.strptime('2020-07-27T23:00:53+00:00', '%Y-%m-%dT%H:%M:%S+00:00')`` ? – sushanth Jul 28 '20 at 03:46
  • @Sushanth That solves it, if it is 00:00 at the end of the timestamp but not if they are not 00:00. – Marius Jul 28 '20 at 03:54
  • What are the timezone and locales settings on both systems? Consider stripping off TZ before you parse – Rob Raymond Jul 28 '20 at 04:20
  • @RobRaymond They both give the same output for time.tzname – Marius Jul 28 '20 at 05:41
  • 1
    Since `strptime` uses the system's C library, the behaviour of %z can be different on different systems. If you have Python >= 3.7 available, you could try to parse via `datetime.fromisoformat('string')`. – FObersteiner Jul 28 '20 at 06:24
  • The code mentioned above works fine for me with no errors - Python3.8.0~win32 – JenilDave Jul 28 '20 at 10:09
  • @MrFuppes your solution worked. I guess you're right on the behaviour of %z being different on different systems. – Marius Jul 28 '20 at 20:49

0 Answers0