3

Does anyone know how can I convert unix timestamp to readable dates in Python? I've been using this:

print(
datetime.datetime.fromtimestamp(
    int("1284101485")
).strftime('%Y-%m-%d %H:%M:%S')
)

but it gets taxing when I have to convert many.

Appreciate the help.

Harry
  • 67
  • 1
  • 11
  • Where are the many timestamps stored? In a file? – Philip Adler Aug 03 '17 at 08:00
  • How many is many? What's an acceptable time in your context? What have you tried so far? – Nath Aug 03 '17 at 08:02
  • '%Y-%m-%d %H:%M:%S' can be rewritten as: '%x %X' – Paddez Aug 03 '17 at 08:02
  • @Paddez No it can't – donkopotamus Aug 03 '17 at 08:13
  • @donkopotamus: `>> datetime.datetime.fromtimestamp(int("1284101485")).strftime('%x %X') >> '09/10/10 06:51:25'` UNIX Timestamp -> Readable date. – Paddez Aug 03 '17 at 08:36
  • @Paddez Exactly ... `%x %X` does not "replace" `%Y-%m-%d %H:%M:%S`, its output is quite different and since it is utterly ambiguous it is not a readable date ... Day-month ordering differs between locales, how does a reader know whether that is the 9th of October or the 10th of September? Is it 1910 or 2010? – donkopotamus Aug 03 '17 at 23:00
  • @donkopotamus In that case, you can probably just go with '%c' :) – Paddez Aug 04 '17 at 08:38
  • @PhilipAdler I've stored them in a csv file – Harry Aug 06 '17 at 13:11
  • @Nath I have about 200 timestamps. I haven't tried anything yet, been basically doing it one by one – Harry Aug 06 '17 at 13:12
  • Wait is "taxing" a performance concern? – Nath Aug 06 '17 at 23:17
  • @Nath I'm just wondering if there's a function that's able to convert all of them in one go. It's fine if there's none, and totally fine if you don't know either :) – Harry Aug 07 '17 at 06:57

3 Answers3

1
print (datetime.datetime.utcfromtimestamp(1284101485))
Ivan Sheihets
  • 802
  • 8
  • 17
1

I think some of the confusion around this question stems from the word "Taxing", people often assume that this means from a computation time perspective but I suspect that you have not yet learned about for loops

In essence, you need to capture the timestamps in some kind of iterable, given that you are using a csv I suggest you look at the csv python module. Then you can iterate over the iterable:

for timestamp in timestamp_collection:
    print(datetime.datetime.fromtimestamp(
            int("1284101485")
            ).strftime('%Y-%m-%d %H:%M:%S')
    )
Philip Adler
  • 2,111
  • 17
  • 25
  • This definitely helps! Understand the confusion, my apologies for not being as clear. In my defence: -I just picked up coding -English isn't my first language If I understand your code correctly, can timestamp_collection be a list? #can I change the code to this, to basically reiterate all the values in the timestamp_collection list? for timestamp in timestamp_collection: print(datetime.datetime.fromtimestamp( int("timestamp") ).strftime('%Y-%m-%d %H:%M:%S') ) – Harry Aug 08 '17 at 02:43
  • @harry yes- timestamp_collection can definitely be a list! Also, if an answer is helpful please make sure to upvote it, and if it is correct, please mark it correct. – Philip Adler Aug 08 '17 at 08:11
  • perfect, already upvoted your answer! certainly most helpful! – Harry Aug 09 '17 at 01:38
0

If time format isn't really important to you, this will do:

>>> from time import ctime as convert
>>> convert(1284101485)
'Fri Sep 10 10:51:25 2010'

Example of usage:

number = 1000

# Here we generate a list with given number of timestamps.
from random import randint

timestamps = []
for i in range(0, number):
    timestamps += [randint(0, 2147483647)]

# Here is actual example of massive conversation.
from time import ctime as convert

for i in range(0, number):
    print(str(i+1)+"\t"+str(timestamps[i])+"\t->\t"+convert(timestamps[i]))

Example generates a list with timestamps (ready-to-use data). Next we convert timestamps (in loop) and print them. Code processes (generates, converts and prints) 200 timestamps faster than I can blink, 1k timestamps in less than 0.5 second and 100k in less than 12 seconds. Tested on old PC.

MOPO3OB
  • 383
  • 3
  • 16