By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

A question about time

P: n/a
Hello all,

I have a list of servers which an application connects to. If the
connection fails, the application should mark the server as temporarily
unavailable, and should try to use the server again after x units of time.

In C, I would do this:

server.invalidUntil = time(NULL) + 5*60; // five minute delay

...and the check:

if(time(NULL) > server.invalidUtil)
{
// use server
}

So the whole thing very simple... But how do I do that in Python?

I have found datetime.datetime.now(), but I don't understand what
"format" it returns the time in. I assume it's an object of some sort..
But how do I do if I want the current time as an integer (unix
timestamp) in Python?
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2005-06-09, Jan Danielsson <ja************@gmail.com> wrote:
In C, I would do this:

server.invalidUntil = time(NULL) + 5*60; // five minute delay
In Python, I would do this:

server.invalidUntil = time.time() + 5*60 # five minute delay
..and the check:

if(time(NULL) > server.invalidUtil)
{
// use server
}
if time() > server.invalidUntil:
# user server
So the whole thing very simple...


Yup.

--
Grant Edwards grante Yow! Look!! Karl Malden!
at
visi.com
Jul 19 '05 #2

P: n/a
On 2005-06-09, Grant Edwards <gr****@visi.com> wrote:
On 2005-06-09, Jan Danielsson <ja************@gmail.com> wrote:
In C, I would do this:

server.invalidUntil = time(NULL) + 5*60; // five minute delay


In Python, I would do this:

server.invalidUntil = time.time() + 5*60 # five minute delay
..and the check:

if(time(NULL) > server.invalidUtil)
{
// use server
}


if time() > server.invalidUntil:
# user server


Um, that should have been

if time.time() > server.invalidUntil:
# use server
So the whole thing very simple...


Yup.


Not quite simple enough that I could get it right the first time.

--
Grant Edwards grante Yow! I need to discuss
at BUY-BACK PROVISIONS
visi.com with at least six studio
SLEAZEBALLS!!
Jul 19 '05 #3

P: n/a
Grant Edwards wrote:
In C, I would do this:

server.invalidUntil = time(NULL) + 5*60; // five minute delay


In Python, I would do this:

server.invalidUntil = time.time() + 5*60 # five minute delay


Ah. Well. Um. I feel like an idiot. I found datetime by accident, and
thought "it involves time, so this must be what I'm looking for!".

Anyway; thanks, that was exactly what I was looking for.
Jul 19 '05 #4

P: n/a
Jan Danielsson wrote:
Hello all,

I have a list of servers which an application connects to. If the
connection fails, the application should mark the server as temporarily
unavailable, and should try to use the server again after x units of time.

In C, I would do this:

server.invalidUntil = time(NULL) + 5*60; // five minute delay

..and the check:

if(time(NULL) > server.invalidUtil)
{
// use server
}

So the whole thing very simple... But how do I do that in Python?

I have found datetime.datetime.now(), but I don't understand what
"format" it returns the time in. I assume it's an object of some sort..
Everything in Python is an object. Objects can be inspected. The builtin
function repr(obj) gives a diagnostic and often compilable
REPResentation of the object, and str(obj) [think STRing] gives a
"pretty" picture. Sometimes repr() and str() produce the same results.
To understand what "format" it's in, read the documentation; in this
case, it's found at:

http://www.python.org/doc/2.4.1/lib/...-datetime.html

and play around with the command-line interpreter (example below) or
your favourite IDE.
import datetime
t1 = datetime.datetime.now(); t2 = t1 + datetime.timedelta(minutes=5) [somewhat later] t3 = datetime.datetime.now()
for x in t1, t2, t3: print str(x), repr(x) ....
2005-06-10 07:39:15.312000 datetime.datetime(2005, 6, 10, 7, 39, 15, 312000)
2005-06-10 07:44:15.312000 datetime.datetime(2005, 6, 10, 7, 44, 15, 312000)
2005-06-10 07:56:03.031000 datetime.datetime(2005, 6, 10, 7, 56, 3, 31000) t3 > t2

True
But how do I do if I want the current time as an integer (unix
timestamp) in Python?


The Python time module would be a good starting point.

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.