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 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
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: 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,...
|
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...
| |
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...
|
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...
|
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 projectplanning, coding, testing,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |