🎁 Get the FREE AI Skills Starter Guide β€” Subscribe β†’
BytesAgainBytesAgain
πŸ¦€ ClawHub

Timestamp Converter

by @leonardodpanda

Convert between Unix timestamps, ISO 8601 dates, and human-readable formats. Use when working with timestamps from APIs, logs, or databases that need convers...

Versionv1.0.0
Installs2
⚑ When to Use
TriggerAction
- Working with log files containing Unix timestamps
- Converting between timezones for international users
- Calculating time differences between dates
- Batch converting timestamps in data processing
- Formatting dates for different locales
πŸ’‘ Examples

Convert Unix Timestamp

from datetime import datetime, timezone

def unix_to_datetime(timestamp, tz=None): """ Convert Unix timestamp to datetime Args: timestamp: Unix timestamp (seconds or milliseconds) tz: Target timezone (default: UTC) """ # Auto-detect seconds vs milliseconds if timestamp > 1e10: timestamp = timestamp / 1000 dt = datetime.fromtimestamp(timestamp, tz=timezone.utc) if tz: dt = dt.astimezone(tz) return dt

Usage

unix_to_datetime(1704067200)

Result: 2024-01-01 00:00:00+00:00

unix_to_datetime(1704067200000) # Milliseconds

Result: 2024-01-01 00:00:00+00:00

Format Conversion

def format_timestamp(timestamp, fmt='%Y-%m-%d %H:%M:%S'):
    """Convert timestamp to custom format"""
    dt = unix_to_datetime(timestamp)
    return dt.strftime(fmt)

Common formats

format_timestamp(1704067200, '%Y-%m-%d') # 2024-01-01 format_timestamp(1704067200, '%d/%m/%Y') # 01/01/2024 format_timestamp(1704067200, '%B %d, %Y') # January 01, 2024 format_timestamp(1704067200, '%I:%M %p') # 12:00 AM

ISO 8601 Conversion

from datetime import datetime

def parse_iso8601(iso_string): """Parse ISO 8601 string to datetime""" # Handle various ISO formats iso_string = iso_string.replace('Z', '+00:00') return datetime.fromisoformat(iso_string)

def to_iso8601(dt, include_ms=False): """Convert datetime to ISO 8601 string""" if include_ms: return dt.isoformat() return dt.strftime('%Y-%m-%dT%H:%M:%S%z')

Usage

parse_iso8601('2024-01-01T00:00:00Z') to_iso8601(datetime.now())

Timezone Conversion

from zoneinfo import ZoneInfo

def convert_timezone(timestamp, from_tz='UTC', to_tz='America/New_York'): """Convert timestamp between timezones""" dt = unix_to_datetime(timestamp) # Set source timezone if from_tz != 'UTC': dt = dt.replace(tzinfo=ZoneInfo(from_tz)) # Convert to target timezone return dt.astimezone(ZoneInfo(to_tz))

Usage

convert_timezone(1704067200, 'UTC', 'Asia/Shanghai')

Result: 2024-01-01 08:00:00+08:00

Date Arithmetic

from datetime import timedelta

def add_time(timestamp, days=0, hours=0, minutes=0): """Add time to timestamp""" dt = unix_to_datetime(timestamp) delta = timedelta(days=days, hours=hours, minutes=minutes) return dt + delta

def time_diff(timestamp1, timestamp2): """Calculate difference between two timestamps""" dt1 = unix_to_datetime(timestamp1) dt2 = unix_to_datetime(timestamp2) diff = dt2 - dt1 return { 'days': diff.days, 'seconds': diff.seconds, 'total_seconds': diff.total_seconds(), 'hours': diff.total_seconds() / 3600 }

Usage

add_time(1704067200, days=7) # Add 1 week time_diff(1704067200, 1704672000) # Difference between timestamps

View on ClawHub
TERMINAL
clawhub install timestamp-converter

πŸ§ͺ Use this skill with your agent

Most visitors already have an agent. Pick your environment, install or copy the workflow, then run the smoke-test prompt above.

πŸ” Can't find the right skill?

Search 60,000+ AI agent skills β€” free, no login needed.

Search Skills β†’