-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp.sesi
More file actions
128 lines (108 loc) · 4.35 KB
/
Copy pathtimestamp.sesi
File metadata and controls
128 lines (108 loc) · 4.35 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
allow "std/time" in with {
now, format
}
/*
Convert the current date into a local readable format.
Supports friendly aliases ("number", "name") or standard styles ("short", "medium", "long", "full").
Also supports locale-specific formatting (e.g., "en-US", "fr-FR", "ja-JP").
*/
export fn getDate(locale: string, dateStyle: string) -> string {
let numerical = "short"
let with_letters = "medium"
let resolvedStyle = dateStyle
if dateStyle == "number" {
resolvedStyle = numerical
} else if dateStyle == "name" {
resolvedStyle = with_letters
}
if locale == null || locale == "" {
locale = "en-US" // Default to US English if no locale is specified
}
let timestamp = format(now(), {"locale": locale, "dateStyle": resolvedStyle})
return timestamp
}
/*
Convert the current time into a local readable format.
Supports friendly aliases ("basic", "in_ms", "detailed") or standard styles.
Also supports locale-specific formatting (e.g., "en-US", "fr-FR", "ja-JP") and time zone specification (e.g., "America/New_York", "Europe/London").
*/
export fn localTime(timeZone: string, locale: string, timeStyle: string) -> string {
let compacted = "short"
let milliseconds = "medium"
let verbose = "full"
if timeZone == null || timeZone == "" {
timeZone = "America/New_York" // Default to Eastern Time if no time zone is specified
}
if locale == null || locale == "" {
locale = "en-US" // Default to US English if no locale is specified
}
let resolvedStyle = timeStyle
if timeStyle == "basic" || timeStyle == null || timeStyle == "" {
resolvedStyle = compacted
} else if timeStyle == "in_ms" {
resolvedStyle = milliseconds
} else if timeStyle == "detailed" {
resolvedStyle = verbose
}
let timestamp = format(now(), {"timeZone": timeZone, "locale": locale, "timeStyle": resolvedStyle})
return timestamp
}
/*
Convert the current time into a universal readable format (UTC).
*/
export fn universalTime() -> string {
let timestamp = format(now(), {"timeZone": "UTC", "timeStyle": "medium"})
return timestamp + " UTC"
}
/*
Format the time specifically for safe use in filenames.
Replaces spaces, colons, and AM/PM markers to prevent filesystem errors.
Supports locale-specific formatting and time zone specification.
*/
export fn formattedTime(timeZone: string, locale: string) -> string {
let dateRef = getDate("number") // e.g., "6/4/2024"
let timeRef = localTime(timeZone, locale, "in_ms") // e.g., "12:04:32 PM"
// Sequentially sanitize the string for cross-platform filesystem safety
let safeTime = swap(timeRef, " ", "-")
let safeDate = swap(dateRef, "/", "-")
let safeT = swap(safeTime, ":", "-")
let safe = safeDate + "-" + safeT
return safe // e.g., "6-4-2024-12-04-32-PM"
}
/*
Register the functions as Sesi-native AI tools.
This allows any reasoning model in your parent script to dynamically query the system clock.
*/
export fn time_tools() {
let tool_refs = {}
// Scoped helper function to register and track tools
fn register_tool(name: string, func, desc: string) {
define_tool(name, func, desc)
tool_refs[name] = desc
}
/*
Register the tools for easy calls within your scripts
*/
register_tool("getDate", getDate, "Retrieve the current date. Accepts 'number', 'name', or standard styles.")
register_tool("localTime", localTime, "Retrieve the local time. Accepts 'basic', 'in_ms', 'detailed', or standard styles.")
register_tool("universalTime", universalTime, "Retrieve the current universal time (UTC) in short format.")
register_tool("formattedTime", formattedTime, "Retrieve a filesystem-safe timestamp (e.g., '12-04-32-PM') for file naming.")
let _registry = list_tools()
print "⏱️ timestamp tool registry loaded —" str(len(_registry)) "tools available"
print to_json(tool_refs)
}
/*
Example usage:
You can use these in your script easily.
Just allow "timestamp" in with {getDate, localTime, universalTime, or formattedTime}, or simply as TS
- `basic` reads out `12:00 PM`
- `in_ms` reads out `12:00:00 PM`
- `detailed` reads out `12:00:00 PM Eastern Daylight Time`
time_tools()
let local = TS.localTime("detailed")
let universal = TS.universalTime()
let formatted = TS.formattedTime()
print "Current time:" local
print "Current time (UTC):" universal
print "Formatted time:" formatted
*/