time.gmtime() vs time.localtime()
time.gmtime() and time.localtime() are both Python Time Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. time.gmtime(): Converts a timestamp to a time.struct_time in UTC. Its syntax is time.gmtime(secs=None), and it returns struct time (UTC) time.localtime(): Converts a timestamp to a time.struct_time in local time. Its syntax is time.localtime(secs=None), and it returns struct time (local) Example of time.gmtime(): t = time.gmtime()
print(t.tm_year, t.tm_mon, t.tm_mday). Example of time.localtime(): t = time.localtime()
print(t.tm_hour) # Current hour. Read the full references for time.gmtime() and time.localtime() to see parameters, edge cases, and example code side by side. Other Time Module APIs worth comparing: time.time(), time.sleep(), time.strftime(), time.strptime(), time.perf_counter(), time.monotonic(). Both belong to the Time Module category in the Python Reference documentation. Try both in the Python playground to see the behavioral difference live.