LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2014, 02:41 AM   #1
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Rep: Reputation: 135Reputation: 135
python datetime to epoch


I have a dattime object in following format:

Code:
%Y-%m-%d %H:%M:%S
How can I convert it to epoch seconds.

I am trying to convert like this :

Code:
epoch_start = time.mktime(time.strptime(i, "%Y-%m-%d %H:%M:%S"));
Here
Code:
i
holds the value :
Code:
2014-09-26 12:24:23
But its throwing error:

Code:
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.
 
Old 09-25-2014, 08:07 AM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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()
 
Old 09-25-2014, 08:17 AM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by weibullguy View Post
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.

So I cant use datetime.now().

The value of i varries as stored in database.
 
Old 09-25-2014, 08:35 AM   #4
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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.
 
Old 09-25-2014, 08:52 AM   #5
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Unhappy

Quote:
Originally Posted by weibullguy View Post
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'
 
Old 09-25-2014, 08:56 AM   #6
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Sorry. One too many datetime in the epoch line. Try this...

Code:
from datetime import datetime

epoch = datetime(1970,1,1)
i = datetime.now()

delta_time = (i - epoch).total_seconds()
 
Old 09-25-2014, 09:46 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
I don't know how accurate it is, but this is something that came up in a Google search:

Code:
>>> import datetime
>>> import time
>>> time.mktime(datetime.datetime.now().timetuple())
1411656461.0
Critique?

There were many other hits, including this thread:

http://stackoverflow.com/q/6999726

Last edited by dugan; 09-25-2014 at 09:49 AM.
 
Old 10-01-2014, 01:41 AM   #8
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
I got the epoch value by :

Quote:
import time
import calendar

start_epoch = int(round(time.mktime(row[3].timetuple())))
Thanks all for your responses.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
more or less datetime in mysql trscookie Programming 3 09-11-2009 08:59 PM
Perl DateTime abdul_zu Linux - General 1 01-14-2006 02:55 AM
MySQL datetime jabfinger Programming 2 07-25-2005 12:27 PM
Epoch time the other way! michedlp Programming 0 04-25-2004 03:51 AM
php :: datetime gmarais Programming 3 03-06-2004 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration