ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Traceback (most recent call last):
File "test1.py", line 29, in <module>
epoch_start = time.mktime(time.strptime(i, "%Y-%m-%d %H:%M:%S"));
File "C:\Python27\lib\_strptime.py", line 467, in _strptime_time
return _strptime(data_string, format)[0]
File "C:\Python27\lib\_strptime.py", line 322, in _strptime
found = format_regex.match(data_string)
TypeError: expected string or buffer
Last edited by divyashree; 09-25-2014 at 03:01 AM.
Probably because i is a datetime object not a string or buffer. If you want to convert a python datetime to seconds since epoch you should do it explicitly:
Code:
from datetime import datetime
epoch = datetime.datetime(1970,1,1)
i = datetime.now()
delta_time = (i - epoch).total_seconds()
Probably because i is a datetime object not a string or buffer. If you want to convert a python datetime to seconds since epoch you should do it explicitly:
Code:
i = datetime.now()
Thanks weibullguy for the response. The problem is that I am getting the value of i from database after query, in the format as I mentioned.
I was only using datetime.now() for the example. As long as i is a datetime object, which you state it is in your original post, the remainder of the code will calculate the total number of seconds since epoch.
I was only using datetime.now() for the example. As long as i is a datetime object, which you state it is in your original post, the remainder of the code will calculate the total number of seconds since epoch.
Your code is throwing error:
Code:
Traceback (most recent call last):
File "test2.py", line 5, in <module>
epoch = datetime.datetime(1970,1,1)
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.