File size: 3,177 Bytes
53b2ac9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# SPDX-License-Identifier: MIT
"""
Timestamp Plugin for TrashClaw

Convert between Unix timestamps and human-readable dates. Show current time.

Plugin contract:
  TOOL_DEF = dict with name, description, parameters (OpenAI function schema)
  run(**kwargs) -> str
"""

import time
from datetime import datetime, timezone

TOOL_DEF = {
    "name": "timestamp",
    "description": "Convert between Unix timestamps and human-readable dates, or show current time.",
    "parameters": {
        "type": "object",
        "properties": {
            "unix": {
                "type": "number",
                "description": "Unix timestamp (seconds) to convert to human date"
            },
            "date": {
                "type": "string",
                "description": "Human date string (ISO 8601, e.g. '2026-03-25T12:00:00') to convert to Unix"
            },
            "now": {
                "type": "boolean",
                "description": "If true, show current time in multiple formats"
            }
        },
        "required": []
    }
}


def run(unix: float = None, date: str = "", now: bool = False, **kwargs) -> str:
    results = []

    if now or (unix is None and not date):
        ts = time.time()
        dt = datetime.fromtimestamp(ts, tz=timezone.utc)
        local_dt = datetime.fromtimestamp(ts)
        results.append(
            f"Current Time:\n"
            f"  Unix:  {int(ts)}\n"
            f"  UTC:   {dt.strftime('%Y-%m-%d %H:%M:%S %Z')}\n"
            f"  Local: {local_dt.strftime('%Y-%m-%d %H:%M:%S')}"
        )

    if unix is not None:
        try:
            dt_utc = datetime.fromtimestamp(float(unix), tz=timezone.utc)
            dt_local = datetime.fromtimestamp(float(unix))
            results.append(
                f"Unix {int(unix)} =\n"
                f"  UTC:   {dt_utc.strftime('%Y-%m-%d %H:%M:%S %Z')}\n"
                f"  Local: {dt_local.strftime('%Y-%m-%d %H:%M:%S')}\n"
                f"  ISO:   {dt_utc.isoformat()}"
            )
        except (ValueError, OSError, OverflowError) as e:
            results.append(f"Error converting unix timestamp: {e}")

    if date:
        formats = [
            "%Y-%m-%dT%H:%M:%S",
            "%Y-%m-%dT%H:%M:%SZ",
            "%Y-%m-%d %H:%M:%S",
            "%Y-%m-%d",
            "%d/%m/%Y %H:%M:%S",
            "%d/%m/%Y",
            "%m/%d/%Y",
        ]
        parsed = None
        for fmt in formats:
            try:
                parsed = datetime.strptime(date.strip(), fmt)
                break
            except ValueError:
                continue

        if parsed:
            ts = int(parsed.timestamp())
            results.append(
                f"'{date}' =\n"
                f"  Unix:    {ts}\n"
                f"  Parsed:  {parsed.strftime('%Y-%m-%d %H:%M:%S')}\n"
                f"  ISO:     {parsed.isoformat()}"
            )
        else:
            results.append(
                f"Error: could not parse '{date}'. "
                f"Supported formats: ISO 8601, YYYY-MM-DD, DD/MM/YYYY, MM/DD/YYYY"
            )

    return "\n\n".join(results) if results else "Error: provide 'unix', 'date', or set 'now' to true."