Compare calendar timegm vs time mktime in Python



In Python, the mktime() function (from the time module) assumes that the passed tuple is in local time, while the calendar.timegm() (from the calendar module) assumes it's in GMT/UTC.

Depending on the interpretation, the tuple represents a different time, so both functions return different values (seconds since the epoch are UTC-based). The difference between the values should be equal to the time zone offset of your local time zone.

Understanding time.mktime() in Local Time Context

The Python time.mktime() method converts the object form of local time into seconds since the epoch (January 1, 1970, 00:00:00 UTC).

This method is the inverse function of localtime() and accepts a struct_time object or a full 9-tuple as its argument. Following are the values in the 9-tuple -

  • tm_year
  • tm_mon
  • tm_mday
  • tm_hour
  • tm_min
  • tm_sec
  • tm_wday
  • tm_yday
  • tm_isdst
  • tm_zone
  • tm_gmtoff

If the value passed to this function is not a valid time, it will raise either OverflowError or ValueError.

Example

The following program demonstrates basic usage of time.mktime() function to convert a local time tuple (year, month, day, etc.) into a Unix timestamp representing seconds since the epoch.

import time

# Create a time tuple (local time)
time_tuple = (2025, 7, 15, 12, 0, 0, 0, 0, -1)

# Convert to timestamp using mktime (local time)
timestamp = time.mktime(time_tuple)
print("Local timestamp:", timestamp)

Following is the output of the above code -

Local timestamp: 1752589800.0

Working with calendar.timegm() for UTC Conversion

The calendar.timegm() function in Python is used to convert a time tuple representing a UTC to a POSIX timestamp (seconds since the epoch). This function is the inverse of time.gmtime(), which converts a timestamp to a time tuple.

This function accepts a tuple of 9 elements as a parameter representing a UTC (year, month, day, hour, minute, second, weekday, day of the year, DST flag). As a result, it returns an integer representing the corresponding timestamp.

Example

The following program creates a time tuple representing July 15, 2025, 12:00 PM UTC. Then it uses a calendar.timegm() function to convert this tuple into a Unix timestamp, representing the number of seconds since the epoch.

import calendar

# Create a time tuple for July 15, 2025 at 12:00 PM (UTC)
time_tuple = (2025, 7, 15, 12, 0, 0, 0, 0, 0)

# Convert to timestamp using timegm (UTC time)
timestamp = calendar.timegm(time_tuple)
print("UTC timestamp:", timestamp)

Following is the output of the above code -

UTC timestamp: 1752582600

Comparing Both Outputs

To understand the difference clearly, let's print both timestamps for the same datetime tuple.

Example

The following program converts a date to local and UTC timestamps. The time.mktime() calculates the local timestamp and calendar.timegm() calculates the UTC timestamp. Finally, it prints the difference between them, reflecting the local timezone offset.

Here, the difference (7200 seconds) reflects the local time zone offset (e.g, GMT+2). This shows how the same time tuple is interpreted differently by each function.

import time
import calendar

time_tuple = (2025, 7, 15, 12, 0, 0, 0, 0, -1)

local_ts = time.mktime(time_tuple)
utc_ts = calendar.timegm(time_tuple)

print("Local timestamp:", local_ts)
print("UTC timestamp  :", utc_ts)
print("Difference     :", local_ts - utc_ts)

Following is the output of the above code -

Local timestamp: 1752589800.0
UTC timestamp  : 1752582600
Difference     : 7200.0

Difference between calendar.timegm() and time.mktime()

Some of the key differences between calendar.timegm() and time.mktime() are as follows -

time.mktime() calendar.timegm()
Assumes the time tuple is in local time Assumes time tuple is in UTC/GMT
Affected by the system's time zone and DST Unaffected by time zone or DST
Returns local timestamp Returns a UTC timestamp
Used for local scheduling or logging Used for UTC-based data and APIs
Can cause bugs if the UTC is passed Safer for standardised timestamps
Updated on: 2025-05-19T17:37:30+05:30

938 Views

Kickstart Your Career

Get certified by completing the course

Get Started