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 5 118102
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
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()
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
[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 ;-)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
...
|
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...
|
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...
|
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'...
|
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= _
>>>...
|
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
| |