Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Epoch time into the datetime

Tags:

python

I am getting a response from the rest is an Epoch time format like

start_time = 1234566 end_time = 1234578 

I want to convert that epoch seconds in MySQL format time so that I could store the differences in my MySQL database.

I tried:

>>> import time >>> time.gmtime(123456) time.struct_time(tm_year=1970, tm_mon=1, tm_mday=2, tm_hour=10, tm_min=17, tm_sec=36, tm_wday=4, tm_yday=2, tm_isdst=0) 

The above result is not what I am expecting. I want it be like

2012-09-12 21:00:00 

Please suggest how can I achieve this?

Also, Why I am getting TypeError: a float is required for

>>> getbbb_class.end_time = 1347516459425 >>> mend = time.gmtime(getbbb_class.end_time).tm_hour Traceback (most recent call last):   ... TypeError: a float is required 
like image 573
user1667633 Avatar asked Sep 13 '12 06:09

user1667633


People also ask

How do I convert epoch to date?

Because our Epoch time is specified in milliseconds, we may convert it to seconds. To convert milliseconds to seconds, first, divide the millisecond count by 1000. Later, we use DATEADD() to add the number of seconds since the epoch, which is January 1, 1970 and cast the result to retrieve the date since the epoch.

How do I convert epoch time to manual date?

You can take an epoch time divided by 86400 (seconds in a day) floored and add 719163 (the days up to the year 1970) to pass to it. Awesome, this is as manual as it gets.

How do I convert Unix epoch time to datetime in Excel?

If you have a list of timestamp needed to convert to date, you can do as below steps: 1. In a blank cell next to your timestamp list and type this formula =(((A1/60)/60)/24)+DATE(1970,1,1), press Enter key, then drag the auto fill handle to a range you need.

What is epoch datetime?

In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system.

What is Python epoch datetime?

8. Python epoch DateTime to milliseconds 9. Python epoch timestamp to datetime 10. Python epoch datetime year is out of range/Python Invalid argument The epoch time is also called Unix time, POSIX time, and Unix timestamp. The epoch time means the number of seconds that have passed since January 1 1970 excluding leap seconds.

How to convert Epoch time to a numeric value?

You can convert it to numeric like: float (getbbb_class.end_time) If you have epoch in milliseconds a possible solution is convert to seconds:

Why can't I create a datetime after a Unix epoch?

The error you are getting is because your times are just a floating-point number, but that is not how you are trying to handle them. Assuming these are seconds after Unix epoch, you need to create your datetimes using a timedelta from a start point defined as the Unix epoch:

What is epoch in Unix?

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym...


2 Answers

To convert your time value (float or int) to a formatted string, use:

import time  time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370)) 

For example:

import time  my_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))  print(my_time) 
like image 183
ron rothman Avatar answered Sep 21 '22 23:09

ron rothman


You can also use datetime:

>>> import datetime >>> datetime.datetime.fromtimestamp(1347517370).strftime('%c')   '2012-09-13 02:22:50' 
like image 30
png Avatar answered Sep 24 '22 23:09

png