# 46. Python's time Module

Here are 10 Python code snippets that demonstrate the use of the `time` module, covering various time-related functionalities such as sleeping, measuring elapsed time, formatting time, and more.

***

**1. Sleeping for a Specified Time**

Pausing the program execution for a specified number of seconds.

```python
import time

print("Program starts")
time.sleep(3)  # Pauses for 3 seconds
print("Program resumes after 3 seconds")
```

This will make the program pause for 3 seconds before resuming execution.

***

**2. Measuring Elapsed Time with time.time()**

Using `time.time()` to measure the elapsed time between two points in the program.

```python
import time

start_time = time.time()

# Some time-consuming task
time.sleep(2)

end_time = time.time()
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time} seconds")
```

This example measures the time taken for a sleep period of 2 seconds.

***

**3. Formatting Current Time with time.strftime()**

Formatting the current time as a human-readable string.

```python
import time

current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print("Current time:", current_time)
```

This will print the current date and time in the format `YYYY-MM-DD HH:MM:SS`.

***

**4. Converting Time to Seconds with time.mktime()**

Converting a `struct_time` to seconds since the epoch.

```python
import time

time_tuple = time.localtime()  # Current local time as struct_time
seconds_since_epoch = time.mktime(time_tuple)
print(f"Seconds since epoch: {seconds_since_epoch}")
```

This converts the current time to the number of seconds since the Unix epoch.

***

**5. Getting the Current Time in Epoch Format**

Getting the current time in seconds since the Unix epoch.

```python
import time

epoch_time = time.time()
print(f"Current epoch time: {epoch_time}")
```

This prints the current time in seconds since January 1, 1970.

***

**6. Pausing Program for User Input Delay**

Pausing the program until the user presses a key.

```python
import time

print("Press Enter to continue...")
time.sleep(2)
input("Press Enter to continue after delay.")
```

This introduces a delay before accepting user input.

***

**7. Time-based Loop (Periodic Tasks)**

Running a periodic task every 2 seconds.

```python
import time

for i in range(5):
    print(f"Task {i+1} started")
    time.sleep(2)  # Sleep for 2 seconds between tasks
```

This example runs a task every 2 seconds.

***

**8. Get the Time in Different Time Zones**

Using `time` and `pytz` module (if installed) to get the time in different time zones.

```python
import time
import pytz

timezone = pytz.timezone("US/Eastern")
eastern_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f"Time in US Eastern: {eastern_time}")
```

This shows how you could use `time` with `pytz` for time zone handling.

***

**9. Calculating Execution Time Using time.perf\_counter()**

Using `time.perf_counter()` for precise time measurements.

```python
import time

start_time = time.perf_counter()

# Simulate some task
time.sleep(1.5)

end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time} seconds")
```

`time.perf_counter()` provides a high-resolution timer to measure short durations accurately.

***

**10. Getting the Current Time in UTC**

Getting the current time in UTC format.

```python
import time

utc_time = time.gmtime()
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", utc_time)
print(f"Current UTC time: {formatted_time}")
```

This will print the current time in UTC, formatted as `YYYY-MM-DD HH:MM:SS`.

***

These examples showcase how you can work with time-related functionality in Python, including sleeping, measuring time intervals, formatting time, and working with different time zones.
