1

What I want to achieve is easy:

time.time() is not quite readable. How to get the following:

e.g.

time.time() //say, it's May 15 2013 13:15:46

How to get the following given time.time() above:

May 15 2013 12:15:46

May 15 2013 14:15:46

May 15 2013 13:14:46

May 15 2013 13:16:46

I am looking for something like:

def back_an_hr(current_time):
    .....
def back_a_min(current_time):
    .....

back_an_hr(time.time()) # this brings time.time() back an hr
back_a_min(time.time()) # this brings time.time() back a min
Shengjie
  • 12,336
  • 29
  • 98
  • 139

3 Answers3

7

You might be better off with the datetime module:

>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2013, 5, 15, 15, 30, 17, 908152)
>>> onehour = datetime.timedelta(hours=1)
>>> oneminute = datetime.timedelta(minutes=1)
>>> now + onehour
datetime.datetime(2013, 5, 15, 16, 30, 17, 908152)
>>> now + oneminute
datetime.datetime(2013, 5, 15, 15, 31, 17, 908152)
>>> now.strftime("%b %d %Y %H:%M:%S")
'May 15 2013 15:30:17'
>>> (now - onehour).strftime("%b %d %Y %H:%M:%S")
'May 15 2013 14:30:17'
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Just in case if OP needs the Unix time, he can get it from the `datetime` like `time.mktime(dt.timetuple())` – bereal May 15 '13 at 13:35
0

Python has a module called datetime and timedelta. With These modules you can define your own functions back_an_hr(current_time) and back_a_min(current_time)

The timedelta takes an offset and you can define the offset as being a day, month, year, hour minute or second.

mnain
  • 46
  • 2
0

time.time() gives number of seconds as a float.

time.time() - (60 * 60 * 24) # 1 day
beiller
  • 3,105
  • 1
  • 11
  • 19