如何使用一天中的当前本地时间来控制没有日期的进程

How To Use The Current Local Time of Day To Control A Process Without Dates

我需要在太平洋夏令时间每天 8:00 下午或 8:00 下午后尽快停止进程。我看到了很多关于时间函数的其他用途,但这些例子似乎都是时间上的差异,某事的时间安排,或者总是包含日期。

有人可以提供一个例子,说明我是如何制作这样的东西的吗...? 如果当前时间 >= 8:00 下午 PDT: time.sleep(99999999)

另外,我需要导入什么,如何导入?

我知道的方法是:

import datetime #this is the way to import a module named datetime...
import time     #this module is used to sleep

a = str(datetime.datetime.now())   #a = '2016-03-27 00:20:28.107000' #for example
a = a.split(' ')[1]          #get rid of the date and keep only the '00:20:28.107000' part
if a.startwith('20:00'): time.sleep(3600*12) #sleep for 12 hours (43200 seconds)

希望对您有所帮助

编辑:将时区更改为 pdt:

import datetime,pytz #for python_TimeZone
a = str(datetime.datetime.now(pytz.timezone('US/Pacific'))) #basically get the time in the specified zone
#from here continue as above...

取自:http://www.saltycrane.com/blog/2009/05/converting-time-zones-datetime-objects-python/

祝你好运(下次尝试在google中搜索...)

实际上我最终结合了 hai tederry 的答案和在 http://www.saltycrane.com/blog/2008/06/how-to-get-current-date-and-time-in/ 上找到的答案 结果:

import time                             # import time library               
import datetime                         # this is the way to import a module named datetime...
import pytz                             # imports Python time zone (needed to first do: sudo easy_install --pytz upgrade) 
now = datetime.datetime.now(pytz.timezone('US/Pacific'))
timenow = now.strftime("%I:%M %p")
print timenow
if timenow > "08:30 PM" and timenow < "08:45 PM":
   print "Programmed Pause"
   time.sleep(999999)

这会以您当地的 12 小时格式创建一个时间输出,AM/PM 可以在 if 语句等中使用