This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/tools/datetime.go
2026-02-25 23:56:27 +00:00

67 lines
1.9 KiB
Go

package tools
import (
"context"
"encoding/json"
"fmt"
"time"
)
func init() {
Register(&DateTimeTool{})
}
// ═══════════════════════════════════════════
// datetime
// ═══════════════════════════════════════════
type DateTimeTool struct{}
func (t *DateTimeTool) Definition() ToolDef {
return ToolDef{
Name: "datetime",
DisplayName: "Date & Time",
Description: "Get the current date, time, timezone, and day of week. Use this whenever you need to know the current date or time, calculate days between dates, or answer questions about what day it is. Do NOT guess dates — always call this tool.",
Category: "utilities",
Parameters: JSONSchema(map[string]interface{}{
"timezone": Prop("string", "IANA timezone name, e.g. 'America/New_York', 'UTC', 'Europe/London'. Defaults to UTC."),
}, nil),
}
}
func (t *DateTimeTool) Execute(ctx context.Context, execCtx ExecutionContext, argsJSON string) (string, error) {
var args struct {
Timezone string `json:"timezone"`
}
if argsJSON != "" {
_ = json.Unmarshal([]byte(argsJSON), &args)
}
loc := time.UTC
if args.Timezone != "" {
parsed, err := time.LoadLocation(args.Timezone)
if err != nil {
return "", fmt.Errorf("invalid timezone %q: %w", args.Timezone, err)
}
loc = parsed
}
now := time.Now().In(loc)
year, week := now.ISOWeek()
result := map[string]interface{}{
"datetime": now.Format(time.RFC3339),
"date": now.Format("2006-01-02"),
"time": now.Format("15:04:05"),
"day": now.Weekday().String(),
"timezone": loc.String(),
"unix": now.Unix(),
"iso_week": fmt.Sprintf("%d-W%02d", year, week),
"day_of_year": now.YearDay(),
}
b, _ := json.Marshal(result)
return string(b), nil
}