get_now_unix()#

get_now_unix()

This is a simple utility function that returns the current time as a Unix timestamp, in UTC.

That means it gives you:

  • the number of seconds since the Unix epoch (1970-01-01 00:00:00 UTC),

  • as a floating point number (includes fractions of a second).

How it works#

  1. Imports datetime and timezone from Python’s standard library.

  2. Uses:

datetime.now(timezone.utc)

to get the current time explicitly in UTC.

  1. Calls .timestamp() on that datetime object, which converts it to a Unix timestamp (a float).

  2. Returns that value.

Example usage#

now_unix = get_now_unix()
print(now_unix)
# → 1751234567.890123

Why explicitly UTC?#

Without specifying timezone.utc, datetime.now() would return local time, which might vary based on your server or laptop’s timezone. By doing datetime.now(timezone.utc), this function ensures it always returns UTC, which is critical for consistent comparisons in workflows like Tapis job metadata.

Typical use cases#

  • Comparing against job times (like created_unix or ended_unix fields).

  • Logging events in a consistent UTC format.

  • Calculating durations by subtracting from other Unix timestamps.

In short#

get_now_unix() gives you the current moment in UTC, expressed in seconds since epoch — a lightweight, always-consistent time format ideal for automation and data tracking.

Files#

You can find these files in Community Data.

get_now_unix.py
def get_now_unix():
    """
    Get the current time as a Unix timestamp in UTC.

    Returns
    -------
    float
        Current time in seconds since the Unix epoch (1970-01-01 00:00:00 UTC).

    Example
    -------
    now = get_now_unix()
    print(f"Current Unix time: {now}")
    # → 1751234567.890123

    Author
    ------
    Silvia Mazzoni, DesignSafe (silviamazzoni@yahoo.com)

    Date
    ----
    2025-08-14

    Version
    -------
    1.0
    """

    from datetime import datetime, timezone
    return datetime.now(timezone.utc).timestamp()