views:

162

answers:

3

In the code shown below, I need to manipulate the time var in python to display a date/time stamp in python to represent that delay. for example, when the user enters the delay time in hours, i need to set the jcarddeliver var to update itself with the value of the current date/time + delay also it should update the date var as well. for example, if the date is 24 Feb and time is 15:00 hrs and the delay time is 10 hrs, the jcarddeliver date should change to 25 Feb.

jcarddate = time.strftime("%a %m/%d/%y", time.localtime())
jcardtime = time.strftime("%H:%M:%S", time.localtime())
delay = raw_input"enter the delay: "
jcarddeliver = ??

I just hope I am making sense.

A: 

" i need to set the jcarddeliver var to update itself with the value of the current date/time + delay"

How about reformulating this to

jcarddeliver should be the current date-time plus the delay.

The "update itself" isn't perfectly sensible.

Try the following:

  1. Try the most obvious way of computing "current date-time plus the delay"

  2. print the result.

  3. Try using localtime() on this result. What do you get?

S.Lott
+1  A: 

The result of time.time() is a floating point value of the number of seconds since the Epoch. You can add seconds to this value and use time.localtime(), time.ctime() and other functions to get the result in various forms:

>>> now = time.time()
>>> time.ctime(now)
'Fri Sep 04 16:19:59 2009' # <-- this is local time
>>> then = now + (10.0 * 60.0 * 60.0) # <-- 10 hours in seconds
>>> time.ctime(then)
'Sat Sep 05 02:19:59 2009'
D.Shawley
I would use Brian's solution (http://stackoverflow.com/questions/1381315/manipulating-time-in-python/1381769) since the `datetime.timedelta()` usage is considerably clearer.
D.Shawley
+2  A: 

You could try the datetime module, e.g.

import datetime
now = datetime.datetime.now()
delay = float (raw_input ("enter delay (s): "))
dt = datetime.timedelta (seconds=delay)
then = now + dt
print now
print then
Brian Hawkins
from what i see, the now and then variables can only be displayed correctly using the print module. what if i have to pass these values to the DB. it gives me an error saying, not all arguments converted during string formatting.
amit
In the above example the variables 'now' and 'then' are datetime objects. When you print them they're converted to a string. If you really want a string try the str() function, e.g. str(now). Of course you can also use now.year, now.month, etc. More info at http://docs.python.org/library/datetime.html.
Brian Hawkins