-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.py
More file actions
52 lines (41 loc) · 1.69 KB
/
time.py
File metadata and controls
52 lines (41 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sqlite3
import datetime
# Connect to the SQLite database (or create it if it doesn't exist)
conn = sqlite3.connect('testtime.db')
cursor = conn.cursor()
# Create a table if it doesn't exist
cursor.execute('''CREATE TABLE IF NOT EXISTS user_sessions
(id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT,
start_time DATETIME,
end_time DATETIME,
total_time INTEGER)''')
# Function to calculate total time difference
# class TimeFunction:
def calculate_total_time(start_time, end_time):
duration = end_time - start_time
total_seconds = duration.total_seconds()
total_minutes = int(total_seconds / 60)
return total_minutes
# Function to log in user
def log_in_user(username):
return datetime.datetime.now()
# Function to log off user and calculate total time spent
def log_off_user(username, start_time):
end_time = datetime.datetime.now()
total_time = calculate_total_time(start_time, end_time)
#return total_time
# Insert the user session details into the database
cursor.execute("INSERT INTO user_sessions (username, start_time, end_time, total_time) VALUES (?, ?, ?, ?)",
(username, start_time, end_time, total_time))
# Commit the changes
conn.commit()
# Print a message to confirm successful log off
print(f"User '{username}' logged off. Total time spent: {total_time} minutes.")
# Example usage
username = "John" # Replace with the actual username
start_time = log_in_user(username)
# Perform user activities...
log_off_user(username, start_time)
# Close the database connection
conn.close()