2

I need to find the Time which is an hour head of current system time in python, for example if current time is 2:00 the end time should be 3:00 so that i can compare that my current time is within the range of the time period.Actually I need to carry out a task only during the time period ,So can someone help me!

Gelineau
  • 2,031
  • 4
  • 20
  • 30
  • So you need to find the timestamp 1 hour ahead of the currrent time.?/ – Sreeram TP Jul 19 '18 at 05:52
  • 1
    Possible duplicate of [How to check if the current time is in range in python?](https://stackoverflow.com/questions/10747974/how-to-check-if-the-current-time-is-in-range-in-python) – Nazim Kerimbekov Jul 19 '18 at 05:52

2 Answers2

2

Use datetime.now() to get the current time, save it, call datetime.now() again when you need to check the time and subtract it by the starting time to get a timedelta to check if it's less than 1 hour:

from datetime import datetime, timedelta
start = datetime.now()
while datetime.now() - start <= timedelta(hours=1):
    do_work()
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

@blhsing is a good answer but there is a simpler and cost-effective way:

import time

end = time.clock() + 3600  # add hour

while time.clock() < end:
  ...
Yakir Tsuberi
  • 1,373
  • 1
  • 12
  • 16