3

I want to be able to implement a condition in my program where it would only run for N number of hours, maybe the user could specify N, but let's not jump ahead.

I figured I could use datetime.now() and store the value below in a variable, time

>>> time 
>>> time = datetime.datetime(2013, 12, 9, 21, 50, 32, 405329)

Any ideas on how I can access the fourth field between the (--), seeing as it's a string?

My condition would be something like while time != timeEnd where timeEnd would be the value of below:

>>> timeEnd = datetime.datetime(2013, 12, 9, 21+N, 50, 32, 405329)

Thanks

codaamok
  • 717
  • 3
  • 11
  • 21
  • 1
    What `--` are you talking about? – abarnert Dec 09 '13 at 22:04
  • Are you being pedantic? – codaamok Dec 09 '13 at 22:12
  • You don't want to do date/time math examining just the hour field -- you could easily terminate at (3 hours + 1 minute) or (5 hours - 1 minute), or if some tasks are very long-running, could even span days by mistake. Use a time delta of some kind as the answers below suggest. – Chris Johnson Dec 09 '13 at 22:14
  • 1
    @adampski: No, I don't understand what you're talking about at all. Either you're asking about a literal `--` somewhere, in which case there is no such thing, or you're using it as a placeholder for a string that exists somewhere in your example, in which case there is _still_ no such thing; there aren't any strings visible anywhere. – abarnert Dec 09 '13 at 23:08
  • @abarnet OK well (--) refers to the properties of the datetime object. Just couldn't believe to write them all out, it was a short-hand expressions of all values. – codaamok Dec 10 '13 at 01:07

4 Answers4

6

Don't use unaware local datetime objects such as returned by now() unless you want to show them to a local user. There are many perils.

There is a difference between "What time is it?" and "How many seconds elapsed?". The former is easier to find out.

To run for N hours, you could:

from time import monotonic as timer # or time.time if it is not available

endtime = timer() + N * 3600
while timer() < endtime:
    # do your thing (mind what clocks use sleep(), join(), lock.acquire())

It works even if computer time has changed during the program execution manually or due to DST transition. See Rational section from pep-418 for introducing time.monotonic in Python.

You can choose other timers depending on your needs/available systems. For example, you could use a timer that provides better precision but might overflow sooner or that takes into account NTP adjacements that might provide better clock than your local CPU or the time while the system were asleep or suspended (imagine what do you want to happen after you open the cover of your notebook after several hours).

datetime.now() object (naive broken-down time) might be useful if you want something happen at some local time regardless of how many seconds passed since now e.g., to do something after 7pm whatever day (before midnight):

from datetime import datetime, time

if datetime.now().time() > time(19):
   # do something
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
2

You can add a timedelta to a datetime to get a new datetime.

>>> import datetime
>>> now = datetime.datetime.now()
>>> end_time = now + datetime.timedelta(hours=6)
>>> print now
2013-12-09 17:03:06.979628
>>> print end_time
2013-12-09 23:03:06.979628
>>> 

Then...

while datetime.datetime.now() < end_time:
    pass # do work
FogleBird
  • 74,300
  • 25
  • 125
  • 131
  • `datetime.now() < end_time` might not give your `6` hours. It may happen an hour sooner/later (due to DST) or any other amount of time if computer clock were manually adjusted. Or if computer were suspended for sometime; you might not want all events happen at once when you open notebook cover. [You can avoid such issues](http://stackoverflow.com/a/20529909/4279) – jfs Dec 11 '13 at 21:11
1
>>> from datetime import datetime
>>> d = datetime.now()
>>> d
datetime.datetime(2013, 12, 9, 23, 0, 20, 669780)
>>> d.hour
23

If you want to check whether script is running for N hours, I'd suggest checking the (now() - start_time).total_seconds() value. It'd tell you for how many seconds the script has been running.

Likewise, you can set the timeEnd like so timeEnd = time + timedelta(hours=N).

Maciej Gol
  • 15,394
  • 4
  • 33
  • 51
0

I want to be able to implement a condition in my program where it would only run for N number of hours, maybe the user could specify N, but let's not jump ahead.

Any ideas on how I can access the fourth field between the (--), seeing as it's a string?

I don't see any -- in your code, or any strings. You've got a datetime object, and it has an hour member. However, I can't imagine how you'd use such a thing in your code anyway.

Just subtract two datetime objects, and you get back a timedelta object. And you can compare that timedelta object to another one. For example:

>>> start_time = datetime.datetime.now()

# 2 hours later

>>> end_time = datetime.datetime.now()
>>> duration = end_time - start_time
>>> duration > datetime.timedelta(hours=5)
False

# another 4 hours

>>> datetime.datetime.now() - start_time > datetime.timedelta(hours=5)
True
abarnert
  • 354,177
  • 51
  • 601
  • 671