Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Time Functions in Python?
Python provides a comprehensive library to read, represent and manipulate time information through the time module. Date, time and datetime are objects in Python, so whenever we perform operations on them, we work with objects rather than strings or timestamps.
The time module follows the EPOCH convention, which refers to the starting point for time calculations. In Unix systems, EPOCH time started from January 1, 1970, 12:00 AM UTC.
Understanding EPOCH Time
To determine the EPOCH time value on your system ?
import time print(time.gmtime(0))
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
Time Ticks
A tick refers to a time interval represented as a floating-point number measured in seconds. Time calculations may involve Daylight Saving Time (DST), where clocks move forward one hour during summer and back again in fall.
Essential Time Functions
time.time() Function
The time.time() function measures the number of seconds since the epoch as a floating-point value ?
import time
print("Number of seconds elapsed since the epoch:", time.time())
Number of seconds elapsed since the epoch: 1553262407.0398576
Measuring Execution Time
You can calculate elapsed wall-clock time between two points ?
import time
start = time.time()
print("Time elapsed on working...")
time.sleep(0.9)
end = time.time()
print("Time consumed in working:", end - start)
Time elapsed on working... Time consumed in working: 0.9219651222229004
time.ctime() Function
The time.ctime() function converts seconds since epoch into a human-readable string. If no argument is passed, it returns the current time ?
import time
print('The current local time is:', time.ctime())
newtime = time.time() + 60
print('60 secs from now:', time.ctime(newtime))
The current local time is: Fri Mar 22 19:43:11 2019 60 secs from now: Fri Mar 22 19:44:11 2019
time.sleep() Function
The time.sleep() function halts execution for a specified number of seconds. It's useful when waiting for file operations or database commits ?
import time
print("Time starts from:", time.ctime())
print('Waiting for 5 sec.')
time.sleep(5)
print("Time ends at:", time.ctime())
Time starts from: Fri Mar 22 20:00:00 2019 Waiting for 5 sec. Time ends at: Fri Mar 22 20:00:05 2019
Working with struct_time
The time.struct_time class provides access to specific fields of a date through named attributes ?
import time
print('Current local time:', time.ctime())
t = time.localtime()
print('Day of month:', t.tm_mday)
print('Day of week:', t.tm_wday)
print('Day of year:', t.tm_yday)
Current local time: Fri Mar 22 20:10:25 2019 Day of month: 22 Day of week: 4 Day of year: 81
Formatting Time with strftime()
The time.strftime() function converts time tuples or struct_time objects to formatted strings ?
import time
now = time.localtime(time.time())
print("Current date time:", time.asctime(now))
print(time.strftime("%y/%m/%d %H:%M", now))
print(time.strftime("%a %b %d", now))
print(time.strftime("%c", now))
print(time.strftime("%I %p", now))
print(time.strftime("%Y-%m-%d %H:%M:%S", now))
Current date time: Fri Mar 22 20:13:43 2019 19/03/22 20:13 Fri Mar 22 Fri Mar 22 20:13:43 2019 08 PM 2019-03-22 20:13:43
Timezone Information
Python provides two properties for timezone information ?
import time
print("Timezone offset:", time.timezone)
print("Timezone names:", time.tzname)
Timezone offset: -19800
Timezone names: ('India Standard Time', 'India Daylight Time')
Conclusion
Python's time module provides essential functions for time manipulation, from basic epoch calculations to formatted string outputs. Use time.time() for timestamps, sleep() for delays, and strftime() for custom formatting.
