473,491 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 118188
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
4190
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
16697
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
4034
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
2954
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
16203
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
6386
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
38905
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
3751
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
2056
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...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.