You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/python/concepts/dates/terms/time/time.md
+47-4Lines changed: 47 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,24 +12,67 @@ CatalogContent:
12
12
- 'paths/computer-science'
13
13
---
14
14
15
-
The `datetime.time()` method returns a time in seconds that has passed since the epoch set on the computer.
15
+
The **`datetime.time()`** method returns a time in seconds that has passed since the epoch set on the computer. The [`.sleep()`](https://www.codecademy.com/resources/docs/python/time-module/sleep) function suspends execution of the current thread for the specified number of seconds.
The epoch is usually set as `January 1, 1970, 00:00:00 (UTC)` on most operating systems, excluding any leap seconds.
24
25
26
+
## Example
27
+
28
+
The following example demonstrates using `datetime.time()` to track when tea brewing starts and ends, with `.sleep()` creating a pause to simulate brewing time:
29
+
30
+
```py
31
+
import datetime
32
+
import time
33
+
34
+
# Create a simple timer for brewing tea
35
+
defbrew_tea():
36
+
# Get start time
37
+
start_time = datetime.datetime.now().time()
38
+
print(f"Starting to brew tea at: {start_time.strftime('%H:%M:%S')}")
39
+
40
+
print("Brewing...")
41
+
time.sleep(3) # Wait for 3 seconds
42
+
43
+
end_time = datetime.datetime.now().time()
44
+
print(f"Tea is ready at: {end_time.strftime('%H:%M:%S')}")
45
+
46
+
brew_tea()
47
+
```
48
+
49
+
The output of the above code will be:
50
+
51
+
```shell
52
+
Starting to brew tea at: 21:54:54
53
+
Brewing...
54
+
Tea is ready at: 21:54:57
55
+
```
56
+
57
+
> **Note:** Times shown will reflect system's current time.
58
+
25
59
## Codebyte Example
26
60
27
-
The time can be retrieved and stored in a variable as shown below:
61
+
Run the following code to get a better understanding of how `.time()` and `.sleep()` works:
0 commit comments