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
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.
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.
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.
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.
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.
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:
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:
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...
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)
You can also use datetime
:
>>> import datetime >>> datetime.datetime.fromtimestamp(1347517370).strftime('%c') '2012-09-13 02:22:50'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With