Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Convert seconds from epoch time into human readable time [duplicate]

Tags:

Originally I made this code to convert date into human readable time:

    a = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S.%f")     b = datetime.datetime.now()     c = b - a     days, hours, minutes, seconds = int(c.days), int(c.seconds // 3600), int(c.seconds % 3600 / 60.0), int(c.seconds % 60.0)     return days, hours, minutes, seconds     EXAMPLE OUTPUT: 1 days, 4 hours, 24 minutes, 37 seconds 

and I'm trying to make it using epoch time, but I have no idea on to make it calculate days hours and etc.

    a = last_epoch #last epoch recorded     b = time.time() #current epoch time     c = b - a #returns seconds     hours = c // 3600 / 24 #the only thing I managed to figure out 
like image 850
Daniel Hyuuga Avatar asked Oct 09 '14 11:10

Daniel Hyuuga


People also ask

How do you convert epoch time to human readable?

Convert from epoch to human-readable dateString date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000)); Epoch in seconds, remove '*1000' for milliseconds. myString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch.

How do you convert seconds to HH MM SS in Python?

Use the timedelta() constructor and pass the seconds value to it using the seconds argument. The timedelta constructor creates the timedelta object, representing time in days, hours, minutes, and seconds ( days, hh:mm:ss.ms ) format. For example, datetime.

How do you convert time to seconds in Python epoch?

To convert a datetime to seconds, subtracts the input datetime from the epoch time. For Python, the epoch time starts at 00:00:00 UTC on 1 January 1970. Subtraction gives you the timedelta object. Use the total_seconds() method of a timedelta object to get the number of seconds since the epoch.


1 Answers

import datetime timestamp = 1339521878.04  value = datetime.datetime.fromtimestamp(timestamp) print(value.strftime('%Y-%m-%d %H:%M:%S')) 
like image 118
pyprism Avatar answered Sep 23 '22 13:09

pyprism