Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

python - Calculating time in AM and PM format

I am developing a calculator that allows users to calculate the amount of hours worked for the day by giving me their start and end time. I keep getting the following error:

Traceback (most recent call last):
  File "C:/Users/jbtai/PycharmProjects/worktimecalculator/test.py", line 52, in <module>
    print(Monday())          #Monday Main Function
  File "C:/Users/jbtai/PycharmProjects/worktimecalculator/test.py", line 31, in Monday
    start_time = datetime.datetime.strptime(start_time, '%I:%M %p')  # converting starttime to datetime object
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.8_3.8.2032.0_x64__qbz5n2kfra8p0lib\_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.8_3.8.2032.0_x64__qbz5n2kfra8p0lib\_strptime.py", line 352, in _strptime
    raise ValueError("unconverted data remains: %s" %
ValueError: unconverted data remains:

Does anyone know why this is happening? I am basically trying to convert the start_time and the end_time to datetime using AM and PM and then subtract the breaktime from it. All of my code is bellow.

Code:

import datetime


def start():
    print('33[1m' + 'Welcome to WorkTime Calculator!
')
    firstInputMessage = input('Would you like to calculate your time worked for the week? ')
    if firstInputMessage == 'yes' or 'y' or 'ye' or 'yee':
        print('Loading... Please Wait
')
    elif firstInputMessage != 'yes' or 'y' or 'ye' or 'yee':  # start
        quit()


def workhourscalculator(start_time, end_time):
    '''
    start_time and end_time should be datetime objects
    '''
    return (end_time - start_time).seconds  # time delta object


def Monday():
    start()    #start function
    mondayDate = input(str('Enter Monday''s date! ')) # Date '''Use later for csv file

    start_time = input(str(
        'Enter the time you started on Monday separated by a colon with AM/PM. If you did not work on Monday, please enter 0. '))
    end_time = input(str(
        'Enter the time you stopped working on Monday separated by a colon with AM/PM. If you did not work on Monday, please enter 0. '))
    result = '0'
    if start_time != 0 and end_time != 0:
        start_time = datetime.datetime.strptime(start_time, '%I:%M %p')  # converting starttime to datetime object
        end_time = datetime.datetime.strptime(end_time, '%I:%M %p')  # example 01:00 PM
        difference_in_seconds = workhourscalculator(start_time, end_time)
        result_hours = str(difference_in_seconds // 3600).zfill(2)
        result_minutes = str((difference_in_seconds % 3600) // 60).zfill(2)
        result = f'{result_hours}:{result_minutes}'

        breakTime = input('Enter your break time for Monday. ')
        breakTime = result - breakTime
        print(breakTime)
        #User friendly result calculation
        user_friendly_result = 'On Monday, you worked for'
        for x in [(result_hours, 'hours'), (result_minutes, 'minutes')]:
            if int(x[0]) != 0:
                user_friendly_result += f' {int(x[0])} {x[1]}'
        user_friendly_result += '.'
        return user_friendly_result
    return result


if __name__ == '__main__':
    print(Monday())          #Monday Main Function
question from:https://stackoverflow.com/questions/65928435/calculating-time-in-am-and-pm-format

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...