473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

datetime to timestamp

Hi.

How can I convert a python datetime to a timestamp? It's easy to convert
a timestamp to datetime (datetime.datetime.fromtimestamp(), but the
other way around...?)

-Simen

Aug 11 '06 #1
5 118177
On 11/08/2006 11:10 PM, Simen Haugen wrote:
Hi.

How can I convert a python datetime to a timestamp? It's easy to convert
a timestamp to datetime (datetime.datetime.fromtimestamp(), but the
other way around...?)

-Simen
Is the timetuple() method what you want?

#>>import datetime
#>>n = datetime.datetime.now()
#>>n
datetime.datetime(2006, 8, 11, 23, 32, 43, 109000)
#>>n.timetuple()
(2006, 8, 11, 23, 32, 43, 4, 223, -1)

Cheers,
John
Aug 11 '06 #2
On 11/08/2006 11:35 PM, John Machin wrote:
On 11/08/2006 11:10 PM, Simen Haugen wrote:
>Hi.

How can I convert a python datetime to a timestamp? It's easy to convert
a timestamp to datetime (datetime.datetime.fromtimestamp(), but the
other way around...?)

-Simen

Is the timetuple() method what you want?

#>>import datetime
#>>n = datetime.datetime.now()
#>>n
datetime.datetime(2006, 8, 11, 23, 32, 43, 109000)
#>>n.timetuple()
(2006, 8, 11, 23, 32, 43, 4, 223, -1)

Aaaarrrggghhh no it's not what you want -- looks like you have to do the
arithmetic yourself, starting with toordinal()
Aug 11 '06 #3
John Machin <sj******@lexicon.netwrote:
>On 11/08/2006 11:35 PM, John Machin wrote:
>On 11/08/2006 11:10 PM, Simen Haugen wrote:
>>How can I convert a python datetime to a timestamp? It's easy to convert
a timestamp to datetime (datetime.datetime.fromtimestamp(), but the
other way around...?)
Is the timetuple() method what you want?
[ ... ]
Aaaarrrggghhh no it's not what you want -- [ ... ]
It is if you only want it to the second. It just needs a time.mktime():
>>n = time.time()
n
1155307613.4550381
>>d = datetime.datetime.fromtimestamp(n)
time.mktime(d.timetuple())
1155307613.0

(timetuple() is responsible for the loss of the fractional part.)

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Aug 11 '06 #4
[Simen Haugen]
>>How can I convert a python datetime to a timestamp? It's easy to convert
a timestamp to datetime (datetime.datetime.fromtimestamp(), but the
other way around...?)
[John Machin]
>Is the timetuple() method what you want?

#>>import datetime
#>>n = datetime.datetime.now()
#>>n
datetime.datetime(2006, 8, 11, 23, 32, 43, 109000)
#>>n.timetuple()
(2006, 8, 11, 23, 32, 43, 4, 223, -1)
[also John]
Aaaarrrggghhh no it's not what you want
Yes, it is ;-)
-- looks like you have to do the arithmetic yourself, starting with toordinal()
It's just one step from the time tuple:

import time
time.mktime(some_datetime_object.timetuple())

The datetime module intended to be an island of relative sanity.
Because the range of dates "timestamps" can represent varies across
platforms (and even "the epoch" varies), datetime doesn't even try to
produce timestamps directly -- datetime is more of an alternative to
"seconds from the epoch" schemes. Because datetime objects have
greater range and precision than timestamps, conversion is
problem-free in only one direction. It's not a coincidence that
that's the only direction datetime supplies ;-)
Aug 11 '06 #5

Tim Peters wrote:
[Simen Haugen]
>How can I convert a python datetime to a timestamp? It's easy to convert
a timestamp to datetime (datetime.datetime.fromtimestamp(), but the
other way around...?)

[John Machin]
Is the timetuple() method what you want?

#>>import datetime
#>>n = datetime.datetime.now()
#>>n
datetime.datetime(2006, 8, 11, 23, 32, 43, 109000)
#>>n.timetuple()
(2006, 8, 11, 23, 32, 43, 4, 223, -1)

[also John]
Aaaarrrggghhh no it's not what you want

Yes, it is ;-)
-- looks like you have to do the arithmetic yourself, starting with toordinal()

It's just one step from the time tuple:

import time
time.mktime(some_datetime_object.timetuple())
Thanks, Tim. In mitigation, yer honour, I'd like to draw your attention
to the local time (23:32); the defendant was rushing to do the OP's
RTFantasticM for him before retiring for the night, and not doing it
well enough :-)
Cheers,
John

Aug 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Martin Bless | last post by:
Below is what I'm currently using to construct datatime objects from strings. Date and time objects are made up similar. To and from string conversions are frequently needed in SQL neighborhood. ...
1
by: David Greenberg | last post by:
Hi I'm interested in using the timestamp data type & I have some questions. As far as I can understand the contant of a timestamp column is a binary value. Is there any connection between that...
7
by: mybappy | last post by:
Hi: I am trying to use timestamp field of SQL Server to maintain concurrency. My problem is how do I store the timestamp value in my webform. The hidden field does not work as I get some cast...
3
by: Peter Wone | last post by:
One of my webservices has a parameter of type System.DateTime. It compiles ok but at runtime I get this exception: An unhandled exception of type 'System.Web.Services.Protocols.SoapException'...
2
by: John Reese | last post by:
Hi. >>> import time, calendar, datetime >>> n= 1133893540.874922 >>> datetime.datetime.fromtimestamp(n) datetime.datetime(2005, 12, 6, 10, 25, 40, 874922) >>> lt= _ >>>...
22
by: Mal Ball | last post by:
I hope I have the right forum for this question. I have an existing Windows application which uses a SQL Server database and stored procedures. I am now developing a web application to use the same...
6
by: Brandon | last post by:
I'm using PHP with MySQL 4.x and was having trouble converting a datetime from MySQL into a formatted string until I ran across this solution that converts a YYYY-MM-DD HH:MM:SS string into a...
5
by: kyosohma | last post by:
Hi, I am populating a mySQL database with data from the MS Access database. I have successfully figured out how to extract the data from Access, and I can insert the data successfully into mySQL...
8
by: kanwal | last post by:
Hi, I have millions of records in my xxxxx table in mysql. And I have a column of time in which I have stored the timestamp using php time() function. Now I wanna write an SQL query to fetch...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.