-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeT.py
More file actions
77 lines (44 loc) · 3.51 KB
/
Copy pathtimeT.py
File metadata and controls
77 lines (44 loc) · 3.51 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# ===================== SUMMARY OF time MODULE =====================
# time module is used to work with time-related operations in Python.
# time.time() gives current time in seconds from 1 Jan 1970.
# time.ctime() converts seconds into readable date and time format.
# time.sleep() stops the program for a few seconds.
# time.localtime() gives current local time in structured format.
# time.strftime() formats date/time into our required format.
# time.strptime() converts string date/time into time structure.
# time.perf_counter() is used to measure execution time accurately.
# ================================================================
import time # importing the time module
current_seconds = time.time() # getting current time in seconds from 1 Jan 1970
print(current_seconds) # printing current time in seconds
readable_time = time.ctime(current_seconds) # converting seconds into readable date and time
print(readable_time) # printing readable date and time
print(time.ctime()) # directly printing current readable date and time
local_time = time.localtime() # getting current local time as a structured object
print(local_time) # printing complete local time structure
print(local_time.tm_year) # printing current year
print(local_time.tm_mon) # printing current month number
print(local_time.tm_mday) # printing current day of the month
print(local_time.tm_hour) # printing current hour
print(local_time.tm_min) # printing current minute
print(local_time.tm_sec) # printing current second
formatted_date = time.strftime("%d-%m-%Y") # formatting current date as day-month-year
print(formatted_date) # printing formatted date
formatted_time = time.strftime("%H:%M:%S") # formatting current time as hour-minute-second
print(formatted_time) # printing formatted time
formatted_full = time.strftime("%d-%b-%Y %I:%M:%S %p") # formatting date and time in readable format
print(formatted_full) # printing formatted full date and time
date_string = "28-Jun-2026" # creating a date string
converted_date = time.strptime(date_string, "%d-%b-%Y") # converting string date into time structure
print(converted_date) # printing converted time structure
seconds_from_date = time.mktime(converted_date) # converting time structure into seconds
print(seconds_from_date) # printing seconds value of given date
print("Program starts") # printing starting message
time.sleep(2) # stopping program execution for 2 seconds
print("Program ends after 2 seconds") # printing message after delay
start = time.perf_counter() # storing start time for measuring execution time
for i in range(1000000): # running loop one million times
pass # doing nothing inside loop
end = time.perf_counter() # storing end time after loop finishes
execution_time = end - start # calculating total execution time
print("Execution time:", execution_time) # printing execution time