Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Mastering Python’s time Module: Functions, Structs, and Practical Examples

Python time Module

Explore the standard time module in Python, learn its core functions, and see clear examples that demonstrate real-world usage.

Python’s time module supplies tools for working with timestamps, scheduling, and measuring elapsed time. Before using any function, import the module:

import time

time.time()

The time() function returns the current time in seconds since the Unix epoch (00:00:00 UTC on 1 January 1970). On Unix-like systems this epoch value is fixed; on Windows it is derived from the same point.

import time
seconds = time.time()
print("Seconds since epoch =", seconds)

time.ctime()

Converts a POSIX timestamp (seconds since epoch) into a readable string in the local time zone.

import time
seconds = 1545925769.9618232
local_time = time.ctime(seconds)
print("Local time:", local_time)

Typical output:

Local time: Thu Dec 27 15:49:29 2018

time.sleep()

Delays the execution of the current thread for the specified number of seconds.

import time
print("This is printed immediately.")
time.sleep(2.4)
print("This is printed after 2.4 seconds.")

For more details, see the Python sleep() documentation.


time.struct_time Class

Functions like gmtime() and asctime() return or accept a time.struct_time object, a 9‑tuple representing a calendar time.

time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27,
                 tm_hour=6, tm_min=35, tm_sec=17,
                 tm_wday=3, tm_yday=361, tm_isdst=0)
IndexAttributeValues
0tm_year0000 … 9999
1tm_mon1 … 12
2tm_mday1 … 31
3tm_hour0 … 23
4tm_min0 … 59
5tm_sec0 … 61
6tm_wday0 (Mon) … 6 (Sun)
7tm_yday1 … 366
8tm_isdst0, 1 or -1

You can access elements either by index or by attribute name.


time.localtime()

Converts a POSIX timestamp into a struct_time expressed in the local time zone. If omitted, the current time is used.

import time
result = time.localtime(1545925769)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)

Typical output:

result: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27,
tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3,
tm_yday=361, tm_isdst=0)

year: 2018
tm_hour: 15

time.gmtime()

Same as localtime() but returns UTC (GMT) time. Omitted argument defaults to the current epoch value.

import time
result = time.gmtime(1545925769)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)

Typical output:

result: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=28,
tm_hour=8, tm_min=44, tm_sec=4, tm_wday=4,
tm_yday=362, tm_isdst=0)

year: 2018
tm_hour: 8

time.mktime()

Converts a local struct_time (or its 9‑tuple) back into a POSIX timestamp. It is effectively the inverse of localtime().

import time
t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)
local_time = time.mktime(t)
print("Local time:", local_time)

Example showing the round‑trip:

import time
seconds = 1545925769
t = time.localtime(seconds)
print("t1:", t)
s = time.mktime(t)
print("\ns:", s)

Typical output:

t1: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27,
tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3,
tm_yday=361, tm_isdst=0)

s: 1545925769.0

time.asctime()

Formats a struct_time (or tuple) into a human‑readable string.

import time
t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)
result = time.asctime(t)
print("Result:", result)

Typical output:

Result: Fri Dec 28 08:44:04 2018

time.strftime()

Formats a struct_time into a custom string based on format codes.

import time
named_tuple = time.localtime()
time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple)
print(time_string)

Typical output:

12/28/2018, 09:47:41

Common format codes include:

For more format codes, see the time.strftime() documentation.


time.strptime()

Parses a string according to a specified format and returns a struct_time.

import time
time_string = "21 June, 2018"
result = time.strptime(time_string, "%d %B, %Y")
print(result)

Typical output:

time.struct_time(tm_year=2018, tm_mon=6, tm_mday=21,
tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3,
tm_yday=172, tm_isdst=-1)

Python

  1. Python Keywords and Identifiers: Mastering Reserved Words and Naming Conventions
  2. Python Modules: Creation, Importing, and Advanced Usage
  3. Python Regular Expressions (re Module) – A Practical Guide
  4. Mastering Python's strftime(): Convert Dates and Times to Readable Strings
  5. Master Python's strptime() for Accurate Date Parsing
  6. Python: Retrieve Current Time and Timezone Data with Ease
  7. Mastering Python's time.sleep(): Best Practices, Multithreading, and Real‑World Examples
  8. Python timeit() – Measuring Execution Time with Practical Examples
  9. Mastering Dates & Times in Python: A Practical Guide
  10. Python Modules: Organizing Your Code for Clarity and Reusability