Skip to content

Feature Request: Option to convert Unix/Linux Epoch Timestamp into Date/Time #388

@jpummill

Description

@jpummill

Thank you for creating and sharing tabulate.

I have a feature request that I think would be useful.

Add an option to review the first couple rows of data to determine if any columns contain Unix/Linux Epoch timestamp values (either in seconds or milliseconds) and convert then automatically convert those columns into Date/Time values.

I am providing an example function that just detects whether a column is likely an epoch timestamp but it doesn't actually change the timestamp to a datetime value, but that is pretty easy to add.

You could also enhance the code by allowing the user to set the following:

  • The size of the window to allow (min_dt & max_dt). I just used +/- 10 years from current but this could be passed in.
  • Format of the datetime data.

`
def is_likely_epoch(n, unit="s"):

if not isinstance(n, (int, float)):
    return False

# Convert milliseconds to seconds if needed
if unit == "ms":
    n = n / 1000.0

try:
    now = datetime.datetime.now(datetime.timezone.utc)

    # Define dynamic bounds: now ± 10 years
    min_dt = now - datetime.timedelta(days=365 * 10)
    max_dt = now + datetime.timedelta(days=365 * 10)

    MIN_EPOCH_S = min_dt.timestamp()
    MAX_EPOCH_S = max_dt.timestamp()

    # Check if value falls within bounds
    if not (MIN_EPOCH_S <= n <= MAX_EPOCH_S):
        return False

    # Attempt conversion to datetime
    datetime.datetime.fromtimestamp(n, tz=datetime.timezone.utc)
    return True

except (ValueError, OSError, OverflowError):
    return False  

`

Thank you for considering this request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions