0

I'm using an API. The API returns JSON data with a timestamp. The response looks like this:

{'time_gmt': 1213329060, 'day_ind': 'N' ...}

I've familiarity with Python's DateTime objects, but I'm not able to comprehend this time format. Is there any way we can convert it into a more readable format?

Rishabh Agrahari
  • 3,447
  • 2
  • 21
  • 22
  • 1
    Unix timestamp? Try in https://www.unixtimestamp.com/index.php – Alex Yu Jan 09 '19 at 15:02
  • 2
    What you call `GMT time format` is the readable format. That looks like a Unix timestamp. – Panagiotis Kanavos Jan 09 '19 at 15:02
  • @PanagiotisKanavos Oh I didn't know about Unix timestamp. I'm sorry, would update the title. – Rishabh Agrahari Jan 09 '19 at 15:02
  • 1
    `GMT` refers to a specific timezone. `time_gmt` means that the time is in that timezone, not that it has a specific format. There's no specific JSON date format but the defacto standard is the ISO8601 format, ie `2019-01-01T17:04+02:00`. You should probably change the API or contact the author to use the standard version if possible – Panagiotis Kanavos Jan 09 '19 at 15:03
  • @PanagiotisKanavos Thank you so much, I've found the solution on SO, should I close the question? – Rishabh Agrahari Jan 09 '19 at 15:04
  • 1
    Turning unix timestamps into readable strings can be done like this: `import datetime; print(datetime.datetime.fromtimestamp(1213329060))` – Mattias Nilsson Jan 09 '19 at 15:05
  • Possible duplicate of [Converting unix timestamp string to readable date](https://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date) – Rishabh Agrahari Jan 09 '19 at 15:07

2 Answers2

1
import datetime
print(
    datetime.datetime.fromtimestamp(
        int("1213329060")
    ).strftime('%Y-%m-%d %H:%M:%S')
)
RamiReddy P
  • 1,628
  • 1
  • 18
  • 29
1

Like everyone else said before me, that is indeed a UNIX timestamp. Very common.

How to convert that into a readable format has been asked before a few times. Here is a nice example: converting-unix-timestamp-string-to-readable-date

Which is the same example that everyone else here posted before me :)

What is missing, is that this website here explains what the flags are that are used to convert the UNIX timestamp: http://strftime.org/

Paddy
  • 123
  • 7