问题
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
回答1:
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
回答2:
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
回答3:
>>> 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)
.
回答4:
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
来源:https://stackoverflow.com/questions/20481689/accessing-datetime-now-values-in-python