只想知道我是否可以将变量设置为午夜时间。
我希望注销为raw_input,午夜为固定值
到目前为止,我有这个:#!/usr/bin/python
from datetime import datetime
from Tkinter import *
import math
#Variablesr
FMT = '%H%M' #Time Format
rate1 = 35.34 #Base Hourly Rate
rate2 = 35.34 #Base Hourly Rate
rate3 = 35.34 #Base Hourly Rate
rate4 = 35.34 #Base Hourly Rate
rate5 = 35.34 #Base Hourly Rate
rate6 = 50 #Base Hourly Rate
rate7 = 70 #Base Hourly Rate
Midnight = 0000,FMT
amp = 2.40 #Morning shift penalties
pmp = 2.50 #Afternoon shift penalties
ns = 4.4 #Night shift penalties
cabAll = 8.34 #Cab Allowance
signOnSun1 = raw_input("What time did you sign on Sunday: ");
signOffSun1 = raw_input("What time did you sign off Sunday: ");
diff = (datetime.strptime(signOffSun1, FMT) - datetime.strptime (Midnight, FMT))
print diff
答案 0 :(得分:0)
你没有看到使用signOnSun1
所以不完全确定你想要什么,但这应该更接近你想要的东西:
from datetime import datetime
#Variablesr
FMT = '%H:%M' #Time Format use HH:MM
rate1 = 35.34 #Base Hourly Rate
rate2 = 35.34 #Base Hourly Rate
rate3 = 35.34 #Base Hourly Rate
rate4 = 35.34 #Base Hourly Rate
rate5 = 35.34 #Base Hourly Rate
rate6 = 50 #Base Hourly Rate
rate7 = 70 #Base Hourly Rate
Midnight = "00:00" # make midnight a string
amp = 2.40 #Morning shift penalties
pmp = 2.50 #Afternoon shift penalties
ns = 4.4 #Night shift penalties
cabAll = 8.34 #Cab Allowance
# make sure user know format to use
signOnSun1 = raw_input("What time did you sign on Sunday, enter in format HH:MM: ")
signOffSun1 = raw_input("What time did you sign off Sunday, enter in format HH:MM: ")
# use Midnight string and FMT
diff = (datetime.strptime(signOffSun1, FMT) - datetime.strptime(Midnight,FMT))
print diff
实际上你应该确保用户使用try / except块输入正确的数据,如果你比较午夜之前和之后的时间你会被抓住
如果所有时间都是午夜,您可以将日期设置硬编码到午夜后的一天:
FMT = '%H:%M-%Y-%m-%d' #Time Format
rate1 = 35.34 #Base Hourly Rate
rate2 = 35.34 #Base Hourly Rate
rate3 = 35.34 #Base Hourly Rate
rate4 = 35.34 #Base Hourly Rate
rate5 = 35.34 #Base Hourly Rate
rate6 = 50 #Base Hourly Rate
rate7 = 70 #Base Hourly Rate
Midnight = "00:00-1900-01-02"
amp = 2.40 #Morning shift penalties
pmp = 2.50 #Afternoon shift penalties
ns = 4.4 #Night shift penalties
cabAll = 8.34 #Cab Allowance
signOnSun1 = raw_input("What time did you sign on Sunday, enter in format HH:MM: ")
signOffSun1 = raw_input("What time did you sign off Sunday, enter in format HH:MM: ")
diff = datetime.strptime(Midnight, FMT) - datetime.strptime("{}-1900-01-01".format(signOnSun1), FMT)
print diff
答案 1 :(得分:0)
如果您正在处理人们工作的小时数,那么您应该考虑DST转换(以及其他原因的UTC偏移的变化),否则结果可能是错误的(通常是一小时)。
正确找到午夜并非易事。见How do I get the UTC time of “midnight” for a given timezone?
要获得正确的结果,您应该指定除时间之外的日期,例如,假设最后一个星期日。您需要知道本地时区和(为了便携性)您需要一个历史时区数据库,例如pytz
模块提供的数据库。 tzlocal
模块可以找到您当地的时区:
from datetime import datetime, timedelta
from tzlocal import get_localzone # $ pip install tzlocal
from pytz import AmbiguousTimeError
DAY = timedelta(1)
local_timezone = get_localzone() # get pytz timezone
time_format = "%H:%M"
def asktime(prompt, format):
while True:
try:
return datetime.strptime(raw_input(prompt), format)
except ValueError:
print('Invalid time format, expected %s. Try again.' % format)
def disambiguate(time, date):
d = datetime.combine(date, time).replace(tzinfo=None)
try:
return local_timezone.localize(d, is_dst=None)
except AmbiguousTimeError:
is_dst = yes_or_no('Was it summer time (%s)?' % d)
return local_timezone.localize(d, is_dst=is_dst)
# allow NonExistentTimeError to propagate
# find last Sunday
sunday = datetime.now(local_timezone)
while sunday.weekday() != 6: # Sunday is 6
sunday -= DAY
# get 'sign on', 'sign off' times
#NOTE: assume, no 24h+ shifts
signon = asktime("What time did you sign on Sunday: ", time_format)
signoff = asktime("What time did you sign off Sunday: ", time_format)
if signoff < signon: # signon is a day before (Saturday)
signon = disambiguate(signon, sunday.date() - DAY)
signoff = disambiguate(signoff, sunday)
print("Signon time %s" % signon)
print("Signoff time %s" % signoff)
diff = signoff - signon
print("The difference %s" % diff)
如果&#39;登录&#39;,&#39;注销&#39;时间是在DST过渡期间(&#34;后退&#34;)然后相同的本地时间可能发生两次。然后(除了约会)你需要知道这是否是一个夏天(&#39;登录&#39;,&#39;签署&#39;),以使时间明确。
yes_or_no()
是一个小的效用函数:
def yes_or_no(prompt):
while True:
answer = raw_input(prompt)
if answer.lower() in {'yes', 'no'}:
return answer == 'yes'
print("Please, answer 'yes' or 'no'. Try again.")
如果您不需要处理当地时区,那么一切都会简单得多,您可以要求UTC时间:
signon = asktime("What UTC time did you sign on Sunday: ", time_format)
signoff = asktime("What UTC time did you sign off Sunday: ", time_format)
# support 24h+ shifts
while signoff < signon: # signon is on a previous date
signon -= DAY
diff = signoff - signon
print(diff)
这就是为什么建议使用UTC时间而不是本地时间(如果可以请求人们提供UTC时间)。
如果您无法安装tzlocal
模块并且无法使用UTC时间,那么您可以使用time.mktime()
来消除当地时间的歧义(它可能不如上述方法可靠使用tz数据库),请参阅my answer to "Find if 24 hrs have passed between datetimes - Python"。