From a75bff67cfa9bd1d444b41c1953b921a2403ed58 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Wed, 6 Apr 2022 16:05:09 -0500 Subject: [PATCH] basic calculations on time, figuring hours clocked --- data.db | Bin 12288 -> 12288 bytes database.py | 14 ++++---- datetime_test.py | 32 ++++++++++-------- main.py | 4 +++ user_input.py | 86 +++++++++++++++++++++++++++++++++++++---------- 5 files changed, 99 insertions(+), 37 deletions(-) diff --git a/data.db b/data.db index f2bd1324848518d23d475cce9dd48d40e943d4ef..47c68a63a2554aa4d0c410f57e43cd1baef0983d 100644 GIT binary patch delta 185 zcmZojXh@hK&1f)D#+lJzW5PmyZvG7nO#FEa{CWKQHVY~w@z*;rvN1S13W^#T7#Zmr znCKdqDHxhsnHXD{nCe-Ynp&8eqsf?B85!#tnHicI80V&zWMmdIFfi}|F%&p3;V{qC hM9003K)COZHC delta 218 zcmZojXh@hK&8R(5#+gxjW5Pmyeg*~x7XCm6{)_yJ`K$Q@Hwy|l^4DvyurW9?h>98+ z7#ZmrnCKdqDHxhs8Jb%e8tEAun;00I6qcvvgRmMiNTskano2V(V>3M?BTHjbkV+uZ zVFIa>ltfc!YGrDnXKHF-U}2G&4}_Wd1wd#}pvwqSE{&<&&_vJD(8AQ%G&eIlu`(yI Npt2+*KW~wO007jIGOGXp diff --git a/database.py b/database.py index 466075c..29ed1cf 100644 --- a/database.py +++ b/database.py @@ -1,6 +1,6 @@ import sqlite3 from user_input import * -from datetime import datetime +from datetime import datetime, timedelta # establish a connection to a .db file and create a cursor object con = sqlite3.connect('data.db') @@ -50,12 +50,14 @@ def get_table_length(): table_rows.append(row) return len(table_rows) - -def get_stamp_by_date(): - pass - def get_weekly_hours(): - pass + current_time = datetime.now() + minus_week = current_time - timedelta(days = 7) + + return cur.execute(""" + SELECT time_in, time_out FROM TIMESTAMPS + WHERE time_in>:minus_week + """, {"minus_week": minus_week}) def get_all_hours(): pass diff --git a/datetime_test.py b/datetime_test.py index e59747e..85078aa 100644 --- a/datetime_test.py +++ b/datetime_test.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timedelta from time import time timestamp1 = "Feb 12 08:02:32 2014" @@ -7,17 +7,21 @@ timestamp2 = "Feb 21 11:52:02 2014" converted1 = datetime.strptime(timestamp1, "%b %d %H:%M:%S %Y") converted2 = datetime.strptime(timestamp2, "%b %d %H:%M:%S %Y") +# print(converted1) +# print(converted2) + +# difference = converted2 - converted1 +# days_extracted = difference.days + +# print(difference) + +# def days_to_minutes(days): +# hours = days * 24 +# minutes = hours * 60 +# return minutes + +# print(days_to_minutes(days_extracted)) + + print(converted1) -print(converted2) - -difference = converted2 - converted1 -days_extracted = difference.days - -print(difference) - -def days_to_minutes(days): - hours = days * 24 - minutes = hours * 60 - return minutes - -print(days_to_minutes(days_extracted)) \ No newline at end of file +print(converted1 - timedelta(days=7)) diff --git a/main.py b/main.py index ac685fb..0fe7310 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,8 @@ from user_input import * from database import * +print(""" +Welcome to the personal time stamp program. +This program is intended to help you keep track of your work hours on personal projects.""") + parse_input() diff --git a/user_input.py b/user_input.py index edd9cc4..6d73fb0 100644 --- a/user_input.py +++ b/user_input.py @@ -1,19 +1,19 @@ from database import * from datetime import * -user_prompt = """ -Welcome to the personal time stamp program. -This program is intended to help you keep track of your work hours on personal projects. +# String format for datetime string +# "%Y-%m-%d %H:%M:%S.%f" +user_prompt = """ Please choose from the following options: -1) Insert a new timestamp +1) New time punch 2) Select all timestamps (option: limit number of results) 3) Find a timestamp by session id 4) Calculate total hours for the week 5) Calculate complete sum of hours """ -# Inner functions detailed below: +# New time punch: def handle_first_option(): in_out = input("Punching in or out? i/o \n") @@ -23,8 +23,7 @@ def handle_first_option(): print("Please enter the following details:") name = input("Your name: ") purpose = input("Current task: ") - - print("\nPlease confirm your details:") + print("Please confirm your details:") confirmation = input(f'{name} clocking in at {time_of_stamp} for {purpose}. Confirm? y/n ') if confirmation == 'n': @@ -48,7 +47,7 @@ def handle_first_option(): elif in_out == 'o': time_of_stamp = datetime.now() print("Preparing to punch out.") - session = input("Please provide your session ID." ) + session = input("Please provide your session ID: ") punch_out(time_of_stamp, session) print("Punch out successful. Returning...") parse_input() @@ -56,7 +55,7 @@ def handle_first_option(): print("Invalid input. Please try again:") handle_first_option() - +# Select time stamps: def handle_second_option(): limit_results = input("Returning all timestamps. Limit results? y/n \n") if limit_results == 'n': @@ -64,6 +63,17 @@ def handle_second_option(): for row in output: print(row) + another_selection = input("Make another selection? y/n \n") + + if another_selection == 'y': + parse_input() + elif another_selection == 'n': + pass + else: + print("Make another selection:") + parse_input() + + elif limit_results == 'y': def find_row_limit(): user_limit = input("How many rows? \n") @@ -105,11 +115,51 @@ def handle_second_option(): print("Please provide a valid selection.") handle_second_option() - +# Find stamp by session id def handle_third_option(): - stamp = datetime.now() - array = test_select_new(stamp, 'mikayla', 'not python') - print(array) + user_id = input("Provide your session id: ") + response = get_created_id(user_id) + + print("Data fetched successfully:") + for data in response: + print(data) + + new_selection = input("Make another selection? y/n ") + if new_selection == 'y': + parse_input() + elif new_selection == 'n': + pass + else: + print("Invalid input.") + parse_input() + +# Calculate weekly hours +def handle_fourth_option(): + print("Find weekly hours.") + # target = input("Enter a target user name: ") + # print(f'Find weekly hours for {target}') + result = get_weekly_hours() + + total_hours = [] + for data in result: + inner_comparison = [] + for each in data: + inner_comparison.append(datetime.strptime(each, "%Y-%m-%d %H:%M:%S.%f")) + total_hours.append(inner_comparison[1] - inner_comparison[0]) + + print(total_hours) + + hours = 0 + minutes = 0 + total = 0 + for item in total_hours: + total += item.seconds + (item.microseconds / 10 ** 6) + + hours = total / (60 * 60) + minutes = total / 60 + print(f'Weekly hours: {round(hours, 3)}') + print(f'In minutes: {round(minutes, 3)}') + def __admin__(): print("ADMIN PORTAL") @@ -118,6 +168,7 @@ def __admin__(): 1) Delete all rows from table (table and schema will persist) 2) Drop table (database will reinitialize on next render) 3) Backup data from SQLite file. + 4) Populate with hard coded values Take care, some actions will permanently delete all data.\n """) @@ -131,6 +182,8 @@ def __admin__(): print("Drop table. Confirm?") elif int(admin_prompt) == 3: print("Back up all SQLite data.") + elif int(admin_prompt) == 4: + print("Populate hard coded data.") else: print("Invalid input.") @@ -142,6 +195,7 @@ def parse_input(): print(user_prompt) response = input("Enter your selection: ") + # Error handling try: response = int(response) except ValueError: @@ -151,12 +205,10 @@ def parse_input(): print("Please provide a valid input.") parse_input() - - if type(response) == int: print(f'You selected {response}. Working...') - + # Directing to secondary functions if response == 1: handle_first_option() elif response == 2: @@ -164,7 +216,7 @@ def parse_input(): elif response == 3: handle_third_option() elif response == 4: - pass + handle_fourth_option() elif response == 5: pass elif response == 90909: