473,608 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ 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.datet ime.fromtimesta mp(), but the
other way around...?)

-Simen

Aug 11 '06 #1
5 118208
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.datet ime.fromtimesta mp(), but the
other way around...?)

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

#>>import datetime
#>>n = datetime.dateti me.now()
#>>n
datetime.dateti me(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.datet ime.fromtimesta mp(), but the
other way around...?)

-Simen

Is the timetuple() method what you want?

#>>import datetime
#>>n = datetime.dateti me.now()
#>>n
datetime.dateti me(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******@lexic on.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.datet ime.fromtimesta mp(), but the
other way around...?)
Is the timetuple() method what you want?
[ ... ]
Aaaarrrggghh h 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.4550 381
>>d = datetime.dateti me.fromtimestam p(n)
time.mktime(d .timetuple())
1155307613.0

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

--
\S -- si***@chiark.gr eenend.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.datet ime.fromtimesta mp(), but the
other way around...?)
[John Machin]
>Is the timetuple() method what you want?

#>>import datetime
#>>n = datetime.dateti me.now()
#>>n
datetime.datet ime(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(som e_datetime_obje ct.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.datet ime.fromtimesta mp(), but the
other way around...?)

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

#>>import datetime
#>>n = datetime.dateti me.now()
#>>n
datetime.dateti me(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(som e_datetime_obje ct.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
4199
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) Is there reason enough to ask for a 'datetime.fromstr(...)' method? At least to convert ISO timestamps? (2) You've got a better/different idea? Glad to see. Don't let me die with this open question ... ;-)
1
16702
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 value and a valid date (as the word timestamp means) or is it a left over from the days when the timestamp value really was a datetime type (so it says in Books Online) ? or is it just a unique identification of a row (a tuple id) ? I tried...
7
4049
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 error. If I can not store the value, how should I return the timestamp to the database (with other values) to compare with the current timestamp to check for updates. You answers will be greatly appreciated.
3
2964
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' occurred in system.web.services.dll Additional information: Server was unable to process request. --> Invalid cast from System.DateTime to System.Byte. Can anyone tell me how to deal with this issue?
2
16208
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= _ >>> datetime.datetime.utcfromtimestamp(n) datetime.datetime(2005, 12, 6, 18, 25, 40, 874922) >>> gmt= _
22
6405
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 database. The original Update and Delete SP's all use a timestamp for concurreny checking. I am trying to use the same Update SP from my sqlDataSource but I keep getting the following error:
6
38922
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 Unix-style timestamp and then formats it. $timestamp = "2005-04-06 15:43:34"; $time = strtotime($timestamp); print date('Y-m-d \a\t H:i', $time)."\n"; However, it seems kind of counter productive. After all, aren't people
5
3759
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 with Python. My problem is that I keep hitting screwy records with what appears to be a malformed dbiDate object when I insert certain records. I get the following traceback: Traceback (most recent call last):
8
2065
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 the records either for year (2006) or for month and year (Jan 2006) Currently I had implement this logic:
0
8063
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8498
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8478
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6817
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6014
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3962
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1598
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1331
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.