如何在 python 中设置一天开始的自定义时间?
How to set a custom time for the start of a day in python?
我希望一天从 06:30 开始,在 15:00 结束。我在工作日而不是全天 24 小时使用它。
我用它来计算完成一项工作所花费的时间。如果结束时间在 11:30.
之后,午餐减去 30 分钟
或者,如果有人比我更擅长数学,可以弄清楚如何用已经写的东西计算出来
import PySimpleGUI as sg
import datetime as dt
startRow = sg.InputText('', size=(3,1), key='startMonth'), sg.T('/'), sg.InputText('', size=(3,1), key='startDay'), sg.Text(' '*5), sg.InputText('', size=(3,1), key='startHour'), sg.T(':'), sg.InputText('', size=(3,1), key='startMinute')
endRow = sg.InputText('', size=(3,1), key='endMonth') , sg.T('/'), sg.InputText('', size=(3,1), key='endDay'),sg.Text(' '*5), sg.InputText('', size=(3,1), key='endHour'), sg.T(':'), sg.InputText('', size=(2,1), key='endMinute')
layout = [
[sg.T('Enter the times you wish to calculate')],
[sg.T('(Uses 24-Hour format)')],
[sg.T('Enter a start Date and Time', size=(30,1))],
[startRow],
[sg.T('Enter an end Date and Time', size=(30,1))],
[endRow],
[sg.Text(' '*50, key='result')],
[sg.Button('Submit'), sg.CButton('Cancel')]
]
window = sg.Window('Process Time Calculator', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
break
startMonth = int(values['startMonth'])
startDay = int(values['startDay'])
startHour = int(values['startHour'])
startMinute = int(values['startMinute'])
yr = dt.datetime.now()
endMonth = int(values['endMonth'])
endDay = int(values['endDay'])
endHour = int(values['endHour'])
endMinute = int(values['endMinute'])
startTime = dt.timedelta(hours=startHour, minutes=startMinute)
endTime = dt.timedelta(hours=endHour, minutes=endMinute)
startDate = dt.datetime(year=yr.year, month=startMonth, day=startDay)
endDate = dt.datetime(year=yr.year, month=endMonth, day=endDay)
startDateTime = dt.datetime(year=yr.year, month=startMonth, day=startDay) + startTime
endDateTime = dt.datetime(year=yr.year, month=endMonth, day=endDay) + endTime
timeCalc = endDateTime - startDateTime
result = timeCalc.total_seconds()
finalResult = float(result / 3600.0)
if startDate != endDate:
if finalResult >= 24:
finalResult = finalResult - 16.5
elif startDate == endDate and endTime > dt.datetime(hour=11, minute=30):
finalResult = finalResult - .5
print("Final Result: " + str(finalResult) + "\nDone.")
print('Start date: ' + str(startDateTime))
print('End date: ' + str(endDateTime))
window['result'].update(finalResult)
window.refresh()
认为这就是您想要的。没有尝试“第二天”的事情,所以你必须测试几次并让我们知道它是否仍然需要调整。但无论哪种方式,这都应该朝着正确的方向发展。
#! /usr/bin/env python3
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import PySimpleGUI as sg
import datetime as dt
## pip3 install pysimplegui
## print( help( sg ) ) ## was looking for a way to increase fontsize...
now = dt.datetime.now()
startRow = sg.InputText( now.month, size=( 3, 1 ), key='startMonth' ), \
sg.T( '/' ), sg.InputText( now.day, size=( 3, 1 ), key='startDay' ), \
sg.Text( 'Start' ), sg.InputText( '6', size=( 3, 1 ), key='startHour' ), \
sg.T( ':' ), sg.InputText( '30', size=( 3, 1 ), key='startMinute' )
endRow = sg.InputText( now.month, size=( 3, 1 ), key='endMonth' ), \
sg.T( '/' ), sg.InputText( now.day, size=( 3, 1 ), key='endDay' ), \
sg.Text( ' End ' ), sg.InputText( '15', size=( 3, 1 ), key='endHour' ), \
sg.T( ':' ), sg.InputText( '00', size=( 2, 1 ), key='endMinute' )
layout = [
[ sg.T( 'Enter the times you wish to calculate' ) ],
[ sg.T( '( Uses 24-Hour format )' ) ],
[ startRow ],
[ sg.T( 'MM / DD ~ ~ ~ ~ HH : mm ', size=( 30, 1 ) ) ],
[ endRow ],
[ sg.Text( ' ' *50, key='result' ) ],
[ sg.Button( 'Submit' ), sg.CButton( 'Cancel' ) ]
]
window = sg.Window( 'Process Time Calculator', layout )
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel': break ## if user closes window or clicks cancel
startMonth = int( values[ 'startMonth' ] )
startDay = int( values[ 'startDay' ] )
startHour = int( values[ 'startHour' ] )
startMinute = int( values[ 'startMinute' ] )
now = dt.datetime.now()
endMonth = int( values[ 'endMonth' ] )
endDay = int( values[ 'endDay' ] )
endHour = int( values[ 'endHour' ] )
endMinute = int( values[ 'endMinute' ] )
startTime = dt.timedelta( hours=startHour, minutes=startMinute )
endTime = dt.timedelta( hours=endHour, minutes=endMinute )
startDate = dt.datetime( year=now.year, month=startMonth, day=startDay )
endDate = dt.datetime( year=now.year, month=endMonth, day=endDay )
startDateTime = dt.datetime( year=now.year, month=startMonth, day=startDay ) +startTime
endDateTime = dt.datetime( year=now.year, month=endMonth, day=endDay ) +endTime
timeCalc = endDateTime -startDateTime
result = timeCalc.total_seconds()
finalResult = float( result /3600.0 )
if startDate != endDate:
if finalResult >= 24:
finalResult -= 16.5
elif startDate == endDate and endHour > 12 or ( endHour == 11 and endMinute >= 30 ):
finalResult = f'{finalResult -0.5} w/ lunch'
print( f'Final Result: {finalResult}\nDone.' )
print( f'Start date: {startDateTime}' )
print( f'End date: {endDateTime}\n' )
window[ 'result' ].update( finalResult )
window.refresh()
## eof ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我花了很长时间才把数学写出来,让它对我有意义。我终于把它带到了现在可以工作的地方。超过 1 天,超过 24 小时,超过 48 小时(如周末)的计算器。
import PySimpleGUI as sg
import datetime as dt
now = dt.datetime.now()
startRow = sg.InputText('', size=(3,1), key='startMonth'), sg.T('/'), sg.InputText('', size=(3,1), key='startDay'), sg.Text(' '*5), sg.InputText('', size=(3,1), key='startHour'), sg.T(':'), sg.InputText('', size=(3,1), key='startMinute')
endRow = sg.InputText('', size=(3,1), key='endMonth') , sg.T('/'), sg.InputText('', size=(3,1), key='endDay'),sg.Text(' '*5), sg.InputText('', size=(3,1), key='endHour'), sg.T(':'), sg.InputText('', size=(2,1), key='endMinute')
layout = [
[sg.T('Enter the times you wish to calculate')],
[sg.T('(Uses 24-Hour format)')],
[sg.T('Enter a start Date and Time', size=(30,1))],
[startRow],
[sg.T('Enter an end Date and Time', size=(30,1))],
[endRow],
[sg.Text(' '*50, key='result')],
[sg.Button('Submit'), sg.CButton('Cancel')]
]
window = sg.Window('Process Time Calculator', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
break
now = dt.datetime.now()
#Start Date Values
startMonth = int(values['startMonth'])
startDay = int(values['startDay'])
startHour = int(values['startHour'])
startMinute = int(values['startMinute'])
startTime = dt.timedelta(hours=startHour, minutes=startMinute)
startDate = dt.datetime(year=now.year, month=startMonth, day=startDay)
startDateTime = dt.datetime(year=now.year, month=startMonth, day=startDay) + startTime
#End Date Values
endMonth = int(values['endMonth'])
endDay = int(values['endDay'])
endHour = int(values['endHour'])
endMinute = int(values['endMinute'])
endTime = dt.timedelta(hours=endHour, minutes=endMinute)
endDate = dt.datetime(year=now.year, month=endMonth, day=endDay)
endDateTime = dt.datetime(year=now.year, month=endMonth, day=endDay) + endTime
#The difference in time between the entered dates and times
timeCalc = endDateTime - startDateTime
#Convert the difference in time to seconds.
result = timeCalc.total_seconds()
#Divide by the amount of seconds in an hour.
finalResult = float(result / 3600.0)
if startDate != endDate:
if finalResult > 48:
if endHour > 12 or ( endHour >= 11 and endMinute >= 30 ):
finalResult -= 0.5
finalResult -= 64
#This is for time over a 2 day weekend, no lunch on weekends so the day is 24 hours not 23.5. 24 + 24 + 15.5 (-.5 for lunch) The third level if statement accounts for the following monday's lunch break.
elif finalResult >= 24:
if endHour > 12 or ( endHour >= 11 and endMinute >= 30 ):
finalResult -= 0.5
finalResult -= 16
# 1500 to 06:30 is 15.5 hours with a 30 minute lunch is 16 hours.
elif startDate == endDate and endHour > 12 or ( endHour >= 11 and endMinute >= 30 ):
finalResult -= 0.5
print(f'Final Result: {finalResult} \nDone')
print(f'Start date: {startDateTime} ' )
print(f'End date: {endDateTime}')
window['result'].update(finalResult)
window.refresh()
我希望一天从 06:30 开始,在 15:00 结束。我在工作日而不是全天 24 小时使用它。
我用它来计算完成一项工作所花费的时间。如果结束时间在 11:30.
之后,午餐减去 30 分钟或者,如果有人比我更擅长数学,可以弄清楚如何用已经写的东西计算出来
import PySimpleGUI as sg
import datetime as dt
startRow = sg.InputText('', size=(3,1), key='startMonth'), sg.T('/'), sg.InputText('', size=(3,1), key='startDay'), sg.Text(' '*5), sg.InputText('', size=(3,1), key='startHour'), sg.T(':'), sg.InputText('', size=(3,1), key='startMinute')
endRow = sg.InputText('', size=(3,1), key='endMonth') , sg.T('/'), sg.InputText('', size=(3,1), key='endDay'),sg.Text(' '*5), sg.InputText('', size=(3,1), key='endHour'), sg.T(':'), sg.InputText('', size=(2,1), key='endMinute')
layout = [
[sg.T('Enter the times you wish to calculate')],
[sg.T('(Uses 24-Hour format)')],
[sg.T('Enter a start Date and Time', size=(30,1))],
[startRow],
[sg.T('Enter an end Date and Time', size=(30,1))],
[endRow],
[sg.Text(' '*50, key='result')],
[sg.Button('Submit'), sg.CButton('Cancel')]
]
window = sg.Window('Process Time Calculator', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
break
startMonth = int(values['startMonth'])
startDay = int(values['startDay'])
startHour = int(values['startHour'])
startMinute = int(values['startMinute'])
yr = dt.datetime.now()
endMonth = int(values['endMonth'])
endDay = int(values['endDay'])
endHour = int(values['endHour'])
endMinute = int(values['endMinute'])
startTime = dt.timedelta(hours=startHour, minutes=startMinute)
endTime = dt.timedelta(hours=endHour, minutes=endMinute)
startDate = dt.datetime(year=yr.year, month=startMonth, day=startDay)
endDate = dt.datetime(year=yr.year, month=endMonth, day=endDay)
startDateTime = dt.datetime(year=yr.year, month=startMonth, day=startDay) + startTime
endDateTime = dt.datetime(year=yr.year, month=endMonth, day=endDay) + endTime
timeCalc = endDateTime - startDateTime
result = timeCalc.total_seconds()
finalResult = float(result / 3600.0)
if startDate != endDate:
if finalResult >= 24:
finalResult = finalResult - 16.5
elif startDate == endDate and endTime > dt.datetime(hour=11, minute=30):
finalResult = finalResult - .5
print("Final Result: " + str(finalResult) + "\nDone.")
print('Start date: ' + str(startDateTime))
print('End date: ' + str(endDateTime))
window['result'].update(finalResult)
window.refresh()
认为这就是您想要的。没有尝试“第二天”的事情,所以你必须测试几次并让我们知道它是否仍然需要调整。但无论哪种方式,这都应该朝着正确的方向发展。
#! /usr/bin/env python3
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import PySimpleGUI as sg
import datetime as dt
## pip3 install pysimplegui
## print( help( sg ) ) ## was looking for a way to increase fontsize...
now = dt.datetime.now()
startRow = sg.InputText( now.month, size=( 3, 1 ), key='startMonth' ), \
sg.T( '/' ), sg.InputText( now.day, size=( 3, 1 ), key='startDay' ), \
sg.Text( 'Start' ), sg.InputText( '6', size=( 3, 1 ), key='startHour' ), \
sg.T( ':' ), sg.InputText( '30', size=( 3, 1 ), key='startMinute' )
endRow = sg.InputText( now.month, size=( 3, 1 ), key='endMonth' ), \
sg.T( '/' ), sg.InputText( now.day, size=( 3, 1 ), key='endDay' ), \
sg.Text( ' End ' ), sg.InputText( '15', size=( 3, 1 ), key='endHour' ), \
sg.T( ':' ), sg.InputText( '00', size=( 2, 1 ), key='endMinute' )
layout = [
[ sg.T( 'Enter the times you wish to calculate' ) ],
[ sg.T( '( Uses 24-Hour format )' ) ],
[ startRow ],
[ sg.T( 'MM / DD ~ ~ ~ ~ HH : mm ', size=( 30, 1 ) ) ],
[ endRow ],
[ sg.Text( ' ' *50, key='result' ) ],
[ sg.Button( 'Submit' ), sg.CButton( 'Cancel' ) ]
]
window = sg.Window( 'Process Time Calculator', layout )
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel': break ## if user closes window or clicks cancel
startMonth = int( values[ 'startMonth' ] )
startDay = int( values[ 'startDay' ] )
startHour = int( values[ 'startHour' ] )
startMinute = int( values[ 'startMinute' ] )
now = dt.datetime.now()
endMonth = int( values[ 'endMonth' ] )
endDay = int( values[ 'endDay' ] )
endHour = int( values[ 'endHour' ] )
endMinute = int( values[ 'endMinute' ] )
startTime = dt.timedelta( hours=startHour, minutes=startMinute )
endTime = dt.timedelta( hours=endHour, minutes=endMinute )
startDate = dt.datetime( year=now.year, month=startMonth, day=startDay )
endDate = dt.datetime( year=now.year, month=endMonth, day=endDay )
startDateTime = dt.datetime( year=now.year, month=startMonth, day=startDay ) +startTime
endDateTime = dt.datetime( year=now.year, month=endMonth, day=endDay ) +endTime
timeCalc = endDateTime -startDateTime
result = timeCalc.total_seconds()
finalResult = float( result /3600.0 )
if startDate != endDate:
if finalResult >= 24:
finalResult -= 16.5
elif startDate == endDate and endHour > 12 or ( endHour == 11 and endMinute >= 30 ):
finalResult = f'{finalResult -0.5} w/ lunch'
print( f'Final Result: {finalResult}\nDone.' )
print( f'Start date: {startDateTime}' )
print( f'End date: {endDateTime}\n' )
window[ 'result' ].update( finalResult )
window.refresh()
## eof ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我花了很长时间才把数学写出来,让它对我有意义。我终于把它带到了现在可以工作的地方。超过 1 天,超过 24 小时,超过 48 小时(如周末)的计算器。
import PySimpleGUI as sg
import datetime as dt
now = dt.datetime.now()
startRow = sg.InputText('', size=(3,1), key='startMonth'), sg.T('/'), sg.InputText('', size=(3,1), key='startDay'), sg.Text(' '*5), sg.InputText('', size=(3,1), key='startHour'), sg.T(':'), sg.InputText('', size=(3,1), key='startMinute')
endRow = sg.InputText('', size=(3,1), key='endMonth') , sg.T('/'), sg.InputText('', size=(3,1), key='endDay'),sg.Text(' '*5), sg.InputText('', size=(3,1), key='endHour'), sg.T(':'), sg.InputText('', size=(2,1), key='endMinute')
layout = [
[sg.T('Enter the times you wish to calculate')],
[sg.T('(Uses 24-Hour format)')],
[sg.T('Enter a start Date and Time', size=(30,1))],
[startRow],
[sg.T('Enter an end Date and Time', size=(30,1))],
[endRow],
[sg.Text(' '*50, key='result')],
[sg.Button('Submit'), sg.CButton('Cancel')]
]
window = sg.Window('Process Time Calculator', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
break
now = dt.datetime.now()
#Start Date Values
startMonth = int(values['startMonth'])
startDay = int(values['startDay'])
startHour = int(values['startHour'])
startMinute = int(values['startMinute'])
startTime = dt.timedelta(hours=startHour, minutes=startMinute)
startDate = dt.datetime(year=now.year, month=startMonth, day=startDay)
startDateTime = dt.datetime(year=now.year, month=startMonth, day=startDay) + startTime
#End Date Values
endMonth = int(values['endMonth'])
endDay = int(values['endDay'])
endHour = int(values['endHour'])
endMinute = int(values['endMinute'])
endTime = dt.timedelta(hours=endHour, minutes=endMinute)
endDate = dt.datetime(year=now.year, month=endMonth, day=endDay)
endDateTime = dt.datetime(year=now.year, month=endMonth, day=endDay) + endTime
#The difference in time between the entered dates and times
timeCalc = endDateTime - startDateTime
#Convert the difference in time to seconds.
result = timeCalc.total_seconds()
#Divide by the amount of seconds in an hour.
finalResult = float(result / 3600.0)
if startDate != endDate:
if finalResult > 48:
if endHour > 12 or ( endHour >= 11 and endMinute >= 30 ):
finalResult -= 0.5
finalResult -= 64
#This is for time over a 2 day weekend, no lunch on weekends so the day is 24 hours not 23.5. 24 + 24 + 15.5 (-.5 for lunch) The third level if statement accounts for the following monday's lunch break.
elif finalResult >= 24:
if endHour > 12 or ( endHour >= 11 and endMinute >= 30 ):
finalResult -= 0.5
finalResult -= 16
# 1500 to 06:30 is 15.5 hours with a 30 minute lunch is 16 hours.
elif startDate == endDate and endHour > 12 or ( endHour >= 11 and endMinute >= 30 ):
finalResult -= 0.5
print(f'Final Result: {finalResult} \nDone')
print(f'Start date: {startDateTime} ' )
print(f'End date: {endDateTime}')
window['result'].update(finalResult)
window.refresh()