D
D
DVoropaev2019-07-02 11:08:14
Python
DVoropaev, 2019-07-02 11:08:14

How to convert number of seconds to date and time in python?

There is a time given in the format (seconds since 00:00:00 01/01/2010 UTC). how to turn it into a convenient view?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Kuzmichev, 2019-07-02
@Assargin

import datetime

MY_UTC_EPOCH_START = 1262304000  # 00:00:00 01.01.2010 UTC

def my_utcfromtimestamp(ts):
    return datetime.datetime.utcfromtimestamp(ts + MY_UTC_EPOCH_START)

The function works in exactly the same way as the standard datetime.datetime.utcfromtimestampone, only applying the correction for the beginning of your "UTC epoch" (midnight January 1, 2010 GMT):
>>> my_utcfromtimestamp(1)
datetime.datetime(2010, 1, 1, 0, 0, 1)
>>> my_utcfromtimestamp(123)
datetime.datetime(2010, 1, 1, 0, 2, 3)
>>> my_utcfromtimestamp(3600)
datetime.datetime(2010, 1, 1, 1, 0)

M
milssky, 2019-07-02
@milssky

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

Something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question