0

I can't for the life of me figure out how to convert a timestamp on the form 1433140740000+0200 to a datetime object or to any humanly readable representation. Also, what format is this specifically? I'm assuming the +0200 represents a timezone.

I can only seem to find questions regarding timestamps without timezones, such as this answer, where int("1433140740000+0200") would give me an error. Any help is appreciated. Thanks!

Edit: As mentioned in a comment, further examination of the API from which I am getting these values reveals other timestamps with different values for what I thought to represent timezones. E.g: 315529200000+0100. The entire line of data looks like this: "ArrivalTime": "/Date(1433051640000+0200)/", and the full response can be found here.

Second edit: As far as I can tell, the timestamps are unix timestamps, but they're given in milliseconds (hence the trailing zeros), and the +0200 indicates timezone UTC+02:00. So for now, I'll just trim out the extra zeros and the timezone, and convert as shown in the linked question, before adding the timezone manually afterwards. The timestamps with +0100 remain a mystery to me, but I've found they're always the same date, 1/1/1980 12:00am. They also have a different identifier: ActualTime, as opposed to ArrivalTime on the others. Anyway, thanks for the help guys!

Community
  • 1
  • 1
Plasma
  • 1,903
  • 1
  • 22
  • 37
  • It's time offsets then... if you take a flight an hour west that takes exactly an hour, then you arrive at the same time you left. However, it'd be -0100 from your origin. – Jon Clements May 30 '15 at 23:56
  • The API is for the local public transportation, so I doubt it's offset, really. In addition, all the timestamps with +0100 seems to represent midnight 1/1/1980 for some reason, while the ones with +0200 make sense. – Plasma May 31 '15 at 00:01
  • ahhh.... got to love sane data - sounds like you're going to have fun with that :) – Jon Clements May 31 '15 at 00:02
  • Haha indeed, they could at the very least mention what it's supposed to represent. – Plasma May 31 '15 at 00:09

1 Answers1

1

You can use string split to remove the timezone

import datetime
intstring = int( ('1433140740000+0200').split('+')[0])

print(
datetime.datetime.fromtimestamp(intstring/1000).strftime('%Y-%m-%d %H:%M:%S')
)

I had to change it to this to make it work

intstring /1000

optimcode
  • 332
  • 2
  • 12
  • how did he get these 0000 in timestamp? – Igor Barinov May 30 '15 at 23:12
  • He will need to figure out what the timestamp really means. – optimcode May 30 '15 at 23:13
  • 2
    @themaze note - this won't work with negative timestamp offsets (ie... you might also need to throw in a split on `-` as well) – Jon Clements May 30 '15 at 23:14
  • This may do the trick intstring = '1433140740000+0200' intstring = int(re.split(r'(\d+)', intstring)[1]) – optimcode May 30 '15 at 23:20
  • 1
    @themaze Just slice and take off the last five `[:-5]` chars instead – Jon Clements May 30 '15 at 23:23
  • Hi guys, I got the timestamps from an API, but it's quite poorly documented, so return values are not explained. Exploring the API, I find other timestamps, this time with +0100, suggesting these are, in fact, not timezones. The full value is this: ArrivalTime": "/Date(1433140740000+0200)/. Thanks for replies! – Plasma May 30 '15 at 23:28