Search code examples
pythonunix-timestamp

Converting a column of Unix time stamp to UTC format


I have to convert the col5 of Unix time stamp to UTC format (YYYY-MM-DD hh:mm:ss) with python script and create a new col6 for UTC format in the following data.

Data:

col1 col2 col3 col4 col5

0 0 0 1 1231006505 1 1 0 1 1231469665 2 2 0 1 1231469744 3 3 0 1 1231470173 4 4 0 1 1231470988 5 5 0 1 1231471428 6 6 0 1 1231471789 7 7 0 1 1231472369 8 8 0 1 1231472743 9 9 0 1 1231473279 10 10 0 1 1231473952 11 11 0 1 1231474360 12 12 0 1 1231474888 13 13 0 1 1231475020 14 14 0 1 1231475589 15 15 0 1 1231562746 16 16 0 1 1231562758 17 17 0 1 1231563791 18 18 0 1 1231564334 19 19 0 1 1231564974 20 20 0 1 1231565995 21 21 0 1 1231566553 22 22 0 1 1231567467

I am new to python .Any help regarding this would be highly appreciated .

Thanks in advance .

Regards

Rubz


Solution

  • You can use the datetime module to format the data into any date format you desire.

    from datetime import datetime
    
    unix_timestamp = float("1231006505")
    utc_timestamp = datetime.utcfromtimestamp(unix_timestamp)
    print(utc_timestamp.strftime("%Y-%m-%d %H:%M:%S"))
    

    You can look at all the different format options for strftime() here

    Good luck!