how to get unix timestamp from python

How to Get Unix Timestamp from Python: Seconds and Milliseconds

In Python, a Unix timestamp (also known as POSIX time) represents the number of seconds or milliseconds elapsed since January 1, 1970, 00:00:00 UTC (the “Unix epoch”). This guide explains how to generate both second-level and millisecond-level timestamps using Python’s standard libraries.

1. What is a Unix Timestamp?

A Unix timestamp is a numeric value that measures time in seconds (or milliseconds) since the Unix epoch. It is widely used in programming, databases, and APIs for time-related operations.
Example:

  • 1712345678 → 2024-04-06 12:34:38 UTC
  • 1712345678901 → 2024-04-06 12:34:38.901 UTC

2. Getting Second-Level Timestamps

Use Python’s built-in time or datetime modules to get second-level timestamps.

Method 1: Using time.time()

import time

timestamp_seconds = time.time()
print(timestamp_seconds)  # Output: 1712345678.1234567 (floating-point seconds)
  • time.time() returns a floating-point number representing seconds since the epoch.
  • Truncate it to an integer for a clean second-level timestamp: timestamp_seconds = int(time.time()) print(timestamp_seconds) # Output: 1712345678

Method 2: Using datetime.datetime.now().timestamp()

from datetime import datetime

current_time = datetime.now()
timestamp_seconds = current_time.timestamp()
print(timestamp_seconds)  # Output: 1712345678.1234567
  • datetime.timestamp() also returns seconds since the epoch as a float.
  • Convert to an integer for a second-level timestamp: timestamp_seconds = int(current_time.timestamp()) print(timestamp_seconds) # Output: 1712345678

3. Getting Millisecond-Level Timestamps

To get timestamps in milliseconds, multiply the second-level value by 1000 or use microsecond precision.

Method 1: Convert Seconds to Milliseconds

import time

timestamp_seconds = time.time()
timestamp_milliseconds = int(timestamp_seconds * 1000)
print(timestamp_milliseconds)  # Output: 1712345678123

Method 2: Use datetime Microseconds

from datetime import datetime

current_time = datetime.now()
timestamp_milliseconds = int(current_time.timestamp() * 1000)
print(timestamp_milliseconds)  # Output: 1712345678123

Method 3: Direct Microsecond Calculation

from datetime import datetime

current_time = datetime.now()
# Get total microseconds since epoch and convert to milliseconds
timestamp_milliseconds = int(current_time.timestamp() * 1000)
print(timestamp_milliseconds)  # Output: 1712345678123

4. Handling Time Zones

Unix timestamps are UTC-based. Always use UTC to avoid timezone-related errors:

from datetime import datetime, timezone

current_time_utc = datetime.now(timezone.utc)
timestamp_seconds = current_time_utc.timestamp()
print(timestamp_seconds)  # Output: 1712345678.1234567

5. Convert Timestamp Back to Readable Time

Reverse the process to convert a timestamp to a human-readable format:

from datetime import datetime

timestamp = 1712345678
dt_object = datetime.utcfromtimestamp(timestamp)
print(dt_object)  # Output: 2024-04-06 12:34:38

Summary Table

Timestamp TypeMethodExample CodeOutput Format
Secondstime.time()int(time.time())1712345678
Millisecondstime.time() * 1000int(time.time() * 1000)1712345678123
Secondsdatetime.now().timestamp()int(datetime.now().timestamp())1712345678
Millisecondsdatetime.now().timestamp() * 1000int(datetime.now().timestamp() * 1000)1712345678123

Key Notes

  • Use time.time() for simple timestamp generation.
  • Use datetime for advanced time operations (e.g., timezone handling).
  • Always validate timestamps in UTC to avoid timezone ambiguities.

By following these methods, you can efficiently generate and work with Unix timestamps in Python for both second and millisecond precision.