| 1 | 1 | new file mode 100644 |
| index 0000000..589e3d0 | ||
| --- /dev/null | ||
| +++ b/.gitignore | ||
| @@ -0,0 +1,9 @@ | ||
| 1 | +/plugin_time | |
| 2 | +/plugin_time.musl | |
| 3 | +/plugin_time.arm64 | |
| 4 | +/plugin_time.exe | |
| 5 | +/plugin_time.asan | |
| 6 | +*.o | |
| 7 | +*.tmp | |
| 8 | +__pycache__/ | |
| 9 | +*.pyc |
| 1 | 1 | new file mode 100644 |
| index 0000000..ef80120 | ||
| --- /dev/null | ||
| +++ b/LICENSE | ||
| @@ -0,0 +1,21 @@ | ||
| 1 | +MIT License | |
| 2 | + | |
| 3 | +Copyright (c) 2026 SCORPIOX INC | |
| 4 | + | |
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 6 | +of this software and associated documentation files (the "Software"), to deal | |
| 7 | +in the Software without restriction, including without limitation the rights | |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 9 | +copies of the Software, and to permit persons to whom the Software is | |
| 10 | +furnished to do so, subject to the following conditions: | |
| 11 | + | |
| 12 | +The above copyright notice and this permission notice shall be included in all | |
| 13 | +copies or substantial portions of the Software. | |
| 14 | + | |
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 21 | +SOFTWARE. |
| 1 | 1 | new file mode 100644 |
| index 0000000..62017d7 | ||
| --- /dev/null | ||
| +++ b/Makefile | ||
| @@ -0,0 +1,74 @@ | ||
| 1 | +# plugin_time - SCORPIOX BOT open-source world-time MCP connector | |
| 2 | +# | |
| 3 | +# The only non-stdlib dependency is the vendored yyjson JSON parser under | |
| 4 | +# vendor/yyjson (MIT, upstream yyjson/yyjson); it is compiled into the binary, | |
| 5 | +# so every artefact stays a standalone executable with zero shared-library | |
| 6 | +# requirements on the node. | |
| 7 | +# | |
| 8 | +# The IANA timezone table lives in src/tzdata.h, generated from a compiled tz | |
| 9 | +# database by tools/gen_tzdata.py. Regenerate it only when you mean to move to | |
| 10 | +# a newer tzdata release, and re-run the suite: every epoch in the tests is | |
| 11 | +# absolute, so a tzdata change that moves a zone shows up as a failure rather | |
| 12 | +# than as a silently different answer. | |
| 13 | +# | |
| 14 | +# Targets: | |
| 15 | +# make native build -> ./plugin_time | |
| 16 | +# make musl static musl x86_64 -> ./plugin_time.musl | |
| 17 | +# make arm64 static arm64 musl (aarch64 cross) -> ./plugin_time.arm64 | |
| 18 | +# make win64 MinGW-w64 static -> ./plugin_time.exe | |
| 19 | +# make macos Apple clang -> ./plugin_time (run this on macOS) | |
| 20 | +# make asan ASan + UBSan + _FORTIFY_SOURCE=2 -> ./plugin_time.asan | |
| 21 | +# make tzdata regenerate src/tzdata.h from /usr/share/zoneinfo | |
| 22 | +# make ui regenerate src/ui_html.h from src/ui.html | |
| 23 | +# make test run tests/run.sh | |
| 24 | +# make clean | |
| 25 | + | |
| 26 | +CC ?= cc | |
| 27 | +CFLAGS ?= -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings | |
| 28 | +CPPFLAGS = -I src -I vendor -I vendor/yyjson | |
| 29 | +SRCS = src/plugin_time.c src/timeutil.c src/tz.c vendor/yyjson/yyjson.c | |
| 30 | +HDRS = src/plugintime.h src/tz.h src/tzdata.h src/ui_html.h src/bot_theme_plugin.h | |
| 31 | + | |
| 32 | +MUSL_CC ?= musl-gcc | |
| 33 | +ARM64_CC ?= aarch64-linux-gnu-gcc | |
| 34 | +WIN64_CC ?= x86_64-w64-mingw32-gcc | |
| 35 | + | |
| 36 | +all: plugin_time | |
| 37 | + | |
| 38 | +plugin_time: $(SRCS) $(HDRS) | |
| 39 | + $(CC) $(CFLAGS) $(CPPFLAGS) $(SRCS) -o $@ | |
| 40 | + | |
| 41 | +musl: plugin_time.musl | |
| 42 | +plugin_time.musl: $(SRCS) $(HDRS) | |
| 43 | + $(MUSL_CC) $(CFLAGS) -static $(CPPFLAGS) $(SRCS) -o $@ | |
| 44 | + | |
| 45 | +arm64: plugin_time.arm64 | |
| 46 | +plugin_time.arm64: $(SRCS) $(HDRS) | |
| 47 | + $(ARM64_CC) $(CFLAGS) -static $(CPPFLAGS) $(SRCS) -o $@ | |
| 48 | + | |
| 49 | +win64: plugin_time.exe | |
| 50 | +plugin_time.exe: $(SRCS) $(HDRS) | |
| 51 | + $(WIN64_CC) $(CFLAGS) -static -D_WIN32_WINNT=0x0601 $(CPPFLAGS) $(SRCS) -o $@ | |
| 52 | + | |
| 53 | +macos: $(SRCS) $(HDRS) | |
| 54 | + clang $(CFLAGS) $(CPPFLAGS) $(SRCS) -o plugin_time | |
| 55 | + | |
| 56 | +asan: plugin_time.asan | |
| 57 | +plugin_time.asan: $(SRCS) $(HDRS) | |
| 58 | + $(CC) -O1 -g -std=c99 -Wall -Wextra -D_FORTIFY_SOURCE=2 \ | |
| 59 | + -fsanitize=address,undefined -fno-omit-frame-pointer \ | |
| 60 | + $(CPPFLAGS) $(SRCS) -o $@ | |
| 61 | + | |
| 62 | +tzdata: | |
| 63 | + python3 tools/gen_tzdata.py | |
| 64 | + | |
| 65 | +ui: | |
| 66 | + python3 tools/gen_ui.py | |
| 67 | + | |
| 68 | +test: plugin_time | |
| 69 | + sh tests/run.sh | |
| 70 | + | |
| 71 | +clean: | |
| 72 | + rm -f plugin_time plugin_time.musl plugin_time.arm64 plugin_time.exe plugin_time.asan | |
| 73 | + | |
| 74 | +.PHONY: all musl arm64 win64 macos asan tzdata ui test clean |
| 1 | 1 | new file mode 100644 |
| index 0000000..717bebd | ||
| --- /dev/null | ||
| +++ b/README.md | ||
| @@ -0,0 +1,189 @@ | ||
| 1 | +# SCORPIOX BOT — time | |
| 2 | + | |
| 3 | +World time and timezone conversion for your agents and your own node. | |
| 4 | + | |
| 5 | +This is the reference MCP **time** server, rebuilt as one static C99 binary with | |
| 6 | +the IANA tz database compiled into it. Three tools — `get_current_time`, | |
| 7 | +`convert_time`, `get_time_ago` — and the same three operations back a World | |
| 8 | +Clock, converter and time-ago panel inside SCORPIOX BOT. No account, no tzdata | |
| 9 | +package, no CDN, **no network request of any kind**. | |
| 10 | + | |
| 11 | +``` | |
| 12 | +make # native build -> ./plugin_time | |
| 13 | +make musl # static musl x86_64 -> ./plugin_time.musl | |
| 14 | +make arm64 # static aarch64 -> ./plugin_time.arm64 | |
| 15 | +make win64 # MinGW-w64 static PE -> ./plugin_time.exe | |
| 16 | +make macos # Apple clang (on macOS) -> ./plugin_time | |
| 17 | +make tzdata # regenerate src/tzdata.h from the host tz database | |
| 18 | +make ui # regenerate src/ui_html.h from src/ui.html | |
| 19 | +make test # offline suite (190+ assertions) | |
| 20 | +python3 tests/e2e.py # Playwright viewport suite (1440 / 1024 / 390, dark + light) | |
| 21 | +``` | |
| 22 | + | |
| 23 | +## Why the database is embedded | |
| 24 | + | |
| 25 | +A timezone connector that reads the host's tz database answers differently on | |
| 26 | +every node: a Windows box has none at all, macOS's filesystem is case-insensitive | |
| 27 | +so `europe/warsaw` resolves while Linux refuses it, and a slim container may ship | |
| 28 | +an older tzdata release. Embedding the table makes the same call return the same | |
| 29 | +string on every platform — which is the whole point of a tool an agent reasons | |
| 30 | +about. | |
| 31 | + | |
| 32 | +`tools/gen_tzdata.py` reads the compiled TZif files and emits `src/tzdata.h`: | |
| 33 | + | |
| 34 | +* all **598** IANA keys (links included) for an exact, case-sensitive lookup; | |
| 35 | +* every real UTC transition from **2015** onward — 10 341 of them, out to 2087 | |
| 36 | + for the zones that still generate them — so DST rules, Ramadan exceptions | |
| 37 | + (`Africa/Casablanca`) and redesigned rules (`Asia/Hebron`) are exact rather | |
| 38 | + than approximated; | |
| 39 | +* each zone's own POSIX TZ footer string, used only **beyond** the last embedded | |
| 40 | + transition, which is exactly what a host does when it runs out of table. | |
| 41 | + | |
| 42 | +Regeneration is deterministic: the same tz database produces the same bytes, and | |
| 43 | +`tests/run.sh` re-runs the generator and compares. | |
| 44 | + | |
| 45 | +## Install | |
| 46 | + | |
| 47 | +```sh | |
| 48 | +cp plugin_time ~/.scorpiox/plugins/plugin_time | |
| 49 | +chmod +x ~/.scorpiox/plugins/plugin_time | |
| 50 | + | |
| 51 | +# or from the market | |
| 52 | +curl -s -H "X-Bot-Password: $BOT_PASSWORD" \ | |
| 53 | + "http://127.0.0.1:8150/plugin_manage?action=install&slug=time&mode=prebuilt" | |
| 54 | +``` | |
| 55 | + | |
| 56 | +Windows: `%LOCALAPPDATA%\scorpiox\plugins\plugin_time.exe`. | |
| 57 | +BotCloud container: `/workspace/.scorpiox/plugins/plugin_time`. | |
| 58 | + | |
| 59 | +## Five modes, one binary | |
| 60 | + | |
| 61 | +| Mode | Trigger | Response | | |
| 62 | +|---|---|---| | |
| 63 | +| Discovery | `--meta` or `SX_PLUGIN_META=1` | `{"name":"time","desc":"World time MCP","kind":"mcp","mcp":true,"daemon":false,"menus":false,"verified":"live","category":"utilities","version":"1.0.0"}` (unauthenticated) | | |
| 64 | +| **MCP tool provider** | `--mcp` | newline-delimited **JSON-RPC 2.0 over stdio** — `initialize`, `tools/list`, `tools/call`, `ping` | | |
| 65 | +| API | `?action=...`, `?format=json`, or any `POST` | `application/json; charset=utf-8` | | |
| 66 | +| UI | plain `GET` (`?embed=1` aware) | the World Clock page, `text/html` | | |
| 67 | +| Info | `--version`, `--help`, `--schema` | id + version, usage, the tool schemas | | |
| 68 | + | |
| 69 | +Modes 3 and 4 sit behind `require_node_auth()`: `X-Bot-Password`, | |
| 70 | +`Authorization: Bearer <pwd>`, or `?pwd=`. With `BOT_PASSWORD` unset the check is | |
| 71 | +a no-op, which is the intended behaviour on a single-user box. `--meta` and | |
| 72 | +`--mcp` stay open — the node spawns the stdio server itself, so a CGI password | |
| 73 | +must not stop it, or the router could never reach an installed connector. | |
| 74 | + | |
| 75 | +### Why `--mcp` and not an MCP server entry | |
| 76 | + | |
| 77 | +`plugin_time` is a **connector plugin**, not a new MCP server. `scorpiox-bot-api` | |
| 78 | +keeps one lightweight stdio router (`sx-connector-mcp`) and this plugin is | |
| 79 | +discovered by scan, so installing it never consumes one of `clang`'s 16 MCP | |
| 80 | +server slots and never adds 3 tools to every prompt. An agent reaches the tools | |
| 81 | +through `connector_list` / `connector_tools` / `connector_call`. | |
| 82 | + | |
| 83 | +## Tools | |
| 84 | + | |
| 85 | +### `get_current_time` | |
| 86 | + | |
| 87 | +```json | |
| 88 | +{"timezone": "Pacific/Auckland"} | |
| 89 | +``` | |
| 90 | +```json | |
| 91 | +{ | |
| 92 | + "timezone": "Pacific/Auckland", | |
| 93 | + "datetime": "2026-11-14T13:00:00+13:00", | |
| 94 | + "day_of_week": "Saturday", | |
| 95 | + "is_dst": true, | |
| 96 | + "utc_offset": "+13:00", | |
| 97 | + "abbreviation": "NZDT" | |
| 98 | +} | |
| 99 | +``` | |
| 100 | + | |
| 101 | +`timezone` is optional and defaults to the node's own zone, which the tool | |
| 102 | +description names so a model can default to it. `utc_offset` and `abbreviation` | |
| 103 | +are additions to the reference shape — a caller should not have to re-derive | |
| 104 | +either from the ISO string. | |
| 105 | + | |
| 106 | +### `convert_time` | |
| 107 | + | |
| 108 | +```json | |
| 109 | +{"source_timezone": "Pacific/Auckland", "time": "09:00", | |
| 110 | + "target_timezone": "America/New_York"} | |
| 111 | +``` | |
| 112 | +```json | |
| 113 | +{ | |
| 114 | + "source": {"timezone": "Pacific/Auckland", "datetime": "2026-11-14T09:00:00+13:00", "...": "..."}, | |
| 115 | + "target": {"timezone": "America/New_York", "datetime": "2026-11-13T15:00:00-05:00", "...": "..."}, | |
| 116 | + "time_difference": "-18.0h", | |
| 117 | + "later_occurrence": null | |
| 118 | +} | |
| 119 | +``` | |
| 120 | + | |
| 121 | +* `time` must be exactly `HH:MM`, 24-hour. `9:00`, `24:00`, `12:60` and | |
| 122 | + `12:30:00` are all refused with the reference server's own message. | |
| 123 | +* A wall time **skipped** by a forward clock change is an error naming the date: | |
| 124 | + `Invalid time: 01:30 does not exist in Europe/London on 2026-03-29 (skipped by | |
| 125 | + a clock change)` — never a silently shifted answer. | |
| 126 | +* A wall time **repeated** by a backward clock change answers the first | |
| 127 | + occurrence in `source`/`target` and puts the second under `later_occurrence`, | |
| 128 | + with its own `note` and `time_difference`. That is Python's `fold=0`/`fold=1` | |
| 129 | + behaviour, and it is the difference between "01:30" being one time and two. | |
| 130 | +* `time_difference` is `+3.0h` for whole hours and `+5.75h` / `+13.75h` for the | |
| 131 | + fractional zones (Kathmandu, Chatham). | |
| 132 | + | |
| 133 | +### `get_time_ago` | |
| 134 | + | |
| 135 | +```json | |
| 136 | +{"timezone": "Europe/London", "hours_ago": 5} | |
| 137 | +``` | |
| 138 | + | |
| 139 | +The span is subtracted from the **UTC instant**, not the local wall clock, so | |
| 140 | +"5 hours ago" across a clock change is five real hours. Every component | |
| 141 | +(`days_ago`, `hours_ago`, `minutes_ago`, `seconds_ago`) defaults to 0 and must be | |
| 142 | +non-negative. | |
| 143 | + | |
| 144 | +## The page | |
| 145 | + | |
| 146 | +`GET /plugin_time` serves a bot-themed World Clock: a live clock in any zone, a | |
| 147 | +converter with both sides named, an editable world-clock table (persisted per | |
| 148 | +browser), a time-ago panel and the live tool list. Every value on the page comes | |
| 149 | +from a tool call, so a node whose system clock is off shows the tool's answer | |
| 150 | +rather than the browser's. | |
| 151 | + | |
| 152 | +Dark and light come from the bot's own tokens and its `localStorage.bot_theme` | |
| 153 | +key; `?theme=light|dark` and the `{type:'sx-theme'}` postMessage both work, and | |
| 154 | +`?embed=1` hides the standalone chrome inside a bot modal. Zero `<script src=>`, | |
| 155 | +zero `<link rel=stylesheet>`, zero CDN — it renders offline and inside a strict | |
| 156 | +CSP. | |
| 157 | + | |
| 158 | +## API actions | |
| 159 | + | |
| 160 | +| Action | Arguments | Result | | |
| 161 | +|---|---|---| | |
| 162 | +| `current` (or `now`, or `format=json`) | `timezone` | the `get_current_time` object | | |
| 163 | +| `convert` | `source_timezone`, `time`, `target_timezone` | the `convert_time` object | | |
| 164 | +| `ago` | `timezone`, `days_ago`, `hours_ago`, `minutes_ago`, `seconds_ago` | the `get_time_ago` object | | |
| 165 | +| `zones` | — | every embedded key, sorted, plus the node's local zone | | |
| 166 | +| `tools` | — | the `tools/list` array | | |
| 167 | +| `info` | — | id, version, kind, zone count, local zone | | |
| 168 | + | |
| 169 | +Arguments come from the query string or a JSON body (`POST`), and the API | |
| 170 | +returns **byte-identical** objects to the MCP tools — `tests/run.sh` asserts | |
| 171 | +that, because a page that disagrees with the tool is worse than no page. | |
| 172 | + | |
| 173 | +## Safety | |
| 174 | + | |
| 175 | +`tests/run.sh` asserts the connector sources contain no `popen`, `system`, | |
| 176 | +`socket`, `connect`, `fork`, `execv`, no `fopen` for write/append, no `remove`, | |
| 177 | +`unlink`, `rmdir`, `mkdir`, `rename`, `chmod`, and no `curl`/`wget`. Every | |
| 178 | +`yyjson` string goes through the copying variant, so no document can be built | |
| 179 | +from a stack buffer that has already been reused. The ASan + UBSan + | |
| 180 | +`_FORTIFY_SOURCE=2` build runs the whole tool set, the repeated-hour path and the | |
| 181 | +page with the sanitizers on. | |
| 182 | + | |
| 183 | +The binary reads: the system clock, `$TZ`, `/etc/localtime` and `/etc/timezone` | |
| 184 | +(on POSIX), and the platform's own timezone info (on Windows). It writes nothing. | |
| 185 | + | |
| 186 | +## Licence | |
| 187 | + | |
| 188 | +MIT. The IANA timezone database it embeds is public domain; the generator and the | |
| 189 | +table are data, not code lifted from another implementation. |
| 1 | 1 | new file mode 100644 |
| index 0000000..259a265 | ||
| --- /dev/null | ||
| +++ b/docs/SECURITY.md | ||
| @@ -0,0 +1,52 @@ | ||
| 1 | +# Security notes | |
| 2 | + | |
| 3 | +## What the binary reads | |
| 4 | + | |
| 5 | +* the system clock (`time()`), for "now" - overridable with `SX_TIME_NOW` for | |
| 6 | + deterministic tests; | |
| 7 | +* `$TZ`, `/etc/localtime` (readlink) and `/etc/timezone` on POSIX, and the | |
| 8 | + platform timezone information on Windows, to name the node's own zone. | |
| 9 | + | |
| 10 | +That is the whole list. Everything else - every offset, abbreviation and DST | |
| 11 | +rule - comes from the table compiled into the binary. | |
| 12 | + | |
| 13 | +## What it does not do | |
| 14 | + | |
| 15 | +`tests/run.sh` asserts the connector sources contain none of: | |
| 16 | + | |
| 17 | +`popen`, `system`, `socket`, `connect`, `fork`, `execv`, `fopen` for write or | |
| 18 | +append, `remove`, `unlink`, `rmdir`, `mkdir`, `rename`, `chmod`, `curl`, `wget`. | |
| 19 | + | |
| 20 | +So there is no file written, no directory created, no program run and no outbound | |
| 21 | +request. The plugin is a pure function of (instant, zone name). | |
| 22 | + | |
| 23 | +## Authentication | |
| 24 | + | |
| 25 | +The page and the JSON API sit behind `require_node_auth()`, which accepts | |
| 26 | +`X-Bot-Password`, `Authorization: Bearer <pwd>` or `?pwd=` and 401s when | |
| 27 | +`BOT_PASSWORD` is set and none match. `--meta` and `--mcp` stay open: the node | |
| 28 | +spawns the stdio server itself, and `/plugins` must be able to discover the | |
| 29 | +connector. | |
| 30 | + | |
| 31 | +A bare `curl http://<node>:8150/plugin_time` on a node with `BOT_PASSWORD` set | |
| 32 | +answers **401**, which is the first check to run after installing anything. | |
| 33 | + | |
| 34 | +## Input handling | |
| 35 | + | |
| 36 | +* A timezone argument is validated against the embedded key list - exact, | |
| 37 | + case-sensitive - after a cheap rejection of `..`, `/`, `\`, `%`, control | |
| 38 | + characters and anything over 96 bytes. An unknown key is an error naming it; | |
| 39 | + it is never treated as a path. | |
| 40 | +* A JSON string whose declared length differs from its C string length (an | |
| 41 | + embedded NUL) is refused rather than silently truncated to its prefix. | |
| 42 | +* `time` must be exactly five characters of `HH:MM` with a real hour and minute. | |
| 43 | +* Every `yyjson` value is emitted with the *copying* constructor. The | |
| 44 | + non-copying one stores the caller's pointer, and every description and zone | |
| 45 | + name in this code lives in a stack buffer that is reused - which produces | |
| 46 | + valid-looking JSON with the wrong text. | |
| 47 | + | |
| 48 | +## Build hardening | |
| 49 | + | |
| 50 | +`make asan` builds with `-fsanitize=address,undefined -D_FORTIFY_SOURCE=2`, and | |
| 51 | +the suite drives the whole tool set, the repeated-hour path (the allocation-heavy | |
| 52 | +one) and the page through it, asserting no sanitizer output. |
| 1 | 1 | new file mode 100644 |
| index 0000000..6c269a0 |

| 1 | 1 | new file mode 100644 |
| index 0000000..394832b |

| 1 | 1 | new file mode 100644 |
| index 0000000..a55bad3 |

| 1 | 1 | new file mode 100644 |
| index 0000000..ee0485b |

| 1 | 1 | new file mode 100644 |
| index 0000000..8e45d4f |

| 1 | 1 | new file mode 100644 |
| index 0000000..8e554cc |

| 1 | 1 | new file mode 100644 |
| index 0000000..b66deb1 | ||
| --- /dev/null | ||
| +++ b/docs/usage.md | ||
| @@ -0,0 +1,105 @@ | ||
| 1 | +# Using the time connector | |
| 2 | + | |
| 3 | +Three MCP tools and one page, all backed by a timezone database compiled into the | |
| 4 | +binary. This guide is written for a person setting it up, not for an agent. | |
| 5 | + | |
| 6 | +## 1. Install it | |
| 7 | + | |
| 8 | +Through SCORPIOX BOT: **Settings -> Plugins -> Marketplace**, find **time** and | |
| 9 | +pick a target. The prebuilt binary for your node's platform is verified against | |
| 10 | +its published SHA-256 and run through a `--meta` smoke test before it is moved | |
| 11 | +into place. | |
| 12 | + | |
| 13 | +By hand: | |
| 14 | + | |
| 15 | +```sh | |
| 16 | +cp plugin_time ~/.scorpiox/plugins/plugin_time | |
| 17 | +chmod +x ~/.scorpiox/plugins/plugin_time | |
| 18 | +``` | |
| 19 | + | |
| 20 | +Windows puts it in `%LOCALAPPDATA%\scorpiox\plugins\plugin_time.exe`; a BotCloud | |
| 21 | +container uses `/workspace/.scorpiox/plugins/plugin_time`. | |
| 22 | + | |
| 23 | +No restart is needed: the node discovers `plugin_*` on every request. | |
| 24 | + | |
| 25 | +## 2. Check the zone name you want | |
| 26 | + | |
| 27 | +```sh | |
| 28 | +curl -s -H "X-Bot-Password: $BOT_PASSWORD" \ | |
| 29 | + "http://127.0.0.1:8150/plugin_time?action=zones" | head -c 200 | |
| 30 | +``` | |
| 31 | + | |
| 32 | +Every key is an exact IANA name. `Europe/Warsaw` is a key; `europe/warsaw` is | |
| 33 | +not, on any platform, on purpose - a connector that accepts both on macOS and | |
| 34 | +only one on Linux makes the same call mean different things per node. | |
| 35 | + | |
| 36 | +## 3. Ask for the time | |
| 37 | + | |
| 38 | +```sh | |
| 39 | +curl -s -H "X-Bot-Password: $BOT_PASSWORD" \ | |
| 40 | + "http://127.0.0.1:8150/plugin_time?action=current&timezone=Pacific/Auckland" | |
| 41 | +``` | |
| 42 | + | |
| 43 | +```json | |
| 44 | +{"ok":true,"timezone":"Pacific/Auckland","datetime":"2026-11-14T13:00:00+13:00", | |
| 45 | + "day_of_week":"Saturday","is_dst":true,"utc_offset":"+13:00","abbreviation":"NZDT"} | |
| 46 | +``` | |
| 47 | + | |
| 48 | +Omit `timezone` and you get the node's own zone. | |
| 49 | + | |
| 50 | +## 4. Convert | |
| 51 | + | |
| 52 | +```sh | |
| 53 | +curl -s -H "X-Bot-Password: $BOT_PASSWORD" \ | |
| 54 | + "http://127.0.0.1:8150/plugin_time?action=convert&source_timezone=Pacific/Auckland&time=09:00&target_timezone=America/New_York" | |
| 55 | +``` | |
| 56 | + | |
| 57 | +Two things surprise people, and both are deliberate: | |
| 58 | + | |
| 59 | +* **A time that does not exist is an error.** On the morning a zone springs | |
| 60 | + forward, 02:30 never happens; the answer says so and names the date instead of | |
| 61 | + quietly returning 03:30 or 01:30. | |
| 62 | +* **A time that happens twice is reported twice.** On the night a zone falls | |
| 63 | + back, 01:30 happens at +01:00 and again at +00:00. `source`/`target` are the | |
| 64 | + first; `later_occurrence` is the second, with its own note. | |
| 65 | + | |
| 66 | +## 5. Ask what the time was | |
| 67 | + | |
| 68 | +```sh | |
| 69 | +curl -s -H "X-Bot-Password: $BOT_PASSWORD" \ | |
| 70 | + "http://127.0.0.1:8150/plugin_time?action=ago&timezone=Europe/London&hours_ago=5" | |
| 71 | +``` | |
| 72 | + | |
| 73 | +The span is subtracted from the UTC instant, so five hours across a clock change | |
| 74 | +is five real hours - not five readings of the wall clock, which would be four or | |
| 75 | +six. | |
| 76 | + | |
| 77 | +## 6. From an agent | |
| 78 | + | |
| 79 | +`plugin_time` is a connector, reached through the single `sx-connector-mcp` | |
| 80 | +router rather than its own MCP server slot: | |
| 81 | + | |
| 82 | +```sh | |
| 83 | +sx-connector-mcp --list # time should be listed | |
| 84 | +sx-connector-mcp --tools time # the three schemas | |
| 85 | +sx-connector-mcp --call time convert_time \ | |
| 86 | + '{"source_timezone":"Pacific/Auckland","time":"09:00","target_timezone":"Asia/Tokyo"}' | |
| 87 | +``` | |
| 88 | + | |
| 89 | +## 7. The page | |
| 90 | + | |
| 91 | +Open **Settings -> Plugins -> time -> Open**, or `/plugin_time` on the node | |
| 92 | +directly. The clock, the converter and the world-clock table all call the tools | |
| 93 | +every second, so what you see is what an agent gets. Add zones with the picker; | |
| 94 | +the list is remembered in your browser. | |
| 95 | + | |
| 96 | +## 8. Updating the database | |
| 97 | + | |
| 98 | +```sh | |
| 99 | +make tzdata # regenerate src/tzdata.h from /usr/share/zoneinfo | |
| 100 | +make test # the epochs in the suite are absolute, so a moved zone fails loudly | |
| 101 | +``` | |
| 102 | + | |
| 103 | +Only do that deliberately: a tzdata release can change historical transitions, | |
| 104 | +and the suite pinning absolute instants is what turns such a change into a | |
| 105 | +visible failure rather than a silently different answer. |
| 1 | 1 | new file mode 100644 |
| index 0000000..4643bd2 | ||
| --- /dev/null | ||
| +++ b/plugin.json | ||
| @@ -0,0 +1,24 @@ | ||
| 1 | +{ | |
| 2 | + "slug": "time", | |
| 3 | + "id": "plugin_time", | |
| 4 | + "name": "time", | |
| 5 | + "desc": "World time MCP", | |
| 6 | + "title": "World Time & Timezone Converter (MCP)", | |
| 7 | + "summary": "Zero-dependency MCP connector for timezones: get_current_time, convert_time and get_time_ago over IANA names, with the same answer on every node. The tz database is embedded in the binary (598 zones, real UTC transitions through 2087 plus each zone's own POSIX rule beyond that), so a Windows node with no tzdata resolves Pacific/Auckland exactly like a Linux one, and europe/warsaw is refused everywhere instead of only on case-sensitive filesystems. A repeated hour at a DST fall-back answers both occurrences, and a wall time skipped at spring-forward is an error naming the date rather than a silently shifted answer. The same three operations back a World Clock, converter and time-ago panel on the plugin page. Zero network requests.", | |
| 8 | + "kind": "mcp", | |
| 9 | + "mcp": true, | |
| 10 | + "daemon": false, | |
| 11 | + "menus": false, | |
| 12 | + "verified": "live", | |
| 13 | + "category": "utilities", | |
| 14 | + "license": "MIT", | |
| 15 | + "capabilities": [ | |
| 16 | + "mcp_stdio_server", | |
| 17 | + "embedded_iana_tzdata", | |
| 18 | + "no_network", | |
| 19 | + "no_state_write" | |
| 20 | + ], | |
| 21 | + "trust_notice": "Executes native C code on your node. It reads the node's own timezone name (TZ, /etc/localtime, /etc/timezone, or the platform's offset) and the system clock, and nothing else: no file is written, no directory is scanned, no program is run, and it makes zero outbound network requests. All timezone rules come from the IANA database compiled into this binary.", | |
| 22 | + "author": "SCORPIOX INC", | |
| 23 | + "homepage": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-time" | |
| 24 | +} |
| 1 | 1 | new file mode 100644 |
| index 0000000..0dd243b | ||
| --- /dev/null | ||
| +++ b/src/bot_theme_plugin.h | ||
| @@ -0,0 +1,129 @@ | ||
| 1 | +/* | |
| 2 | + * bot_theme_plugin.h -- SCORPIOX BOT theme tokens for node plugins. | |
| 3 | + * | |
| 4 | + * A trimmed copy of scorpiox-bot-web/bot_theme.h (the bot's single source of | |
| 5 | + * truth for its dual theme). A plugin runs inside the bot (Hub modal, Session | |
| 6 | + * Card modal, Chat floating window) or in its own tab, and in every case it | |
| 7 | + * must look like part of the bot: same tokens, same dark/light pair, same | |
| 8 | + * localStorage key, same prepaint order. | |
| 9 | + * | |
| 10 | + * Three rules, and nothing else: | |
| 11 | + * | |
| 12 | + * 1. Colours come from these tokens only. No plugin-private palette. | |
| 13 | + * 2. The theme is decided BEFORE first paint, in this order: | |
| 14 | + * ?theme=light|dark -> localStorage.bot_theme -> prefers-color-scheme | |
| 15 | + * so a plugin opened from the bot paints in the bot's theme from frame 0. | |
| 16 | + * 3. When the bot's theme changes it posts {type:'sx-theme',theme:'...'} into | |
| 17 | + * this window; the plugin follows without a reload. The plugin asks once | |
| 18 | + * with {type:'sx-theme-request'} at startup, which covers the case where | |
| 19 | + * it was already open when the bot changed theme. | |
| 20 | + * | |
| 21 | + * The bottom of :root carries aliases for the names older plugin CSS already | |
| 22 | + * uses (--surface, --surface2, --muted, --accent, --accent2, --danger, --warn, | |
| 23 | + * --ok), so adopting the bot palette is a swap, not a redesign. Because they | |
| 24 | + * are var() references, one definition covers both themes. | |
| 25 | + * | |
| 26 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | |
| 27 | + */ | |
| 28 | +#ifndef SX_BOT_THEME_PLUGIN_H | |
| 29 | +#define SX_BOT_THEME_PLUGIN_H | |
| 30 | + | |
| 31 | +/* Dark is the default; light is applied by html[data-theme=light] (chosen by | |
| 32 | + * ?theme= or the bot) or by prefers-color-scheme when nothing chose. */ | |
| 33 | +#define SX_BOT_THEME_TOKENS \ | |
| 34 | +":root{color-scheme:dark;" \ | |
| 35 | + "--bg:#000000;--bg-2:#08080a;--bg-card:#1c1c1e;" \ | |
| 36 | + "--surface-2:rgba(255,255,255,.06);--surface-hover:rgba(255,255,255,.05);" \ | |
| 37 | + "--line:rgba(255,255,255,.08);--line-2:rgba(255,255,255,.16);--border:rgba(255,255,255,.08);" \ | |
| 38 | + "--text:#f5f5f7;--text-2:#86868b;--dim:#86868b;" \ | |
| 39 | + "--blue:#0a84ff;--green:#30d158;--orange:#ff9f0a;--red:#ff453a;--gray:#8e8e93;" \ | |
| 40 | + "--indigo:#5e5ce6;--teal:#64d2ff;--purple:#bf5af2;--yellow:#ffd60a;" \ | |
| 41 | + "--input-bg:rgba(255,255,255,.04);--modal-bg:#1c1c1e;--menu-bg:#1c1c1e;--code-bg:rgba(255,255,255,.06);" \ | |
| 42 | + "--mono:'SF Mono',ui-monospace,Menlo,Consolas,'DejaVu Sans Mono',monospace;" \ | |
| 43 | + "--font:-apple-system,BlinkMacSystemFont,'SF Pro Display','SF Pro Text','Inter','Segoe UI',Roboto,Helvetica,Arial,sans-serif;" \ | |
| 44 | + "--shadow:0 1px 2px rgba(0,0,0,.35);" \ | |
| 45 | + "--shadow-lg:0 30px 80px rgba(0,0,0,.65),0 0 0 1px rgba(255,255,255,.04);" \ | |
| 46 | + "--ease:cubic-bezier(.16,1,.3,1);" \ | |
| 47 | + "--surface:var(--bg-card);--surface2:var(--surface-2);--muted:var(--text-2);" \ | |
| 48 | + "--accent:var(--blue);--accent2:var(--green);--accent-dim:#0071e3;" \ | |
| 49 | + "--danger:var(--red);--err:var(--red);--warn:var(--orange);--ok:var(--green);" \ | |
| 50 | +"}" \ | |
| 51 | +"@media (prefers-color-scheme:light){html:not([data-theme=dark]){" \ | |
| 52 | + "--bg:#f5f5f7;--bg-2:#ffffff;--bg-card:#ffffff;" \ | |
| 53 | + "--surface-2:rgba(0,0,0,.04);--surface-hover:#ffffff;" \ | |
| 54 | + "--line:rgba(0,0,0,.06);--line-2:rgba(0,0,0,.12);--border:rgba(0,0,0,.06);" \ | |
| 55 | + "--text:#1d1d1f;--text-2:#6e6e73;--dim:#6e6e73;" \ | |
| 56 | + "--blue:#007aff;--green:#34c759;--orange:#ff9500;--red:#ff3b30;--gray:#8e8e93;" \ | |
| 57 | + "--indigo:#5856d6;--teal:#32ade6;--purple:#af52de;--yellow:#b8860b;" \ | |
| 58 | + "--accent-dim:#0066d6;" \ | |
| 59 | + "--input-bg:#f5f5f7;--modal-bg:#ffffff;--menu-bg:#ffffff;--code-bg:rgba(0,0,0,.05);" \ | |
| 60 | + "--shadow:0 2px 10px rgba(0,0,0,.03);" \ | |
| 61 | + "--shadow-lg:0 30px 80px rgba(0,0,0,.18),0 0 0 1px rgba(0,0,0,.04);" \ | |
| 62 | + "color-scheme:light}}" \ | |
| 63 | +"html[data-theme=light]{" \ | |
| 64 | + "--bg:#f5f5f7;--bg-2:#ffffff;--bg-card:#ffffff;" \ | |
| 65 | + "--surface-2:rgba(0,0,0,.04);--surface-hover:#ffffff;" \ | |
| 66 | + "--line:rgba(0,0,0,.06);--line-2:rgba(0,0,0,.12);--border:rgba(0,0,0,.06);" \ | |
| 67 | + "--text:#1d1d1f;--text-2:#6e6e73;--dim:#6e6e73;" \ | |
| 68 | + "--blue:#007aff;--green:#34c759;--orange:#ff9500;--red:#ff3b30;--gray:#8e8e93;" \ | |
| 69 | + "--indigo:#5856d6;--teal:#32ade6;--purple:#af52de;--yellow:#b8860b;" \ | |
| 70 | + "--accent-dim:#0066d6;" \ | |
| 71 | + "--input-bg:#f5f5f7;--modal-bg:#ffffff;--menu-bg:#ffffff;--code-bg:rgba(0,0,0,.05);" \ | |
| 72 | + "--shadow:0 2px 10px rgba(0,0,0,.03);" \ | |
| 73 | + "--shadow-lg:0 30px 80px rgba(0,0,0,.18),0 0 0 1px rgba(0,0,0,.04);" \ | |
| 74 | + "color-scheme:light}" \ | |
| 75 | +"html{color-scheme:dark}html[data-theme=dark]{color-scheme:dark}" \ | |
| 76 | +"*{box-sizing:border-box}" \ | |
| 77 | +"body{margin:0;background:var(--bg);color:var(--text);font-family:var(--font);" \ | |
| 78 | + "-webkit-font-smoothing:antialiased}" \ | |
| 79 | +"button{font-family:inherit;letter-spacing:inherit}" \ | |
| 80 | +"button:active{transform:scale(.98)}" \ | |
| 81 | +"select option{background:var(--menu-bg);color:var(--text)}" \ | |
| 82 | +"a{color:var(--blue);text-decoration:none;font-weight:600}" \ | |
| 83 | +"/* Embed mode (?embed=1): the host owns the title and the theme, so the plugin" \ | |
| 84 | +" * hides its own back link and its own toggle. */" \ | |
| 85 | +"html.embedded .embed-hide,html.embedded #themeToggle{display:none!important}" \ | |
| 86 | +"@media(prefers-reduced-motion:reduce){*{animation:none!important;transition:none!important}}" | |
| 87 | + | |
| 88 | +/* Pre-paint: decide the theme before the first frame. Must be the first thing | |
| 89 | + * in <head> so there is no flash of the wrong theme. Also flags embed mode as | |
| 90 | + * html.embedded so the embed rules apply before first paint. */ | |
| 91 | +#define SX_BOT_THEME_PREPAINT \ | |
| 92 | +"<script>(function(){var t='';" \ | |
| 93 | +"try{var m=/[?&]theme=(light|dark)/.exec(location.search);if(m)t=m[1];}catch(e){}" \ | |
| 94 | +"if(!t){try{var s=localStorage.getItem('bot_theme');if(s==='light'||s==='dark')t=s;}catch(e){}}" \ | |
| 95 | +"if(!t){try{t=(window.matchMedia&&window.matchMedia('(prefers-color-scheme: light)').matches)?'light':'dark';}catch(e){t='dark';}}" \ | |
| 96 | +"document.documentElement.setAttribute('data-theme',t);" \ | |
| 97 | +"try{if(/[?&]embed=(1|true)(&|$)/.test(location.search))document.documentElement.classList.add('embedded');}catch(e){}" \ | |
| 98 | +"try{localStorage.setItem('bot_theme',t);}catch(e){}" \ | |
| 99 | +"})();</script>\n" | |
| 100 | + | |
| 101 | +/* Handoff: follow the bot. The host posts {type:'sx-theme',theme}; we ask once | |
| 102 | + * at startup in case it changed while this page was already open. Fires the | |
| 103 | + * same 'bot-theme' CustomEvent the bot pages use, so canvas surfaces can | |
| 104 | + * re-skin themselves from one listener. */ | |
| 105 | +#define SX_BOT_THEME_HANDOFF \ | |
| 106 | +"<script>(function(){" \ | |
| 107 | +"function apply(t){if(t!=='light'&&t!=='dark')return;" \ | |
| 108 | +"if(document.documentElement.getAttribute('data-theme')===t)return;" \ | |
| 109 | +"document.documentElement.setAttribute('data-theme',t);" \ | |
| 110 | +"try{localStorage.setItem('bot_theme',t);}catch(e){}" \ | |
| 111 | +"try{window.dispatchEvent(new CustomEvent('bot-theme',{detail:t}));}catch(e){}}" \ | |
| 112 | +"window.sxApplyTheme=apply;" \ | |
| 113 | +"window.sxTheme=function(){var t=document.documentElement.getAttribute('data-theme');return (t==='light'||t==='dark')?t:'dark';};" \ | |
| 114 | +"window.addEventListener('message',function(e){var d=e&&e.data;if(d&&d.type==='sx-theme')apply(d.theme);});" \ | |
| 115 | +"try{if(window.parent&&window.parent!==window)window.parent.postMessage({type:'sx-theme-request'},'*');}catch(e){}" \ | |
| 116 | +"})();</script>\n" | |
| 117 | + | |
| 118 | +/* Standalone toggle (hidden in embed mode by the CSS above). Only useful when | |
| 119 | + * the plugin is opened in its own tab; inside the bot the host owns the theme. | |
| 120 | + * Give it class 'icon-btn'-free styling from the page, or use the helper JS. */ | |
| 121 | +#define SX_BOT_THEME_TOGGLE_BTN \ | |
| 122 | +"<button class='embed-hide' id='themeToggle' type='button' title='Toggle appearance'>Theme</button>" | |
| 123 | + | |
| 124 | +#define SX_BOT_THEME_TOGGLE_JS \ | |
| 125 | +"<script>(function(){var b=document.getElementById('themeToggle');if(!b)return;" \ | |
| 126 | +"b.onclick=function(){window.sxApplyTheme(window.sxTheme()==='dark'?'light':'dark');};" \ | |
| 127 | +"})();</script>\n" | |
| 128 | + | |
| 129 | +#endif /* SX_BOT_THEME_PLUGIN_H */ |
| 1 | 1 | new file mode 100644 |
| index 0000000..9442641 | ||
| --- /dev/null | ||
| +++ b/src/plugin_time.c | ||
| @@ -0,0 +1,705 @@ | ||
| 1 | +/* | |
| 2 | + * plugin_time.c - entry point and HTTP front end for the SCORPIOX BOT world | |
| 3 | + * time connector. | |
| 4 | + * | |
| 5 | + * One binary, five modes, exactly as the plugin_<name> spec requires: | |
| 6 | + * | |
| 7 | + * 1. --meta / SX_PLUGIN_META=1 -> one line of discovery JSON, exit 0. | |
| 8 | + * Unauthenticated on purpose: /plugins probes it, and the | |
| 9 | + * sx-connector-mcp router probes it before it will run anything else. | |
| 10 | + * 2. --mcp -> MCP JSON-RPC 2.0 over stdio, one request | |
| 11 | + * per line, one response per line. Through the single sx-connector-mcp | |
| 12 | + * router this is what makes the plugin a tool provider for SCORPIOX CODE | |
| 13 | + * with no clang change and no extra MCP server slot. | |
| 14 | + * 3. --schema -> the tools/list array, for a human. | |
| 15 | + * 4. ?action=... | ?format=json | POST -> the JSON API the page talks to. | |
| 16 | + * Same three tools as --mcp, so the page can never drift from the agent. | |
| 17 | + * 5. GET (default) -> the World Clock & Timezone Converter page. | |
| 18 | + * | |
| 19 | + * Modes 4 and 5 sit behind require_node_auth(): a node's :8150 is reachable | |
| 20 | + * from the LAN. --meta and --mcp stay open so discovery and the router work; | |
| 21 | + * neither answers a web request. | |
| 22 | + * | |
| 23 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +#if defined(_WIN32) && !defined(_WIN32_WINNT) | |
| 27 | +# define _WIN32_WINNT 0x0601 | |
| 28 | +#endif | |
| 29 | + | |
| 30 | +#if !defined(_WIN32) | |
| 31 | +# define _POSIX_C_SOURCE 200809L | |
| 32 | +# define _DEFAULT_SOURCE 1 | |
| 33 | +# if defined(__APPLE__) | |
| 34 | +# define _DARWIN_C_SOURCE 1 | |
| 35 | +# endif | |
| 36 | +#endif | |
| 37 | + | |
| 38 | +#include <stdio.h> | |
| 39 | +#include <stdlib.h> | |
| 40 | +#include <string.h> | |
| 41 | +#include <ctype.h> | |
| 42 | + | |
| 43 | +#if defined(_WIN32) | |
| 44 | +# define WIN32_LEAN_AND_MEAN | |
| 45 | +# include <windows.h> | |
| 46 | +# include <io.h> | |
| 47 | +# include <fcntl.h> | |
| 48 | +/* MinGW's CRT starts stdout in TEXT mode and rewrites every \n into \r\n. A | |
| 49 | + * CGI header block must end with exactly CRLF CRLF; text mode turns that into | |
| 50 | + * \r\r\n\r\r\n, scorpiox-server never finds the separator, and the whole reply | |
| 51 | + * (headers included) is served as a body. Force binary stdio before main(). */ | |
| 52 | +__attribute__((constructor)) | |
| 53 | +static void sx_plugin_binary_stdio(void) | |
| 54 | +{ | |
| 55 | + _setmode(_fileno(stdout), _O_BINARY); | |
| 56 | + _setmode(_fileno(stdin), _O_BINARY); | |
| 57 | +} | |
| 58 | +#else | |
| 59 | +# include <unistd.h> | |
| 60 | +#endif | |
| 61 | + | |
| 62 | +#include "plugintime.h" | |
| 63 | +#include "tz.h" | |
| 64 | +#include "bot_theme_plugin.h" | |
| 65 | +#include "ui_html.h" | |
| 66 | + | |
| 67 | +/* ------------------------------------------------------------------ */ | |
| 68 | +/* --meta */ | |
| 69 | +/* ------------------------------------------------------------------ */ | |
| 70 | + | |
| 71 | +static int mode_meta(void) | |
| 72 | +{ | |
| 73 | + printf("{\"name\":\"%s\",\"desc\":\"%s\",\"kind\":\"mcp\",\"mcp\":true," | |
| 74 | + "\"daemon\":false,\"menus\":false,\"verified\":\"live\"," | |
| 75 | + "\"category\":\"utilities\",\"version\":\"%s\"}\n", | |
| 76 | + PLUGIN_NAME, PLUGIN_DESC, PLUGIN_VER); | |
| 77 | + fflush(stdout); | |
| 78 | + return 0; | |
| 79 | +} | |
| 80 | + | |
| 81 | +/* ------------------------------------------------------------------ */ | |
| 82 | +/* MCP over stdio */ | |
| 83 | +/* ------------------------------------------------------------------ */ | |
| 84 | + | |
| 85 | +static void send_raw(const char *json) | |
| 86 | +{ | |
| 87 | + fputs(json, stdout); | |
| 88 | + fputc('\n', stdout); | |
| 89 | + fflush(stdout); | |
| 90 | +} | |
| 91 | + | |
| 92 | +static void send_result_raw(const char *id_json, const char *result_json) | |
| 93 | +{ | |
| 94 | + buf_t b; | |
| 95 | + buf_init(&b); | |
| 96 | + buf_puts(&b, "{\"jsonrpc\":\"2.0\",\"id\":"); | |
| 97 | + buf_puts(&b, id_json ? id_json : "null"); | |
| 98 | + buf_puts(&b, ",\"result\":"); | |
| 99 | + buf_puts(&b, result_json ? result_json : "{}"); | |
| 100 | + buf_puts(&b, "}"); | |
| 101 | + send_raw(buf_str(&b)); | |
| 102 | + buf_free(&b); | |
| 103 | +} | |
| 104 | + | |
| 105 | +static void send_result(yyjson_val *id, const char *result_json) | |
| 106 | +{ | |
| 107 | + char *js = id ? yyjson_val_write(id, 0, NULL) : NULL; | |
| 108 | + send_result_raw(js ? js : "null", result_json); | |
| 109 | + free(js); | |
| 110 | +} | |
| 111 | + | |
| 112 | +static void send_error(yyjson_val *id, int code, const char *msg) | |
| 113 | +{ | |
| 114 | + buf_t b; | |
| 115 | + char *js = id ? yyjson_val_write(id, 0, NULL) : NULL; | |
| 116 | + | |
| 117 | + buf_init(&b); | |
| 118 | + buf_puts(&b, "{\"jsonrpc\":\"2.0\",\"id\":"); | |
| 119 | + /* A parse error has no id to echo; the spec says answer with null, and a | |
| 120 | + * silent drop makes a broken client look like a hung server. */ | |
| 121 | + buf_puts(&b, js ? js : "null"); | |
| 122 | + buf_printf(&b, ",\"error\":{\"code\":%d,\"message\":", code); | |
| 123 | + json_str(&b, msg); | |
| 124 | + buf_puts(&b, "}}"); | |
| 125 | + send_raw(buf_str(&b)); | |
| 126 | + buf_free(&b); | |
| 127 | + free(js); | |
| 128 | +} | |
| 129 | + | |
| 130 | +static void handle_request(yyjson_val *root, yyjson_val *id, const char *method) | |
| 131 | +{ | |
| 132 | + if (strcmp(method, "initialize") == 0) { | |
| 133 | + buf_t b; | |
| 134 | + char *local = NULL; | |
| 135 | + buf_init(&b); | |
| 136 | + buf_puts(&b, "{\"protocolVersion\":\"" MCP_PROTOCOL "\"," | |
| 137 | + "\"capabilities\":{\"tools\":{\"listChanged\":false}}," | |
| 138 | + "\"serverInfo\":{\"name\":\"" SERVER_NAME "\",\"version\":\""); | |
| 139 | + buf_puts(&b, PLUGIN_VER); | |
| 140 | + buf_puts(&b, "\"},\"instructions\":"); | |
| 141 | + { | |
| 142 | + buf_t d; | |
| 143 | + buf_init(&d); | |
| 144 | + buf_puts(&d, "IANA timezone queries and wall-clock conversion for " | |
| 145 | + "three tools: get_current_time, convert_time and " | |
| 146 | + "get_time_ago. Times are resolved from an embedded tz " | |
| 147 | + "database, so every platform answers identically. The " | |
| 148 | + "server's local timezone is "); | |
| 149 | + buf_puts(&d, tz_local_name()); | |
| 150 | + buf_puts(&d, "."); | |
| 151 | + json_str(&b, buf_str(&d)); | |
| 152 | + buf_free(&d); | |
| 153 | + } | |
| 154 | + buf_puts(&b, "}"); | |
| 155 | + send_result(id, buf_str(&b)); | |
| 156 | + buf_free(&b); | |
| 157 | + free(local); | |
| 158 | + return; | |
| 159 | + } | |
| 160 | + if (strcmp(method, "ping") == 0) { | |
| 161 | + send_result(id, "{}"); | |
| 162 | + return; | |
| 163 | + } | |
| 164 | + if (strcmp(method, "tools/list") == 0) { | |
| 165 | + char *tools = tools_list_json(); | |
| 166 | + buf_t b; | |
| 167 | + buf_init(&b); | |
| 168 | + buf_puts(&b, "{\"tools\":"); | |
| 169 | + buf_puts(&b, tools ? tools : "[]"); | |
| 170 | + buf_puts(&b, "}"); | |
| 171 | + send_result(id, buf_str(&b)); | |
| 172 | + buf_free(&b); | |
| 173 | + free(tools); | |
| 174 | + return; | |
| 175 | + } | |
| 176 | + if (strcmp(method, "tools/call") == 0) { | |
| 177 | + yyjson_val *params = yyjson_obj_get(root, "params"); | |
| 178 | + const char *name = NULL; | |
| 179 | + yyjson_val *args = NULL; | |
| 180 | + tool_out_t t; | |
| 181 | + char *res; | |
| 182 | + | |
| 183 | + if (params && yyjson_is_obj(params)) { | |
| 184 | + yyjson_val *nv = yyjson_obj_get(params, "name"); | |
| 185 | + if (nv && yyjson_is_str(nv)) name = yyjson_get_str(nv); | |
| 186 | + args = yyjson_obj_get(params, "arguments"); | |
| 187 | + if (args && !yyjson_is_obj(args)) args = NULL; | |
| 188 | + } | |
| 189 | + if (!name) { | |
| 190 | + send_error(id, -32602, "tools/call needs params.name"); | |
| 191 | + return; | |
| 192 | + } | |
| 193 | + if (tool_dispatch(name, args, &t) != 0) { | |
| 194 | + buf_t b; | |
| 195 | + buf_init(&b); | |
| 196 | + buf_puts(&b, "Unknown tool: "); | |
| 197 | + buf_puts(&b, name); | |
| 198 | + send_error(id, -32602, buf_str(&b)); | |
| 199 | + buf_free(&b); | |
| 200 | + tool_free(&t); | |
| 201 | + return; | |
| 202 | + } | |
| 203 | + res = tool_mcp_result(&t); | |
| 204 | + send_result(id, res ? res : "{\"content\":[],\"isError\":true}"); | |
| 205 | + free(res); | |
| 206 | + tool_free(&t); | |
| 207 | + return; | |
| 208 | + } | |
| 209 | + if (strcmp(method, "resources/list") == 0) { | |
| 210 | + send_result(id, "{\"resources\":[]}"); | |
| 211 | + return; | |
| 212 | + } | |
| 213 | + if (strcmp(method, "resources/templates/list") == 0) { | |
| 214 | + send_result(id, "{\"resourceTemplates\":[]}"); | |
| 215 | + return; | |
| 216 | + } | |
| 217 | + if (strcmp(method, "prompts/list") == 0) { | |
| 218 | + send_result(id, "{\"prompts\":[]}"); | |
| 219 | + return; | |
| 220 | + } | |
| 221 | + send_error(id, -32601, "Method not found"); | |
| 222 | +} | |
| 223 | + | |
| 224 | +static int mode_mcp(void) | |
| 225 | +{ | |
| 226 | + char *line = (char *)malloc((size_t)STDIN_LINE_MAX); | |
| 227 | + if (!line) return 1; | |
| 228 | + | |
| 229 | + for (;;) { | |
| 230 | + size_t n = 0; | |
| 231 | + int c; | |
| 232 | + | |
| 233 | + /* Read one line by hand: fgets with a fixed buffer would truncate a | |
| 234 | + * long arguments object, and a truncated line parses as valid-but- | |
| 235 | + * wrong JSON instead of failing. */ | |
| 236 | + for (;;) { | |
| 237 | + c = fgetc(stdin); | |
| 238 | + if (c == EOF) break; | |
| 239 | + if (c == '\n') break; | |
| 240 | + if (n + 1 < (size_t)STDIN_LINE_MAX) line[n++] = (char)c; | |
| 241 | + } | |
| 242 | + line[n] = '\0'; | |
| 243 | + if (n == 0) { | |
| 244 | + if (c == EOF) { free(line); return 0; } | |
| 245 | + continue; | |
| 246 | + } | |
| 247 | + { | |
| 248 | + yyjson_doc *doc = yyjson_read(line, n, 0); | |
| 249 | + yyjson_val *root, *mv, *id; | |
| 250 | + const char *method; | |
| 251 | + | |
| 252 | + if (!doc) { | |
| 253 | + send_error(NULL, -32700, "Parse error: not valid JSON"); | |
| 254 | + continue; | |
| 255 | + } | |
| 256 | + root = yyjson_doc_get_root(doc); | |
| 257 | + if (!root || !yyjson_is_obj(root)) { | |
| 258 | + yyjson_doc_free(doc); | |
| 259 | + send_error(NULL, -32600, "Invalid request: expected an object"); | |
| 260 | + continue; | |
| 261 | + } | |
| 262 | + mv = yyjson_obj_get(root, "method"); | |
| 263 | + method = (mv && yyjson_is_str(mv)) ? yyjson_get_str(mv) : NULL; | |
| 264 | + id = yyjson_obj_get(root, "id"); | |
| 265 | + | |
| 266 | + if (!method) { | |
| 267 | + yyjson_doc_free(doc); | |
| 268 | + if (yyjson_obj_get(root, "result") || | |
| 269 | + yyjson_obj_get(root, "error")) continue; | |
| 270 | + send_error(id, -32600, "Invalid request: missing method"); | |
| 271 | + continue; | |
| 272 | + } | |
| 273 | + if (!id) { /* notification: no reply */ | |
| 274 | + yyjson_doc_free(doc); | |
| 275 | + continue; | |
| 276 | + } | |
| 277 | + handle_request(root, id, method); | |
| 278 | + yyjson_doc_free(doc); | |
| 279 | + } | |
| 280 | + } | |
| 281 | +} | |
| 282 | + | |
| 283 | +/* ------------------------------------------------------------------ */ | |
| 284 | +/* Query parameters */ | |
| 285 | +/* ------------------------------------------------------------------ */ | |
| 286 | + | |
| 287 | +static int get_query_val(const char *qs, const char *key, char *out, size_t outsz) | |
| 288 | +{ | |
| 289 | + size_t klen; | |
| 290 | + | |
| 291 | + if (!qs || !key || !out || outsz == 0) return 0; | |
| 292 | + out[0] = '\0'; | |
| 293 | + klen = strlen(key); | |
| 294 | + while (*qs) { | |
| 295 | + if (strncmp(qs, key, klen) == 0 && qs[klen] == '=') { | |
| 296 | + size_t i = 0; | |
| 297 | + qs += klen + 1; | |
| 298 | + while (*qs && *qs != '&' && i + 1 < outsz) { | |
| 299 | + /* %XX and + decode: a timezone name never needs it, but an | |
| 300 | + * `America/Argentina/Buenos_Aires` over the wire does not, and | |
| 301 | + * a page passing `time=09:30` from a form does. */ | |
| 302 | + if (*qs == '%' && isxdigit((unsigned char)qs[1]) && | |
| 303 | + isxdigit((unsigned char)qs[2])) { | |
| 304 | + int hi = isdigit((unsigned char)qs[1]) ? qs[1] - '0' | |
| 305 | + : (tolower((unsigned char)qs[1]) - 'a' + 10); | |
| 306 | + int lo = isdigit((unsigned char)qs[2]) ? qs[2] - '0' | |
| 307 | + : (tolower((unsigned char)qs[2]) - 'a' + 10); | |
| 308 | + out[i++] = (char)((hi << 4) | lo); | |
| 309 | + qs += 3; | |
| 310 | + continue; | |
| 311 | + } | |
| 312 | + out[i++] = (*qs == '+') ? ' ' : *qs; | |
| 313 | + qs++; | |
| 314 | + } | |
| 315 | + out[i] = '\0'; | |
| 316 | + return 1; | |
| 317 | + } | |
| 318 | + qs = strchr(qs, '&'); | |
| 319 | + if (!qs) break; | |
| 320 | + qs++; | |
| 321 | + } | |
| 322 | + return 0; | |
| 323 | +} | |
| 324 | + | |
| 325 | +/* ------------------------------------------------------------------ */ | |
| 326 | +/* Node authentication */ | |
| 327 | +/* */ | |
| 328 | +/* The node's :8150 is LAN-reachable, so the page and the JSON API must */ | |
| 329 | +/* not answer a bare curl. Three credential forms because three callers */ | |
| 330 | +/* differ: bot-web proxies with a header, the plugin iframe appends */ | |
| 331 | +/* ?pwd=, and a script may send a bearer token. --meta and --mcp return */ | |
| 332 | +/* above this, so discovery and the router stay open. */ | |
| 333 | +/* ------------------------------------------------------------------ */ | |
| 334 | + | |
| 335 | +static void require_node_auth(void) | |
| 336 | +{ | |
| 337 | + const char *np = getenv("BOT_PASSWORD"); | |
| 338 | + const char *xp, *auth, *qs; | |
| 339 | + char qpwd[256]; | |
| 340 | + | |
| 341 | + if (!np || !*np) return; | |
| 342 | + | |
| 343 | + xp = getenv("HTTP_X_BOT_PASSWORD"); | |
| 344 | + if (xp && strcmp(xp, np) == 0) return; | |
| 345 | + | |
| 346 | + auth = getenv("HTTP_AUTHORIZATION"); | |
| 347 | + if (auth) { | |
| 348 | + while (*auth == ' ') auth++; | |
| 349 | + if (strncmp(auth, "Bearer ", 7) == 0) { | |
| 350 | + auth += 7; | |
| 351 | + while (*auth == ' ') auth++; | |
| 352 | + if (strcmp(auth, np) == 0) return; | |
| 353 | + } | |
| 354 | + } | |
| 355 | + | |
| 356 | + qs = getenv("QUERY_STRING"); | |
| 357 | + if (qs && get_query_val(qs, "pwd", qpwd, sizeof qpwd) && | |
| 358 | + strcmp(qpwd, np) == 0) return; | |
| 359 | + | |
| 360 | + fputs("Status: 401 Unauthorized\r\n" | |
| 361 | + "Content-Type: application/json; charset=utf-8\r\n" | |
| 362 | + "Cache-Control: no-store\r\n" | |
| 363 | + "X-Content-Type-Options: nosniff\r\n\r\n" | |
| 364 | + "{\"ok\":false,\"error\":\"invalid node password\"}\n", stdout); | |
| 365 | + exit(0); | |
| 366 | +} | |
| 367 | + | |
| 368 | +/* ------------------------------------------------------------------ */ | |
| 369 | +/* Responses */ | |
| 370 | +/* ------------------------------------------------------------------ */ | |
| 371 | + | |
| 372 | +static void json_header(void) | |
| 373 | +{ | |
| 374 | + fputs("Content-Type: application/json; charset=utf-8\r\n" | |
| 375 | + "Cache-Control: no-store\r\n" | |
| 376 | + "X-Content-Type-Options: nosniff\r\n\r\n", stdout); | |
| 377 | +} | |
| 378 | + | |
| 379 | +static void json_raw(const char *json) | |
| 380 | +{ | |
| 381 | + json_header(); | |
| 382 | + fputs(json ? json : "{\"ok\":true}", stdout); | |
| 383 | + fputc('\n', stdout); | |
| 384 | + fflush(stdout); | |
| 385 | +} | |
| 386 | + | |
| 387 | +static void json_err(int code, const char *msg) | |
| 388 | +{ | |
| 389 | + buf_t b; | |
| 390 | + const char *status = "Status: 500 Internal Server Error\r\n"; | |
| 391 | + | |
| 392 | + if (code == 400) status = "Status: 400 Bad Request\r\n"; | |
| 393 | + else if (code == 404) status = "Status: 404 Not Found\r\n"; | |
| 394 | + else if (code == 413) status = "Status: 413 Payload Too Large\r\n"; | |
| 395 | + | |
| 396 | + buf_init(&b); | |
| 397 | + buf_puts(&b, status); | |
| 398 | + buf_puts(&b, "Content-Type: application/json; charset=utf-8\r\n" | |
| 399 | + "Cache-Control: no-store\r\n" | |
| 400 | + "X-Content-Type-Options: nosniff\r\n\r\n"); | |
| 401 | + buf_puts(&b, "{\"ok\":false,\"error\":"); | |
| 402 | + json_str(&b, msg ? msg : "error"); | |
| 403 | + buf_puts(&b, "}\n"); | |
| 404 | + fputs(buf_str(&b), stdout); | |
| 405 | + fflush(stdout); | |
| 406 | + buf_free(&b); | |
| 407 | +} | |
| 408 | + | |
| 409 | +/* Reads the request body when CONTENT_LENGTH says there is one. Never blocks | |
| 410 | + * on an inherited empty stdin. */ | |
| 411 | +static char g_body[REQ_BODY_MAX]; | |
| 412 | +static yyjson_doc *g_body_doc = NULL; | |
| 413 | + | |
| 414 | +static int read_body(void) | |
| 415 | +{ | |
| 416 | + const char *cl = getenv("CONTENT_LENGTH"); | |
| 417 | + long len; | |
| 418 | + size_t got; | |
| 419 | + | |
| 420 | + g_body[0] = '\0'; | |
| 421 | + if (!cl || !*cl) return 0; | |
| 422 | + len = strtol(cl, NULL, 10); | |
| 423 | + if (len <= 0) return 0; | |
| 424 | + if ((size_t)len >= sizeof g_body) return -1; | |
| 425 | + got = fread(g_body, 1, (size_t)len, stdin); | |
| 426 | + g_body[got] = '\0'; | |
| 427 | + return (int)got; | |
| 428 | +} | |
| 429 | + | |
| 430 | +static yyjson_val *body_root(void) | |
| 431 | +{ | |
| 432 | + if (!g_body_doc) { | |
| 433 | + if (!g_body[0]) return NULL; | |
| 434 | + g_body_doc = yyjson_read(g_body, strlen(g_body), 0); | |
| 435 | + if (!g_body_doc) return NULL; | |
| 436 | + } | |
| 437 | + return yyjson_doc_get_root(g_body_doc); | |
| 438 | +} | |
| 439 | + | |
| 440 | +static int param_str(const char *qs, const char *key, char *out, size_t outsz) | |
| 441 | +{ | |
| 442 | + yyjson_val *root, *v; | |
| 443 | + | |
| 444 | + if (out && outsz) out[0] = '\0'; | |
| 445 | + if (qs && get_query_val(qs, key, out, outsz) && out && out[0]) return 1; | |
| 446 | + root = body_root(); | |
| 447 | + v = root ? yyjson_obj_get(root, key) : NULL; | |
| 448 | + if (v && yyjson_is_str(v)) { | |
| 449 | + snprintf(out, outsz, "%s", yyjson_get_str(v)); | |
| 450 | + return 1; | |
| 451 | + } | |
| 452 | + return 0; | |
| 453 | +} | |
| 454 | + | |
| 455 | +/* ------------------------------------------------------------------ */ | |
| 456 | +/* HTML page */ | |
| 457 | +/* ------------------------------------------------------------------ */ | |
| 458 | + | |
| 459 | +/* Expand the two control-byte placeholders from src/ui.html into the bot | |
| 460 | + * theme macros, so the page and the bot palette can never drift apart. */ | |
| 461 | +static void emit_ui(void) | |
| 462 | +{ | |
| 463 | + const char *p = UI_HTML; | |
| 464 | + | |
| 465 | + while (*p) { | |
| 466 | + if (*p == '\001') { | |
| 467 | + fputs(SX_BOT_THEME_PREPAINT "<style>" SX_BOT_THEME_TOKENS | |
| 468 | + ":root{--tx:var(--text);--mu:var(--text-2);--ln:var(--line);--ac:var(--blue)}" | |
| 469 | + "</style>", stdout); | |
| 470 | + p++; | |
| 471 | + } else if (*p == '\002') { | |
| 472 | + fputs(SX_BOT_THEME_HANDOFF, stdout); | |
| 473 | + p++; | |
| 474 | + } else { | |
| 475 | + putchar(*p++); | |
| 476 | + } | |
| 477 | + } | |
| 478 | +} | |
| 479 | + | |
| 480 | +static void mode_html(void) | |
| 481 | +{ | |
| 482 | + fputs("Content-Type: text/html; charset=utf-8\r\n" | |
| 483 | + "Cache-Control: no-store\r\n" | |
| 484 | + "X-Content-Type-Options: nosniff\r\n" | |
| 485 | + "X-Frame-Options: SAMEORIGIN\r\n" | |
| 486 | + "Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'; " | |
| 487 | + "style-src 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; " | |
| 488 | + "form-action 'none'; base-uri 'none'\r\n\r\n", stdout); | |
| 489 | + emit_ui(); | |
| 490 | +} | |
| 491 | + | |
| 492 | +/* ------------------------------------------------------------------ */ | |
| 493 | +/* JSON API */ | |
| 494 | +/* ------------------------------------------------------------------ */ | |
| 495 | + | |
| 496 | +/* Run one tool for an HTTP action. The page sends the same argument names as | |
| 497 | + * the MCP schema, so one dispatch serves both surfaces. */ | |
| 498 | +static void api_run(const char *tool, const char *qs) | |
| 499 | +{ | |
| 500 | + yyjson_mut_doc *md = yyjson_mut_doc_new(NULL); | |
| 501 | + yyjson_mut_val *args; | |
| 502 | + yyjson_doc *adoc; | |
| 503 | + yyjson_val *av; | |
| 504 | + tool_out_t t; | |
| 505 | + char *res; | |
| 506 | + const char *keys[] = { "timezone", "time", "source_timezone", | |
| 507 | + "target_timezone", "days_ago", "hours_ago", | |
| 508 | + "minutes_ago", "seconds_ago" }; | |
| 509 | + size_t i; | |
| 510 | + | |
| 511 | + if (!md) { json_err(500, "out of memory"); return; } | |
| 512 | + args = yyjson_mut_obj(md); | |
| 513 | + yyjson_mut_doc_set_root(md, args); | |
| 514 | + | |
| 515 | + for (i = 0; i < sizeof keys / sizeof keys[0]; i++) { | |
| 516 | + yyjson_val *bv; | |
| 517 | + char sval[512]; | |
| 518 | + | |
| 519 | + bv = body_root(); | |
| 520 | + if (bv && yyjson_is_obj(bv)) { | |
| 521 | + yyjson_val *v = yyjson_obj_get(bv, keys[i]); | |
| 522 | + if (v && yyjson_is_str(v)) { | |
| 523 | + yyjson_mut_obj_add_strcpy(md, args, keys[i], yyjson_get_str(v)); | |
| 524 | + continue; | |
| 525 | + } | |
| 526 | + if (v && yyjson_is_int(v)) { | |
| 527 | + yyjson_mut_obj_add_int(md, args, keys[i], | |
| 528 | + (long long)yyjson_get_sint(v)); | |
| 529 | + continue; | |
| 530 | + } | |
| 531 | + } | |
| 532 | + if (qs && get_query_val(qs, keys[i], sval, sizeof sval) && sval[0]) { | |
| 533 | + /* The integer fields are numeric arguments; a query string always | |
| 534 | + * arrives as text, so re-type them here rather than making every | |
| 535 | + * caller remember which surface wants which type. */ | |
| 536 | + if (strstr(keys[i], "_ago")) { | |
| 537 | + char *end = NULL; | |
| 538 | + long long n = strtoll(sval, &end, 10); | |
| 539 | + if (end && *end == '\0') | |
| 540 | + yyjson_mut_obj_add_int(md, args, keys[i], n); | |
| 541 | + else | |
| 542 | + yyjson_mut_obj_add_strcpy(md, args, keys[i], sval); | |
| 543 | + } else { | |
| 544 | + /* strcpy, not str: `sval` is one stack buffer reused for every | |
| 545 | + * key in this loop, so a borrowed pointer leaves every argument | |
| 546 | + * pointing at the last value it held. Silent, and the JSON | |
| 547 | + * still parses. */ | |
| 548 | + yyjson_mut_obj_add_strcpy(md, args, keys[i], sval); | |
| 549 | + } | |
| 550 | + } | |
| 551 | + } | |
| 552 | + | |
| 553 | + /* The tool implementations read an immutable yyjson_val; serialise the | |
| 554 | + * mutable object and parse it back so both surfaces share one argument | |
| 555 | + * shape (and so the HTTP path exercises the same reader as --mcp). */ | |
| 556 | + { | |
| 557 | + char *text = yyjson_mut_write(md, 0, NULL); | |
| 558 | + yyjson_mut_doc_free(md); | |
| 559 | + adoc = text ? yyjson_read(text, strlen(text), 0) : NULL; | |
| 560 | + free(text); | |
| 561 | + } | |
| 562 | + av = adoc ? yyjson_doc_get_root(adoc) : NULL; | |
| 563 | + | |
| 564 | + if (strcmp(tool, "get_current_time") == 0) tool_get_current_time(av, &t); | |
| 565 | + else if (strcmp(tool, "convert_time") == 0) tool_convert_time(av, &t); | |
| 566 | + else if (strcmp(tool, "get_time_ago") == 0) tool_get_time_ago(av, &t); | |
| 567 | + else { if (adoc) yyjson_doc_free(adoc); json_err(400, "unknown tool"); return; } | |
| 568 | + | |
| 569 | + res = tool_api_result(&t); | |
| 570 | + json_raw(res ? res : "{\"ok\":false,\"error\":\"no result\"}"); | |
| 571 | + free(res); | |
| 572 | + tool_free(&t); | |
| 573 | + if (adoc) yyjson_doc_free(adoc); | |
| 574 | +} | |
| 575 | + | |
| 576 | +/* The page's zone picker: every embedded key plus the local one. */ | |
| 577 | +static void api_zones(void) | |
| 578 | +{ | |
| 579 | + buf_t b; | |
| 580 | + int i; | |
| 581 | + | |
| 582 | + buf_init(&b); | |
| 583 | + buf_puts(&b, "{\"ok\":true,\"count\":"); | |
| 584 | + buf_printf(&b, "%d", tz_count()); | |
| 585 | + buf_puts(&b, ",\"local\":"); | |
| 586 | + json_str(&b, tz_local_name()); | |
| 587 | + buf_puts(&b, ",\"zones\":["); | |
| 588 | + for (i = 0; i < tz_count(); i++) { | |
| 589 | + if (i) buf_putc(&b, ','); | |
| 590 | + json_str(&b, tz_name_at(i)); | |
| 591 | + } | |
| 592 | + buf_puts(&b, "]}"); | |
| 593 | + json_raw(buf_str(&b)); | |
| 594 | + buf_free(&b); | |
| 595 | +} | |
| 596 | + | |
| 597 | +static int api_handle(void) | |
| 598 | +{ | |
| 599 | + const char *qs = getenv("QUERY_STRING"); | |
| 600 | + const char *method = getenv("REQUEST_METHOD"); | |
| 601 | + char action[64]; | |
| 602 | + int is_get; | |
| 603 | + | |
| 604 | + if (!qs) qs = ""; | |
| 605 | + is_get = !method || strcmp(method, "GET") == 0; | |
| 606 | + | |
| 607 | + require_node_auth(); | |
| 608 | + | |
| 609 | + if (!is_get && read_body() < 0) { | |
| 610 | + json_err(413, "request body too large"); | |
| 611 | + return 0; | |
| 612 | + } | |
| 613 | + | |
| 614 | + /* Two separate buffers: parsing `format` into `action` would clobber the | |
| 615 | + * action we just parsed and silently answer the default one. */ | |
| 616 | + action[0] = '\0'; | |
| 617 | + if (!get_query_val(qs, "action", action, sizeof action) || !action[0]) | |
| 618 | + param_str(qs, "action", action, sizeof action); | |
| 619 | + | |
| 620 | + if (!action[0]) { | |
| 621 | + if (strstr(qs, "format=json")) snprintf(action, sizeof action, "current"); | |
| 622 | + else { mode_html(); return 0; } | |
| 623 | + } | |
| 624 | + | |
| 625 | + if (strcmp(action, "current") == 0 || strcmp(action, "now") == 0) | |
| 626 | + api_run("get_current_time", qs); | |
| 627 | + else if (strcmp(action, "convert") == 0) | |
| 628 | + api_run("convert_time", qs); | |
| 629 | + else if (strcmp(action, "ago") == 0) | |
| 630 | + api_run("get_time_ago", qs); | |
| 631 | + else if (strcmp(action, "zones") == 0) | |
| 632 | + api_zones(); | |
| 633 | + else if (strcmp(action, "tools") == 0) { | |
| 634 | + char *t = tools_list_json(); | |
| 635 | + buf_t b; | |
| 636 | + buf_init(&b); | |
| 637 | + buf_puts(&b, "{\"ok\":true,\"tools\":"); | |
| 638 | + buf_puts(&b, t ? t : "[]"); | |
| 639 | + buf_puts(&b, "}"); | |
| 640 | + json_raw(buf_str(&b)); | |
| 641 | + buf_free(&b); | |
| 642 | + free(t); | |
| 643 | + } else if (strcmp(action, "info") == 0) { | |
| 644 | + buf_t b; | |
| 645 | + buf_init(&b); | |
| 646 | + buf_puts(&b, "{\"ok\":true,\"id\":\"" PLUGIN_ID "\",\"version\":\"" | |
| 647 | + PLUGIN_VER "\",\"kind\":\"mcp\",\"mcp\":true,\"zones\":"); | |
| 648 | + buf_printf(&b, "%d", tz_count()); | |
| 649 | + buf_puts(&b, ",\"local\":"); | |
| 650 | + json_str(&b, tz_local_name()); | |
| 651 | + buf_puts(&b, "}"); | |
| 652 | + json_raw(buf_str(&b)); | |
| 653 | + buf_free(&b); | |
| 654 | + } else { | |
| 655 | + json_err(400, "unknown action"); | |
| 656 | + } | |
| 657 | + if (g_body_doc) { yyjson_doc_free(g_body_doc); g_body_doc = NULL; } | |
| 658 | + return 0; | |
| 659 | +} | |
| 660 | + | |
| 661 | +/* ------------------------------------------------------------------ */ | |
| 662 | +/* main */ | |
| 663 | +/* ------------------------------------------------------------------ */ | |
| 664 | + | |
| 665 | +static void print_help(void) | |
| 666 | +{ | |
| 667 | + int i; | |
| 668 | + printf("%s %s - SCORPIOX BOT world time MCP connector\n\n" | |
| 669 | + "modes:\n" | |
| 670 | + " --meta print plugin discovery metadata and exit\n" | |
| 671 | + " --mcp serve MCP JSON-RPC 2.0 over stdin/stdout\n" | |
| 672 | + " --schema print the tools/list array\n" | |
| 673 | + " --version print version\n" | |
| 674 | + " (no args) CGI: World Clock page + ?action= API\n\n" | |
| 675 | + "tools: ", PLUGIN_ID, PLUGIN_VER); | |
| 676 | + for (i = 0; i < TOOL_COUNT; i++) | |
| 677 | + printf("%s%s", i ? ", " : "", TOOL_NAMES[i]); | |
| 678 | + printf("\nzones: %d embedded IANA zones; local is %s\n", | |
| 679 | + tz_count(), tz_local_name()); | |
| 680 | +} | |
| 681 | + | |
| 682 | +int main(int argc, char **argv) | |
| 683 | +{ | |
| 684 | + int i; | |
| 685 | + | |
| 686 | + for (i = 1; i < argc; i++) { | |
| 687 | + if (strcmp(argv[i], "--meta") == 0) return mode_meta(); | |
| 688 | + if (strcmp(argv[i], "--mcp") == 0) return mode_mcp(); | |
| 689 | + if (strcmp(argv[i], "--version") == 0) { | |
| 690 | + printf("%s %s\n", PLUGIN_ID, PLUGIN_VER); | |
| 691 | + return 0; | |
| 692 | + } | |
| 693 | + if (strcmp(argv[i], "--schema") == 0) { | |
| 694 | + char *t = tools_list_json(); | |
| 695 | + printf("%s\n", t ? t : "[]"); | |
| 696 | + free(t); | |
| 697 | + return 0; | |
| 698 | + } | |
| 699 | + if (strcmp(argv[i], "--help") == 0) { print_help(); return 0; } | |
| 700 | + } | |
| 701 | + if (getenv("SX_PLUGIN_META")) return mode_meta(); | |
| 702 | + if (getenv("SX_PLUGIN_MCP")) return mode_mcp(); | |
| 703 | + | |
| 704 | + return api_handle(); | |
| 705 | +} |
| 1 | 1 | new file mode 100644 |
| index 0000000..0aba4dd | ||
| --- /dev/null | ||
| +++ b/src/plugintime.h | ||
| @@ -0,0 +1,112 @@ | ||
| 1 | +/* | |
| 2 | + * plugintime.h - shared declarations for plugin_time. | |
| 3 | + * | |
| 4 | + * NOT time.h: gcc searches -I directories for #include <...> as well, so a | |
| 5 | + * local time.h silently shadows the C library's <time.h> and every struct tm | |
| 6 | + * in the tree stops existing. | |
| 7 | + * | |
| 8 | + * plugin_time is a SCORPIOX BOT connector plugin that reimplements the | |
| 9 | + * reference MCP "time" server in pure C99: IANA timezone queries and | |
| 10 | + * wall-clock conversions, served as MCP tools over stdio and as a JSON API | |
| 11 | + * behind a browser UI. | |
| 12 | + * | |
| 13 | + * plugin_time.c main(), --meta, --mcp loop, CGI dispatch, auth, HTML | |
| 14 | + * timeutil.c the three tools, ISO formatting, tool schemas | |
| 15 | + * tz.c the embedded IANA table: keys, offsets, wall resolution | |
| 16 | + * | |
| 17 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | |
| 18 | + */ | |
| 19 | +#ifndef SX_PLUGIN_TIME_H | |
| 20 | +#define SX_PLUGIN_TIME_H | |
| 21 | + | |
| 22 | +#include <stddef.h> | |
| 23 | +#include <stdint.h> | |
| 24 | +#include "yyjson.h" | |
| 25 | + | |
| 26 | +#define PLUGIN_SLUG "time" | |
| 27 | +#define PLUGIN_ID "plugin_time" | |
| 28 | +#define PLUGIN_VER "1.0.0" | |
| 29 | +#define PLUGIN_NAME "time" | |
| 30 | +#define PLUGIN_DESC "World time MCP" | |
| 31 | + | |
| 32 | +#define MCP_PROTOCOL "2025-06-18" | |
| 33 | +#define SERVER_NAME "time-server" | |
| 34 | + | |
| 35 | +#define TZ_NAME_MAX 96 | |
| 36 | +#define STDIN_LINE_MAX (1024 * 1024) | |
| 37 | +#define REQ_BODY_MAX (1024 * 1024) | |
| 38 | + | |
| 39 | +/* ------------------------------------------------------------------ */ | |
| 40 | +/* Growable byte buffer (timeutil.c) */ | |
| 41 | +/* ------------------------------------------------------------------ */ | |
| 42 | + | |
| 43 | +typedef struct { | |
| 44 | + char *p; | |
| 45 | + size_t n; | |
| 46 | + size_t cap; | |
| 47 | +} buf_t; | |
| 48 | + | |
| 49 | +void buf_init(buf_t *b); | |
| 50 | +int buf_need(buf_t *b, size_t extra); | |
| 51 | +void buf_add(buf_t *b, const char *s, size_t n); | |
| 52 | +void buf_puts(buf_t *b, const char *s); | |
| 53 | +void buf_putc(buf_t *b, int c); | |
| 54 | +void buf_printf(buf_t *b, const char *fmt, ...); | |
| 55 | +const char *buf_str(const buf_t *b); | |
| 56 | +void buf_free(buf_t *b); | |
| 57 | + | |
| 58 | +/* JSON string escape into a buf_t (safe against control characters). */ | |
| 59 | +void json_str(buf_t *b, const char *s); | |
| 60 | + | |
| 61 | +/* ------------------------------------------------------------------ */ | |
| 62 | +/* Time formatting */ | |
| 63 | +/* ------------------------------------------------------------------ */ | |
| 64 | + | |
| 65 | +/* Local wall time for a UTC instant in `offset` (east of UTC, seconds). */ | |
| 66 | +void time_split(int64_t utc, int offset, int *year, int *month, int *day, | |
| 67 | + int *hour, int *min, int *sec, int *weekday); | |
| 68 | +/* ISO-8601 with seconds precision and a numeric offset, e.g. | |
| 69 | + * 2026-11-14T13:21:10+13:00. `out` needs ~40 bytes. */ | |
| 70 | +void time_iso(int64_t utc, int offset, char *out, size_t outsz); | |
| 71 | +/* "+13:00", "-05:00", "+05:45", "+00:00". */ | |
| 72 | +void time_offset_str(int offset, char *out, size_t outsz); | |
| 73 | +/* "Friday" */ | |
| 74 | +const char *time_weekday_name(int weekday); | |
| 75 | +/* The current UTC instant; SX_TIME_NOW pins it for deterministic tests. */ | |
| 76 | +int64_t time_now_utc(void); | |
| 77 | + | |
| 78 | +/* "+3.0h" / "+5.75h" / "-0.5h" - the reference server's format. */ | |
| 79 | +void time_diff_str(int source_off, int target_off, char *out, size_t outsz); | |
| 80 | + | |
| 81 | +/* ------------------------------------------------------------------ */ | |
| 82 | +/* Tool results */ | |
| 83 | +/* ------------------------------------------------------------------ */ | |
| 84 | + | |
| 85 | +typedef struct { | |
| 86 | + yyjson_mut_doc *md; /* owns root */ | |
| 87 | + yyjson_mut_val *root; | |
| 88 | + char *error; /* error text when is_error */ | |
| 89 | + int is_error; | |
| 90 | +} tool_out_t; | |
| 91 | + | |
| 92 | +void tool_out_init(tool_out_t *t); | |
| 93 | +void tool_err(tool_out_t *t, const char *fmt, ...); | |
| 94 | +void tool_free(tool_out_t *t); | |
| 95 | + | |
| 96 | +void tool_get_current_time(yyjson_val *args, tool_out_t *t); | |
| 97 | +void tool_convert_time(yyjson_val *args, tool_out_t *t); | |
| 98 | +void tool_get_time_ago(yyjson_val *args, tool_out_t *t); | |
| 99 | + | |
| 100 | +/* The full tools/list array as JSON text (caller frees). */ | |
| 101 | +char *tools_list_json(void); | |
| 102 | +int tool_dispatch(const char *name, yyjson_val *args, tool_out_t *out); | |
| 103 | +/* MCP result: content[] + structuredContent + isError. Caller frees. */ | |
| 104 | +char *tool_mcp_result(tool_out_t *t); | |
| 105 | +/* HTTP API result: {"ok":true,...} or {"ok":false,"error":"..."}. Caller frees. */ | |
| 106 | +char *tool_api_result(tool_out_t *t); | |
| 107 | + | |
| 108 | +/* The three tool names, for --help and the page's tool list. */ | |
| 109 | +extern const char *const TOOL_NAMES[]; | |
| 110 | +extern const int TOOL_COUNT; | |
| 111 | + | |
| 112 | +#endif /* SX_PLUGIN_TIME_H */ |
| 1 | 1 | new file mode 100644 |
| index 0000000..bcbfcb9 | ||
| --- /dev/null | ||
| +++ b/src/timeutil.c | ||
| @@ -0,0 +1,825 @@ | ||
| 1 | +/* | |
| 2 | + * timeutil.c - the tools of plugin_time and the formatting they share. | |
| 3 | + * | |
| 4 | + * Three tools, matching the reference `mcp_server_time` server: | |
| 5 | + * | |
| 6 | + * get_current_time -> {timezone, datetime, day_of_week, is_dst} | |
| 7 | + * convert_time -> {source, target, time_difference, later_occurrence} | |
| 8 | + * get_time_ago -> {timezone, datetime, day_of_week, is_dst} | |
| 9 | + * | |
| 10 | + * The reference server's semantics are reproduced, not approximated: | |
| 11 | + * | |
| 12 | + * - an omitted timezone defaults to the node's own zone, and the tool | |
| 13 | + * descriptions name it so a model can default to it; | |
| 14 | + * - an unknown key is a tool error whose text starts "Invalid timezone:"; | |
| 15 | + * - `time` is HH:MM in 24-hour form, and anything else is | |
| 16 | + * "Invalid time format. Expected HH:MM [24-hour format]"; | |
| 17 | + * - a wall time skipped by a forward clock change is an error naming the | |
| 18 | + * date, not a silently shifted answer; | |
| 19 | + * - a wall time repeated by a backward clock change answers the FIRST | |
| 20 | + * occurrence in source/target and reports the second under | |
| 21 | + * `later_occurrence`, which is the behaviour of Python's fold=0/fold=1; | |
| 22 | + * - `time_difference` is "+3.0h", and a fractional zone like Nepal's +05:45 | |
| 23 | + * prints "+5.75h" (two decimals, trailing zeros stripped). | |
| 24 | + * | |
| 25 | + * Nothing in here reads the clock directly: now_utc() honours SX_TIME_NOW | |
| 26 | + * (an ISO-8601 UTC instant) so the suite can pin a date and prove a DST edge | |
| 27 | + * with the same answer on every platform. | |
| 28 | + * | |
| 29 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | |
| 30 | + */ | |
| 31 | +#if defined(_WIN32) && !defined(_WIN32_WINNT) | |
| 32 | +# define _WIN32_WINNT 0x0601 | |
| 33 | +#endif | |
| 34 | + | |
| 35 | +#if !defined(_WIN32) | |
| 36 | +# define _POSIX_C_SOURCE 200809L | |
| 37 | +# define _DEFAULT_SOURCE 1 | |
| 38 | +#endif | |
| 39 | + | |
| 40 | +#include <stdio.h> | |
| 41 | +#include <stdlib.h> | |
| 42 | +#include <string.h> | |
| 43 | +#include <stdarg.h> | |
| 44 | +#include <ctype.h> | |
| 45 | +#include <time.h> | |
| 46 | + | |
| 47 | +#include "plugintime.h" | |
| 48 | +#include "tz.h" | |
| 49 | + | |
| 50 | +const char *const TOOL_NAMES[] = { | |
| 51 | + "get_current_time", "convert_time", "get_time_ago" | |
| 52 | +}; | |
| 53 | +const int TOOL_COUNT = (int)(sizeof TOOL_NAMES / sizeof TOOL_NAMES[0]); | |
| 54 | + | |
| 55 | +/* ------------------------------------------------------------------ */ | |
| 56 | +/* Growable buffer */ | |
| 57 | +/* ------------------------------------------------------------------ */ | |
| 58 | + | |
| 59 | +void buf_init(buf_t *b) | |
| 60 | +{ | |
| 61 | + b->p = NULL; | |
| 62 | + b->n = 0; | |
| 63 | + b->cap = 0; | |
| 64 | +} | |
| 65 | + | |
| 66 | +int buf_need(buf_t *b, size_t extra) | |
| 67 | +{ | |
| 68 | + size_t want = b->n + extra + 1; | |
| 69 | + char *np; | |
| 70 | + size_t cap; | |
| 71 | + | |
| 72 | + if (want <= b->cap) return 0; | |
| 73 | + cap = b->cap ? b->cap : 256; | |
| 74 | + while (cap < want) { | |
| 75 | + if (cap > (size_t)-1 / 2) return -1; | |
| 76 | + cap *= 2; | |
| 77 | + } | |
| 78 | + np = (char *)realloc(b->p, cap); | |
| 79 | + if (!np) return -1; | |
| 80 | + b->p = np; | |
| 81 | + b->cap = cap; | |
| 82 | + return 0; | |
| 83 | +} | |
| 84 | + | |
| 85 | +void buf_add(buf_t *b, const char *s, size_t n) | |
| 86 | +{ | |
| 87 | + if (buf_need(b, n) != 0) return; | |
| 88 | + if (n) memcpy(b->p + b->n, s, n); | |
| 89 | + b->n += n; | |
| 90 | + b->p[b->n] = '\0'; | |
| 91 | +} | |
| 92 | + | |
| 93 | +void buf_puts(buf_t *b, const char *s) | |
| 94 | +{ | |
| 95 | + if (s) buf_add(b, s, strlen(s)); | |
| 96 | +} | |
| 97 | + | |
| 98 | +void buf_putc(buf_t *b, int c) | |
| 99 | +{ | |
| 100 | + char ch = (char)c; | |
| 101 | + buf_add(b, &ch, 1); | |
| 102 | +} | |
| 103 | + | |
| 104 | +void buf_printf(buf_t *b, const char *fmt, ...) | |
| 105 | +{ | |
| 106 | + va_list ap; | |
| 107 | + int need; | |
| 108 | + char tmp[512]; | |
| 109 | + | |
| 110 | + va_start(ap, fmt); | |
| 111 | + need = vsnprintf(tmp, sizeof tmp, fmt, ap); | |
| 112 | + va_end(ap); | |
| 113 | + if (need < 0) return; | |
| 114 | + if ((size_t)need < sizeof tmp) { | |
| 115 | + buf_add(b, tmp, (size_t)need); | |
| 116 | + return; | |
| 117 | + } | |
| 118 | + if (buf_need(b, (size_t)need) != 0) return; | |
| 119 | + va_start(ap, fmt); | |
| 120 | + vsnprintf(b->p + b->n, (size_t)need + 1, fmt, ap); | |
| 121 | + va_end(ap); | |
| 122 | + b->n += (size_t)need; | |
| 123 | +} | |
| 124 | + | |
| 125 | +const char *buf_str(const buf_t *b) | |
| 126 | +{ | |
| 127 | + return b->p ? b->p : ""; | |
| 128 | +} | |
| 129 | + | |
| 130 | +void buf_free(buf_t *b) | |
| 131 | +{ | |
| 132 | + free(b->p); | |
| 133 | + buf_init(b); | |
| 134 | +} | |
| 135 | + | |
| 136 | +void json_str(buf_t *b, const char *s) | |
| 137 | +{ | |
| 138 | + buf_putc(b, '"'); | |
| 139 | + if (s) { | |
| 140 | + for (; *s; s++) { | |
| 141 | + unsigned char c = (unsigned char)*s; | |
| 142 | + switch (c) { | |
| 143 | + case '"': buf_puts(b, "\\\""); break; | |
| 144 | + case '\\': buf_puts(b, "\\\\"); break; | |
| 145 | + case '\n': buf_puts(b, "\\n"); break; | |
| 146 | + case '\r': buf_puts(b, "\\r"); break; | |
| 147 | + case '\t': buf_puts(b, "\\t"); break; | |
| 148 | + default: | |
| 149 | + if (c < 0x20) buf_printf(b, "\\u%04x", c); | |
| 150 | + else buf_putc(b, (char)c); | |
| 151 | + } | |
| 152 | + } | |
| 153 | + } | |
| 154 | + buf_putc(b, '"'); | |
| 155 | +} | |
| 156 | + | |
| 157 | +/* ------------------------------------------------------------------ */ | |
| 158 | +/* Formatting */ | |
| 159 | +/* ------------------------------------------------------------------ */ | |
| 160 | + | |
| 161 | +void time_split(int64_t utc, int offset, int *year, int *month, int *day, | |
| 162 | + int *hour, int *min, int *sec, int *weekday) | |
| 163 | +{ | |
| 164 | + int64_t local = utc + offset; | |
| 165 | + int64_t days = local / 86400; | |
| 166 | + int64_t rem = local % 86400; | |
| 167 | + int y, m, d; | |
| 168 | + | |
| 169 | + /* Floor division: a negative instant must round toward the past, not | |
| 170 | + * toward zero, or a timestamp before 1970 gets the wrong date. */ | |
| 171 | + if (rem < 0) { | |
| 172 | + rem += 86400; | |
| 173 | + days -= 1; | |
| 174 | + } | |
| 175 | + tz_civil_from_days(days, &y, &m, &d); | |
| 176 | + if (year) *year = y; | |
| 177 | + if (month) *month = m; | |
| 178 | + if (day) *day = d; | |
| 179 | + if (hour) *hour = (int)(rem / 3600); | |
| 180 | + if (min) *min = (int)((rem % 3600) / 60); | |
| 181 | + if (sec) *sec = (int)(rem % 60); | |
| 182 | + if (weekday) *weekday = tz_weekday(days); | |
| 183 | +} | |
| 184 | + | |
| 185 | +void time_offset_str(int offset, char *out, size_t outsz) | |
| 186 | +{ | |
| 187 | + int a = offset < 0 ? -offset : offset; | |
| 188 | + char sign = offset < 0 ? '-' : '+'; | |
| 189 | + | |
| 190 | + snprintf(out, outsz, "%c%02d:%02d", sign, a / 3600, (a % 3600) / 60); | |
| 191 | +} | |
| 192 | + | |
| 193 | +void time_iso(int64_t utc, int offset, char *out, size_t outsz) | |
| 194 | +{ | |
| 195 | + int y, m, d, hh, mm, ss; | |
| 196 | + char off[16]; | |
| 197 | + | |
| 198 | + time_split(utc, offset, &y, &m, &d, &hh, &mm, &ss, NULL); | |
| 199 | + time_offset_str(offset, off, sizeof off); | |
| 200 | + snprintf(out, outsz, "%04d-%02d-%02dT%02d:%02d:%02d%s", y, m, d, hh, mm, ss, off); | |
| 201 | +} | |
| 202 | + | |
| 203 | +static const char *const WEEKDAYS[7] = { | |
| 204 | + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" | |
| 205 | +}; | |
| 206 | + | |
| 207 | +const char *time_weekday_name(int weekday) | |
| 208 | +{ | |
| 209 | + if (weekday < 0 || weekday > 6) return WEEKDAYS[0]; | |
| 210 | + return WEEKDAYS[weekday]; | |
| 211 | +} | |
| 212 | + | |
| 213 | +void time_diff_str(int source_off, int target_off, char *out, size_t outsz) | |
| 214 | +{ | |
| 215 | + int diff = target_off - source_off; | |
| 216 | + double hours = (double)diff / 3600.0; | |
| 217 | + char tmp[32]; | |
| 218 | + | |
| 219 | + /* The reference prints one decimal when the difference is a whole number | |
| 220 | + * of hours, otherwise two decimals with trailing zeros stripped. */ | |
| 221 | + if (diff % 3600 == 0) { | |
| 222 | + snprintf(out, outsz, "%+.1fh", hours); | |
| 223 | + return; | |
| 224 | + } | |
| 225 | + snprintf(tmp, sizeof tmp, "%+.2f", hours); | |
| 226 | + { | |
| 227 | + size_t n = strlen(tmp); | |
| 228 | + while (n > 0 && tmp[n - 1] == '0') tmp[--n] = '\0'; | |
| 229 | + if (n > 0 && tmp[n - 1] == '.') tmp[--n] = '\0'; | |
| 230 | + } | |
| 231 | + snprintf(out, outsz, "%sh", tmp); | |
| 232 | +} | |
| 233 | + | |
| 234 | +/* ------------------------------------------------------------------ */ | |
| 235 | +/* "now" */ | |
| 236 | +/* ------------------------------------------------------------------ */ | |
| 237 | + | |
| 238 | +static int parse_iso_utc(const char *s, int64_t *out) | |
| 239 | +{ | |
| 240 | + int y = 0, mo = 0, d = 0, hh = 0, mm = 0, ss = 0; | |
| 241 | + int n = 0; | |
| 242 | + | |
| 243 | + if (!s || !*s) return -1; | |
| 244 | + if (sscanf(s, "%4d-%2d-%2dT%2d:%2d:%2d%n", &y, &mo, &d, &hh, &mm, &ss, &n) < 6) | |
| 245 | + return -1; | |
| 246 | + if (s[n] && s[n] != 'Z' && s[n] != 'z' && s[n] != '+' && s[n] != '-') return -1; | |
| 247 | + if (mo < 1 || mo > 12 || d < 1 || d > 31) return -1; | |
| 248 | + if (hh < 0 || hh > 23 || mm < 0 || mm > 59 || ss < 0 || ss > 60) return -1; | |
| 249 | + *out = tz_days_from_civil(y, mo, d) * 86400 + hh * 3600 + mm * 60 + ss; | |
| 250 | + return 0; | |
| 251 | +} | |
| 252 | + | |
| 253 | +/* The current UTC instant. SX_TIME_NOW ("2026-03-29T01:30:00Z") pins it so a | |
| 254 | + * test can assert a DST edge without waiting for one; an unparseable value is | |
| 255 | + * ignored rather than fatal, because a broken env var must not take the | |
| 256 | + * connector down. */ | |
| 257 | +int64_t time_now_utc(void) | |
| 258 | +{ | |
| 259 | + const char *env = getenv("SX_TIME_NOW"); | |
| 260 | + int64_t pinned; | |
| 261 | + | |
| 262 | + if (env && *env && parse_iso_utc(env, &pinned) == 0) return pinned; | |
| 263 | + return (int64_t)time(NULL); | |
| 264 | +} | |
| 265 | + | |
| 266 | +/* ------------------------------------------------------------------ */ | |
| 267 | +/* Result plumbing */ | |
| 268 | +/* ------------------------------------------------------------------ */ | |
| 269 | + | |
| 270 | +void tool_out_init(tool_out_t *t) | |
| 271 | +{ | |
| 272 | + memset(t, 0, sizeof *t); | |
| 273 | +} | |
| 274 | + | |
| 275 | +void tool_err(tool_out_t *t, const char *fmt, ...) | |
| 276 | +{ | |
| 277 | + char tmp[768]; | |
| 278 | + va_list ap; | |
| 279 | + | |
| 280 | + va_start(ap, fmt); | |
| 281 | + vsnprintf(tmp, sizeof tmp, fmt, ap); | |
| 282 | + va_end(ap); | |
| 283 | + if (!t->error) { | |
| 284 | + size_t n = strlen(tmp); | |
| 285 | + t->error = (char *)malloc(n + 1); | |
| 286 | + if (t->error) memcpy(t->error, tmp, n + 1); | |
| 287 | + } | |
| 288 | + t->is_error = 1; | |
| 289 | +} | |
| 290 | + | |
| 291 | +void tool_free(tool_out_t *t) | |
| 292 | +{ | |
| 293 | + if (!t) return; | |
| 294 | + if (t->md) yyjson_mut_doc_free(t->md); | |
| 295 | + free(t->error); | |
| 296 | + memset(t, 0, sizeof *t); | |
| 297 | +} | |
| 298 | + | |
| 299 | +static void tool_start(tool_out_t *t) | |
| 300 | +{ | |
| 301 | + if (t->md) return; | |
| 302 | + t->md = yyjson_mut_doc_new(NULL); | |
| 303 | + if (!t->md) { tool_err(t, "out of memory"); return; } | |
| 304 | + t->root = yyjson_mut_obj(t->md); | |
| 305 | + if (!t->root) { | |
| 306 | + yyjson_mut_doc_free(t->md); | |
| 307 | + t->md = NULL; | |
| 308 | + tool_err(t, "out of memory"); | |
| 309 | + return; | |
| 310 | + } | |
| 311 | + yyjson_mut_doc_set_root(t->md, t->root); | |
| 312 | +} | |
| 313 | + | |
| 314 | +/* COPY, not reference: yyjson_mut_obj_add_str stores the caller's pointer, so a | |
| 315 | + * stack buffer (every description and timezone name in this file) is reused and | |
| 316 | + * the document serialises garbage. Silent, valid-looking JSON - pinned by the | |
| 317 | + * "description is not the timezone text" assertion in tests/run.sh. */ | |
| 318 | +static void add_str(yyjson_mut_doc *md, yyjson_mut_val *obj, | |
| 319 | + const char *k, const char *v) | |
| 320 | +{ | |
| 321 | + yyjson_mut_obj_add_strcpy(md, obj, k, v ? v : ""); | |
| 322 | +} | |
| 323 | + | |
| 324 | +static void add_bool(yyjson_mut_doc *md, yyjson_mut_val *obj, | |
| 325 | + const char *k, int v) | |
| 326 | +{ | |
| 327 | + yyjson_mut_obj_add_bool(md, obj, k, v ? true : false); | |
| 328 | +} | |
| 329 | + | |
| 330 | +/* Fill a TimeResult object. The two single-result tools fill the root object | |
| 331 | + * directly; convert_time nests one per side, so they are the same four fields | |
| 332 | + * everywhere and a caller never has to unwrap a "result" key. */ | |
| 333 | +static void time_result_fill(yyjson_mut_doc *md, yyjson_mut_val *o, | |
| 334 | + const tz_zone *z, int64_t utc, const char *name) | |
| 335 | +{ | |
| 336 | + int off, dst = 0; | |
| 337 | + char iso[64], abbr[24]; | |
| 338 | + | |
| 339 | + off = tz_offset(z, utc, &dst, abbr, sizeof abbr); | |
| 340 | + time_iso(utc, off, iso, sizeof iso); | |
| 341 | + | |
| 342 | + add_str(md, o, "timezone", name); | |
| 343 | + add_str(md, o, "datetime", iso); | |
| 344 | + { | |
| 345 | + int wd = 0; | |
| 346 | + time_split(utc, off, NULL, NULL, NULL, NULL, NULL, NULL, &wd); | |
| 347 | + add_str(md, o, "day_of_week", time_weekday_name(wd)); | |
| 348 | + } | |
| 349 | + add_bool(md, o, "is_dst", dst); | |
| 350 | + /* Two fields the reference does not carry: a caller reading the text | |
| 351 | + * block should not have to re-derive the numeric offset or the zone | |
| 352 | + * abbreviation, and neither changes the meaning of the standard four. */ | |
| 353 | + { | |
| 354 | + char offs[16]; | |
| 355 | + time_offset_str(off, offs, sizeof offs); | |
| 356 | + add_str(md, o, "utc_offset", offs); | |
| 357 | + add_str(md, o, "abbreviation", abbr); | |
| 358 | + } | |
| 359 | +} | |
| 360 | + | |
| 361 | +static yyjson_mut_val *time_result_obj(yyjson_mut_doc *md, const tz_zone *z, | |
| 362 | + int64_t utc, const char *name) | |
| 363 | +{ | |
| 364 | + yyjson_mut_val *o = yyjson_mut_obj(md); | |
| 365 | + time_result_fill(md, o, z, utc, name); | |
| 366 | + return o; | |
| 367 | +} | |
| 368 | + | |
| 369 | +/* Resolve a timezone argument; fills `name` (the default when absent). */ | |
| 370 | +static const tz_zone *zone_arg(yyjson_val *args, const char *key, | |
| 371 | + char *name, size_t namesz, tool_out_t *t) | |
| 372 | +{ | |
| 373 | + yyjson_val *v = args ? yyjson_obj_get(args, key) : NULL; | |
| 374 | + | |
| 375 | + if (v && yyjson_is_str(v)) { | |
| 376 | + const char *s = yyjson_get_str(v); | |
| 377 | + /* A string whose declared length is longer than the C string means an | |
| 378 | + * embedded NUL: refuse it rather than resolving a prefix. */ | |
| 379 | + if (strlen(s) != yyjson_get_len(v)) { | |
| 380 | + tool_err(t, "Invalid timezone: the name contains a NUL byte"); | |
| 381 | + return NULL; | |
| 382 | + } | |
| 383 | + if (!*s) { | |
| 384 | + tool_err(t, "Invalid timezone: the name is empty"); | |
| 385 | + return NULL; | |
| 386 | + } | |
| 387 | + snprintf(name, namesz, "%s", s); | |
| 388 | + } else if (v && !yyjson_is_null(v)) { | |
| 389 | + tool_err(t, "%s must be a string (an IANA timezone name)", key); | |
| 390 | + return NULL; | |
| 391 | + } else { | |
| 392 | + snprintf(name, namesz, "%s", tz_local_name()); | |
| 393 | + } | |
| 394 | + { | |
| 395 | + const tz_zone *z = tz_find(name); | |
| 396 | + if (!z) { | |
| 397 | + tool_err(t, "Invalid timezone: No time zone found with key %s", name); | |
| 398 | + return NULL; | |
| 399 | + } | |
| 400 | + return z; | |
| 401 | + } | |
| 402 | +} | |
| 403 | + | |
| 404 | +static int int_arg(yyjson_val *args, const char *key, int dflt, int lo, int hi, | |
| 405 | + tool_out_t *t) | |
| 406 | +{ | |
| 407 | + yyjson_val *v = args ? yyjson_obj_get(args, key) : NULL; | |
| 408 | + long long n; | |
| 409 | + | |
| 410 | + if (!v || yyjson_is_null(v)) return dflt; | |
| 411 | + if (yyjson_is_int(v)) { | |
| 412 | + n = (long long)yyjson_get_sint(v); | |
| 413 | + } else if (yyjson_is_str(v)) { | |
| 414 | + char *end = NULL; | |
| 415 | + n = strtoll(yyjson_get_str(v), &end, 10); | |
| 416 | + if (!end || *end) { | |
| 417 | + tool_err(t, "%s must be an integer (whole number)", key); | |
| 418 | + return dflt; | |
| 419 | + } | |
| 420 | + } else { | |
| 421 | + tool_err(t, "%s must be an integer (whole number)", key); | |
| 422 | + return dflt; | |
| 423 | + } | |
| 424 | + if (n < lo || n > hi) { | |
| 425 | + tool_err(t, "%s must be between %d and %d", key, lo, hi); | |
| 426 | + return dflt; | |
| 427 | + } | |
| 428 | + return (int)n; | |
| 429 | +} | |
| 430 | + | |
| 431 | +/* ------------------------------------------------------------------ */ | |
| 432 | +/* get_current_time */ | |
| 433 | +/* ------------------------------------------------------------------ */ | |
| 434 | + | |
| 435 | +void tool_get_current_time(yyjson_val *args, tool_out_t *t) | |
| 436 | +{ | |
| 437 | + char name[TZ_NAME_MAX + 1]; | |
| 438 | + const tz_zone *z; | |
| 439 | + int64_t now = time_now_utc(); | |
| 440 | + | |
| 441 | + tool_out_init(t); | |
| 442 | + z = zone_arg(args, "timezone", name, sizeof name, t); | |
| 443 | + if (!z) return; | |
| 444 | + | |
| 445 | + tool_start(t); | |
| 446 | + if (t->is_error) return; | |
| 447 | + time_result_fill(t->md, t->root, z, now, name); | |
| 448 | +} | |
| 449 | + | |
| 450 | +/* ------------------------------------------------------------------ */ | |
| 451 | +/* get_time_ago */ | |
| 452 | +/* ------------------------------------------------------------------ */ | |
| 453 | + | |
| 454 | +void tool_get_time_ago(yyjson_val *args, tool_out_t *t) | |
| 455 | +{ | |
| 456 | + char name[TZ_NAME_MAX + 1]; | |
| 457 | + const tz_zone *z; | |
| 458 | + int d, h, m, s; | |
| 459 | + int64_t span, past; | |
| 460 | + | |
| 461 | + tool_out_init(t); | |
| 462 | + z = zone_arg(args, "timezone", name, sizeof name, t); | |
| 463 | + if (!z) return; | |
| 464 | + | |
| 465 | + d = int_arg(args, "days_ago", 0, 0, 365000, t); | |
| 466 | + if (t->is_error) return; | |
| 467 | + h = int_arg(args, "hours_ago", 0, 0, 100000000, t); | |
| 468 | + if (t->is_error) return; | |
| 469 | + m = int_arg(args, "minutes_ago", 0, 0, 100000000, t); | |
| 470 | + if (t->is_error) return; | |
| 471 | + s = int_arg(args, "seconds_ago", 0, 0, 100000000, t); | |
| 472 | + if (t->is_error) return; | |
| 473 | + | |
| 474 | + /* The span is subtracted from the UTC instant, not the local wall clock: | |
| 475 | + * "5 hours ago" across a clock change is five real hours back. */ | |
| 476 | + span = (int64_t)d * 86400 + (int64_t)h * 3600 + (int64_t)m * 60 + (int64_t)s; | |
| 477 | + past = time_now_utc() - span; | |
| 478 | + | |
| 479 | + tool_start(t); | |
| 480 | + if (t->is_error) return; | |
| 481 | + time_result_fill(t->md, t->root, z, past, name); | |
| 482 | +} | |
| 483 | + | |
| 484 | +/* ------------------------------------------------------------------ */ | |
| 485 | +/* convert_time */ | |
| 486 | +/* ------------------------------------------------------------------ */ | |
| 487 | + | |
| 488 | +/* "HH:MM" -> seconds after midnight. Rejects anything else, including a | |
| 489 | + * trailing colon, a 24th hour or a 60th minute. */ | |
| 490 | +static int parse_hhmm(const char *s, int *out) | |
| 491 | +{ | |
| 492 | + /* Exactly HH:MM. sscanf("%2d:%2d") would also accept "9:00" and " 9:00", | |
| 493 | + * and the reference server rejects both, so the shape is checked by hand | |
| 494 | + * before a number is read. */ | |
| 495 | + if (!s || strlen(s) != 5) return -1; | |
| 496 | + if (!isdigit((unsigned char)s[0]) || !isdigit((unsigned char)s[1])) return -1; | |
| 497 | + if (s[2] != ':') return -1; | |
| 498 | + if (!isdigit((unsigned char)s[3]) || !isdigit((unsigned char)s[4])) return -1; | |
| 499 | + { | |
| 500 | + int h = (s[0] - '0') * 10 + (s[1] - '0'); | |
| 501 | + int m = (s[3] - '0') * 10 + (s[4] - '0'); | |
| 502 | + if (h > 23 || m > 59) return -1; | |
| 503 | + *out = h * 3600 + m * 60; | |
| 504 | + } | |
| 505 | + return 0; | |
| 506 | +} | |
| 507 | + | |
| 508 | +void tool_convert_time(yyjson_val *args, tool_out_t *t) | |
| 509 | +{ | |
| 510 | + char src_name[TZ_NAME_MAX + 1]; | |
| 511 | + char dst_name[TZ_NAME_MAX + 1]; | |
| 512 | + const tz_zone *zs, *zd; | |
| 513 | + yyjson_val *tv; | |
| 514 | + const char *time_str = NULL; | |
| 515 | + int mins; | |
| 516 | + int64_t now, today, wall; | |
| 517 | + tz_cand_t cand[TZ_MAX_CANDIDATES]; | |
| 518 | + int ncand; | |
| 519 | + int i; | |
| 520 | + int off_s, off_t; | |
| 521 | + char diff[32]; | |
| 522 | + int so, mo, dd; | |
| 523 | + | |
| 524 | + tool_out_init(t); | |
| 525 | + | |
| 526 | + tv = args ? yyjson_obj_get(args, "time") : NULL; | |
| 527 | + if (!tv || !yyjson_is_str(tv)) { | |
| 528 | + tool_err(t, "Missing required argument: time"); | |
| 529 | + return; | |
| 530 | + } | |
| 531 | + time_str = yyjson_get_str(tv); | |
| 532 | + if (strlen(time_str) != yyjson_get_len(tv)) { | |
| 533 | + tool_err(t, "Invalid time format. Expected HH:MM [24-hour format]"); | |
| 534 | + return; | |
| 535 | + } | |
| 536 | + if (parse_hhmm(time_str, &mins) != 0) { | |
| 537 | + tool_err(t, "Invalid time format. Expected HH:MM [24-hour format]"); | |
| 538 | + return; | |
| 539 | + } | |
| 540 | + | |
| 541 | + zs = zone_arg(args, "source_timezone", src_name, sizeof src_name, t); | |
| 542 | + if (!zs) return; | |
| 543 | + zd = zone_arg(args, "target_timezone", dst_name, sizeof dst_name, t); | |
| 544 | + if (!zd) return; | |
| 545 | + | |
| 546 | + /* "Today" in the source zone, at the requested wall time. */ | |
| 547 | + now = time_now_utc(); | |
| 548 | + off_s = tz_offset(zs, now, NULL, NULL, 0); | |
| 549 | + time_split(now, off_s, NULL, NULL, NULL, NULL, NULL, NULL, NULL); | |
| 550 | + { | |
| 551 | + int y, m, d; | |
| 552 | + time_split(now, off_s, &y, &m, &d, NULL, NULL, NULL, NULL); | |
| 553 | + today = tz_days_from_civil(y, m, d); | |
| 554 | + so = y; mo = m; dd = d; | |
| 555 | + } | |
| 556 | + wall = today * 86400 + mins; | |
| 557 | + | |
| 558 | + ncand = tz_resolve_wall(zs, wall, cand, TZ_MAX_CANDIDATES); | |
| 559 | + if (ncand == 0) { | |
| 560 | + char date[32]; | |
| 561 | + time_split(wall, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL); | |
| 562 | + snprintf(date, sizeof date, "%04d-%02d-%02d", so, mo, dd); | |
| 563 | + tool_err(t, "Invalid time: %s does not exist in %s on %s " | |
| 564 | + "(skipped by a clock change)", time_str, src_name, date); | |
| 565 | + return; | |
| 566 | + } | |
| 567 | + | |
| 568 | + tool_start(t); | |
| 569 | + if (t->is_error) return; | |
| 570 | + | |
| 571 | + { | |
| 572 | + yyjson_mut_val *src_obj = time_result_obj(t->md, zs, cand[0].utc, src_name); | |
| 573 | + yyjson_mut_val *dst_obj = time_result_obj(t->md, zd, cand[0].utc, dst_name); | |
| 574 | + | |
| 575 | + off_s = tz_offset(zs, cand[0].utc, NULL, NULL, 0); | |
| 576 | + off_t = tz_offset(zd, cand[0].utc, NULL, NULL, 0); | |
| 577 | + time_diff_str(off_s, off_t, diff, sizeof diff); | |
| 578 | + | |
| 579 | + yyjson_mut_obj_add_val(t->md, t->root, "source", src_obj); | |
| 580 | + yyjson_mut_obj_add_val(t->md, t->root, "target", dst_obj); | |
| 581 | + add_str(t->md, t->root, "time_difference", diff); | |
| 582 | + | |
| 583 | + /* A repeated hour has two instants. The reference reports both rather | |
| 584 | + * than presenting the first as the only answer. */ | |
| 585 | + if (ncand > 1) { | |
| 586 | + yyjson_mut_val *later = yyjson_mut_obj(t->md); | |
| 587 | + yyjson_mut_val *lsrc, *ldst; | |
| 588 | + char note[512]; | |
| 589 | + char src_iso[64], later_iso[64]; | |
| 590 | + char d2[32]; | |
| 591 | + int o_s; | |
| 592 | + | |
| 593 | + o_s = tz_offset(zs, cand[0].utc, NULL, NULL, 0); | |
| 594 | + time_iso(cand[0].utc, o_s, src_iso, sizeof src_iso); | |
| 595 | + o_s = tz_offset(zs, cand[1].utc, NULL, NULL, 0); | |
| 596 | + time_iso(cand[1].utc, o_s, later_iso, sizeof later_iso); | |
| 597 | + | |
| 598 | + snprintf(note, sizeof note, | |
| 599 | + "%s occurs twice in %s on %04d-%02d-%02d because a clock " | |
| 600 | + "change repeats it: first at %s, then at %s. source and " | |
| 601 | + "target are the first occurrence; this is the second.", | |
| 602 | + time_str, src_name, so, mo, dd, src_iso, later_iso); | |
| 603 | + add_str(t->md, later, "note", note); | |
| 604 | + | |
| 605 | + lsrc = time_result_obj(t->md, zs, cand[1].utc, src_name); | |
| 606 | + ldst = time_result_obj(t->md, zd, cand[1].utc, dst_name); | |
| 607 | + off_s = tz_offset(zs, cand[1].utc, NULL, NULL, 0); | |
| 608 | + off_t = tz_offset(zd, cand[1].utc, NULL, NULL, 0); | |
| 609 | + time_diff_str(off_s, off_t, d2, sizeof d2); | |
| 610 | + | |
| 611 | + yyjson_mut_obj_add_val(t->md, later, "source", lsrc); | |
| 612 | + yyjson_mut_obj_add_val(t->md, later, "target", ldst); | |
| 613 | + add_str(t->md, later, "time_difference", d2); | |
| 614 | + yyjson_mut_obj_add_val(t->md, t->root, "later_occurrence", later); | |
| 615 | + } else { | |
| 616 | + yyjson_mut_obj_add_null(t->md, t->root, "later_occurrence"); | |
| 617 | + } | |
| 618 | + } | |
| 619 | + (void)i; | |
| 620 | + (void)off_s; | |
| 621 | + (void)off_t; | |
| 622 | +} | |
| 623 | + | |
| 624 | +/* ------------------------------------------------------------------ */ | |
| 625 | +/* Schemas and dispatch */ | |
| 626 | +/* ------------------------------------------------------------------ */ | |
| 627 | + | |
| 628 | +char *tools_list_json(void) | |
| 629 | +{ | |
| 630 | + buf_t b; | |
| 631 | + const char *local = tz_local_name(); | |
| 632 | + yyjson_mut_doc *md; | |
| 633 | + yyjson_mut_val *arr, *tool, *schema, *props, *req, *item; | |
| 634 | + | |
| 635 | + buf_init(&b); | |
| 636 | + md = yyjson_mut_doc_new(NULL); | |
| 637 | + if (!md) return NULL; | |
| 638 | + arr = yyjson_mut_arr(md); | |
| 639 | + yyjson_mut_doc_set_root(md, arr); | |
| 640 | + | |
| 641 | + /* get_current_time */ | |
| 642 | + tool = yyjson_mut_obj(md); | |
| 643 | + add_str(md, tool, "name", "get_current_time"); | |
| 644 | + { | |
| 645 | + char desc[512]; | |
| 646 | + snprintf(desc, sizeof desc, | |
| 647 | + "Get current time in a specific timezone"); | |
| 648 | + add_str(md, tool, "description", desc); | |
| 649 | + } | |
| 650 | + schema = yyjson_mut_obj(md); | |
| 651 | + add_str(md, schema, "type", "object"); | |
| 652 | + props = yyjson_mut_obj(md); | |
| 653 | + item = yyjson_mut_obj(md); | |
| 654 | + { | |
| 655 | + char td[512]; | |
| 656 | + add_str(md, item, "type", "string"); | |
| 657 | + snprintf(td, sizeof td, "IANA timezone name (e.g., 'America/New_York', " | |
| 658 | + "'Europe/London'). Optional: defaults to the server's local " | |
| 659 | + "timezone, '%s', when omitted.", local); | |
| 660 | + add_str(md, item, "description", td); | |
| 661 | + add_str(md, item, "default", local); | |
| 662 | + } | |
| 663 | + yyjson_mut_obj_add_val(md, props, "timezone", item); | |
| 664 | + yyjson_mut_obj_add_val(md, schema, "properties", props); | |
| 665 | + req = yyjson_mut_arr(md); | |
| 666 | + yyjson_mut_obj_add_val(md, tool, "inputSchema", schema); | |
| 667 | + yyjson_mut_obj_add_bool(md, tool, "readOnlyHint", true); | |
| 668 | + yyjson_mut_arr_append(arr, tool); | |
| 669 | + (void)req; | |
| 670 | + | |
| 671 | + /* convert_time */ | |
| 672 | + tool = yyjson_mut_obj(md); | |
| 673 | + add_str(md, tool, "name", "convert_time"); | |
| 674 | + add_str(md, tool, "description", "Convert time between timezones"); | |
| 675 | + schema = yyjson_mut_obj(md); | |
| 676 | + add_str(md, schema, "type", "object"); | |
| 677 | + props = yyjson_mut_obj(md); | |
| 678 | + { | |
| 679 | + char td[512]; | |
| 680 | + item = yyjson_mut_obj(md); | |
| 681 | + add_str(md, item, "type", "string"); | |
| 682 | + snprintf(td, sizeof td, "Source IANA timezone name (e.g., " | |
| 683 | + "'America/New_York', 'Europe/London'). Optional: defaults to " | |
| 684 | + "the server's local timezone, '%s', when omitted.", local); | |
| 685 | + add_str(md, item, "description", td); | |
| 686 | + add_str(md, item, "default", local); | |
| 687 | + yyjson_mut_obj_add_val(md, props, "source_timezone", item); | |
| 688 | + | |
| 689 | + item = yyjson_mut_obj(md); | |
| 690 | + add_str(md, item, "type", "string"); | |
| 691 | + add_str(md, item, "description", "Time to convert in 24-hour format (HH:MM)"); | |
| 692 | + yyjson_mut_obj_add_val(md, props, "time", item); | |
| 693 | + | |
| 694 | + item = yyjson_mut_obj(md); | |
| 695 | + add_str(md, item, "type", "string"); | |
| 696 | + snprintf(td, sizeof td, "Target IANA timezone name (e.g., 'Asia/Tokyo', " | |
| 697 | + "'America/Los_Angeles'). Optional: defaults to the server's " | |
| 698 | + "local timezone, '%s', when omitted.", local); | |
| 699 | + add_str(md, item, "description", td); | |
| 700 | + add_str(md, item, "default", local); | |
| 701 | + yyjson_mut_obj_add_val(md, props, "target_timezone", item); | |
| 702 | + } | |
| 703 | + yyjson_mut_obj_add_val(md, schema, "properties", props); | |
| 704 | + req = yyjson_mut_arr(md); | |
| 705 | + yyjson_mut_arr_add_str(md, req, "time"); | |
| 706 | + yyjson_mut_obj_add_val(md, schema, "required", req); | |
| 707 | + yyjson_mut_obj_add_val(md, tool, "inputSchema", schema); | |
| 708 | + yyjson_mut_obj_add_bool(md, tool, "readOnlyHint", true); | |
| 709 | + yyjson_mut_arr_append(arr, tool); | |
| 710 | + | |
| 711 | + /* get_time_ago */ | |
| 712 | + tool = yyjson_mut_obj(md); | |
| 713 | + add_str(md, tool, "name", "get_time_ago"); | |
| 714 | + add_str(md, tool, "description", | |
| 715 | + "Get the time a given number of days, hours, minutes and seconds " | |
| 716 | + "ago in a specific timezone"); | |
| 717 | + schema = yyjson_mut_obj(md); | |
| 718 | + add_str(md, schema, "type", "object"); | |
| 719 | + props = yyjson_mut_obj(md); | |
| 720 | + { | |
| 721 | + char td[512]; | |
| 722 | + item = yyjson_mut_obj(md); | |
| 723 | + add_str(md, item, "type", "string"); | |
| 724 | + snprintf(td, sizeof td, "IANA timezone name (e.g., 'America/New_York', " | |
| 725 | + "'Europe/London'). Optional: defaults to the server's local " | |
| 726 | + "timezone, '%s', when omitted.", local); | |
| 727 | + add_str(md, item, "description", td); | |
| 728 | + add_str(md, item, "default", local); | |
| 729 | + yyjson_mut_obj_add_val(md, props, "timezone", item); | |
| 730 | + | |
| 731 | + item = yyjson_mut_obj(md); | |
| 732 | + add_str(md, item, "type", "integer"); | |
| 733 | + add_str(md, item, "description", "Whole days before now"); | |
| 734 | + yyjson_mut_obj_add_int(md, item, "minimum", 0); | |
| 735 | + yyjson_mut_obj_add_val(md, props, "days_ago", item); | |
| 736 | + | |
| 737 | + item = yyjson_mut_obj(md); | |
| 738 | + add_str(md, item, "type", "integer"); | |
| 739 | + add_str(md, item, "description", "Whole hours before now"); | |
| 740 | + yyjson_mut_obj_add_int(md, item, "minimum", 0); | |
| 741 | + yyjson_mut_obj_add_val(md, props, "hours_ago", item); | |
| 742 | + | |
| 743 | + item = yyjson_mut_obj(md); | |
| 744 | + add_str(md, item, "type", "integer"); | |
| 745 | + add_str(md, item, "description", "Whole minutes before now"); | |
| 746 | + yyjson_mut_obj_add_int(md, item, "minimum", 0); | |
| 747 | + yyjson_mut_obj_add_val(md, props, "minutes_ago", item); | |
| 748 | + | |
| 749 | + item = yyjson_mut_obj(md); | |
| 750 | + add_str(md, item, "type", "integer"); | |
| 751 | + add_str(md, item, "description", "Whole seconds before now"); | |
| 752 | + yyjson_mut_obj_add_int(md, item, "minimum", 0); | |
| 753 | + yyjson_mut_obj_add_val(md, props, "seconds_ago", item); | |
| 754 | + } | |
| 755 | + yyjson_mut_obj_add_val(md, schema, "properties", props); | |
| 756 | + yyjson_mut_obj_add_val(md, tool, "inputSchema", schema); | |
| 757 | + yyjson_mut_obj_add_bool(md, tool, "readOnlyHint", true); | |
| 758 | + yyjson_mut_arr_append(arr, tool); | |
| 759 | + | |
| 760 | + { | |
| 761 | + char *text = yyjson_mut_write(md, 0, NULL); | |
| 762 | + yyjson_mut_doc_free(md); | |
| 763 | + return text; | |
| 764 | + } | |
| 765 | +} | |
| 766 | + | |
| 767 | +int tool_dispatch(const char *name, yyjson_val *args, tool_out_t *out) | |
| 768 | +{ | |
| 769 | + if (!name) return -1; | |
| 770 | + if (strcmp(name, "get_current_time") == 0) { tool_get_current_time(args, out); return 0; } | |
| 771 | + if (strcmp(name, "convert_time") == 0) { tool_convert_time(args, out); return 0; } | |
| 772 | + if (strcmp(name, "get_time_ago") == 0) { tool_get_time_ago(args, out); return 0; } | |
| 773 | + return -1; | |
| 774 | +} | |
| 775 | + | |
| 776 | +char *tool_mcp_result(tool_out_t *t) | |
| 777 | +{ | |
| 778 | + buf_t b; | |
| 779 | + | |
| 780 | + buf_init(&b); | |
| 781 | + if (t->is_error) { | |
| 782 | + buf_puts(&b, "{\"content\":[{\"type\":\"text\",\"text\":"); | |
| 783 | + json_str(&b, t->error ? t->error : "error"); | |
| 784 | + buf_puts(&b, "}],\"isError\":true}"); | |
| 785 | + return b.p; | |
| 786 | + } | |
| 787 | + { | |
| 788 | + char *payload = t->md ? yyjson_mut_write(t->md, 0, NULL) : NULL; | |
| 789 | + buf_puts(&b, "{\"content\":[{\"type\":\"text\",\"text\":"); | |
| 790 | + json_str(&b, payload ? payload : "{}"); | |
| 791 | + buf_puts(&b, "}],\"isError\":false"); | |
| 792 | + if (payload) { | |
| 793 | + buf_puts(&b, ",\"structuredContent\":"); | |
| 794 | + buf_puts(&b, payload); | |
| 795 | + } | |
| 796 | + buf_puts(&b, "}"); | |
| 797 | + free(payload); | |
| 798 | + } | |
| 799 | + return b.p; | |
| 800 | +} | |
| 801 | + | |
| 802 | +char *tool_api_result(tool_out_t *t) | |
| 803 | +{ | |
| 804 | + buf_t b; | |
| 805 | + | |
| 806 | + buf_init(&b); | |
| 807 | + if (t->is_error) { | |
| 808 | + buf_puts(&b, "{\"ok\":false,\"error\":"); | |
| 809 | + json_str(&b, t->error ? t->error : "error"); | |
| 810 | + buf_puts(&b, "}"); | |
| 811 | + return b.p; | |
| 812 | + } | |
| 813 | + { | |
| 814 | + char *payload = t->md ? yyjson_mut_write(t->md, 0, NULL) : NULL; | |
| 815 | + buf_puts(&b, "{\"ok\":true"); | |
| 816 | + if (payload && payload[0] == '{' && payload[1] != '}') { | |
| 817 | + buf_putc(&b, ','); | |
| 818 | + buf_puts(&b, payload + 1); | |
| 819 | + } else { | |
| 820 | + buf_putc(&b, '}'); | |
| 821 | + } | |
| 822 | + free(payload); | |
| 823 | + } | |
| 824 | + return b.p; | |
| 825 | +} |
| 1 | 1 | new file mode 100644 |
| index 0000000..2d507c4 | ||
| --- /dev/null | ||
| +++ b/src/tz.c | ||
| @@ -0,0 +1,738 @@ | ||
| 1 | +/* | |
| 2 | + * tz.c - IANA timezone resolution over the embedded table (src/tzdata.h). | |
| 3 | + * | |
| 4 | + * The design mirrors what the OS does, in three steps: | |
| 5 | + * | |
| 6 | + * 1. Look the zone up in the exact-key table. Never a filesystem path, so | |
| 7 | + * `europe/warsaw` is refused on every platform instead of only on the | |
| 8 | + * case-sensitive ones. | |
| 9 | + * 2. Answer a UTC instant from the embedded transition list, falling back | |
| 10 | + * to the zone's own POSIX TZ footer string beyond the last embedded | |
| 11 | + * transition - exactly what zoneinfo itself does after it runs out of | |
| 12 | + * the compiled table. | |
| 13 | + * 3. For a wall-clock instant the caller supplies (the `convert_time` | |
| 14 | + * tool), find the UTC instants that map to it. A forward clock change | |
| 15 | + * makes a wall time that does not exist (0 candidates); a backward | |
| 16 | + * change repeats an hour (2 candidates). Guessing one silently is how a | |
| 17 | + * converter reports a time that never happened. | |
| 18 | + * | |
| 19 | + * Nothing here reads the clock: every function is a pure function of its | |
| 20 | + * arguments, so a test can pin a date in 2035 and get the same answer on | |
| 21 | + * Linux, macOS and Windows. | |
| 22 | + * | |
| 23 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | |
| 24 | + */ | |
| 25 | +#if defined(_WIN32) && !defined(_WIN32_WINNT) | |
| 26 | +# define _WIN32_WINNT 0x0601 | |
| 27 | +#endif | |
| 28 | + | |
| 29 | +#if !defined(_WIN32) | |
| 30 | +# define _POSIX_C_SOURCE 200809L | |
| 31 | +# define _DEFAULT_SOURCE 1 | |
| 32 | +# if defined(__APPLE__) | |
| 33 | +# define _DARWIN_C_SOURCE 1 | |
| 34 | +# endif | |
| 35 | +#endif | |
| 36 | + | |
| 37 | +#include <stdio.h> | |
| 38 | +#include <stdlib.h> | |
| 39 | +#include <string.h> | |
| 40 | +#include <time.h> | |
| 41 | +#include <ctype.h> | |
| 42 | + | |
| 43 | +#if defined(_WIN32) | |
| 44 | +# define WIN32_LEAN_AND_MEAN | |
| 45 | +# include <windows.h> | |
| 46 | +#else | |
| 47 | +# include <unistd.h> | |
| 48 | +# include <limits.h> | |
| 49 | +#endif | |
| 50 | + | |
| 51 | +#include "tz.h" | |
| 52 | +#include "tzdata.h" | |
| 53 | + | |
| 54 | +/* ------------------------------------------------------------------ */ | |
| 55 | +/* The zone record */ | |
| 56 | +/* */ | |
| 57 | +/* tz_zone is the generated struct plus the name pointer, so callers */ | |
| 58 | +/* never index the string pool themselves. */ | |
| 59 | +/* ------------------------------------------------------------------ */ | |
| 60 | + | |
| 61 | +struct tz_zone { | |
| 62 | + const char *name; | |
| 63 | + const tz_zone_t *rec; | |
| 64 | + int index; | |
| 65 | +}; | |
| 66 | + | |
| 67 | +static tz_zone *g_zones = NULL; | |
| 68 | +static int g_nzones = 0; | |
| 69 | + | |
| 70 | +static void zones_build(void) | |
| 71 | +{ | |
| 72 | + int i; | |
| 73 | + if (g_zones) return; | |
| 74 | + g_zones = (tz_zone *)calloc((size_t)TZ_ZONE_COUNT, sizeof *g_zones); | |
| 75 | + if (!g_zones) return; | |
| 76 | + for (i = 0; i < TZ_ZONE_COUNT; i++) { | |
| 77 | + g_zones[i].name = TZ_NAME_POOL + TZ_ZONES[i].name; | |
| 78 | + g_zones[i].rec = &TZ_ZONES[i]; | |
| 79 | + g_zones[i].index = i; | |
| 80 | + } | |
| 81 | + g_nzones = TZ_ZONE_COUNT; | |
| 82 | +} | |
| 83 | + | |
| 84 | +int tz_count(void) | |
| 85 | +{ | |
| 86 | + zones_build(); | |
| 87 | + return g_nzones; | |
| 88 | +} | |
| 89 | + | |
| 90 | +const char *tz_name_at(int index) | |
| 91 | +{ | |
| 92 | + zones_build(); | |
| 93 | + if (index < 0 || index >= g_nzones) return NULL; | |
| 94 | + return g_zones[index].name; | |
| 95 | +} | |
| 96 | + | |
| 97 | +const tz_zone *tz_find(const char *name) | |
| 98 | +{ | |
| 99 | + int lo, hi; | |
| 100 | + | |
| 101 | + if (!name || !*name) return NULL; | |
| 102 | + /* An IANA key can never contain these: refused before any search so a | |
| 103 | + * path, a traversal or a `..` cannot be mistaken for a zone. */ | |
| 104 | + if (strstr(name, "..") || strchr(name, '\\') || strchr(name, '\n') || | |
| 105 | + strchr(name, '\r') || strchr(name, '%')) | |
| 106 | + return NULL; | |
| 107 | + if (name[0] == '/' || name[0] == '.' || name[0] == ' ') return NULL; | |
| 108 | + if (strlen(name) > 96) return NULL; | |
| 109 | + | |
| 110 | + zones_build(); | |
| 111 | + if (!g_zones) return NULL; | |
| 112 | + | |
| 113 | + /* The generated table is sorted by key, so this is a plain binary search | |
| 114 | + * with a case-sensitive compare - the whole point of the exact match. */ | |
| 115 | + lo = 0; | |
| 116 | + hi = g_nzones - 1; | |
| 117 | + while (lo <= hi) { | |
| 118 | + int mid = lo + (hi - lo) / 2; | |
| 119 | + int cmp = strcmp(name, g_zones[mid].name); | |
| 120 | + if (cmp == 0) return &g_zones[mid]; | |
| 121 | + if (cmp < 0) hi = mid - 1; | |
| 122 | + else lo = mid + 1; | |
| 123 | + } | |
| 124 | + return NULL; | |
| 125 | +} | |
| 126 | + | |
| 127 | +/* ------------------------------------------------------------------ */ | |
| 128 | +/* Civil date arithmetic (proleptic Gregorian, days since 1970-01-01) */ | |
| 129 | +/* ------------------------------------------------------------------ */ | |
| 130 | + | |
| 131 | +int64_t tz_days_from_civil(int y, int m, int d) | |
| 132 | +{ | |
| 133 | + int64_t era, yoe, doy, doe; | |
| 134 | + | |
| 135 | + y -= (m <= 2); | |
| 136 | + era = (y >= 0 ? y : y - 399) / 400; | |
| 137 | + yoe = y - era * 400; /* 0..399 */ | |
| 138 | + doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; /* 0..365 */ | |
| 139 | + doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; /* 0..146096 */ | |
| 140 | + return era * 146097 + doe - 719468; | |
| 141 | +} | |
| 142 | + | |
| 143 | +void tz_civil_from_days(int64_t days, int *y, int *m, int *d) | |
| 144 | +{ | |
| 145 | + int64_t z, era, doe, yoe, doy, mp; | |
| 146 | + int yy, mm, dd; | |
| 147 | + | |
| 148 | + z = days + 719468; | |
| 149 | + era = (z >= 0 ? z : z - 146096) / 146097; | |
| 150 | + doe = z - era * 146097; | |
| 151 | + yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; | |
| 152 | + yy = (int)(yoe + era * 400); | |
| 153 | + doy = doe - (365 * yoe + yoe / 4 - yoe / 100); | |
| 154 | + mp = (5 * doy + 2) / 153; | |
| 155 | + dd = (int)(doy - (153 * mp + 2) / 5 + 1); | |
| 156 | + mm = (int)(mp + (mp < 10 ? 3 : -9)); | |
| 157 | + if (y) *y = yy + (mm <= 2); | |
| 158 | + if (m) *m = mm; | |
| 159 | + if (d) *d = dd; | |
| 160 | +} | |
| 161 | + | |
| 162 | +int tz_weekday(int64_t days) | |
| 163 | +{ | |
| 164 | + /* 1970-01-01 was a Thursday (4). */ | |
| 165 | + int64_t w = (days + 4) % 7; | |
| 166 | + if (w < 0) w += 7; | |
| 167 | + return (int)w; | |
| 168 | +} | |
| 169 | + | |
| 170 | +/* ------------------------------------------------------------------ */ | |
| 171 | +/* POSIX TZ footer strings */ | |
| 172 | +/* */ | |
| 173 | +/* Beyond the last embedded transition the tz database defers to the */ | |
| 174 | +/* zone's own rule string, e.g. */ | |
| 175 | +/* NZST-12NZDT,M9.5.0,M4.1.0/3 (Auckland) */ | |
| 176 | +/* EST5EDT,M3.2.0,M11.1.0 (New York) */ | |
| 177 | +/* <-02>2<-01>,M3.5.0/-1,M10.5.0/0 (Godthab) */ | |
| 178 | +/* <+00>0<+01>,0/0,J365/25 (Casablanca, Ramadan) */ | |
| 179 | +/* */ | |
| 180 | +/* Offsets in the string are WEST positive, so the east-of-UTC offset we */ | |
| 181 | +/* store everywhere else is the negation. */ | |
| 182 | +/* ------------------------------------------------------------------ */ | |
| 183 | + | |
| 184 | +typedef struct { | |
| 185 | + int kind; /* 0 = Jn, 1 = n (zero-based), 2 = Mm.w.d */ | |
| 186 | + int n; /* Jn / n value, or the day-of-week for rules kind 2 */ | |
| 187 | + int week; /* kind 2: 1..5 (5 = last) */ | |
| 188 | + int month; /* kind 2 */ | |
| 189 | + int time; /* seconds after local midnight, may be <0 or >86400 */ | |
| 190 | +} rule_date; | |
| 191 | + | |
| 192 | +typedef struct { | |
| 193 | + int ok; | |
| 194 | + int std_off; /* east of UTC */ | |
| 195 | + int dst_off; /* east of UTC */ | |
| 196 | + int has_dst; | |
| 197 | + rule_date start, end; | |
| 198 | +} posix_tz; | |
| 199 | + | |
| 200 | +static const char *pz_name(const char *s, char *out, size_t outsz) | |
| 201 | +{ | |
| 202 | + size_t n = 0; | |
| 203 | + if (*s == '<') { | |
| 204 | + s++; | |
| 205 | + while (*s && *s != '>') { | |
| 206 | + if (n + 1 < outsz) out[n++] = *s; | |
| 207 | + s++; | |
| 208 | + } | |
| 209 | + if (*s == '>') s++; | |
| 210 | + } else { | |
| 211 | + while (*s && isalpha((unsigned char)*s)) { | |
| 212 | + if (n + 1 < outsz) out[n++] = *s; | |
| 213 | + s++; | |
| 214 | + } | |
| 215 | + } | |
| 216 | + out[n] = '\0'; | |
| 217 | + return s; | |
| 218 | +} | |
| 219 | + | |
| 220 | +static const char *pz_offset(const char *s, int *out, int *found) | |
| 221 | +{ | |
| 222 | + int sign = 1, h = 0, m = 0, sec = 0; | |
| 223 | + *found = 0; | |
| 224 | + if (*s == '+' || *s == '-') { | |
| 225 | + if (*s == '-') sign = -1; | |
| 226 | + s++; | |
| 227 | + } | |
| 228 | + if (!isdigit((unsigned char)*s) && *s != ':') return s; | |
| 229 | + while (isdigit((unsigned char)*s)) { h = h * 10 + (*s - '0'); s++; } | |
| 230 | + if (*s == ':') { | |
| 231 | + s++; | |
| 232 | + while (isdigit((unsigned char)*s)) { m = m * 10 + (*s - '0'); s++; } | |
| 233 | + if (*s == ':') { | |
| 234 | + s++; | |
| 235 | + while (isdigit((unsigned char)*s)) { sec = sec * 10 + (*s - '0'); s++; } | |
| 236 | + } | |
| 237 | + } | |
| 238 | + *out = sign * (h * 3600 + m * 60 + sec); | |
| 239 | + *found = 1; | |
| 240 | + return s; | |
| 241 | +} | |
| 242 | + | |
| 243 | +static const char *pz_time_of_day(const char *s, int *out) | |
| 244 | +{ | |
| 245 | + int sign = 1, h = 0, m = 0, sec = 0; | |
| 246 | + if (*s == '+' || *s == '-') { | |
| 247 | + if (*s == '-') sign = -1; | |
| 248 | + s++; | |
| 249 | + } | |
| 250 | + while (isdigit((unsigned char)*s)) { h = h * 10 + (*s - '0'); s++; } | |
| 251 | + if (*s == ':') { | |
| 252 | + s++; | |
| 253 | + while (isdigit((unsigned char)*s)) { m = m * 10 + (*s - '0'); s++; } | |
| 254 | + if (*s == ':') { | |
| 255 | + s++; | |
| 256 | + while (isdigit((unsigned char)*s)) { sec = sec * 10 + (*s - '0'); s++; } | |
| 257 | + } | |
| 258 | + } | |
| 259 | + *out = sign * (h * 3600 + m * 60 + sec); | |
| 260 | + return s; | |
| 261 | +} | |
| 262 | + | |
| 263 | +static const char *pz_rule(const char *s, rule_date *r) | |
| 264 | +{ | |
| 265 | + memset(r, 0, sizeof *r); | |
| 266 | + r->time = 2 * 3600; /* POSIX default: 02:00 local */ | |
| 267 | + if (*s == 'M') { | |
| 268 | + int m = 0, w = 0, d = 0; | |
| 269 | + s++; | |
| 270 | + while (isdigit((unsigned char)*s)) { m = m * 10 + (*s - '0'); s++; } | |
| 271 | + if (*s == '.') s++; | |
| 272 | + while (isdigit((unsigned char)*s)) { w = w * 10 + (*s - '0'); s++; } | |
| 273 | + if (*s == '.') s++; | |
| 274 | + while (isdigit((unsigned char)*s)) { d = d * 10 + (*s - '0'); s++; } | |
| 275 | + r->kind = 2; r->month = m; r->week = w; r->n = d; | |
| 276 | + } else if (*s == 'J') { | |
| 277 | + int n = 0; | |
| 278 | + s++; | |
| 279 | + while (isdigit((unsigned char)*s)) { n = n * 10 + (*s - '0'); s++; } | |
| 280 | + r->kind = 0; r->n = n; | |
| 281 | + } else { | |
| 282 | + int n = 0; | |
| 283 | + while (isdigit((unsigned char)*s)) { n = n * 10 + (*s - '0'); s++; } | |
| 284 | + r->kind = 1; r->n = n; | |
| 285 | + } | |
| 286 | + if (*s == '/') s = pz_time_of_day(s + 1, &r->time); | |
| 287 | + return s; | |
| 288 | +} | |
| 289 | + | |
| 290 | +static int posix_parse(const char *s, posix_tz *p) | |
| 291 | +{ | |
| 292 | + int found = 0; | |
| 293 | + char nm[24]; | |
| 294 | + | |
| 295 | + memset(p, 0, sizeof *p); | |
| 296 | + if (!s || !*s || *s == '\n') return -1; | |
| 297 | + | |
| 298 | + s = pz_name(s, nm, sizeof nm); | |
| 299 | + if (!nm[0]) return -1; | |
| 300 | + s = pz_offset(s, &p->std_off, &found); | |
| 301 | + if (!found) return -1; | |
| 302 | + p->std_off = -p->std_off; /* west -> east */ | |
| 303 | + | |
| 304 | + if (*s && *s != ',' && *s != '\n') { | |
| 305 | + s = pz_name(s, nm, sizeof nm); | |
| 306 | + if (!nm[0]) return -1; | |
| 307 | + p->has_dst = 1; | |
| 308 | + if (*s == '+' || *s == '-' || isdigit((unsigned char)*s)) { | |
| 309 | + int v = 0; | |
| 310 | + s = pz_offset(s, &v, &found); | |
| 311 | + if (!found) return -1; | |
| 312 | + p->dst_off = -v; | |
| 313 | + } else { | |
| 314 | + p->dst_off = p->std_off + 3600; /* POSIX default +1h */ | |
| 315 | + } | |
| 316 | + } | |
| 317 | + if (p->has_dst) { | |
| 318 | + if (*s != ',') return -1; /* a DST zone without rules | |
| 319 | + * is not in the shipped | |
| 320 | + * database; refuse rather | |
| 321 | + * than guess */ | |
| 322 | + s = pz_rule(s + 1, &p->start); | |
| 323 | + if (*s != ',') return -1; | |
| 324 | + s = pz_rule(s + 1, &p->end); | |
| 325 | + } | |
| 326 | + p->ok = 1; | |
| 327 | + return 0; | |
| 328 | +} | |
| 329 | + | |
| 330 | +/* The date a rule names in `year`, as days since the epoch. */ | |
| 331 | +static int64_t rule_day(const rule_date *r, int year) | |
| 332 | +{ | |
| 333 | + int64_t jan1 = tz_days_from_civil(year, 1, 1); | |
| 334 | + | |
| 335 | + if (r->kind == 0) { /* Jn: 1..365, no Feb 29 */ | |
| 336 | + int leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); | |
| 337 | + int doy = r->n - 1; | |
| 338 | + if (leap && r->n >= 60) doy += 1; | |
| 339 | + return jan1 + doy; | |
| 340 | + } | |
| 341 | + if (r->kind == 1) { /* n: 0..365, Feb 29 counts */ | |
| 342 | + return jan1 + r->n; | |
| 343 | + } | |
| 344 | + { | |
| 345 | + /* Mm.w.d: day d (0=Sunday) of week w of month m; w=5 means last. */ | |
| 346 | + int64_t first = tz_days_from_civil(year, r->month, 1); | |
| 347 | + int wd = tz_weekday(first); | |
| 348 | + int delta = (r->n - wd + 7) % 7; | |
| 349 | + int64_t day = first + delta + (int64_t)(r->week - 1) * 7; | |
| 350 | + int y2, m2, d2; | |
| 351 | + tz_civil_from_days(day, &y2, &m2, &d2); | |
| 352 | + while (m2 != r->month) { /* week 5 spilled over */ | |
| 353 | + day -= 7; | |
| 354 | + tz_civil_from_days(day, &y2, &m2, &d2); | |
| 355 | + } | |
| 356 | + return day; | |
| 357 | + } | |
| 358 | +} | |
| 359 | + | |
| 360 | +/* The UTC instant `year`'s rule fires at, given the offset in force before it. */ | |
| 361 | +static int64_t rule_utc(const rule_date *r, int year, int before_east) | |
| 362 | +{ | |
| 363 | + return rule_day(r, year) * 86400 + r->time - before_east; | |
| 364 | +} | |
| 365 | + | |
| 366 | +static int posix_is_dst(const posix_tz *p, int64_t utc) | |
| 367 | +{ | |
| 368 | + /* The rule names two instants per year; DST is in force after a start | |
| 369 | + * event and before the next end event. Sorting the events of the | |
| 370 | + * surrounding years and taking the last one at or before `utc` handles a | |
| 371 | + * northern rule, a southern rule (start after end within a year) and a | |
| 372 | + * rule whose changeovers sit at 24:00 or -01:00 without a special case | |
| 373 | + * for any of them. The earlier "does the window contain utc" form | |
| 374 | + * answered DST forever after a southern zone's start date, because | |
| 375 | + * "utc >= start of last season" stays true for the rest of the year. */ | |
| 376 | + struct { int64_t t; int on; } ev[9]; | |
| 377 | + int n = 0, i, j, k; | |
| 378 | + int year, month, day; | |
| 379 | + | |
| 380 | + if (!p->has_dst) return 0; | |
| 381 | + tz_civil_from_days((utc + p->std_off) / 86400, &year, &month, &day); | |
| 382 | + | |
| 383 | + for (k = -1; k <= 1; k++) { | |
| 384 | + int y = year + k; | |
| 385 | + if (n < 8) { ev[n].t = rule_utc(&p->start, y, p->std_off); ev[n++].on = 1; } | |
| 386 | + if (n < 8) { ev[n].t = rule_utc(&p->end, y, p->dst_off); ev[n++].on = 0; } | |
| 387 | + } | |
| 388 | + for (i = 1; i < n; i++) { /* insertion sort, n <= 9 */ | |
| 389 | + for (j = i; j > 0 && ev[j - 1].t > ev[j].t; j--) { | |
| 390 | + int64_t t = ev[j].t; | |
| 391 | + int on = ev[j].on; | |
| 392 | + ev[j].t = ev[j - 1].t; | |
| 393 | + ev[j].on = ev[j - 1].on; | |
| 394 | + ev[j - 1].t = t; | |
| 395 | + ev[j - 1].on = on; | |
| 396 | + } | |
| 397 | + } | |
| 398 | + for (i = n - 1; i >= 0; i--) if (ev[i].t <= utc) return ev[i].on; | |
| 399 | + return 0; | |
| 400 | +} | |
| 401 | +/* ------------------------------------------------------------------ */ | |
| 402 | +/* Offset lookup */ | |
| 403 | +/* ------------------------------------------------------------------ */ | |
| 404 | + | |
| 405 | +static int zone_abbr(const tz_zone *z, uint16_t off, char *abbr, size_t sz) | |
| 406 | +{ | |
| 407 | + const char *a = TZ_ABBR_POOL + off; | |
| 408 | + if (abbr && sz) { | |
| 409 | + size_t n = strlen(a); | |
| 410 | + if (n >= sz) n = sz - 1; | |
| 411 | + memcpy(abbr, a, n); | |
| 412 | + abbr[n] = '\0'; | |
| 413 | + } | |
| 414 | + (void)z; | |
| 415 | + return 0; | |
| 416 | +} | |
| 417 | + | |
| 418 | +int tz_offset(const tz_zone *z, int64_t utc, int *is_dst, | |
| 419 | + char *abbr, size_t abbrsz) | |
| 420 | +{ | |
| 421 | + const tz_zone_t *r; | |
| 422 | + int64_t rel; | |
| 423 | + int lo, hi, hit; | |
| 424 | + posix_tz pz; | |
| 425 | + | |
| 426 | + if (!z) return 0; | |
| 427 | + r = z->rec; | |
| 428 | + | |
| 429 | + if (utc < TZ_WINDOW_EPOCH) { | |
| 430 | + if (is_dst) *is_dst = r->base_dst; | |
| 431 | + zone_abbr(z, r->base_abbr, abbr, abbrsz); | |
| 432 | + return r->base_off; | |
| 433 | + } | |
| 434 | + rel = utc - TZ_WINDOW_EPOCH; | |
| 435 | + | |
| 436 | + /* Last transition at or before `rel` inside this zone's slice. */ | |
| 437 | + lo = 0; | |
| 438 | + hi = (int)r->count - 1; | |
| 439 | + hit = -1; | |
| 440 | + while (lo <= hi) { | |
| 441 | + int mid = lo + (hi - lo) / 2; | |
| 442 | + uint32_t t = TZ_TR_T[r->first + (uint32_t)mid]; | |
| 443 | + if ((int64_t)t <= rel) { hit = mid; lo = mid + 1; } | |
| 444 | + else { hi = mid - 1; } | |
| 445 | + } | |
| 446 | + if (hit >= 0) { | |
| 447 | + uint32_t i = r->first + (uint32_t)hit; | |
| 448 | + /* A query PAST this zone's last embedded transition must go to the | |
| 449 | + * POSIX rule below, not to the last row: the last row is whatever | |
| 450 | + * state the tz database stopped generating transitions in, and for | |
| 451 | + * most zones that is standard time in 2037. Returning it made every | |
| 452 | + * zone answer "no DST" from 2038 on - valid JSON, wrong answer, | |
| 453 | + * forever. */ | |
| 454 | + int past_last = (r->count > 0 && | |
| 455 | + (int64_t)TZ_TR_T[r->first + r->count - 1] < rel); | |
| 456 | + if (!(r->has_rule && past_last)) { | |
| 457 | + if (is_dst) *is_dst = TZ_TR_DST[i] ? 1 : 0; | |
| 458 | + zone_abbr(z, TZ_TR_ABBR[i], abbr, abbrsz); | |
| 459 | + return TZ_TR_OFF[i]; | |
| 460 | + } | |
| 461 | + } | |
| 462 | + | |
| 463 | + /* Before this zone's first embedded transition but after the window | |
| 464 | + * start: the base state still holds. */ | |
| 465 | + if (r->count == 0 || rel < (int64_t)TZ_TR_T[r->first]) { | |
| 466 | + /* Only fall through to the POSIX rule when we are past the zone's | |
| 467 | + * last transition (a zone whose slice is empty uses base forever). */ | |
| 468 | + if (r->count == 0) { | |
| 469 | + if (is_dst) *is_dst = r->base_dst; | |
| 470 | + zone_abbr(z, r->base_abbr, abbr, abbrsz); | |
| 471 | + return r->base_off; | |
| 472 | + } | |
| 473 | + } | |
| 474 | + | |
| 475 | + /* Past the last embedded transition: the zone's own POSIX rule decides, | |
| 476 | + * which is exactly how a host with less tzdata behaves. */ | |
| 477 | + if (r->has_rule && posix_parse(TZ_RULE_POOL + r->rule, &pz) == 0) { | |
| 478 | + int dst = posix_is_dst(&pz, utc); | |
| 479 | + if (is_dst) *is_dst = dst; | |
| 480 | + if (abbr && abbrsz) { | |
| 481 | + /* Reuse the abbreviation of the most recent embedded transition | |
| 482 | + * that had the same DST state; that is the name the zone uses. */ | |
| 483 | + int i; | |
| 484 | + const char *best = NULL; | |
| 485 | + for (i = (int)r->count - 1; i >= 0; i--) { | |
| 486 | + uint32_t j = r->first + (uint32_t)i; | |
| 487 | + if ((TZ_TR_DST[j] ? 1 : 0) == dst) { | |
| 488 | + best = TZ_ABBR_POOL + TZ_TR_ABBR[j]; | |
| 489 | + break; | |
| 490 | + } | |
| 491 | + } | |
| 492 | + if (!best) best = TZ_ABBR_POOL + r->base_abbr; | |
| 493 | + { | |
| 494 | + size_t n = strlen(best); | |
| 495 | + if (n >= abbrsz) n = abbrsz - 1; | |
| 496 | + memcpy(abbr, best, n); | |
| 497 | + abbr[n] = '\0'; | |
| 498 | + } | |
| 499 | + } | |
| 500 | + return dst ? pz.dst_off : pz.std_off; | |
| 501 | + } | |
| 502 | + | |
| 503 | + if (is_dst) *is_dst = r->base_dst; | |
| 504 | + zone_abbr(z, r->base_abbr, abbr, abbrsz); | |
| 505 | + return r->base_off; | |
| 506 | +} | |
| 507 | + | |
| 508 | +/* ------------------------------------------------------------------ */ | |
| 509 | +/* Wall clock -> UTC */ | |
| 510 | +/* */ | |
| 511 | +/* Every distinct offset in force anywhere near `wall` is a candidate; */ | |
| 512 | +/* an offset is valid only if applying it lands on an instant whose */ | |
| 513 | +/* offset is that same value. That test is what distinguishes the two */ | |
| 514 | +/* readings of a repeated hour from a time that never happened. */ | |
| 515 | +/* ------------------------------------------------------------------ */ | |
| 516 | + | |
| 517 | +static int cand_push(tz_cand_t *out, int n, int max, | |
| 518 | + int64_t utc, int off, int dst) | |
| 519 | +{ | |
| 520 | + int i; | |
| 521 | + for (i = 0; i < n; i++) if (out[i].utc == utc) return n; | |
| 522 | + if (n >= max) return n; | |
| 523 | + out[n].utc = utc; | |
| 524 | + out[n].offset = off; | |
| 525 | + out[n].is_dst = dst; | |
| 526 | + return n + 1; | |
| 527 | +} | |
| 528 | + | |
| 529 | +static int cand_cmp(const void *a, const void *b) | |
| 530 | +{ | |
| 531 | + int64_t x = ((const tz_cand_t *)a)->utc; | |
| 532 | + int64_t y = ((const tz_cand_t *)b)->utc; | |
| 533 | + return (x < y) ? -1 : (x > y) ? 1 : 0; | |
| 534 | +} | |
| 535 | + | |
| 536 | +int tz_resolve_wall(const tz_zone *z, int64_t wall, tz_cand_t *out, int outmax) | |
| 537 | +{ | |
| 538 | + const tz_zone_t *r; | |
| 539 | + int offs[TZ_MAX_CANDIDATES * 4]; | |
| 540 | + int noffs = 0; | |
| 541 | + int n = 0, i; | |
| 542 | + int64_t probes[7]; | |
| 543 | + int p; | |
| 544 | + | |
| 545 | + if (!z || !out || outmax <= 0) return 0; | |
| 546 | + r = z->rec; | |
| 547 | + | |
| 548 | + /* Offsets in force just before and just after the wall time, plus every | |
| 549 | + * transition within a two-day margin of it (a shift can be more than an | |
| 550 | + * hour, and a rule can move the change across midnight). */ | |
| 551 | + probes[0] = wall - 3 * 86400; | |
| 552 | + probes[1] = wall - 86400; | |
| 553 | + probes[2] = wall - 3600; | |
| 554 | + probes[3] = wall; | |
| 555 | + probes[4] = wall + 3600; | |
| 556 | + probes[5] = wall + 86400; | |
| 557 | + probes[6] = wall + 3 * 86400; | |
| 558 | + for (p = 0; p < 7; p++) { | |
| 559 | + int dst = 0; | |
| 560 | + int o = tz_offset(z, probes[p], &dst, NULL, 0); | |
| 561 | + int dup = 0; | |
| 562 | + for (i = 0; i < noffs; i++) if (offs[i] == o) { dup = 1; break; } | |
| 563 | + if (!dup && noffs < (int)(sizeof offs / sizeof offs[0])) offs[noffs++] = o; | |
| 564 | + } | |
| 565 | + { | |
| 566 | + int64_t lo = wall - 3 * 86400, hi = wall + 3 * 86400; | |
| 567 | + int64_t rel_lo = lo - TZ_WINDOW_EPOCH, rel_hi = hi - TZ_WINDOW_EPOCH; | |
| 568 | + int j; | |
| 569 | + for (j = 0; j < (int)r->count; j++) { | |
| 570 | + uint32_t k = r->first + (uint32_t)j; | |
| 571 | + int64_t t = (int64_t)TZ_TR_T[k]; | |
| 572 | + if (t < rel_lo) continue; | |
| 573 | + if (t > rel_hi) break; | |
| 574 | + { | |
| 575 | + int o = TZ_TR_OFF[k]; | |
| 576 | + int dup = 0; | |
| 577 | + for (i = 0; i < noffs; i++) if (offs[i] == o) { dup = 1; break; } | |
| 578 | + if (!dup && noffs < (int)(sizeof offs / sizeof offs[0])) | |
| 579 | + offs[noffs++] = o; | |
| 580 | + } | |
| 581 | + } | |
| 582 | + } | |
| 583 | + | |
| 584 | + for (i = 0; i < noffs; i++) { | |
| 585 | + int64_t utc = wall - offs[i]; | |
| 586 | + int dst = 0; | |
| 587 | + int o = tz_offset(z, utc, &dst, NULL, 0); | |
| 588 | + if (o == offs[i]) n = cand_push(out, n, outmax, utc, o, dst); | |
| 589 | + } | |
| 590 | + qsort(out, (size_t)n, sizeof *out, cand_cmp); | |
| 591 | + return n; | |
| 592 | +} | |
| 593 | + | |
| 594 | +/* ------------------------------------------------------------------ */ | |
| 595 | +/* The node's own zone */ | |
| 596 | +/* ------------------------------------------------------------------ */ | |
| 597 | + | |
| 598 | +static char g_local_name[128]; | |
| 599 | +static int g_local_done = 0; | |
| 600 | + | |
| 601 | +static int name_ok(const char *s) | |
| 602 | +{ | |
| 603 | + return s && *s && tz_find(s) != NULL; | |
| 604 | +} | |
| 605 | + | |
| 606 | +#if !defined(_WIN32) | |
| 607 | +/* /etc/localtime is a symlink into the zoneinfo tree on most Linux hosts and | |
| 608 | + * on macOS; the key is the tail of the link target after "zoneinfo/". */ | |
| 609 | +static int name_from_localtime_link(void) | |
| 610 | +{ | |
| 611 | + static const char *paths[2] = { "/etc/localtime", | |
| 612 | + "/var/db/timezone/localtime" }; | |
| 613 | + int i; | |
| 614 | + for (i = 0; i < 2; i++) { | |
| 615 | + char buf[PATH_MAX]; | |
| 616 | + ssize_t n = readlink(paths[i], buf, sizeof buf - 1); | |
| 617 | + const char *p; | |
| 618 | + if (n <= 0) continue; | |
| 619 | + buf[n] = '\0'; | |
| 620 | + p = strstr(buf, "zoneinfo/"); | |
| 621 | + if (p && name_ok(p + 9)) { | |
| 622 | + snprintf(g_local_name, sizeof g_local_name, "%s", p + 9); | |
| 623 | + return 1; | |
| 624 | + } | |
| 625 | + p = strstr(buf, "zoneinfo.default/"); | |
| 626 | + if (p && name_ok(p + 16)) { | |
| 627 | + snprintf(g_local_name, sizeof g_local_name, "%s", p + 16); | |
| 628 | + return 1; | |
| 629 | + } | |
| 630 | + } | |
| 631 | + return 0; | |
| 632 | +} | |
| 633 | + | |
| 634 | +/* Debian/Ubuntu keep the key in /etc/timezone as plain text. */ | |
| 635 | +static int name_from_etc_timezone(void) | |
| 636 | +{ | |
| 637 | + char buf[128]; | |
| 638 | + FILE *f = fopen("/etc/timezone", "r"); | |
| 639 | + size_t n; | |
| 640 | + | |
| 641 | + if (!f) return 0; | |
| 642 | + n = fread(buf, 1, sizeof buf - 1, f); | |
| 643 | + fclose(f); | |
| 644 | + if (n == 0) return 0; | |
| 645 | + buf[n] = '\0'; | |
| 646 | + while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r' || | |
| 647 | + buf[n - 1] == ' ' || buf[n - 1] == '\t')) buf[--n] = '\0'; | |
| 648 | + if (!name_ok(buf)) return 0; | |
| 649 | + snprintf(g_local_name, sizeof g_local_name, "%s", buf); | |
| 650 | + return 1; | |
| 651 | +} | |
| 652 | +#endif | |
| 653 | + | |
| 654 | +/* Offset-only fallback: the current local offset, DST state and - on POSIX - | |
| 655 | + * the abbreviation, matched against the embedded table. An offset alone cannot | |
| 656 | + * distinguish Europe/Paris (CET) from Africa/Lagos (WAT) in winter, so the | |
| 657 | + * abbreviation is the discriminator where the platform reports one. A | |
| 658 | + * "Region/City" key beats a bare legacy key (UTC, GMT, EST) so the answer is | |
| 659 | + * stable and useful; the table is sorted, so it is deterministic. */ | |
| 660 | +static int name_from_offset(void) | |
| 661 | +{ | |
| 662 | + int64_t now = (int64_t)time(NULL); | |
| 663 | + int off; | |
| 664 | + int dst; | |
| 665 | + int i; | |
| 666 | + int fallback = -1; | |
| 667 | + char abbr[24]; | |
| 668 | + | |
| 669 | + abbr[0] = '\0'; | |
| 670 | + | |
| 671 | +#if defined(_WIN32) | |
| 672 | + { | |
| 673 | + TIME_ZONE_INFORMATION tz; | |
| 674 | + DWORD kind = GetTimeZoneInformation(&tz); | |
| 675 | + long bias = tz.Bias + ((kind == TIME_ZONE_ID_DAYLIGHT) | |
| 676 | + ? tz.DaylightBias : tz.StandardBias); | |
| 677 | + off = (int)(-bias * 60); | |
| 678 | + dst = (kind == TIME_ZONE_ID_DAYLIGHT); | |
| 679 | + } | |
| 680 | +#else | |
| 681 | + { | |
| 682 | + time_t t = (time_t)now; | |
| 683 | + struct tm lt; | |
| 684 | + if (!localtime_r(&t, <)) return 0; | |
| 685 | + off = (int)lt.tm_gmtoff; | |
| 686 | + dst = lt.tm_isdst > 0; | |
| 687 | + if (lt.tm_zone) snprintf(abbr, sizeof abbr, "%s", lt.tm_zone); | |
| 688 | + } | |
| 689 | +#endif | |
| 690 | + for (i = 0; i < tz_count(); i++) { | |
| 691 | + const tz_zone *z = tz_find(tz_name_at(i)); | |
| 692 | + const char *nm = tz_name_at(i); | |
| 693 | + char za[24]; | |
| 694 | + int d = 0; | |
| 695 | + int o; | |
| 696 | + | |
| 697 | + if (!z) continue; | |
| 698 | + o = tz_offset(z, now, &d, za, sizeof za); | |
| 699 | + if (o != off || d != dst) continue; | |
| 700 | + if (abbr[0] && strcmp(za, abbr) != 0) continue; | |
| 701 | + if (!strchr(nm, '/')) { | |
| 702 | + if (fallback < 0) fallback = i; | |
| 703 | + continue; | |
| 704 | + } | |
| 705 | + fallback = i; | |
| 706 | + break; | |
| 707 | + } | |
| 708 | + if (fallback >= 0) { | |
| 709 | + snprintf(g_local_name, sizeof g_local_name, "%s", tz_name_at(fallback)); | |
| 710 | + return 1; | |
| 711 | + } | |
| 712 | + return 0; | |
| 713 | +} | |
| 714 | + | |
| 715 | +const char *tz_local_name(void) | |
| 716 | +{ | |
| 717 | + const char *env; | |
| 718 | + | |
| 719 | + if (g_local_done) return g_local_name; | |
| 720 | + g_local_done = 1; | |
| 721 | + | |
| 722 | + env = getenv("TZ"); | |
| 723 | + if (env && *env) { | |
| 724 | + if (env[0] == ':') env++; | |
| 725 | + if (name_ok(env)) { | |
| 726 | + snprintf(g_local_name, sizeof g_local_name, "%s", env); | |
| 727 | + return g_local_name; | |
| 728 | + } | |
| 729 | + } | |
| 730 | +#if !defined(_WIN32) | |
| 731 | + if (name_from_localtime_link()) return g_local_name; | |
| 732 | + if (name_from_etc_timezone()) return g_local_name; | |
| 733 | +#endif | |
| 734 | + if (name_from_offset()) return g_local_name; | |
| 735 | + | |
| 736 | + snprintf(g_local_name, sizeof g_local_name, "UTC"); | |
| 737 | + return g_local_name; | |
| 738 | +} |
| 1 | 1 | new file mode 100644 |
| index 0000000..cd2cedd | ||
| --- /dev/null | ||
| +++ b/src/tz.h | ||
| @@ -0,0 +1,74 @@ | ||
| 1 | +/* | |
| 2 | + * tz.h - IANA timezone resolution for plugin_time. | |
| 3 | + * | |
| 4 | + * The connector cannot read the host's tz database: a Windows node has none, | |
| 5 | + * macOS's is case-insensitive, and a slim container may have a different tzdata | |
| 6 | + * version, all of which would make the same tool call answer differently per | |
| 7 | + * node. So the table is embedded (src/tzdata.h, generated by | |
| 8 | + * tools/gen_tzdata.py) and every platform resolves identically: | |
| 9 | + * | |
| 10 | + * tz_find() exact, case-sensitive IANA key -> zone record (or NULL) | |
| 11 | + * tz_offset() UTC instant -> offset east of UTC, DST flag, abbreviation | |
| 12 | + * tz_resolve_wall() a wall-clock local time -> the UTC instants it can mean | |
| 13 | + * (two of them on a DST fall-back, none in a spring gap) | |
| 14 | + * tz_local_name() the node's own timezone name, best effort | |
| 15 | + * | |
| 16 | + * Times are int64 seconds since the Unix epoch; nothing here reads the system | |
| 17 | + * clock, so every function is deterministic and testable. | |
| 18 | + * | |
| 19 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | |
| 20 | + */ | |
| 21 | +#ifndef SX_PLUGIN_TIME_TZ_H | |
| 22 | +#define SX_PLUGIN_TIME_TZ_H | |
| 23 | + | |
| 24 | +#include <stddef.h> | |
| 25 | +#include <stdint.h> | |
| 26 | + | |
| 27 | +/* A wall-clock resolution can legitimately have two answers (a repeated hour | |
| 28 | + * at a DST fall-back) or zero (an hour skipped at spring-forward). */ | |
| 29 | +#define TZ_MAX_CANDIDATES 4 | |
| 30 | + | |
| 31 | +typedef struct { | |
| 32 | + int64_t utc; | |
| 33 | + int offset; /* east of UTC, seconds */ | |
| 34 | + int is_dst; | |
| 35 | +} tz_cand_t; | |
| 36 | + | |
| 37 | +/* The zone record, opaque outside tz.c. */ | |
| 38 | +typedef struct tz_zone tz_zone; | |
| 39 | + | |
| 40 | +/* Exact IANA key lookup. Case sensitive on every platform on purpose: see the | |
| 41 | + * header comment in tools/gen_tzdata.py. Returns NULL for an unknown key. */ | |
| 42 | +const tz_zone *tz_find(const char *name); | |
| 43 | + | |
| 44 | +/* Number of embedded zones and their keys (sorted), for the picker UI. */ | |
| 45 | +int tz_count(void); | |
| 46 | +const char *tz_name_at(int index); | |
| 47 | + | |
| 48 | +/* Offset east of UTC in force at `utc`. `abbr` (may be NULL) receives the | |
| 49 | + * zone abbreviation, truncated to `abbrsz` including the terminator. */ | |
| 50 | +int tz_offset(const tz_zone *z, int64_t utc, int *is_dst, | |
| 51 | + char *abbr, size_t abbrsz); | |
| 52 | + | |
| 53 | +/* Every UTC instant that has the given wall-clock time in `z`, earliest | |
| 54 | + * first. `wall` is the local time read as if it were UTC (i.e. it already | |
| 55 | + * includes the date and the hh:mm:ss). Returns the count: | |
| 56 | + * 0 = the wall time does not exist (skipped by a forward clock change) | |
| 57 | + * 1 = unambiguous | |
| 58 | + * 2 = the hour repeated (a backward clock change); [0] is the first | |
| 59 | + * occurrence, [1] the second - which is what Python's fold=0/1 gives. | |
| 60 | + */ | |
| 61 | +int tz_resolve_wall(const tz_zone *z, int64_t wall, tz_cand_t *out, int outmax); | |
| 62 | + | |
| 63 | +/* The node's own IANA zone name, best effort: $TZ, then the platform's own | |
| 64 | + * configuration, then an offset match against the embedded table. Never | |
| 65 | + * returns NULL; falls back to "UTC". The result is always a name tz_find() | |
| 66 | + * accepts. */ | |
| 67 | +const char *tz_local_name(void); | |
| 68 | + | |
| 69 | +/* Civil-date helpers (proleptic Gregorian, no leap seconds). */ | |
| 70 | +int64_t tz_days_from_civil(int y, int m, int d); | |
| 71 | +void tz_civil_from_days(int64_t days, int *y, int *m, int *d); | |
| 72 | +int tz_weekday(int64_t days); /* 0 = Sunday */ | |
| 73 | + | |
| 74 | +#endif /* SX_PLUGIN_TIME_TZ_H */ |
| 1 | 1 | new file mode 100644 |
| index 0000000..0b7b41a | ||
| --- /dev/null | ||
| +++ b/src/tzdata.h | ||
| @@ -0,0 +1,3494 @@ | ||
| 1 | +/* | |
| 2 | + * tzdata.h - generated by tools/gen_tzdata.py from the compiled IANA | |
| 3 | + * tz database (version 2025b). DO NOT EDIT. | |
| 4 | + * | |
| 5 | + * 598 zones, 10341 UTC transitions from 2015-01-01 onwards. | |
| 6 | + * | |
| 7 | + * Four parallel arrays index the transitions so the records stay | |
| 8 | + * padding-free on every ABI: seconds since the window start, the UTC | |
| 9 | + * offset east of Greenwich in seconds, the abbreviation pool offset, | |
| 10 | + * and the DST flag. | |
| 11 | + */ | |
| 12 | +#ifndef SX_PLUGIN_TIME_TZDATA_H | |
| 13 | +#define SX_PLUGIN_TIME_TZDATA_H | |
| 14 | + | |
| 15 | +#include <stdint.h> | |
| 16 | + | |
| 17 | +#define TZ_WINDOW_EPOCH 1420070400 | |
| 18 | +#define TZ_ZONE_COUNT 598 | |
| 19 | +#define TZ_TRANS_COUNT 10341 | |
| 20 | + | |
| 21 | +typedef struct { | |
| 22 | + uint32_t name; /* offset into TZ_NAME_POOL */ | |
| 23 | + uint32_t rule; /* offset into TZ_RULE_POOL */ | |
| 24 | + int32_t base_off; /* east-of-UTC seconds before the window */ | |
| 25 | + uint32_t first; /* index of this zone's first transition */ | |
| 26 | + uint16_t count; /* transitions in this zone's slice */ | |
| 27 | + uint16_t base_abbr; /* offset into TZ_ABBR_POOL */ | |
| 28 | + uint8_t base_dst; | |
| 29 | + uint8_t has_rule; | |
| 30 | +} tz_zone_t; | |
| 31 | + | |
| 32 | +static const char TZ_NAME_POOL[] = | |
| 33 | + "Africa/Abidjan\0Africa/Accra\0Africa/Addis_Ababa\0Africa/Algiers\0Africa/Asmara\0Africa/Asmera\0Africa/Bamako\0Africa/Bangui\0Africa/Banjul\0Africa/Bissau\0Africa/Blantyre\0Africa/Brazzaville" | |
| 34 | + "\0Africa/Bujumbura\0Africa/Cairo\0Africa/Casablanca\0Africa/Ceuta\0Africa/Conakry\0Africa/Dakar\0Africa/Dar_es_Salaam\0Africa/Djibouti\0Africa/Douala\0Africa/El_Aaiun\0Africa/Freetown\0Africa/" | |
| 35 | + "Gaborone\0Africa/Harare\0Africa/Johannesburg\0Africa/Juba\0Africa/Kampala\0Africa/Khartoum\0Africa/Kigali\0Africa/Kinshasa\0Africa/Lagos\0Africa/Libreville\0Africa/Lome\0Africa/Luanda\0Africa/" | |
| 36 | + "Lubumbashi\0Africa/Lusaka\0Africa/Malabo\0Africa/Maputo\0Africa/Maseru\0Africa/Mbabane\0Africa/Mogadishu\0Africa/Monrovia\0Africa/Nairobi\0Africa/Ndjamena\0Africa/Niamey\0Africa/Nouakchott\0Af" | |
| 37 | + "rica/Ouagadougou\0Africa/Porto-Novo\0Africa/Sao_Tome\0Africa/Timbuktu\0Africa/Tripoli\0Africa/Tunis\0Africa/Windhoek\0America/Adak\0America/Anchorage\0America/Anguilla\0America/Antigua\0Ameri" | |
| 38 | + "ca/Araguaina\0America/Argentina/Buenos_Aires\0America/Argentina/Catamarca\0America/Argentina/ComodRivadavia\0America/Argentina/Cordoba\0America/Argentina/Jujuy\0America/Argentina/La_Rioj" | |
| 39 | + "a\0America/Argentina/Mendoza\0America/Argentina/Rio_Gallegos\0America/Argentina/Salta\0America/Argentina/San_Juan\0America/Argentina/San_Luis\0America/Argentina/Tucuman\0America/Argentina" | |
| 40 | + "/Ushuaia\0America/Aruba\0America/Asuncion\0America/Atikokan\0America/Atka\0America/Bahia\0America/Bahia_Banderas\0America/Barbados\0America/Belem\0America/Belize\0America/Blanc-Sablon\0Americ" | |
| 41 | + "a/Boa_Vista\0America/Bogota\0America/Boise\0America/Buenos_Aires\0America/Cambridge_Bay\0America/Campo_Grande\0America/Cancun\0America/Caracas\0America/Catamarca\0America/Cayenne\0America/Ca" | |
| 42 | + "yman\0America/Chicago\0America/Chihuahua\0America/Ciudad_Juarez\0America/Coral_Harbour\0America/Cordoba\0America/Costa_Rica\0America/Coyhaique\0America/Creston\0America/Cuiaba\0America/Curac" | |
| 43 | + "ao\0America/Danmarkshavn\0America/Dawson\0America/Dawson_Creek\0America/Denver\0America/Detroit\0America/Dominica\0America/Edmonton\0America/Eirunepe\0America/El_Salvador\0America/Ensenada\0A" | |
| 44 | + "merica/Fort_Nelson\0America/Fort_Wayne\0America/Fortaleza\0America/Glace_Bay\0America/Godthab\0America/Goose_Bay\0America/Grand_Turk\0America/Grenada\0America/Guadeloupe\0America/Guatemala\0" | |
| 45 | + "America/Guayaquil\0America/Guyana\0America/Halifax\0America/Havana\0America/Hermosillo\0America/Indiana/Indianapolis\0America/Indiana/Knox\0America/Indiana/Marengo\0America/Indiana/Petersb" | |
| 46 | + "urg\0America/Indiana/Tell_City\0America/Indiana/Vevay\0America/Indiana/Vincennes\0America/Indiana/Winamac\0America/Indianapolis\0America/Inuvik\0America/Iqaluit\0America/Jamaica\0America/Ju" | |
| 47 | + "juy\0America/Juneau\0America/Kentucky/Louisville\0America/Kentucky/Monticello\0America/Knox_IN\0America/Kralendijk\0America/La_Paz\0America/Lima\0America/Los_Angeles\0America/Louisville\0Ame" | |
| 48 | + "rica/Lower_Princes\0America/Maceio\0America/Managua\0America/Manaus\0America/Marigot\0America/Martinique\0America/Matamoros\0America/Mazatlan\0America/Mendoza\0America/Menominee\0America/Mer" | |
| 49 | + "ida\0America/Metlakatla\0America/Mexico_City\0America/Miquelon\0America/Moncton\0America/Monterrey\0America/Montevideo\0America/Montreal\0America/Montserrat\0America/Nassau\0America/New_York" | |
| 50 | + "\0America/Nipigon\0America/Nome\0America/Noronha\0America/North_Dakota/Beulah\0America/North_Dakota/Center\0America/North_Dakota/New_Salem\0America/Nuuk\0America/Ojinaga\0America/Panama\0Ame" | |
| 51 | + "rica/Pangnirtung\0America/Paramaribo\0America/Phoenix\0America/Port-au-Prince\0America/Port_of_Spain\0America/Porto_Acre\0America/Porto_Velho\0America/Puerto_Rico\0America/Punta_Arenas\0Ame" | |
| 52 | + "rica/Rainy_River\0America/Rankin_Inlet\0America/Recife\0America/Regina\0America/Resolute\0America/Rio_Branco\0America/Rosario\0America/Santa_Isabel\0America/Santarem\0America/Santiago\0Ameri" | |
| 53 | + "ca/Santo_Domingo\0America/Sao_Paulo\0America/Scoresbysund\0America/Shiprock\0America/Sitka\0America/St_Barthelemy\0America/St_Johns\0America/St_Kitts\0America/St_Lucia\0America/St_Thomas\0Am" | |
| 54 | + "erica/St_Vincent\0America/Swift_Current\0America/Tegucigalpa\0America/Thule\0America/Thunder_Bay\0America/Tijuana\0America/Toronto\0America/Tortola\0America/Vancouver\0America/Virgin\0Americ" | |
| 55 | + "a/Whitehorse\0America/Winnipeg\0America/Yakutat\0America/Yellowknife\0Antarctica/Casey\0Antarctica/Davis\0Antarctica/DumontDUrville\0Antarctica/Macquarie\0Antarctica/Mawson\0Antarctica/McMu" | |
| 56 | + "rdo\0Antarctica/Palmer\0Antarctica/Rothera\0Antarctica/South_Pole\0Antarctica/Syowa\0Antarctica/Troll\0Antarctica/Vostok\0Arctic/Longyearbyen\0Asia/Aden\0Asia/Almaty\0Asia/Amman\0Asia/Anadyr\0" | |
| 57 | + "Asia/Aqtau\0Asia/Aqtobe\0Asia/Ashgabat\0Asia/Ashkhabad\0Asia/Atyrau\0Asia/Baghdad\0Asia/Bahrain\0Asia/Baku\0Asia/Bangkok\0Asia/Barnaul\0Asia/Beirut\0Asia/Bishkek\0Asia/Brunei\0Asia/Calcutta\0Asi" | |
| 58 | + "a/Chita\0Asia/Choibalsan\0Asia/Chongqing\0Asia/Chungking\0Asia/Colombo\0Asia/Dacca\0Asia/Damascus\0Asia/Dhaka\0Asia/Dili\0Asia/Dubai\0Asia/Dushanbe\0Asia/Famagusta\0Asia/Gaza\0Asia/Harbin\0Asia/" | |
| 59 | + "Hebron\0Asia/Ho_Chi_Minh\0Asia/Hong_Kong\0Asia/Hovd\0Asia/Irkutsk\0Asia/Istanbul\0Asia/Jakarta\0Asia/Jayapura\0Asia/Jerusalem\0Asia/Kabul\0Asia/Kamchatka\0Asia/Karachi\0Asia/Kashgar\0Asia/Kathm" | |
| 60 | + "andu\0Asia/Katmandu\0Asia/Khandyga\0Asia/Kolkata\0Asia/Krasnoyarsk\0Asia/Kuala_Lumpur\0Asia/Kuching\0Asia/Kuwait\0Asia/Macao\0Asia/Macau\0Asia/Magadan\0Asia/Makassar\0Asia/Manila\0Asia/Muscat\0A" | |
| 61 | + "sia/Nicosia\0Asia/Novokuznetsk\0Asia/Novosibirsk\0Asia/Omsk\0Asia/Oral\0Asia/Phnom_Penh\0Asia/Pontianak\0Asia/Pyongyang\0Asia/Qatar\0Asia/Qostanay\0Asia/Qyzylorda\0Asia/Rangoon\0Asia/Riyadh\0As" | |
| 62 | + "ia/Saigon\0Asia/Sakhalin\0Asia/Samarkand\0Asia/Seoul\0Asia/Shanghai\0Asia/Singapore\0Asia/Srednekolymsk\0Asia/Taipei\0Asia/Tashkent\0Asia/Tbilisi\0Asia/Tehran\0Asia/Tel_Aviv\0Asia/Thimbu\0Asia/" | |
| 63 | + "Thimphu\0Asia/Tokyo\0Asia/Tomsk\0Asia/Ujung_Pandang\0Asia/Ulaanbaatar\0Asia/Ulan_Bator\0Asia/Urumqi\0Asia/Ust-Nera\0Asia/Vientiane\0Asia/Vladivostok\0Asia/Yakutsk\0Asia/Yangon\0Asia/Yekaterinb" | |
| 64 | + "urg\0Asia/Yerevan\0Atlantic/Azores\0Atlantic/Bermuda\0Atlantic/Canary\0Atlantic/Cape_Verde\0Atlantic/Faeroe\0Atlantic/Faroe\0Atlantic/Jan_Mayen\0Atlantic/Madeira\0Atlantic/Reykjavik\0Atlantic" | |
| 65 | + "/South_Georgia\0Atlantic/St_Helena\0Atlantic/Stanley\0Australia/ACT\0Australia/Adelaide\0Australia/Brisbane\0Australia/Broken_Hill\0Australia/Canberra\0Australia/Currie\0Australia/Darwin\0Au" | |
| 66 | + "stralia/Eucla\0Australia/Hobart\0Australia/LHI\0Australia/Lindeman\0Australia/Lord_Howe\0Australia/Melbourne\0Australia/NSW\0Australia/North\0Australia/Perth\0Australia/Queensland\0Australia" | |
| 67 | + "/South\0Australia/Sydney\0Australia/Tasmania\0Australia/Victoria\0Australia/West\0Australia/Yancowinna\0Brazil/Acre\0Brazil/DeNoronha\0Brazil/East\0Brazil/West\0CET\0CST6CDT\0Canada/Atlantic\0C" | |
| 68 | + "anada/Central\0Canada/Eastern\0Canada/Mountain\0Canada/Newfoundland\0Canada/Pacific\0Canada/Saskatchewan\0Canada/Yukon\0Chile/Continental\0Chile/EasterIsland\0Cuba\0EET\0EST\0EST5EDT\0Egypt\0Eir" | |
| 69 | + "e\0Etc/GMT\0Etc/GMT+0\0Etc/GMT+1\0Etc/GMT+10\0Etc/GMT+11\0Etc/GMT+12\0Etc/GMT+2\0Etc/GMT+3\0Etc/GMT+4\0Etc/GMT+5\0Etc/GMT+6\0Etc/GMT+7\0Etc/GMT+8\0Etc/GMT+9\0Etc/GMT-0\0Etc/GMT-1\0Etc/GMT-10\0Etc/GM" | |
| 70 | + "T-11\0Etc/GMT-12\0Etc/GMT-13\0Etc/GMT-14\0Etc/GMT-2\0Etc/GMT-3\0Etc/GMT-4\0Etc/GMT-5\0Etc/GMT-6\0Etc/GMT-7\0Etc/GMT-8\0Etc/GMT-9\0Etc/GMT0\0Etc/Greenwich\0Etc/UCT\0Etc/UTC\0Etc/Universal\0Etc/Zulu\0" | |
| 71 | + "Europe/Amsterdam\0Europe/Andorra\0Europe/Astrakhan\0Europe/Athens\0Europe/Belfast\0Europe/Belgrade\0Europe/Berlin\0Europe/Bratislava\0Europe/Brussels\0Europe/Bucharest\0Europe/Budapest\0Europ" | |
| 72 | + "e/Busingen\0Europe/Chisinau\0Europe/Copenhagen\0Europe/Dublin\0Europe/Gibraltar\0Europe/Guernsey\0Europe/Helsinki\0Europe/Isle_of_Man\0Europe/Istanbul\0Europe/Jersey\0Europe/Kaliningrad\0Euro" | |
| 73 | + "pe/Kiev\0Europe/Kirov\0Europe/Kyiv\0Europe/Lisbon\0Europe/Ljubljana\0Europe/London\0Europe/Luxembourg\0Europe/Madrid\0Europe/Malta\0Europe/Mariehamn\0Europe/Minsk\0Europe/Monaco\0Europe/Moscow" | |
| 74 | + "\0Europe/Nicosia\0Europe/Oslo\0Europe/Paris\0Europe/Podgorica\0Europe/Prague\0Europe/Riga\0Europe/Rome\0Europe/Samara\0Europe/San_Marino\0Europe/Sarajevo\0Europe/Saratov\0Europe/Simferopol\0Eur" | |
| 75 | + "ope/Skopje\0Europe/Sofia\0Europe/Stockholm\0Europe/Tallinn\0Europe/Tirane\0Europe/Tiraspol\0Europe/Ulyanovsk\0Europe/Uzhgorod\0Europe/Vaduz\0Europe/Vatican\0Europe/Vienna\0Europe/Vilnius\0Euro" | |
| 76 | + "pe/Volgograd\0Europe/Warsaw\0Europe/Zagreb\0Europe/Zaporozhye\0Europe/Zurich\0Factory\0GB\0GB-Eire\0GMT\0GMT+0\0GMT-0\0GMT0\0Greenwich\0HST\0Hongkong\0Iceland\0Indian/Antananarivo\0Indian/Chagos\0In" | |
| 77 | + "dian/Christmas\0Indian/Cocos\0Indian/Comoro\0Indian/Kerguelen\0Indian/Mahe\0Indian/Maldives\0Indian/Mauritius\0Indian/Mayotte\0Indian/Reunion\0Iran\0Israel\0Jamaica\0Japan\0Kwajalein\0Libya\0MET\0" | |
| 78 | + "MST\0MST7MDT\0Mexico/BajaNorte\0Mexico/BajaSur\0Mexico/General\0NZ\0NZ-CHAT\0Navajo\0PRC\0PST8PDT\0Pacific/Apia\0Pacific/Auckland\0Pacific/Bougainville\0Pacific/Chatham\0Pacific/Chuuk\0Pacific/Ea" | |
| 79 | + "ster\0Pacific/Efate\0Pacific/Enderbury\0Pacific/Fakaofo\0Pacific/Fiji\0Pacific/Funafuti\0Pacific/Galapagos\0Pacific/Gambier\0Pacific/Guadalcanal\0Pacific/Guam\0Pacific/Honolulu\0Pacific/Johns" | |
| 80 | + "ton\0Pacific/Kanton\0Pacific/Kiritimati\0Pacific/Kosrae\0Pacific/Kwajalein\0Pacific/Majuro\0Pacific/Marquesas\0Pacific/Midway\0Pacific/Nauru\0Pacific/Niue\0Pacific/Norfolk\0Pacific/Noumea\0Pac" | |
| 81 | + "ific/Pago_Pago\0Pacific/Palau\0Pacific/Pitcairn\0Pacific/Pohnpei\0Pacific/Ponape\0Pacific/Port_Moresby\0Pacific/Rarotonga\0Pacific/Saipan\0Pacific/Samoa\0Pacific/Tahiti\0Pacific/Tarawa\0Pacif" | |
| 82 | + "ic/Tongatapu\0Pacific/Truk\0Pacific/Wake\0Pacific/Wallis\0Pacific/Yap\0Poland\0Portugal\0ROC\0ROK\0Singapore\0Turkey\0UCT\0US/Alaska\0US/Aleutian\0US/Arizona\0US/Central\0US/East-Indiana\0US/Easter" | |
| 83 | + "n\0US/Hawaii\0US/Indiana-Starke\0US/Michigan\0US/Mountain\0US/Pacific\0US/Samoa\0UTC\0Universal\0W-SU\0WET\0Zulu\0" | |
| 84 | + ; | |
| 85 | + | |
| 86 | +static const char TZ_ABBR_POOL[] = | |
| 87 | + "GMT\0EAT\0CET\0WAT\0CAT\0EEST\0EET\0+01\0+00\0CEST\0SAST\0HDT\0HST\0AKDT\0AKST\0AST\0-03\0-04\0EST\0CDT\0CST\0-05\0MDT\0MST\0-0430\0PDT\0PST\0EDT\0ADT\0-02\0-01\0NDT\0NST\0+11\0+08\0+07\0+10\0AEST\0AEDT\0+05\0NZST\0NZDT\0+" | |
| 88 | + "03\0+02\0+06\0+12\0+04\0IST\0+09\0+0530\0HKT\0WIB\0WIT\0IDT\0+0430\0PKT\0+0545\0WITA\0KST\0+0630\0+0330\0JST\0WEST\0WET\0ACST\0ACDT\0+0845\0+1030\0AWST\0-06\0-10\0-11\0-12\0-07\0-08\0-09\0+13\0+14\0UTC\0BST\0MSK\0-00\0ME" | |
| 89 | + "ST\0MET\0+1245\0+1345\0ChST\0-0930\0SST\0+1130\0" | |
| 90 | + ; | |
| 91 | + | |
| 92 | +static const char TZ_RULE_POOL[] = | |
| 93 | + "GMT0\0EAT-3\0CET-1\0WAT-1\0CAT-2\0EET-2EEST,M4.5.5/0,M10.5.4/24\0<+00>0<+01>,0/0,J365/25\0CET-1CEST,M3.5.0,M10.5.0/3\0SAST-2\0EET-2\0HST10HDT,M3.2.0,M11.1.0\0AKST9AKDT,M3.2.0,M11.1.0\0AST4\0<-0" | |
| 94 | + "3>3\0EST5\0CST6\0<-04>4\0<-05>5\0MST7MDT,M3.2.0,M11.1.0\0CST6CDT,M3.2.0,M11.1.0\0MST7\0EST5EDT,M3.2.0,M11.1.0\0PST8PDT,M3.2.0,M11.1.0\0AST4ADT,M3.2.0,M11.1.0\0<-02>2<-01>,M3.5.0/-1,M10.5.0/0\0" | |
| 95 | + "CST5CDT,M3.2.0/0,M11.1.0/1\0<-03>3<-02>,M3.2.0,M11.1.0\0<-02>2\0<-04>4<-03>,M9.1.6/24,M4.1.6/24\0NST3:30NDT,M3.2.0,M11.1.0\0<+08>-8\0<+07>-7\0<+10>-10\0AEST-10AEDT,M10.1.0,M4.1.0/3\0<+05>-5" | |
| 96 | + "\0NZST-12NZDT,M9.5.0,M4.1.0/3\0<+03>-3\0<+00>0<+02>-2,M3.5.0/1,M10.5.0/3\0<+12>-12\0<+04>-4\0EET-2EEST,M3.5.0/0,M10.5.0/0\0<+06>-6\0IST-5:30\0<+09>-9\0CST-8\0<+0530>-5:30\0EET-2EEST,M3.5.0/3,M" | |
| 97 | + "10.5.0/4\0EET-2EEST,M3.4.4/50,M10.4.4/50\0HKT-8\0WIB-7\0WIT-9\0IST-2IDT,M3.4.4/26,M10.5.0\0<+0430>-4:30\0PKT-5\0<+0545>-5:45\0<+11>-11\0WITA-8\0PST-8\0KST-9\0<+0630>-6:30\0<+0330>-3:30\0JST-9\0<-0" | |
| 98 | + "1>1<+00>,M3.5.0/0,M10.5.0/1\0WET0WEST,M3.5.0/1,M10.5.0\0<-01>1\0ACST-9:30ACDT,M10.1.0,M4.1.0/3\0AEST-10\0ACST-9:30\0<+0845>-8:45\0<+1030>-10:30<+11>-11,M10.1.0,M4.1.0\0AWST-8\0<-06>6<-05>,M" | |
| 99 | + "9.1.6/22,M4.1.6/22\0GMT0IST,M3.5.0/1,M10.5.0\0<-10>10\0<-11>11\0<-12>12\0<-06>6\0<-07>7\0<-08>8\0<-09>9\0<+01>-1\0<+13>-13\0<+14>-14\0<+02>-2\0UTC0\0GMT0BST,M3.5.0/1,M10.5.0\0EET-2EEST,M3.5.0,M10" | |
| 100 | + ".5.0/3\0MSK-3\0<-00>0\0HST10\0MET-1MEST,M3.5.0,M10.5.0/3\0<+1245>-12:45<+1345>,M9.5.0/2:45,M4.1.0/3:45\0ChST-10\0<-0930>9:30\0SST11\0<+11>-11<+12>,M10.1.0,M4.1.0/3\0" | |
| 101 | + ; | |
| 102 | + | |
| 103 | +static const uint32_t TZ_TR_T[TZ_TRANS_COUNT] = { | |
| 104 | + 262562400,278283600,294012000,310338000,325461600,341787600,356911200,373237200,388965600, | |
| 105 | + 404686800,420415200,436136400,451864800,467586000,483314400,499640400, | |
| 106 | + 514764000,531090000,546818400,562539600,578268000,593989200,609717600,625438800,641167200, | |
| 107 | + 656888400,672616800,688942800,704066400,720392400,7524000,14176800, | |
| 108 | + 17200800,25668000,38973600,45021600,48045600,57722400,70423200,75261600,78890400,89172000, | |
| 109 | + 101872800,106106400,109130400,136951200,139975200,167191200, | |
| 110 | + 170820000,198036000,201060000,228276000,231904800,259120800,262144800,289965600,292989600, | |
| 111 | + 320205600,323834400,351050400,354074400,381895200,384919200,412135200, | |
| 112 | + 415764000,442980000,446004000,473220000,476848800,504064800,507088800,534909600,537933600, | |
| 113 | + 565149600,568778400,595994400,599018400,626234400,629863200,657079200, | |
| 114 | + 660708000,687924000,690948000,718164000,721792800,749008800,752032800,779853600,782877600, | |
| 115 | + 810093600,813722400,840938400,843962400,871178400,874807200,902023200, | |
| 116 | + 905652000,932868000,935892000,963108000,966736800,993952800,996976800,1024797600,1027821600, | |
| 117 | + 1055037600,1058666400,1085882400,1088906400,1116122400,1119751200,1146967200, | |
| 118 | + 1150596000,1177812000,1180836000,1208052000,1211680800,1238896800,1241920800,1269741600, | |
| 119 | + 1272765600,1299981600,1303610400,1330826400,1333850400,1361066400,1364695200,1391911200, | |
| 120 | + 1395540000,1422756000,1425780000,1452996000,1456624800,1483840800,1486864800,1514685600, | |
| 121 | + 1517709600,1544925600,1548554400,1575770400,1578794400,1606010400,1609639200,1636855200, | |
| 122 | + 1640484000,1667700000,1670724000,1697940000,1701568800,1728784800,1731808800,1759629600, | |
| 123 | + 1762653600,1789869600,1793498400,1820714400,1823738400,1850954400,1854583200,1881799200, | |
| 124 | + 1885428000,1912644000,1915668000,1942884000,1946512800,1973728800,1976752800,2004573600, | |
| 125 | + 2007597600,2034813600,2038442400,2065658400,2068682400,2095898400,2099527200,2126743200, | |
| 126 | + 2129767200,2157588000,2160612000,2187828000,2191456800,2218672800,2221696800,2249517600, | |
| 127 | + 2252541600,2279757600,2283386400,7520400,25664400,38970000,57718800,70419600, | |
| 128 | + 89168400,101869200,120618000,133923600,152067600,165373200,183517200,196822800,215571600, | |
| 129 | + 228272400,247021200,259722000,278470800,291776400,309920400,323226000, | |
| 130 | + 341370000,354675600,372819600,386125200,404874000,417574800,436323600,449024400,467773200, | |
| 131 | + 481078800,499222800,512528400,530672400,543978000,562726800,575427600, | |
| 132 | + 594176400,606877200,625626000,638326800,657075600,670381200,688525200,701830800,719974800, | |
| 133 | + 7524000,14176800,17200800,25668000,38973600,45021600,48045600, | |
| 134 | + 57722400,70423200,75261600,78890400,89172000,101872800,106106400,109130400,136951200, | |
| 135 | + 139975200,167191200,170820000,198036000,201060000,228276000,231904800, | |
| 136 | + 259120800,262144800,289965600,292989600,320205600,323834400,351050400,354074400,381895200, | |
| 137 | + 384919200,412135200,415764000,442980000,446004000,473220000,476848800, | |
| 138 | + 504064800,507088800,534909600,537933600,565149600,568778400,595994400,599018400,626234400, | |
| 139 | + 629863200,657079200,660708000,687924000,690948000,718164000,721792800, | |
| 140 | + 749008800,752032800,779853600,782877600,810093600,813722400,840938400,843962400,871178400, | |
| 141 | + 874807200,902023200,905652000,932868000,935892000,963108000,966736800, | |
| 142 | + 993952800,996976800,1024797600,1027821600,1055037600,1058666400,1085882400,1088906400, | |
| 143 | + 1116122400,1119751200,1146967200,1150596000,1177812000,1180836000,1208052000,1211680800, | |
| 144 | + 1238896800,1241920800,1269741600,1272765600,1299981600,1303610400,1330826400,1333850400, | |
| 145 | + 1361066400,1364695200,1391911200,1395540000,1422756000,1425780000,1452996000,1456624800, | |
| 146 | + 1483840800,1486864800,1514685600,1517709600,1544925600,1548554400,1575770400,1578794400, | |
| 147 | + 1606010400,1609639200,1636855200,1640484000,1667700000,1670724000,1697940000,1701568800, | |
| 148 | + 1728784800,1731808800,1759629600,1762653600,1789869600,1793498400,1820714400,1823738400, | |
| 149 | + 1850954400,1854583200,1881799200,1885428000,1912644000,1915668000,1942884000,1946512800, | |
| 150 | + 1973728800,1976752800,2004573600,2007597600,2034813600,2038442400,2065658400,2068682400, | |
| 151 | + 2095898400,2099527200,2126743200,2129767200,2157588000,2160612000,2187828000,2191456800, | |
| 152 | + 2218672800,2221696800,2249517600,2252541600,2279757600,2283386400,192056400,89413200, | |
| 153 | + 94698000,126234000,8121600,21430800,39571200,52880400,71020800,84330000, | |
| 154 | + 88725600,5745600,26305200,37800000,58359600,69249600,89809200,100699200,121258800,132148800, | |
| 155 | + 152708400,163598400,184158000,195652800,216212400,227102400, | |
| 156 | + 247662000,258552000,279111600,290001600,310561200,321451200,342010800,352900800,373460400, | |
| 157 | + 384955200,405514800,416404800,436964400,447854400,468414000,479304000, | |
| 158 | + 499863600,510753600,531313200,542808000,563367600,574257600,594817200,605707200,626266800, | |
| 159 | + 637156800,657716400,668606400,689166000,700056000,720615600,5742000, | |
| 160 | + 26301600,37796400,58356000,69246000,89805600,100695600,121255200,132145200,152704800, | |
| 161 | + 163594800,184154400,195649200,216208800,227098800,247658400,258548400, | |
| 162 | + 279108000,289998000,310557600,321447600,342007200,352897200,373456800,384951600,405511200, | |
| 163 | + 416401200,436960800,447850800,468410400,479300400,499860000,510750000, | |
| 164 | + 531309600,542804400,563364000,574254000,594813600,605703600,626263200,637153200,657712800, | |
| 165 | + 668602800,689162400,700052400,720612000,727413247,727413247,727413247, | |
| 166 | + 727413247,727413247,727413247,727413247,727413247,727413247,727413247,727413247,727413247, | |
| 167 | + 727413247,727413247,6922800,23860800,38977200,55310400,70426800, | |
| 168 | + 86760000,101876400,118814400,133326000,150264000,164775600,181713600,196830000,213163200, | |
| 169 | + 228279600,244612800,259729200,276062400,291178800,308116800,308890800, | |
| 170 | + 727413247,5745600,26305200,37800000,58359600,69249600,89809200,100699200,121258800, | |
| 171 | + 132148800,152708400,163598400,184158000,195652800,216212400,227102400, | |
| 172 | + 247662000,258552000,279111600,290001600,310561200,321451200,342010800,352900800,373460400, | |
| 173 | + 384955200,405514800,416404800,436964400,447854400,468414000,479304000, | |
| 174 | + 499863600,510753600,531313200,542808000,563367600,574257600,594817200,605707200,626266800, | |
| 175 | + 637156800,657716400,668606400,689166000,700056000,720615600,727413247, | |
| 176 | + 8150400,25686000,39600000,57740400,71049600,89190000,102499200,120639600,134553600, | |
| 177 | + 152089200,166003200,183538800,197452800,215593200,228902400,247042800, | |
| 178 | + 727413247,727413247,727413247,5734800,26294400,37789200,58348800,69238800,89798400, | |
| 179 | + 100688400,121248000,132138000,152697600,163587600,184147200,195642000, | |
| 180 | + 216201600,227091600,247651200,258541200,279100800,289990800,310550400,321440400,342000000, | |
| 181 | + 352890000,373449600,384944400,405504000,416394000,436953600,447843600, | |
| 182 | + 468403200,479293200,499852800,510742800,531302400,542797200,563356800,574246800,594806400, | |
| 183 | + 605696400,626256000,637146000,657705600,668595600,689155200,700045200, | |
| 184 | + 720604800,727413247,5734800,26294400,37789200,58348800,69238800,89798400,100688400, | |
| 185 | + 121248000,132138000,152697600,163587600,184147200,195642000,216201600, | |
| 186 | + 227091600,247651200,258541200,279100800,289990800,310550400,321440400,342000000,352890000, | |
| 187 | + 373449600,384944400,405504000,416394000,436953600,447843600,468403200, | |
| 188 | + 479293200,499852800,510742800,531302400,542797200,563356800,574246800,594806400,605696400, | |
| 189 | + 626256000,637146000,657705600,668595600,689155200,700045200,720604800, | |
| 190 | + 4503600,25070400,35953200,56520000,67402800,87969600,98852400,121233600,130302000,727413247, | |
| 191 | + 2707200,42015600,727413247,727413247,727413247,5731200,26290800,37785600,58345200,69235200, | |
| 192 | + 89794800,100684800,121244400,132134400,152694000,163584000,184143600,195638400,216198000, | |
| 193 | + 227088000,247647600,258537600,279097200,289987200,310546800,321436800,341996400,352886400, | |
| 194 | + 373446000,384940800,405500400,416390400,436950000,447840000,468399600,479289600,499849200, | |
| 195 | + 510739200,531298800,542793600,563353200,574243200,594802800,605692800,626252400,637142400, | |
| 196 | + 657702000,668592000,689151600,700041600,720601200,8154000,25689600,39603600, | |
| 197 | + 57744000,71053200,89193600,102502800,120643200,134557200,152092800,166006800,183542400, | |
| 198 | + 197456400,215596800,228906000,247046400,5734800,26294400,37789200, | |
| 199 | + 58348800,69238800,89798400,100688400,121248000,132138000,152697600,163587600,184147200, | |
| 200 | + 195642000,216201600,227091600,247046400,249717600,258541200,279100800, | |
| 201 | + 289990800,310550400,321440400,342000000,352890000,373449600,384944400,405504000,416394000, | |
| 202 | + 436953600,447843600,468403200,479293200,499852800,510742800,531302400, | |
| 203 | + 542797200,563356800,574246800,594806400,605696400,626256000,637146000,657705600,668595600, | |
| 204 | + 689155200,700045200,720604800,727413247,43210800,51076800,74660400, | |
| 205 | + 82526400,106110000,113976000,134535600,147844800,165985200,179294400,197434800,210744000, | |
| 206 | + 228884400,242798400,260334000,273643200,292388400,305697600,322369200, | |
| 207 | + 727413247,4503600,25070400,35953200,56520000,67402800,87969600,98852400,121233600,130302000, | |
| 208 | + 727413247,5738400,26298000,37792800,58352400,69242400,89802000,100692000,121251600, | |
| 209 | + 132141600,152701200,163591200,184143600,5734800,26294400,37789200,58348800,69238800, | |
| 210 | + 89798400,100688400,121248000,132138000,152697600,163587600,184147200,195642000,216201600, | |
| 211 | + 227091600,247651200,258541200,279100800,289990800,310550400,321440400,342000000,352890000, | |
| 212 | + 373449600,384944400,405504000,416394000,436953600,447843600,468403200,479293200,499852800, | |
| 213 | + 510742800,531302400,542797200,563356800,574246800,594806400,605696400,626256000,637146000, | |
| 214 | + 657705600,668595600,689155200,700045200,720604800,5727600,26287200,37782000,58341600, | |
| 215 | + 69231600,89791200,100681200,121240800,132130800,152690400,163580400, | |
| 216 | + 184140000,195634800,216194400,227084400,247644000,258534000,279093600,289983600,310543200, | |
| 217 | + 321433200,341992800,352882800,373442400,384937200,405496800,416386800, | |
| 218 | + 436946400,447836400,468396000,479286000,499845600,510735600,531295200,542790000,563349600, | |
| 219 | + 574239600,594799200,605689200,626248800,637138800,657698400,668588400, | |
| 220 | + 689148000,700038000,720597600,5734800,26294400,37789200,58348800,69238800,89798400, | |
| 221 | + 100688400,121248000,132138000,152697600,163587600,184147200,195642000, | |
| 222 | + 216201600,227091600,247651200,258541200,279100800,289990800,310550400,321440400,342000000, | |
| 223 | + 352890000,373449600,384944400,405504000,416394000,436953600,447843600, | |
| 224 | + 468403200,479293200,499852800,510742800,531302400,542797200,563356800,574246800,594806400, | |
| 225 | + 605696400,626256000,637146000,657705600,668595600,689155200,700045200, | |
| 226 | + 720604800,727413247,5738400,26298000,37792800,58352400,69242400,89802000,100692000, | |
| 227 | + 121251600,132141600,152701200,163591200,184150800,195645600,216205200, | |
| 228 | + 227095200,247654800,258544800,279104400,289994400,310554000,321444000,342003600,352893600, | |
| 229 | + 373453200,384948000,405507600,416397600,436957200,447847200,468406800, | |
| 230 | + 479296800,499856400,510746400,531306000,542800800,563360400,574250400,594810000,605700000, | |
| 231 | + 626259600,637149600,657709200,668599200,689158800,700048800,720608400, | |
| 232 | + 5738400,5727600,26287200,37782000,58341600,69231600,89791200,100681200,121240800,132130800, | |
| 233 | + 152690400,163580400,184140000,195634800,216194400,227084400, | |
| 234 | + 247644000,258534000,279093600,289983600,310543200,321433200,341992800,352882800,373442400, | |
| 235 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000, | |
| 236 | + 499845600,510735600,531295200,542790000,563349600,574239600,594799200,605689200,626248800, | |
| 237 | + 637138800,657698400,668588400,689148000,700038000,720597600,727413247, | |
| 238 | + 5724000,26283600,37778400,58338000,69228000,89787600,100677600,121237200,132127200, | |
| 239 | + 152686800,163576800,184136400,195631200,216190800,227080800,247640400, | |
| 240 | + 258530400,279090000,289980000,310539600,321429600,341989200,352879200,373438800,384933600, | |
| 241 | + 405493200,416383200,436942800,447832800,468392400,479282400,499842000, | |
| 242 | + 510732000,531291600,542786400,563346000,574236000,594795600,605685600,626245200,637135200, | |
| 243 | + 657694800,668584800,689144400,700034400,720594000,7520400,25664400, | |
| 244 | + 38970000,57718800,70419600,89168400,101869200,120618000,133923600,152067600,165373200, | |
| 245 | + 183517200,196822800,215571600,228272400,247021200,259722000,291776400, | |
| 246 | + 309920400,323226000,341370000,354675600,372819600,386125200,404874000,417574800,436323600, | |
| 247 | + 449024400,467773200,481078800,499222800,512528400,530672400,543978000, | |
| 248 | + 562726800,575427600,594176400,606877200,625626000,638326800,657075600,670381200,688525200, | |
| 249 | + 701830800,719974800,727413247,5724000,26283600,37778400,58338000, | |
| 250 | + 69228000,89787600,100677600,121237200,132127200,152686800,163576800,184136400,195631200, | |
| 251 | + 216190800,227080800,247640400,258530400,279090000,289980000,310539600, | |
| 252 | + 321429600,341989200,352879200,373438800,384933600,405493200,416383200,436942800,447832800, | |
| 253 | + 468392400,479282400,499842000,510732000,531291600,542786400,563346000, | |
| 254 | + 574236000,594795600,605685600,626245200,637135200,657694800,668584800,689144400,700034400, | |
| 255 | + 720594000,5727600,100681200,121240800,132130800,152690400,163580400, | |
| 256 | + 184140000,195634800,216194400,227084400,247644000,258534000,279093600,289983600,310543200, | |
| 257 | + 321433200,341992800,352882800,373442400,384937200,405496800,416386800, | |
| 258 | + 436946400,447836400,468396000,479286000,499845600,510735600,531295200,542790000,563349600, | |
| 259 | + 574239600,594799200,605689200,626248800,637138800,657698400,668588400, | |
| 260 | + 689148000,700038000,720597600,727413247,727413247,5724000,26283600,37778400,58338000, | |
| 261 | + 69228000,89787600,100677600,121237200,132127200,152686800,163576800, | |
| 262 | + 184136400,195631200,216190800,227080800,247640400,258530400,279090000,289980000,310539600, | |
| 263 | + 321429600,341989200,352879200,373438800,384933600,405493200,416383200, | |
| 264 | + 436942800,447832800,468392400,479282400,499842000,510732000,531291600,542786400,563346000, | |
| 265 | + 574236000,594795600,605685600,626245200,637135200,657694800,668584800, | |
| 266 | + 689144400,700034400,720594000,5720400,26283600,37774800,58338000,69224400,89787600, | |
| 267 | + 100674000,121237200,132123600,152686800,163573200,184136400,195627600, | |
| 268 | + 216190800,227077200,247640400,258526800,279090000,289976400,310539600,321426000,341989200, | |
| 269 | + 352875600,373438800,384930000,405493200,416379600,436942800,447829200, | |
| 270 | + 468392400,479278800,499842000,510728400,531291600,542782800,563346000,574232400,594795600, | |
| 271 | + 605682000,626245200,637131600,657694800,668581200,689144400,700030800, | |
| 272 | + 720594000,5727600,26287200,37782000,58341600,69231600,89791200,100681200,121240800, | |
| 273 | + 132130800,152690400,163580400,184140000,195634800,216194400,227084400, | |
| 274 | + 247644000,258534000,279093600,289983600,310543200,321433200,341992800,352882800,373442400, | |
| 275 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000, | |
| 276 | + 499845600,510735600,531295200,542790000,563349600,574239600,594799200,605689200,626248800, | |
| 277 | + 637138800,657698400,668588400,689148000,700038000,720597600,5731200, | |
| 278 | + 26290800,37785600,58345200,69235200,89794800,100684800,121244400,132134400,152694000, | |
| 279 | + 163584000,184143600,195638400,216198000,227088000,247647600,258537600, | |
| 280 | + 279097200,289987200,310546800,321436800,341996400,352886400,373446000,384940800,405500400, | |
| 281 | + 416390400,436950000,447840000,468399600,479289600,499849200,510739200, | |
| 282 | + 531298800,542793600,563353200,574243200,594802800,605692800,626252400,637142400,657702000, | |
| 283 | + 668592000,689151600,700041600,720601200,5727600,26287200,37782000, | |
| 284 | + 58341600,69231600,89791200,100681200,121240800,132130800,152690400,163580400,184140000, | |
| 285 | + 195634800,216194400,227084400,247644000,258534000,279093600,289983600, | |
| 286 | + 310543200,321433200,341992800,352882800,373442400,384937200,405496800,416386800,436946400, | |
| 287 | + 447836400,468396000,479286000,499845600,510735600,531295200,542790000, | |
| 288 | + 563349600,574239600,594799200,605689200,626248800,637138800,657698400,668588400,689148000, | |
| 289 | + 700038000,720597600,5727600,26287200,37782000,58341600,69231600, | |
| 290 | + 89791200,100681200,121240800,132130800,152690400,163580400,184140000,195634800,216194400, | |
| 291 | + 227084400,247644000,258534000,279093600,289983600,310543200,321433200, | |
| 292 | + 341992800,352882800,373442400,384937200,405496800,416386800,436946400,447836400,468396000, | |
| 293 | + 479286000,499845600,510735600,531295200,542790000,563349600,574239600, | |
| 294 | + 594799200,605689200,626248800,637138800,657698400,668588400,689148000,700038000,720597600, | |
| 295 | + 5731200,26290800,37785600,58345200,69235200,89794800,100684800, | |
| 296 | + 121244400,132134400,152694000,163584000,184143600,195638400,216198000,227088000,247647600, | |
| 297 | + 258537600,279097200,289987200,310546800,321436800,341996400,352886400, | |
| 298 | + 373446000,384940800,405500400,416390400,436950000,447840000,468399600,479289600,499849200, | |
| 299 | + 510739200,531298800,542793600,563353200,574243200,594802800,605692800, | |
| 300 | + 626252400,637142400,657702000,668592000,689151600,700041600,720601200,5727600,26287200, | |
| 301 | + 37782000,58341600,69231600,89791200,100681200,121240800,132130800, | |
| 302 | + 152690400,163580400,184140000,195634800,216194400,227084400,247644000,258534000,279093600, | |
| 303 | + 289983600,310543200,321433200,341992800,352882800,373442400,384937200, | |
| 304 | + 405496800,416386800,436946400,447836400,468396000,479286000,499845600,510735600,531295200, | |
| 305 | + 542790000,563349600,574239600,594799200,605689200,626248800,637138800, | |
| 306 | + 657698400,668588400,689148000,700038000,720597600,5727600,26287200,37782000,58341600, | |
| 307 | + 69231600,89791200,100681200,121240800,132130800,152690400,163580400, | |
| 308 | + 184140000,195634800,216194400,227084400,247644000,258534000,279093600,289983600,310543200, | |
| 309 | + 321433200,341992800,352882800,373442400,384937200,405496800,416386800, | |
| 310 | + 436946400,447836400,468396000,479286000,499845600,510735600,531295200,542790000,563349600, | |
| 311 | + 574239600,594799200,605689200,626248800,637138800,657698400,668588400, | |
| 312 | + 689148000,700038000,720597600,5727600,26287200,37782000,58341600,69231600,89791200, | |
| 313 | + 100681200,121240800,132130800,152690400,163580400,184140000,195634800, | |
| 314 | + 216194400,227084400,247644000,258534000,279093600,289983600,310543200,321433200,341992800, | |
| 315 | + 352882800,373442400,384937200,405496800,416386800,436946400,447836400, | |
| 316 | + 468396000,479286000,499845600,510735600,531295200,542790000,563349600,574239600,594799200, | |
| 317 | + 605689200,626248800,637138800,657698400,668588400,689148000,700038000, | |
| 318 | + 720597600,5727600,26287200,37782000,58341600,69231600,89791200,100681200,121240800, | |
| 319 | + 132130800,152690400,163580400,184140000,195634800,216194400,227084400, | |
| 320 | + 247644000,258534000,279093600,289983600,310543200,321433200,341992800,352882800,373442400, | |
| 321 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000, | |
| 322 | + 499845600,510735600,531295200,542790000,563349600,574239600,594799200,605689200,626248800, | |
| 323 | + 637138800,657698400,668588400,689148000,700038000,720597600,5734800, | |
| 324 | + 26294400,37789200,58348800,69238800,89798400,100688400,121248000,132138000,152697600, | |
| 325 | + 163587600,184147200,195642000,216201600,227091600,247651200,258541200, | |
| 326 | + 279100800,289990800,310550400,321440400,342000000,352890000,373449600,384944400,405504000, | |
| 327 | + 416394000,436953600,447843600,468403200,479293200,499852800,510742800, | |
| 328 | + 531302400,542797200,563356800,574246800,594806400,605696400,626256000,637146000,657705600, | |
| 329 | + 668595600,689155200,700045200,720604800,5727600,26287200,37782000, | |
| 330 | + 58341600,69231600,89791200,100681200,121240800,132130800,152690400,163580400,184140000, | |
| 331 | + 195634800,216194400,227084400,247644000,258534000,279093600,289983600, | |
| 332 | + 310543200,321433200,341992800,352882800,373442400,384937200,405496800,416386800,436946400, | |
| 333 | + 447836400,468396000,479286000,499845600,510735600,531295200,542790000, | |
| 334 | + 563349600,574239600,594799200,605689200,626248800,637138800,657698400,668588400,689148000, | |
| 335 | + 700038000,720597600,727413247,5742000,26301600,37796400,58356000, | |
| 336 | + 69246000,89805600,100695600,121255200,132145200,152704800,163594800,184154400,195649200, | |
| 337 | + 216208800,227098800,247658400,258548400,279108000,289998000,310557600, | |
| 338 | + 321447600,342007200,352897200,373456800,384951600,405511200,416401200,436960800,447850800, | |
| 339 | + 468410400,479300400,499860000,510750000,531309600,542804400,563364000, | |
| 340 | + 574254000,594813600,605703600,626263200,637153200,657712800,668602800,689162400,700052400, | |
| 341 | + 720612000,5727600,26287200,37782000,58341600,69231600,89791200, | |
| 342 | + 100681200,121240800,132130800,152690400,163580400,184140000,195634800,216194400,227084400, | |
| 343 | + 247644000,258534000,279093600,289983600,310543200,321433200,341992800, | |
| 344 | + 352882800,373442400,384937200,405496800,416386800,436946400,447836400,468396000,479286000, | |
| 345 | + 499845600,510735600,531295200,542790000,563349600,574239600,594799200, | |
| 346 | + 605689200,626248800,637138800,657698400,668588400,689148000,700038000,720597600,5727600, | |
| 347 | + 26287200,37782000,58341600,69231600,89791200,100681200,121240800, | |
| 348 | + 132130800,152690400,163580400,184140000,195634800,216194400,227084400,247644000,258534000, | |
| 349 | + 279093600,289983600,310543200,321433200,341992800,352882800,373442400, | |
| 350 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000,499845600,510735600, | |
| 351 | + 531295200,542790000,563349600,574239600,594799200,605689200,626248800, | |
| 352 | + 637138800,657698400,668588400,689148000,700038000,720597600,5731200,26290800,37785600, | |
| 353 | + 58345200,69235200,89794800,100684800,121244400,132134400,152694000, | |
| 354 | + 163584000,184143600,195638400,216198000,227088000,247647600,258537600,279097200,289987200, | |
| 355 | + 310546800,321436800,341996400,352886400,373446000,384940800,405500400, | |
| 356 | + 416390400,436950000,447840000,468399600,479289600,499849200,510739200,531298800,542793600, | |
| 357 | + 563353200,574243200,594802800,605692800,626252400,637142400,657702000, | |
| 358 | + 668592000,689151600,700041600,720601200,727413247,727413247,5738400,26298000,37792800, | |
| 359 | + 58352400,69242400,89802000,100692000,121251600,132141600,152701200, | |
| 360 | + 163591200,184150800,195645600,216205200,227095200,247654800,258544800,279104400,289994400, | |
| 361 | + 310554000,321444000,342003600,352893600,373453200,384948000,405507600, | |
| 362 | + 416397600,436957200,447847200,468406800,479296800,499856400,510746400,531306000,542800800, | |
| 363 | + 563360400,574250400,594810000,605700000,626259600,637149600,657709200, | |
| 364 | + 668599200,689158800,700048800,720608400,5727600,26287200,37782000,58341600,69231600, | |
| 365 | + 89791200,100681200,121240800,132130800,152690400,163580400,184140000, | |
| 366 | + 195634800,216194400,227084400,247644000,258534000,279093600,289983600,310543200,321433200, | |
| 367 | + 341992800,352882800,373442400,384937200,405496800,416386800,436946400, | |
| 368 | + 447836400,468396000,479286000,499845600,510735600,531295200,542790000,563349600,574239600, | |
| 369 | + 594799200,605689200,626248800,637138800,657698400,668588400,689148000, | |
| 370 | + 700038000,720597600,727413247,727413247,5731200,26290800,37785600,58345200,69235200, | |
| 371 | + 89794800,100684800,121244400,132134400,152694000,163584000,184143600, | |
| 372 | + 195638400,216198000,227088000,247647600,258537600,279097200,289987200,310546800,321436800, | |
| 373 | + 341996400,352886400,373446000,384940800,405500400,416390400,436950000, | |
| 374 | + 447840000,468399600,479289600,499849200,510739200,531298800,542793600,563353200,574243200, | |
| 375 | + 594802800,605692800,626252400,637142400,657702000,668592000,689151600, | |
| 376 | + 700041600,720601200,8154000,25689600,39603600,57744000,71053200,89193600,102502800, | |
| 377 | + 120643200,134557200,152092800,166006800,183542400,197456400,215596800, | |
| 378 | + 228906000,247046400,727413247,5731200,26290800,37785600,58345200,69235200,89794800, | |
| 379 | + 100684800,121244400,132134400,152694000,163584000,184143600,195638400, | |
| 380 | + 216198000,227088000,247647600,258537600,279097200,289987200,310546800,321436800,341996400, | |
| 381 | + 352886400,373446000,384940800,405500400,416390400,436950000,447840000, | |
| 382 | + 468399600,479289600,499849200,510739200,531298800,542793600,563353200,574243200,594802800, | |
| 383 | + 605692800,626252400,637142400,657702000,668592000,689151600,700041600, | |
| 384 | + 720601200,8150400,25686000,39600000,57740400,71049600,89190000,102499200,120639600, | |
| 385 | + 134553600,152089200,166003200,183538800,197452800,215593200,228902400, | |
| 386 | + 247042800,26301600,37796400,58356000,69246000,89805600,100695600,121255200,127908000, | |
| 387 | + 132145200,152704800,163594800,184154400,195649200,216208800,227098800, | |
| 388 | + 247658400,258548400,279108000,289998000,310557600,321447600,342007200,352897200,373456800, | |
| 389 | + 384951600,405511200,416401200,436960800,447850800,468410400,479300400, | |
| 390 | + 499860000,510750000,531309600,542804400,563364000,574254000,594813600,605703600,626263200, | |
| 391 | + 637153200,657712800,668602800,689162400,700052400,720612000,8150400, | |
| 392 | + 25686000,39600000,57740400,71049600,89190000,102499200,120639600,134553600,152089200, | |
| 393 | + 166003200,183538800,197452800,215593200,228902400,247042800,5720400, | |
| 394 | + 26280000,37774800,58334400,69224400,89784000,100674000,121233600,132123600,152683200, | |
| 395 | + 163573200,184132800,195627600,216187200,227077200,247636800,258526800, | |
| 396 | + 279086400,289976400,310536000,321426000,341985600,352875600,373435200,384930000,405489600, | |
| 397 | + 416379600,436939200,447829200,468388800,479278800,499838400,510728400, | |
| 398 | + 531288000,542782800,563342400,574232400,594792000,605682000,626241600,637131600,657691200, | |
| 399 | + 668581200,689140800,700030800,720590400,727413247,5724000,26283600, | |
| 400 | + 37778400,58338000,69228000,89787600,100677600,121237200,132127200,152686800,163576800, | |
| 401 | + 184136400,195631200,216190800,227080800,247640400,258530400,279090000, | |
| 402 | + 289980000,310539600,321429600,341989200,352879200,373438800,384933600,405493200,416383200, | |
| 403 | + 436942800,447832800,468392400,479282400,499842000,510732000,531291600, | |
| 404 | + 542786400,563346000,574236000,594795600,605685600,626245200,637135200,657694800,668584800, | |
| 405 | + 689144400,700034400,720594000,8150400,25686000,39600000,57740400, | |
| 406 | + 71049600,89190000,102499200,120639600,134553600,152089200,166003200,183538800,197452800, | |
| 407 | + 215593200,228902400,247042800,5716800,727413247,5727600,26287200, | |
| 408 | + 37782000,58341600,69231600,89791200,100681200,121240800,132130800,152690400,163580400, | |
| 409 | + 184140000,195634800,216194400,227084400,247644000,258534000,279093600, | |
| 410 | + 289983600,310543200,321433200,341992800,352882800,373442400,384937200,405496800,416386800, | |
| 411 | + 436946400,447836400,468396000,479286000,499845600,510735600,531295200, | |
| 412 | + 542790000,563349600,574239600,594799200,605689200,626248800,637138800,657698400,668588400, | |
| 413 | + 689148000,700038000,720597600,5727600,26287200,37782000,58341600, | |
| 414 | + 69231600,89791200,100681200,121240800,132130800,152690400,163580400,184140000,195634800, | |
| 415 | + 216194400,227084400,247644000,258534000,279093600,289983600,310543200, | |
| 416 | + 321433200,341992800,352882800,373442400,384937200,405496800,416386800,436946400,447836400, | |
| 417 | + 468396000,479286000,499845600,510735600,531295200,542790000,563349600, | |
| 418 | + 574239600,594799200,605689200,626248800,637138800,657698400,668588400,689148000,700038000, | |
| 419 | + 720597600,5727600,26287200,37782000,58341600,69231600,89791200, | |
| 420 | + 100681200,121240800,132130800,152690400,163580400,184140000,195634800,216194400,227084400, | |
| 421 | + 247644000,258534000,279093600,289983600,310543200,321433200,341992800, | |
| 422 | + 352882800,373442400,384937200,405496800,416386800,436946400,447836400,468396000,479286000, | |
| 423 | + 499845600,510735600,531295200,542790000,563349600,574239600,594799200, | |
| 424 | + 605689200,626248800,637138800,657698400,668588400,689148000,700038000,720597600,5727600, | |
| 425 | + 26287200,37782000,58341600,69231600,89791200,100681200,121240800, | |
| 426 | + 132130800,152690400,163580400,184140000,195634800,216194400,227084400,247644000,258534000, | |
| 427 | + 279093600,289983600,310543200,321433200,341992800,352882800,373442400, | |
| 428 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000,499845600,510735600, | |
| 429 | + 531295200,542790000,563349600,574239600,594799200,605689200,626248800, | |
| 430 | + 637138800,657698400,668588400,689148000,700038000,720597600,5742000,26301600,37796400, | |
| 431 | + 58356000,69246000,89805600,100695600,121255200,132145200,152704800, | |
| 432 | + 163594800,184154400,195649200,216208800,227098800,247658400,258548400,279108000,289998000, | |
| 433 | + 310557600,321447600,342007200,352897200,373456800,384951600,405511200, | |
| 434 | + 416401200,436960800,447850800,468410400,479300400,499860000,510750000,531309600,542804400, | |
| 435 | + 563364000,574254000,594813600,605703600,626263200,637153200,657712800, | |
| 436 | + 668602800,689162400,700052400,720612000,727413247,5731200,26290800,37785600,58345200, | |
| 437 | + 69235200,89794800,100684800,121244400,132134400,152694000,163584000, | |
| 438 | + 184143600,195638400,216198000,227088000,247647600,258537600,279097200,289987200,310546800, | |
| 439 | + 321436800,341996400,352886400,373446000,384940800,405500400,416390400, | |
| 440 | + 436950000,447840000,468399600,479289600,499849200,510739200,531298800,542793600,563353200, | |
| 441 | + 574243200,594802800,605692800,626252400,637142400,657702000,668592000, | |
| 442 | + 689151600,700041600,720601200,5731200,26290800,37785600,58345200,69235200,89794800, | |
| 443 | + 100684800,121244400,132134400,152694000,163584000,184143600,195638400, | |
| 444 | + 216198000,227088000,247647600,258537600,279097200,289987200,310546800,321436800,341996400, | |
| 445 | + 352886400,373446000,384940800,405500400,416390400,436950000,447840000, | |
| 446 | + 468399600,479289600,499849200,510739200,531298800,542793600,563353200,574243200,594802800, | |
| 447 | + 605692800,626252400,637142400,657702000,668592000,689151600,700041600, | |
| 448 | + 720601200,5731200,26290800,37785600,58345200,69235200,89794800,100684800,121244400, | |
| 449 | + 132134400,152694000,163584000,184143600,195638400,216198000,227088000, | |
| 450 | + 247647600,258537600,279097200,289987200,310546800,321436800,341996400,352886400,373446000, | |
| 451 | + 384940800,405500400,416390400,436950000,447840000,468399600,479289600, | |
| 452 | + 499849200,510739200,531298800,542793600,563353200,574243200,594802800,605692800,626252400, | |
| 453 | + 637142400,657702000,668592000,689151600,700041600,720601200,7520400, | |
| 454 | + 25664400,38970000,57718800,70419600,89168400,101869200,120618000,133923600,152067600, | |
| 455 | + 165373200,183517200,196822800,215571600,228272400,247021200,259722000, | |
| 456 | + 291776400,309920400,323226000,341370000,354675600,372819600,386125200,404874000,417574800, | |
| 457 | + 436323600,449024400,467773200,481078800,499222800,512528400,530672400, | |
| 458 | + 543978000,562726800,575427600,594176400,606877200,625626000,638326800,657075600,670381200, | |
| 459 | + 688525200,701830800,719974800,727413247,5734800,26294400,37789200, | |
| 460 | + 58348800,69238800,89798400,100688400,121248000,132138000,152697600,163587600,184147200, | |
| 461 | + 195642000,216201600,227091600,247046400,258537600,279097200,289987200, | |
| 462 | + 310546800,321436800,341996400,352886400,373446000,384940800,405500400,416390400,436950000, | |
| 463 | + 447840000,468399600,479289600,499849200,510739200,531298800,542793600, | |
| 464 | + 563353200,574243200,594802800,605692800,626252400,637142400,657702000,668592000,689151600, | |
| 465 | + 700041600,720601200,5727600,26287200,37782000,58341600,69231600, | |
| 466 | + 89791200,100681200,121240800,132130800,152690400,163580400,184140000,195634800,216194400, | |
| 467 | + 227084400,247644000,258534000,279093600,289983600,310543200,321433200, | |
| 468 | + 341992800,352882800,373442400,384937200,405496800,416386800,436946400,447836400,468396000, | |
| 469 | + 479286000,499845600,510735600,531295200,542790000,563349600,574239600, | |
| 470 | + 594799200,605689200,626248800,637138800,657698400,668588400,689148000,700038000,720597600, | |
| 471 | + 727413247,5727600,26287200,69231600,89791200,100681200,121240800, | |
| 472 | + 132130800,152690400,163580400,184140000,195634800,216194400,227084400,247644000,258534000, | |
| 473 | + 279093600,289983600,310543200,321433200,341992800,352882800,373442400, | |
| 474 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000,499845600,510735600, | |
| 475 | + 531295200,542790000,563349600,574239600,594799200,605689200,626248800, | |
| 476 | + 637138800,657698400,668588400,689148000,700038000,720597600,727413247,727413247,43210800, | |
| 477 | + 51076800,60750000,727413247,5731200,26290800,37785600,58345200, | |
| 478 | + 69235200,89794800,100684800,121244400,132134400,152694000,163584000,184143600,195638400, | |
| 479 | + 216198000,227088000,247647600,258537600,279097200,289987200,310546800, | |
| 480 | + 321436800,341996400,352886400,373446000,384940800,405500400,416390400,436950000,447840000, | |
| 481 | + 468399600,479289600,499849200,510739200,531298800,542793600,563353200, | |
| 482 | + 574243200,594802800,605692800,626252400,637142400,657702000,668592000,689151600,700041600, | |
| 483 | + 720601200,5731200,26290800,37785600,58345200,69235200,89794800, | |
| 484 | + 100684800,121244400,132134400,152694000,163584000,184143600,195638400,216198000,227088000, | |
| 485 | + 247647600,258537600,279097200,289987200,310546800,321436800,341996400, | |
| 486 | + 352886400,373446000,384940800,405500400,416390400,436950000,447840000,468399600,479289600, | |
| 487 | + 499849200,510739200,531298800,542793600,563353200,574243200,594802800, | |
| 488 | + 605692800,626252400,637142400,657702000,668592000,689151600,700041600,720601200,727413247, | |
| 489 | + 5731200,26290800,37785600,58345200,69235200,89794800,100684800, | |
| 490 | + 121244400,132134400,152694000,163584000,184143600,195638400,216198000,227088000,247647600, | |
| 491 | + 258537600,279097200,289987200,310546800,321436800,341996400,352886400, | |
| 492 | + 373446000,384940800,405500400,416390400,436950000,447840000,468399600,479289600,499849200, | |
| 493 | + 510739200,531298800,542793600,563353200,574243200,594802800,605692800, | |
| 494 | + 626252400,637142400,657702000,668592000,689151600,700041600,720601200,727413247,727413247, | |
| 495 | + 5738400,26298000,37792800,58352400,69242400,89802000,100692000, | |
| 496 | + 121251600,132141600,152701200,163591200,184150800,195645600,216205200,227095200,247654800, | |
| 497 | + 258544800,279104400,289994400,310554000,321444000,342003600,352893600, | |
| 498 | + 373453200,384948000,405507600,416397600,436957200,447847200,468406800,479296800,499856400, | |
| 499 | + 510746400,531306000,542800800,563360400,574250400,594810000,605700000, | |
| 500 | + 626259600,637149600,657709200,668599200,689158800,700048800,720608400,727413247,43210800, | |
| 501 | + 51076800,74660400,82526400,106110000,113976000,134535600,147844800, | |
| 502 | + 165985200,179294400,197434800,210744000,228884400,242798400,260334000,273643200,292388400, | |
| 503 | + 305697600,323838000,337147200,355287600,368596800,386737200,400046400, | |
| 504 | + 418186800,431496000,450241200,462945600,481690800,495000000,513140400,526449600,544590000, | |
| 505 | + 557899200,576039600,589348800,607489200,620798400,639543600,652248000, | |
| 506 | + 670993200,684302400,702442800,715752000,727413247,4500000,25066800,35949600,56516400, | |
| 507 | + 67399200,87966000,98848800,121230000,130298400,727413247,7520400, | |
| 508 | + 25664400,38970000,57718800,70419600,89168400,101869200,120618000,133923600,152067600, | |
| 509 | + 165373200,183517200,196822800,215571600,228272400,247021200,259722000, | |
| 510 | + 278470800,291776400,309920400,323226000,341370000,354675600,372819600,386125200,404874000, | |
| 511 | + 417574800,436323600,449024400,467773200,481078800,499222800,512528400, | |
| 512 | + 530672400,543978000,562726800,575427600,594176400,606877200,625626000,638326800,657075600, | |
| 513 | + 670381200,688525200,701830800,719974800,727413247,5734800,26294400, | |
| 514 | + 37789200,58348800,69238800,89798400,100688400,121248000,132138000,152697600,163587600, | |
| 515 | + 184147200,195642000,216201600,227091600,247651200,258541200,279100800, | |
| 516 | + 289990800,310550400,321440400,342000000,352890000,373449600,384944400,405504000,416394000, | |
| 517 | + 436953600,447843600,468403200,479293200,499852800,510742800,531302400, | |
| 518 | + 542797200,563356800,574246800,594806400,605696400,626256000,637146000,657705600,668595600, | |
| 519 | + 689155200,700045200,720604800,5742000,26301600,37796400,58356000, | |
| 520 | + 69246000,89805600,100695600,121255200,132145200,152704800,163594800,184154400,195649200, | |
| 521 | + 216208800,227098800,247658400,258548400,279108000,289998000,310557600, | |
| 522 | + 321447600,342007200,352897200,373456800,384951600,405511200,416401200,436960800,447850800, | |
| 523 | + 468410400,479300400,499860000,510750000,531309600,542804400,563364000, | |
| 524 | + 574254000,594813600,605703600,626263200,637153200,657712800,668602800,689162400,700052400, | |
| 525 | + 720612000,5722200,26281800,37776600,58336200,69226200,89785800, | |
| 526 | + 100675800,121235400,132125400,152685000,163575000,184134600,195629400,216189000,227079000, | |
| 527 | + 247638600,258528600,279088200,289978200,310537800,321427800,341987400, | |
| 528 | + 352877400,373437000,384931800,405491400,416381400,436941000,447831000,468390600,479280600, | |
| 529 | + 499840200,510730200,531289800,542784600,563344200,574234200,594793800, | |
| 530 | + 605683800,626243400,637133400,657693000,668583000,689142600,700032600,720592200,5724000, | |
| 531 | + 26283600,37778400,58338000,69228000,89787600,100677600,121237200, | |
| 532 | + 132127200,152686800,163576800,184136400,195631200,216190800,227080800,247640400,258530400, | |
| 533 | + 279090000,289980000,310539600,321429600,341989200,352879200,373438800, | |
| 534 | + 384933600,405493200,416383200,436942800,447832800,468392400,479282400,499842000,510732000, | |
| 535 | + 531291600,542786400,563346000,574236000,594795600,605685600,626245200, | |
| 536 | + 637135200,657694800,668584800,689144400,700034400,720594000,5727600,26287200,37782000, | |
| 537 | + 58341600,69231600,89791200,100681200,121240800,132130800,152690400, | |
| 538 | + 163580400,184140000,195634800,216194400,227084400,247644000,258534000,279093600,289983600, | |
| 539 | + 310543200,321433200,341992800,352882800,373442400,384937200,405496800, | |
| 540 | + 416386800,436946400,447836400,468396000,479286000,499845600,510735600,531295200,542790000, | |
| 541 | + 563349600,574239600,594799200,605689200,626248800,637138800,657698400, | |
| 542 | + 668588400,689148000,700038000,720597600,5738400,26298000,37792800,58352400,69242400, | |
| 543 | + 89802000,100692000,121251600,132141600,152701200,163591200,184150800, | |
| 544 | + 195645600,216205200,227095200,247654800,258544800,279104400,289994400,310554000,321444000, | |
| 545 | + 342003600,352893600,373453200,384948000,405507600,416397600,436957200, | |
| 546 | + 447847200,468406800,479296800,499856400,510746400,531306000,542800800,563360400,574250400, | |
| 547 | + 594810000,605700000,626259600,637149600,657709200,668599200,689158800, | |
| 548 | + 700048800,720608400,5727600,26287200,37782000,58341600,69231600,89791200,100681200, | |
| 549 | + 121240800,132130800,152690400,163580400,184140000,195634800,216194400, | |
| 550 | + 227084400,247644000,258534000,279093600,289983600,310543200,321433200,341992800,352882800, | |
| 551 | + 373442400,384937200,405496800,416386800,436946400,447836400,468396000, | |
| 552 | + 479286000,499845600,510735600,531295200,542790000,563349600,574239600,594799200,605689200, | |
| 553 | + 626248800,637138800,657698400,668588400,689148000,700038000,720597600, | |
| 554 | + 5738400,26298000,37792800,58352400,69242400,89802000,100692000,121251600,132141600, | |
| 555 | + 152701200,163591200,184150800,195645600,216205200,227095200,247654800, | |
| 556 | + 258544800,279104400,289994400,310554000,321444000,342003600,352893600,373453200,384948000, | |
| 557 | + 405507600,416397600,436957200,447847200,468406800,479296800,499856400, | |
| 558 | + 510746400,531306000,542800800,563360400,574250400,594810000,605700000,626259600,637149600, | |
| 559 | + 657709200,668599200,689158800,700048800,720608400,5738400,26298000, | |
| 560 | + 37792800,58352400,69242400,89802000,100692000,121251600,132141600,152701200,163591200, | |
| 561 | + 184143600,5731200,26290800,37785600,58345200,69235200,89794800, | |
| 562 | + 100684800,121244400,132134400,152694000,163584000,184143600,195638400,216198000,227088000, | |
| 563 | + 247647600,258537600,279097200,289987200,310546800,321436800,341996400, | |
| 564 | + 352886400,373446000,384940800,405500400,416390400,436950000,447840000,468399600,479289600, | |
| 565 | + 499849200,510739200,531298800,542793600,563353200,574243200,594802800, | |
| 566 | + 605692800,626252400,637142400,657702000,668592000,689151600,700041600,720601200,5742000, | |
| 567 | + 26301600,37796400,58356000,69246000,89805600,100695600,121255200, | |
| 568 | + 132145200,152704800,163594800,184154400,195649200,216208800,227098800,247658400,258548400, | |
| 569 | + 279108000,289998000,310557600,321447600,342007200,352897200,373456800, | |
| 570 | + 384951600,405511200,416401200,436960800,447850800,468410400,479300400,499860000,510750000, | |
| 571 | + 531309600,542804400,563364000,574254000,594813600,605703600,626263200, | |
| 572 | + 637153200,657712800,668602800,689162400,700052400,720612000,5734800,26294400,37789200, | |
| 573 | + 58348800,69238800,89798400,100688400,121248000,132138000,152697600, | |
| 574 | + 163587600,184147200,195642000,216201600,227091600,247651200,258541200,279100800,289990800, | |
| 575 | + 310550400,321440400,342000000,352890000,373449600,384944400,405504000, | |
| 576 | + 416394000,436953600,447843600,468403200,479293200,499852800,510742800,531302400,542797200, | |
| 577 | + 563356800,574246800,594806400,605696400,626256000,637146000,657705600, | |
| 578 | + 668595600,689155200,700045200,720604800,56995200,100630800,118785600,132681600,150058800, | |
| 579 | + 163526400,181670460,195570000,213120060,227019600,244569660,258220800, | |
| 580 | + 727413247,727413247,727413247,8092800,23817600,39542400,55267200,70992000,86716800, | |
| 581 | + 102441600,118771200,134496000,150220800,165945600,181670400,197395200, | |
| 582 | + 213120000,228844800,244569600,260294400,276019200,292348800,308073600,323798400,339523200, | |
| 583 | + 355248000,370972800,386697600,402422400,418147200,433872000,449596800, | |
| 584 | + 465926400,481651200,497376000,513100800,528825600,544550400,560275200,576000000,591724800, | |
| 585 | + 607449600,623174400,638899200,655228800,670953600,686678400,702403200, | |
| 586 | + 718128000,727413247,8085600,23205600,39535200,54655200,70984800,86104800,102434400, | |
| 587 | + 118159200,134488800,149608800,165938400,181058400,197388000,212508000, | |
| 588 | + 228837600,243957600,260287200,275407200,292341600,307461600,323791200,338911200,355240800, | |
| 589 | + 370360800,386690400,401810400,418140000,433260000,449589600,465314400, | |
| 590 | + 481644000,496764000,513093600,528213600,544543200,559663200,575992800,591112800,607442400, | |
| 591 | + 622562400,638892000,654616800,670946400,686066400,702396000,717516000, | |
| 592 | + 43210800,51076800,60750000,727413247,727413247,8085600,23205600,39535200,54655200,70984800, | |
| 593 | + 86104800,102434400,118159200,134488800,149608800,165938400, | |
| 594 | + 181058400,197388000,212508000,228837600,243957600,260287200,275407200,292341600,307461600, | |
| 595 | + 323791200,338911200,355240800,370360800,386690400,401810400,418140000, | |
| 596 | + 433260000,449589600,465314400,481644000,496764000,513093600,528213600,544543200,559663200, | |
| 597 | + 575992800,591112800,607442400,622562400,638892000,654616800,670946400, | |
| 598 | + 686066400,702396000,717516000,727413247,7520400,25664400,38970000,57718800,70419600, | |
| 599 | + 89168400,101869200,120618000,133923600,152067600,165373200,183517200, | |
| 600 | + 196822800,215571600,228272400,247021200,259722000,278470800,291776400,309920400,323226000, | |
| 601 | + 341370000,354675600,372819600,386125200,404874000,417574800,436323600, | |
| 602 | + 449024400,467773200,481078800,499222800,512528400,530672400,543978000,562726800,575427600, | |
| 603 | + 594176400,606877200,625626000,638326800,657075600,670381200,688525200, | |
| 604 | + 701830800,719974800,727413247,282769200,727413247,7520400,25664400,38970000,57718800, | |
| 605 | + 70419600,89168400,101869200,120618000,133923600,152067600,165373200, | |
| 606 | + 183517200,196822800,215571600,228272400,247021200,259722000,278470800,291776400,309920400, | |
| 607 | + 323226000,341370000,354675600,372819600,386125200,404874000,417574800, | |
| 608 | + 436323600,449024400,467773200,481078800,499222800,512528400,530672400,543978000,562726800, | |
| 609 | + 575427600,594176400,606877200,625626000,638326800,657075600,670381200, | |
| 610 | + 688525200,701830800,719974800,727413247,289159200,727413247,7336800,26085600,39391200, | |
| 611 | + 57535200,70840800,88984800,102290400,120434400,133740000,151884000, | |
| 612 | + 165189600,183938400,196639200,215388000,225669600,246837600,727413247,727413247,727413247, | |
| 613 | + 727413247,727413247,727413247,727413247,727413247,727413247,7516800, | |
| 614 | + 25660800,727413247,727413247,38952000,727413247,7509600,25650000,38959200,57704400,70408800, | |
| 615 | + 89154000,101858400,120603600,133912800,152053200,165362400, | |
| 616 | + 183502800,196812000,215557200,228261600,247006800,259711200,278456400,291765600,309906000, | |
| 617 | + 323215200,341355600,354664800,372805200,386114400,404859600,417564000, | |
| 618 | + 436309200,449013600,467758800,481068000,499208400,512517600,530658000,543967200,562712400, | |
| 619 | + 575416800,594162000,606866400,625611600,638316000,657061200,670370400, | |
| 620 | + 688510800,701820000,719960400,727413247,727413247,38944800,727413247,7408800,23122800, | |
| 621 | + 38858400,54572400,727413247,727413247,727413247,7336800,26082000, | |
| 622 | + 38786400,57531600,70840800,88981200,102290400,120430800,133740000,151880400,165189600, | |
| 623 | + 183934800,196639200,215384400,228088800,246834000,727413247,727413247, | |
| 624 | + 727413247,727413247,727413247,7520400,25664400,38970000,53211600,89168400,101869200, | |
| 625 | + 120618000,133923600,152067600,165373200,183517200,196822800,215571600, | |
| 626 | + 228272400,247021200,259722000,278470800,291776400,309920400,323226000,341370000,354675600, | |
| 627 | + 372819600,386125200,404874000,417574800,436323600,449024400,467773200, | |
| 628 | + 481078800,499222800,512528400,530672400,543978000,562726800,575427600,594176400,606877200, | |
| 629 | + 625626000,638326800,657075600,670381200,688525200,701830800,719974800, | |
| 630 | + 7423200,25480800,38876400,57621600,70326000,89071200,101775600,120520800,133740000, | |
| 631 | + 151966800,165276000,183420000,196725600,215388000,228261600,246927600, | |
| 632 | + 262656000,278377200,293500800,309826800,324345600,341276400,354585600,372726000,386035200, | |
| 633 | + 404780400,417484800,436230000,448934400,467679600,480988800,499129200, | |
| 634 | + 512438400,530578800,543888000,562633200,575337600,594082800,606787200,625532400,638236800, | |
| 635 | + 656982000,670291200,687826800,701740800,718671600,733190400,748911600, | |
| 636 | + 764640000,779756400,796089600,809996400,814233600,814834800,828144000,840841200,844473600, | |
| 637 | + 846284400,859593600,871686000,875318400,877734000,891043200,901926000, | |
| 638 | + 906163200,909183600,922492800,932770800,936403200,941238000,953942400,963615600,967248000, | |
| 639 | + 972687600,985392000,993855600,998092800,1004137200,1017446400,1024700400, | |
| 640 | + 1028332800,1035586800,1048896000,1054940400,1059177600,1067036400,1080345600,1085785200, | |
| 641 | + 1089417600,1099090800,1111795200,1116630000,1120262400,1130540400,1143244800,1146870000, | |
| 642 | + 1151107200,1161990000,1175299200,1177714800,1181347200,1193439600,1206748800,1207954800, | |
| 643 | + 1212192000,1224889200,1238198400,1238799600,1243036800,1256338800,1273276800,1288393200, | |
| 644 | + 1304121600,1319842800,1334361600,1351292400,1365206400,1382742000,1396051200,1414191600, | |
| 645 | + 1427500800,1446246000,1458950400,1477695600,1490400000,1509145200,1521849600,1540594800, | |
| 646 | + 1553904000,1572044400,1585353600,1603494000,1616803200,1635548400,1648252800,1666998000, | |
| 647 | + 1679702400,1697842800,1711756800,1728687600,1743206400,1759532400,1774656000,1789772400, | |
| 648 | + 1806105600,1820617200,1824249600,1824850800,1837555200,1851462000,1855094400,1856300400, | |
| 649 | + 1869004800,1881702000,1885939200,1887750000,1901059200,1912546800,1916179200,1919199600, | |
| 650 | + 1932508800,1942786800,1947024000,1950649200,1963958400,1973631600,1977868800,1982703600, | |
| 651 | + 1995408000,2004476400,2008108800,2014153200,2026857600,2034716400,2038953600,2045602800, | |
| 652 | + 2058912000,2065561200,2069193600,2077052400,2090361600,2096406000,2100038400,2108502000, | |
| 653 | + 2121811200,2126646000,2130883200,2139951600,2153260800,2157490800,2161123200,2172006000, | |
| 654 | + 2184710400,2187730800,2191968000,2203455600,2216160000,2218575600,2222812800,2234905200, | |
| 655 | + 2248214400,2249420400,2253052800,2266354800,7423200,25480800,38876400,57621600, | |
| 656 | + 70326000,89071200,101775600,120520800,133740000,151966800,165276000,183420000,196725600, | |
| 657 | + 215388000,228261600,246927600,262656000,278377200,293500800,309826800, | |
| 658 | + 324345600,341276400,354585600,372726000,386035200,404780400,417484800,436230000,448934400, | |
| 659 | + 467679600,480988800,499129200,512438400,530578800,543888000,562633200, | |
| 660 | + 575337600,594082800,606787200,625532400,638236800,656982000,670291200,687826800,701740800, | |
| 661 | + 718671600,733190400,748911600,764640000,779756400,796089600,809996400, | |
| 662 | + 814233600,814834800,828144000,840841200,844473600,846284400,859593600,871686000,875318400, | |
| 663 | + 877734000,891043200,901926000,906163200,909183600,922492800,932770800, | |
| 664 | + 936403200,941238000,953942400,963615600,967248000,972687600,985392000,993855600,998092800, | |
| 665 | + 1004137200,1017446400,1024700400,1028332800,1035586800,1048896000,1054940400, | |
| 666 | + 1059177600,1067036400,1080345600,1085785200,1089417600,1099090800,1111795200,1116630000, | |
| 667 | + 1120262400,1130540400,1143244800,1146870000,1151107200,1161990000,1175299200,1177714800, | |
| 668 | + 1181347200,1193439600,1206748800,1207954800,1212192000,1224889200,1238198400,1238799600, | |
| 669 | + 1243036800,1256338800,1273276800,1288393200,1304121600,1319842800,1334361600,1351292400, | |
| 670 | + 1365206400,1382742000,1396051200,1414191600,1427500800,1446246000,1458950400,1477695600, | |
| 671 | + 1490400000,1509145200,1521849600,1540594800,1553904000,1572044400,1585353600,1603494000, | |
| 672 | + 1616803200,1635548400,1648252800,1666998000,1679702400,1697842800,1711756800,1728687600, | |
| 673 | + 1743206400,1759532400,1774656000,1789772400,1806105600,1820617200,1824249600,1824850800, | |
| 674 | + 1837555200,1851462000,1855094400,1856300400,1869004800,1881702000,1885939200,1887750000, | |
| 675 | + 1901059200,1912546800,1916179200,1919199600,1932508800,1942786800,1947024000,1950649200, | |
| 676 | + 1963958400,1973631600,1977868800,1982703600,1995408000,2004476400,2008108800,2014153200, | |
| 677 | + 2026857600,2034716400,2038953600,2045602800,2058912000,2065561200,2069193600,2077052400, | |
| 678 | + 2090361600,2096406000,2100038400,2108502000,2121811200,2126646000,2130883200,2139951600, | |
| 679 | + 2153260800,2157490800,2161123200,2172006000,2184710400,2187730800,2191968000,2203455600, | |
| 680 | + 2216160000,2218575600,2222812800,2234905200,2248214400,2249420400,2253052800,2266354800, | |
| 681 | + 727413247,7412400,23126400,38862000,54576000,727413247,727413247,7520400, | |
| 682 | + 26874000,38970000,53125200,727413247,7344000,25657200,38793600,57711600,70243200,89161200, | |
| 683 | + 101692800,120610800,133747200,152060400,165196800,183510000, | |
| 684 | + 196646400,215564400,228096000,247014000,259545600,278463600,291600000,309913200,323049600, | |
| 685 | + 341362800,354499200,372812400,385948800,404866800,417398400,436316400, | |
| 686 | + 448848000,467766000,480902400,499215600,512352000,530665200,543801600,562719600,575251200, | |
| 687 | + 594169200,606700800,625618800,638150400,657068400,670204800,688518000, | |
| 688 | + 701654400,719967600,727413247,727413247,727413247,727413247,727413247,727413247,727413247, | |
| 689 | + 727413247,727413247,727413247,41356800,727413247,727413247,7520400, | |
| 690 | + 25664400,38970000,57718800,70419600,89168400,101869200,120618000,133923600,152067600, | |
| 691 | + 165373200,183517200,196822800,215571600,228272400,247021200,259722000, | |
| 692 | + 278470800,291776400,309920400,323226000,341370000,354675600,372819600,386125200,404874000, | |
| 693 | + 417574800,436323600,449024400,467773200,481078800,499222800,512528400, | |
| 694 | + 530672400,543978000,562726800,575427600,594176400,606877200,625626000,638326800,657075600, | |
| 695 | + 670381200,688525200,701830800,719974800,727413247,49233600,727413247, | |
| 696 | + 727413247,727413247,727413247,19494000,105375600,727413247,289159200,727413247,125258400, | |
| 697 | + 727413247,727413247,727413247,727413247,38937600,727413247,727413247, | |
| 698 | + 727413247,727413247,727413247,727413247,6899400,22793400,38435400,54329400,70057800, | |
| 699 | + 85951800,101593800,117487800,133129800,149023800,164665800,180559800, | |
| 700 | + 196288200,212182200,227824200,243718200,727413247,7344000,25657200,38793600,57711600, | |
| 701 | + 70243200,89161200,101692800,120610800,133747200,152060400,165196800, | |
| 702 | + 183510000,196646400,215564400,228096000,247014000,259545600,278463600,291600000,309913200, | |
| 703 | + 323049600,341362800,354499200,372812400,385948800,404866800,417398400, | |
| 704 | + 436316400,448848000,467766000,480902400,499215600,512352000,530665200,543801600,562719600, | |
| 705 | + 575251200,594169200,606700800,625618800,638150400,657068400,670204800, | |
| 706 | + 688518000,701654400,719967600,727413247,727413247,44395200,727413247,7408800,23122800, | |
| 707 | + 38858400,54572400,727413247,7408800,23122800,38858400,54572400, | |
| 708 | + 727413247,727413247,727413247,727413247,727413247,727413247,727413247,727413247,727413247, | |
| 709 | + 7520400,25664400,38970000,57718800,70419600,89168400,101869200, | |
| 710 | + 120618000,133923600,152067600,165373200,183517200,196822800,215571600,228272400,247021200, | |
| 711 | + 259722000,278470800,291776400,309920400,323226000,341370000,354675600, | |
| 712 | + 372819600,386125200,404874000,417574800,436323600,449024400,467773200,481078800,499222800, | |
| 713 | + 512528400,530672400,543978000,562726800,575427600,594176400,606877200, | |
| 714 | + 625626000,638326800,657075600,670381200,688525200,701830800,719974800,727413247,5724000, | |
| 715 | + 26283600,37778400,58338000,69228000,89787600,100677600,121237200, | |
| 716 | + 132127200,152686800,163576800,184136400,195631200,216190800,227080800,247640400,258530400, | |
| 717 | + 279090000,289980000,310539600,321429600,341989200,352879200,373438800, | |
| 718 | + 384933600,405493200,416383200,436942800,447832800,468392400,479282400,499842000,510732000, | |
| 719 | + 531291600,542786400,563346000,574236000,594795600,605685600,626245200, | |
| 720 | + 637135200,657694800,668584800,689144400,700034400,720594000,7520400,25664400,38970000, | |
| 721 | + 57718800,70419600,89168400,101869200,120618000,133923600,152067600, | |
| 722 | + 165373200,183517200,196822800,215571600,228272400,247021200,259722000,278470800,291776400, | |
| 723 | + 309920400,323226000,341370000,354675600,372819600,386125200,404874000, | |
| 724 | + 417574800,436323600,449024400,467773200,481078800,499222800,512528400,530672400,543978000, | |
| 725 | + 562726800,575427600,594176400,606877200,625626000,638326800,657075600, | |
| 726 | + 670381200,688525200,701830800,719974800,727413247,7520400,25664400,38970000,57718800, | |
| 727 | + 70419600,89168400,101869200,120618000,133923600,152067600,165373200, | |
| 728 | + 183517200,196822800,215571600,228272400,247021200,259722000,278470800,291776400,309920400, | |
| 729 | + 323226000,341370000,354675600,372819600,386125200,404874000,417574800, | |
| 730 | + 436323600,449024400,467773200,481078800,499222800,512528400,530672400,543978000,562726800, | |
| 731 | + 575427600,594176400,606877200,625626000,638326800,657075600,670381200, | |
| 732 | + 688525200,701830800,719974800,7520400,25664400,38970000,57718800,70419600,89168400, | |
| 733 | + 101869200,120618000,133923600,152067600,165373200,183517200,196822800, | |
| 734 | + 215571600,228272400,247021200,259722000,278470800,291776400,309920400,323226000,341370000, | |
| 735 | + 354675600,372819600,386125200,404874000,417574800,436323600,449024400, | |
| 736 | + 467773200,481078800,499222800,512528400,530672400,543978000,562726800,575427600,594176400, | |
| 737 | + 606877200,625626000,638326800,657075600,670381200,688525200,701830800, | |
| 738 | + 719974800,7520400,25664400,38970000,57718800,70419600,89168400,101869200,120618000, | |
| 739 | + 133923600,152067600,165373200,183517200,196822800,215571600,228272400, | |
| 740 | + 247021200,259722000,278470800,291776400,309920400,323226000,341370000,354675600,372819600, | |
| 741 | + 386125200,404874000,417574800,436323600,449024400,467773200,481078800, | |
| 742 | + 499222800,512528400,530672400,543978000,562726800,575427600,594176400,606877200,625626000, | |
| 743 | + 638326800,657075600,670381200,688525200,701830800,719974800,7520400, | |
| 744 | + 25664400,38970000,57718800,70419600,89168400,101869200,120618000,133923600,152067600, | |
| 745 | + 165373200,183517200,196822800,215571600,228272400,247021200,259722000, | |
| 746 | + 278470800,291776400,309920400,323226000,341370000,354675600,372819600,386125200,404874000, | |
| 747 | + 417574800,436323600,449024400,467773200,481078800,499222800,512528400, | |
| 748 | + 530672400,543978000,562726800,575427600,594176400,606877200,625626000,638326800,657075600, | |
| 749 | + 670381200,688525200,701830800,719974800,727413247,727413247,8092800, | |
| 750 | + 23817600,39542400,55267200,70992000,86716800,102441600,118771200,134496000,150220800, | |
| 751 | + 165945600,181670400,197395200,213120000,228844800,244569600,260294400, | |
| 752 | + 276019200,292348800,308073600,323798400,339523200,355248000,370972800,386697600,402422400, | |
| 753 | + 418147200,433872000,449596800,465926400,481651200,497376000,513100800, | |
| 754 | + 528825600,544550400,560275200,576000000,591724800,607449600,623174400,638899200,655228800, | |
| 755 | + 670953600,686678400,702403200,718128000,8094600,23819400,39544200, | |
| 756 | + 55269000,70993800,86718600,102443400,118773000,134497800,150222600,165947400,181672200, | |
| 757 | + 197397000,213121800,228846600,244571400,260296200,276021000,292350600, | |
| 758 | + 308075400,323800200,339525000,355249800,370974600,386699400,402424200,418149000,433873800, | |
| 759 | + 449598600,465928200,481653000,497377800,513102600,528827400,544552200, | |
| 760 | + 560277000,576001800,591726600,607451400,623176200,638901000,655230600,670955400,686680200, | |
| 761 | + 702405000,718129800,8094600,23819400,39544200,55269000,70993800, | |
| 762 | + 86718600,102443400,118773000,134497800,150222600,165947400,181672200,197397000,213121800, | |
| 763 | + 228846600,244571400,260296200,276021000,292350600,308075400,323800200, | |
| 764 | + 339525000,355249800,370974600,386699400,402424200,418149000,433873800,449598600,465928200, | |
| 765 | + 481653000,497377800,513102600,528827400,544552200,560277000,576001800, | |
| 766 | + 591726600,607451400,623176200,638901000,655230600,670955400,686680200,702405000,718129800, | |
| 767 | + 8092800,23817600,39542400,55267200,70992000,86716800,102441600, | |
| 768 | + 118771200,134496000,150220800,165945600,181670400,197395200,213120000,228844800,244569600, | |
| 769 | + 260294400,276019200,292348800,308073600,323798400,339523200,355248000, | |
| 770 | + 370972800,386697600,402422400,418147200,433872000,449596800,465926400,481651200,497376000, | |
| 771 | + 513100800,528825600,544550400,560275200,576000000,591724800,607449600, | |
| 772 | + 623174400,638899200,655228800,670953600,686678400,702403200,718128000,8092800,23817600, | |
| 773 | + 39542400,55267200,70992000,86716800,102441600,118771200,134496000, | |
| 774 | + 150220800,165945600,181670400,197395200,213120000,228844800,244569600,260294400,276019200, | |
| 775 | + 292348800,308073600,323798400,339523200,355248000,370972800,386697600, | |
| 776 | + 402422400,418147200,433872000,449596800,465926400,481651200,497376000,513100800,528825600, | |
| 777 | + 544550400,560275200,576000000,591724800,607449600,623174400,638899200, | |
| 778 | + 655228800,670953600,686678400,702403200,718128000,727413247,8092800,23817600,39542400, | |
| 779 | + 55267200,70992000,86716800,102441600,118771200,134496000,150220800, | |
| 780 | + 165945600,181670400,197395200,213120000,228844800,244569600,260294400,276019200,292348800, | |
| 781 | + 308073600,323798400,339523200,355248000,370972800,386697600,402422400, | |
| 782 | + 418147200,433872000,449596800,465926400,481651200,497376000,513100800,528825600,544550400, | |
| 783 | + 560275200,576000000,591724800,607449600,623174400,638899200,655228800, | |
| 784 | + 670953600,686678400,702403200,718128000,8089200,23815800,39538800,55265400,70988400, | |
| 785 | + 86715000,102438000,118769400,134492400,150219000,165942000,181668600, | |
| 786 | + 197391600,213118200,228841200,244567800,260290800,276017400,292345200,308071800,323794800, | |
| 787 | + 339521400,355244400,370971000,386694000,402420600,418143600,433870200, | |
| 788 | + 449593200,465924600,481647600,497374200,513097200,528823800,544546800,560273400,575996400, | |
| 789 | + 591723000,607446000,623172600,638895600,655227000,670950000,686676600, | |
| 790 | + 702399600,718126200,727413247,8089200,23815800,39538800,55265400,70988400,86715000, | |
| 791 | + 102438000,118769400,134492400,150219000,165942000,181668600,197391600, | |
| 792 | + 213118200,228841200,244567800,260290800,276017400,292345200,308071800,323794800,339521400, | |
| 793 | + 355244400,370971000,386694000,402420600,418143600,433870200,449593200, | |
| 794 | + 465924600,481647600,497374200,513097200,528823800,544546800,560273400,575996400,591723000, | |
| 795 | + 607446000,623172600,638895600,655227000,670950000,686676600,702399600, | |
| 796 | + 718126200,727413247,8092800,23817600,39542400,55267200,70992000,86716800,102441600, | |
| 797 | + 118771200,134496000,150220800,165945600,181670400,197395200,213120000, | |
| 798 | + 228844800,244569600,260294400,276019200,292348800,308073600,323798400,339523200,355248000, | |
| 799 | + 370972800,386697600,402422400,418147200,433872000,449596800,465926400, | |
| 800 | + 481651200,497376000,513100800,528825600,544550400,560275200,576000000,591724800,607449600, | |
| 801 | + 623174400,638899200,655228800,670953600,686678400,702403200,718128000, | |
| 802 | + 8092800,23817600,39542400,55267200,70992000,86716800,102441600,118771200,134496000, | |
| 803 | + 150220800,165945600,181670400,197395200,213120000,228844800,244569600, | |
| 804 | + 260294400,276019200,292348800,308073600,323798400,339523200,355248000,370972800,386697600, | |
| 805 | + 402422400,418147200,433872000,449596800,465926400,481651200,497376000, | |
| 806 | + 513100800,528825600,544550400,560275200,576000000,591724800,607449600,623174400,638899200, | |
| 807 | + 655228800,670953600,686678400,702403200,718128000,8094600,23819400, | |
| 808 | + 39544200,55269000,70993800,86718600,102443400,118773000,134497800,150222600,165947400, | |
| 809 | + 181672200,197397000,213121800,228846600,244571400,260296200,276021000, | |
| 810 | + 292350600,308075400,323800200,339525000,355249800,370974600,386699400,402424200,418149000, | |
| 811 | + 433873800,449598600,465928200,481653000,497377800,513102600,528827400, | |
| 812 | + 544552200,560277000,576001800,591726600,607451400,623176200,638901000,655230600,670955400, | |
| 813 | + 686680200,702405000,718129800,8092800,23817600,39542400,55267200, | |
| 814 | + 70992000,86716800,102441600,118771200,134496000,150220800,165945600,181670400,197395200, | |
| 815 | + 213120000,228844800,244569600,260294400,276019200,292348800,308073600, | |
| 816 | + 323798400,339523200,355248000,370972800,386697600,402422400,418147200,433872000,449596800, | |
| 817 | + 465926400,481651200,497376000,513100800,528825600,544550400,560275200, | |
| 818 | + 576000000,591724800,607449600,623174400,638899200,655228800,670953600,686678400,702403200, | |
| 819 | + 718128000,8092800,23817600,39542400,55267200,70992000,86716800, | |
| 820 | + 102441600,118771200,134496000,150220800,165945600,181670400,197395200,213120000,228844800, | |
| 821 | + 244569600,260294400,276019200,292348800,308073600,323798400,339523200, | |
| 822 | + 355248000,370972800,386697600,402422400,418147200,433872000,449596800,465926400,481651200, | |
| 823 | + 497376000,513100800,528825600,544550400,560275200,576000000,591724800, | |
| 824 | + 607449600,623174400,638899200,655228800,670953600,686678400,702403200,718128000,8092800, | |
| 825 | + 23817600,39542400,55267200,70992000,86716800,102441600,118771200, | |
| 826 | + 134496000,150220800,165945600,181670400,197395200,213120000,228844800,244569600,260294400, | |
| 827 | + 276019200,292348800,308073600,323798400,339523200,355248000,370972800, | |
| 828 | + 386697600,402422400,418147200,433872000,449596800,465926400,481651200,497376000,513100800, | |
| 829 | + 528825600,544550400,560275200,576000000,591724800,607449600,623174400, | |
| 830 | + 638899200,655228800,670953600,686678400,702403200,718128000,8094600,23819400,39544200, | |
| 831 | + 55269000,70993800,86718600,102443400,118773000,134497800,150222600, | |
| 832 | + 165947400,181672200,197397000,213121800,228846600,244571400,260296200,276021000,292350600, | |
| 833 | + 308075400,323800200,339525000,355249800,370974600,386699400,402424200, | |
| 834 | + 418149000,433873800,449598600,465928200,481653000,497377800,513102600,528827400,544552200, | |
| 835 | + 560277000,576001800,591726600,607451400,623176200,638901000,655230600, | |
| 836 | + 670955400,686680200,702405000,718129800,727413247,727413247,4500000,25066800,35949600, | |
| 837 | + 56516400,67399200,87966000,98848800,121230000,130298400,727413247, | |
| 838 | + 727413247,7520400,25664400,38970000,57718800,70419600,89168400,101869200,120618000, | |
| 839 | + 133923600,152067600,165373200,183517200,196822800,215571600,228272400, | |
| 840 | + 247021200,259722000,278470800,291776400,309920400,323226000,341370000,354675600,372819600, | |
| 841 | + 386125200,404874000,417574800,436323600,449024400,467773200,481078800, | |
| 842 | + 499222800,512528400,530672400,543978000,562726800,575427600,594176400,606877200,625626000, | |
| 843 | + 638326800,657075600,670381200,688525200,701830800,719974800,5731200, | |
| 844 | + 26290800,37785600,58345200,69235200,89794800,100684800,121244400,132134400,152694000, | |
| 845 | + 163584000,184143600,195638400,216198000,227088000,247647600,258537600, | |
| 846 | + 279097200,289987200,310546800,321436800,341996400,352886400,373446000,384940800,405500400, | |
| 847 | + 416390400,436950000,447840000,468399600,479289600,499849200,510739200, | |
| 848 | + 531298800,542793600,563353200,574243200,594802800,605692800,626252400,637142400,657702000, | |
| 849 | + 668592000,689151600,700041600,720601200,5724000,26283600,37778400, | |
| 850 | + 58338000,69228000,89787600,100677600,121237200,132127200,152686800,163576800,184136400, | |
| 851 | + 195631200,216190800,227080800,247640400,258530400,279090000,289980000, | |
| 852 | + 310539600,321429600,341989200,352879200,373438800,384933600,405493200,416383200,436942800, | |
| 853 | + 447832800,468392400,479282400,499842000,510732000,531291600,542786400, | |
| 854 | + 563346000,574236000,594795600,605685600,626245200,637135200,657694800,668584800,689144400, | |
| 855 | + 700034400,720594000,5731200,26290800,37785600,58345200,69235200, | |
| 856 | + 89794800,100684800,121244400,132134400,152694000,163584000,184143600,195638400,216198000, | |
| 857 | + 227088000,247647600,258537600,279097200,289987200,310546800,321436800, | |
| 858 | + 341996400,352886400,373446000,384940800,405500400,416390400,436950000,447840000,468399600, | |
| 859 | + 479289600,499849200,510739200,531298800,542793600,563353200,574243200, | |
| 860 | + 594802800,605692800,626252400,637142400,657702000,668592000,689151600,700041600,720601200, | |
| 861 | + 5727600,26287200,37782000,58341600,69231600,89791200,100681200, | |
| 862 | + 121240800,132130800,152690400,163580400,184140000,195634800,216194400,227084400,247644000, | |
| 863 | + 258534000,279093600,289983600,310543200,321433200,341992800,352882800, | |
| 864 | + 373442400,384937200,405496800,416386800,436946400,447836400,468396000,479286000,499845600, | |
| 865 | + 510735600,531295200,542790000,563349600,574239600,594799200,605689200, | |
| 866 | + 626248800,637138800,657698400,668588400,689148000,700038000,720597600,5734800,26294400, | |
| 867 | + 37789200,58348800,69238800,89798400,100688400,121248000,132138000, | |
| 868 | + 152697600,163587600,184147200,195642000,216201600,227091600,247651200,258541200,279100800, | |
| 869 | + 289990800,310550400,321440400,342000000,352890000,373449600,384944400, | |
| 870 | + 405504000,416394000,436953600,447843600,468403200,479293200,499852800,510742800,531302400, | |
| 871 | + 542797200,563356800,574246800,594806400,605696400,626256000,637146000, | |
| 872 | + 657705600,668595600,689155200,700045200,720604800,5722200,26281800,37776600,58336200, | |
| 873 | + 69226200,89785800,100675800,121235400,132125400,152685000,163575000, | |
| 874 | + 184134600,195629400,216189000,227079000,247638600,258528600,279088200,289978200,310537800, | |
| 875 | + 321427800,341987400,352877400,373437000,384931800,405491400,416381400, | |
| 876 | + 436941000,447831000,468390600,479280600,499840200,510730200,531289800,542784600,563344200, | |
| 877 | + 574234200,594793800,605683800,626243400,637133400,657693000,668583000, | |
| 878 | + 689142600,700032600,720592200,5738400,26298000,37792800,58352400,69242400,89802000, | |
| 879 | + 100692000,121251600,132141600,152701200,163591200,184150800,195645600, | |
| 880 | + 216205200,227095200,247654800,258544800,279104400,289994400,310554000,321444000,342003600, | |
| 881 | + 352893600,373453200,384948000,405507600,416397600,436957200,447847200, | |
| 882 | + 468406800,479296800,499856400,510746400,531306000,542800800,563360400,574250400,594810000, | |
| 883 | + 605700000,626259600,637149600,657709200,668599200,689158800,700048800, | |
| 884 | + 720608400,5738400,26298000,37792800,58352400,69242400,89802000,100692000,121251600, | |
| 885 | + 132141600,152701200,163591200,184143600,43210800,51076800,74660400, | |
| 886 | + 82526400,106110000,113976000,134535600,147844800,165985200,179294400,197434800,210744000, | |
| 887 | + 228884400,242798400,260334000,273643200,292388400,305697600,323838000, | |
| 888 | + 337147200,355287600,368596800,386737200,400046400,418186800,431496000,450241200,462945600, | |
| 889 | + 481690800,495000000,513140400,526449600,544590000,557899200,576039600, | |
| 890 | + 589348800,607489200,620798400,639543600,652248000,670993200,684302400,702442800,715752000, | |
| 891 | + 727413247,43210800,51076800,74660400,82526400,106110000,113976000, | |
| 892 | + 134535600,147844800,165985200,179294400,197434800,210744000,228884400,242798400,260334000, | |
| 893 | + 273643200,292388400,305697600,323838000,337147200,355287600,368596800, | |
| 894 | + 386737200,400046400,418186800,431496000,450241200,462945600,481690800,495000000,513140400, | |
| 895 | + 526449600,544590000,557899200,576039600,589348800,607489200,620798400, | |
| 896 | + 639543600,652248000,670993200,684302400,702442800,715752000,727413247,5720400,26283600, | |
| 897 | + 37774800,58338000,69224400,89787600,100674000,121237200,132123600, | |
| 898 | + 152686800,163573200,184136400,195627600,216190800,227077200,247640400,258526800,279090000, | |
| 899 | + 289976400,310539600,321426000,341989200,352875600,373438800,384930000, | |
| 900 | + 405493200,416379600,436942800,447829200,468392400,479278800,499842000,510728400,531291600, | |
| 901 | + 542782800,563346000,574232400,594795600,605682000,626245200,637131600, | |
| 902 | + 657694800,668581200,689144400,700030800,720594000,7520400,25664400,38970000,57718800, | |
| 903 | + 70419600,89168400,101869200,120618000,133923600,152067600,165373200, | |
| 904 | + 183517200,196822800,215571600,228272400,247021200,259722000,278470800,291776400,309920400, | |
| 905 | + 323226000,341370000,354675600,372819600,386125200,404874000,417574800, | |
| 906 | + 4363 |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..589e3d0 | index 0000000..589e3d0 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/.gitignore | +++ b/.gitignore | ||
| @@ -0,0 +1,9 @@ | @@ -0,0 +1,9 @@ | ||
| 1 | +/plugin_time | ||
| 2 | +/plugin_time.musl | ||
| 3 | +/plugin_time.arm64 | ||
| 4 | +/plugin_time.exe | ||
| 5 | +/plugin_time.asan | ||
| 6 | +*.o | ||
| 7 | +*.tmp | ||
| 8 | +__pycache__/ | ||
| 9 | +*.pyc |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..ef80120 | index 0000000..ef80120 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/LICENSE | +++ b/LICENSE | ||
| @@ -0,0 +1,21 @@ | @@ -0,0 +1,21 @@ | ||
| 1 | +MIT License | ||
| 2 | + | ||
| 3 | +Copyright (c) 2026 SCORPIOX INC | ||
| 4 | + | ||
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 6 | +of this software and associated documentation files (the "Software"), to deal | ||
| 7 | +in the Software without restriction, including without limitation the rights | ||
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 9 | +copies of the Software, and to permit persons to whom the Software is | ||
| 10 | +furnished to do so, subject to the following conditions: | ||
| 11 | + | ||
| 12 | +The above copyright notice and this permission notice shall be included in all | ||
| 13 | +copies or substantial portions of the Software. | ||
| 14 | + | ||
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 21 | +SOFTWARE. |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..62017d7 | index 0000000..62017d7 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/Makefile | +++ b/Makefile | ||
| @@ -0,0 +1,74 @@ | @@ -0,0 +1,74 @@ | ||
| 1 | +# plugin_time - SCORPIOX BOT open-source world-time MCP connector | ||
| 2 | +# | ||
| 3 | +# The only non-stdlib dependency is the vendored yyjson JSON parser under | ||
| 4 | +# vendor/yyjson (MIT, upstream yyjson/yyjson); it is compiled into the binary, | ||
| 5 | +# so every artefact stays a standalone executable with zero shared-library | ||
| 6 | +# requirements on the node. | ||
| 7 | +# | ||
| 8 | +# The IANA timezone table lives in src/tzdata.h, generated from a compiled tz | ||
| 9 | +# database by tools/gen_tzdata.py. Regenerate it only when you mean to move to | ||
| 10 | +# a newer tzdata release, and re-run the suite: every epoch in the tests is | ||
| 11 | +# absolute, so a tzdata change that moves a zone shows up as a failure rather | ||
| 12 | +# than as a silently different answer. | ||
| 13 | +# | ||
| 14 | +# Targets: | ||
| 15 | +# make native build -> ./plugin_time | ||
| 16 | +# make musl static musl x86_64 -> ./plugin_time.musl | ||
| 17 | +# make arm64 static arm64 musl (aarch64 cross) -> ./plugin_time.arm64 | ||
| 18 | +# make win64 MinGW-w64 static -> ./plugin_time.exe | ||
| 19 | +# make macos Apple clang -> ./plugin_time (run this on macOS) | ||
| 20 | +# make asan ASan + UBSan + _FORTIFY_SOURCE=2 -> ./plugin_time.asan | ||
| 21 | +# make tzdata regenerate src/tzdata.h from /usr/share/zoneinfo | ||
| 22 | +# make ui regenerate src/ui_html.h from src/ui.html | ||
| 23 | +# make test run tests/run.sh | ||
| 24 | +# make clean | ||
| 25 | + | ||
| 26 | +CC ?= cc | ||
| 27 | +CFLAGS ?= -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings | ||
| 28 | +CPPFLAGS = -I src -I vendor -I vendor/yyjson | ||
| 29 | +SRCS = src/plugin_time.c src/timeutil.c src/tz.c vendor/yyjson/yyjson.c | ||
| 30 | +HDRS = src/plugintime.h src/tz.h src/tzdata.h src/ui_html.h src/bot_theme_plugin.h | ||
| 31 | + | ||
| 32 | +MUSL_CC ?= musl-gcc | ||
| 33 | +ARM64_CC ?= aarch64-linux-gnu-gcc | ||
| 34 | +WIN64_CC ?= x86_64-w64-mingw32-gcc | ||
| 35 | + | ||
| 36 | +all: plugin_time | ||
| 37 | + | ||
| 38 | +plugin_time: $(SRCS) $(HDRS) | ||
| 39 | + $(CC) $(CFLAGS) $(CPPFLAGS) $(SRCS) -o $@ | ||
| 40 | + | ||
| 41 | +musl: plugin_time.musl | ||
| 42 | +plugin_time.musl: $(SRCS) $(HDRS) | ||
| 43 | + $(MUSL_CC) $(CFLAGS) -static $(CPPFLAGS) $(SRCS) -o $@ | ||
| 44 | + | ||
| 45 | +arm64: plugin_time.arm64 | ||
| 46 | +plugin_time.arm64: $(SRCS) $(HDRS) | ||
| 47 | + $(ARM64_CC) $(CFLAGS) -static $(CPPFLAGS) $(SRCS) -o $@ | ||
| 48 | + | ||
| 49 | +win64: plugin_time.exe | ||
| 50 | +plugin_time.exe: $(SRCS) $(HDRS) | ||
| 51 | + $(WIN64_CC) $(CFLAGS) -static -D_WIN32_WINNT=0x0601 $(CPPFLAGS) $(SRCS) -o $@ | ||
| 52 | + | ||
| 53 | +macos: $(SRCS) $(HDRS) | ||
| 54 | + clang $(CFLAGS) $(CPPFLAGS) $(SRCS) -o plugin_time | ||
| 55 | + | ||
| 56 | +asan: plugin_time.asan | ||
| 57 | +plugin_time.asan: $(SRCS) $(HDRS) | ||
| 58 | + $(CC) -O1 -g -std=c99 -Wall -Wextra -D_FORTIFY_SOURCE=2 \ | ||
| 59 | + -fsanitize=address,undefined -fno-omit-frame-pointer \ | ||
| 60 | + $(CPPFLAGS) $(SRCS) -o $@ | ||
| 61 | + | ||
| 62 | +tzdata: | ||
| 63 | + python3 tools/gen_tzdata.py | ||
| 64 | + | ||
| 65 | +ui: | ||
| 66 | + python3 tools/gen_ui.py | ||
| 67 | + | ||
| 68 | +test: plugin_time | ||
| 69 | + sh tests/run.sh | ||
| 70 | + | ||
| 71 | +clean: | ||
| 72 | + rm -f plugin_time plugin_time.musl plugin_time.arm64 plugin_time.exe plugin_time.asan | ||
| 73 | + | ||
| 74 | +.PHONY: all musl arm64 win64 macos asan tzdata ui test clean |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..717bebd | index 0000000..717bebd | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/README.md | +++ b/README.md | ||
| @@ -0,0 +1,189 @@ | @@ -0,0 +1,189 @@ | ||
| 1 | +# SCORPIOX BOT — time | ||
| 2 | + | ||
| 3 | +World time and timezone conversion for your agents and your own node. | ||
| 4 | + | ||
| 5 | +This is the reference MCP **time** server, rebuilt as one static C99 binary with | ||
| 6 | +the IANA tz database compiled into it. Three tools — `get_current_time`, | ||
| 7 | +`convert_time`, `get_time_ago` — and the same three operations back a World | ||
| 8 | +Clock, converter and time-ago panel inside SCORPIOX BOT. No account, no tzdata | ||
| 9 | +package, no CDN, **no network request of any kind**. | ||
| 10 | + | ||
| 11 | +``` | ||
| 12 | +make # native build -> ./plugin_time | ||
| 13 | +make musl # static musl x86_64 -> ./plugin_time.musl | ||
| 14 | +make arm64 # static aarch64 -> ./plugin_time.arm64 | ||
| 15 | +make win64 # MinGW-w64 static PE -> ./plugin_time.exe | ||
| 16 | +make macos # Apple clang (on macOS) -> ./plugin_time | ||
| 17 | +make tzdata # regenerate src/tzdata.h from the host tz database | ||
| 18 | +make ui # regenerate src/ui_html.h from src/ui.html | ||
| 19 | +make test # offline suite (190+ assertions) | ||
| 20 | +python3 tests/e2e.py # Playwright viewport suite (1440 / 1024 / 390, dark + light) | ||
| 21 | +``` | ||
| 22 | + | ||
| 23 | +## Why the database is embedded | ||
| 24 | + | ||
| 25 | +A timezone connector that reads the host's tz database answers differently on | ||
| 26 | +every node: a Windows box has none at all, macOS's filesystem is case-insensitive | ||
| 27 | +so `europe/warsaw` resolves while Linux refuses it, and a slim container may ship | ||
| 28 | +an older tzdata release. Embedding the table makes the same call return the same | ||
| 29 | +string on every platform — which is the whole point of a tool an agent reasons | ||
| 30 | +about. | ||
| 31 | + | ||
| 32 | +`tools/gen_tzdata.py` reads the compiled TZif files and emits `src/tzdata.h`: | ||
| 33 | + | ||
| 34 | +* all **598** IANA keys (links included) for an exact, case-sensitive lookup; | ||
| 35 | +* every real UTC transition from **2015** onward — 10 341 of them, out to 2087 | ||
| 36 | + for the zones that still generate them — so DST rules, Ramadan exceptions | ||
| 37 | + (`Africa/Casablanca`) and redesigned rules (`Asia/Hebron`) are exact rather | ||
| 38 | + than approximated; | ||
| 39 | +* each zone's own POSIX TZ footer string, used only **beyond** the last embedded | ||
| 40 | + transition, which is exactly what a host does when it runs out of table. | ||
| 41 | + | ||
| 42 | +Regeneration is deterministic: the same tz database produces the same bytes, and | ||
| 43 | +`tests/run.sh` re-runs the generator and compares. | ||
| 44 | + | ||
| 45 | +## Install | ||
| 46 | + | ||
| 47 | +```sh | ||
| 48 | +cp plugin_time ~/.scorpiox/plugins/plugin_time | ||
| 49 | +chmod +x ~/.scorpiox/plugins/plugin_time | ||
| 50 | + | ||
| 51 | +# or from the market | ||
| 52 | +curl -s -H "X-Bot-Password: $BOT_PASSWORD" \ | ||
| 53 | + "http://127.0.0.1:8150/plugin_manage?action=install&slug=time&mode=prebuilt" | ||
| 54 | +``` | ||
| 55 | + | ||
| 56 | +Windows: `%LOCALAPPDATA%\scorpiox\plugins\plugin_time.exe`. | ||
| 57 | +BotCloud container: `/workspace/.scorpiox/plugins/plugin_time`. | ||
| 58 | + | ||
| 59 | +## Five modes, one binary | ||
| 60 | + | ||
| 61 | +| Mode | Trigger | Response | | ||
| 62 | +|---|---|---| | ||
| 63 | +| Discovery | `--meta` or `SX_PLUGIN_META=1` | `{"name":"time","desc":"World time MCP","kind":"mcp","mcp":true,"daemon":false,"menus":false,"verified":"live","category":"utilities","version":"1.0.0"}` (unauthenticated) | | ||
| 64 | +| **MCP tool provider** | `--mcp` | newline-delimited **JSON-RPC 2.0 over stdio** — `initialize`, `tools/list`, `tools/call`, `ping` | | ||
| 65 | +| API | `?action=...`, `?format=json`, or any `POST` | `application/json; charset=utf-8` | | ||
| 66 | +| UI | plain `GET` (`?embed=1` aware) | the World Clock page, `text/html` | | ||
| 67 | +| Info | `--version`, `--help`, `--schema` | id + version, usage, the tool schemas | | ||
| 68 | + | ||
| 69 | +Modes 3 and 4 sit behind `require_node_auth()`: `X-Bot-Password`, | ||
| 70 | +`Authorization: Bearer <pwd>`, or `?pwd=`. With `BOT_PASSWORD` unset the check is | ||
| 71 | +a no-op, which is the intended behaviour on a single-user box. `--meta` and | ||
| 72 | +`--mcp` stay open — the node spawns the stdio server itself, so a CGI password | ||
| 73 | +must not stop it, or the router could never reach an installed connector. | ||
| 74 | + | ||
| 75 | +### Why `--mcp` and not an MCP server entry | ||
| 76 | + | ||
| 77 | +`plugin_time` is a **connector plugin**, not a new MCP server. `scorpiox-bot-api` | ||
| 78 | +keeps one lightweight stdio router (`sx-connector-mcp`) and this plugin is | ||
| 79 | +discovered by scan, so installing it never consumes one of `clang`'s 16 MCP | ||
| 80 | +server slots and never adds 3 tools to every prompt. An agent reaches the tools | ||
| 81 | +through `connector_list` / `connector_tools` / `connector_call`. | ||
| 82 | + | ||
| 83 | +## Tools | ||
| 84 | + | ||
| 85 | +### `get_current_time` | ||
| 86 | + | ||
| 87 | +```json | ||
| 88 | +{"timezone": "Pacific/Auckland"} | ||
| 89 | +``` | ||
| 90 | +```json | ||
| 91 | +{ | ||
| 92 | + "timezone": "Pacific/Auckland", | ||
| 93 | + "datetime": "2026-11-14T13:00:00+13:00", | ||
| 94 | + "day_of_week": "Saturday", | ||
| 95 | + "is_dst": true, | ||
| 96 | + "utc_offset": "+13:00", | ||
| 97 | + "abbreviation": "NZDT" | ||
| 98 | +} | ||
| 99 | +``` | ||
| 100 | + | ||
| 101 | +`timezone` is optional and defaults to the node's own zone, which the tool | ||
| 102 | +description names so a model can default to it. `utc_offset` and `abbreviation` | ||
| 103 | +are additions to the reference shape — a caller should not have to re-derive | ||
| 104 | +either from the ISO string. | ||
| 105 | + | ||
| 106 | +### `convert_time` | ||
| 107 | + | ||
| 108 | +```json | ||
| 109 | +{"source_timezone": "Pacific/Auckland", "time": "09:00", | ||
| 110 | + "target_timezone": "America/New_York"} | ||
| 111 | +``` | ||
| 112 | +```json | ||
| 113 | +{ | ||
| 114 | + "source": {"timezone": "Pacific/Auckland", "datetime": "2026-11-14T09:00:00+13:00", "...": "..."}, | ||
| 115 | + "target": {"timezone": "America/New_York", "datetime": "2026-11-13T15:00:00-05:00", "...": "..."}, | ||
| 116 | + "time_difference": "-18.0h", | ||
| 117 | + "later_occurrence": null | ||
| 118 | +} | ||
| 119 | +``` | ||
| 120 | + | ||
| 121 | +* `time` must be exactly `HH:MM`, 24-hour. `9:00`, `24:00`, `12:60` and | ||
| 122 | + `12:30:00` are all refused with the reference server's own message. | ||
| 123 | +* A wall time **skipped** by a forward clock change is an error naming the date: | ||
| 124 | + `Invalid time: 01:30 does not exist in Europe/London on 2026-03-29 (skipped by | ||
| 125 | + a clock change)` — never a silently shifted answer. | ||
| 126 | +* A wall time **repeated** by a backward clock change answers the first | ||
| 127 | + occurrence in `source`/`target` and puts the second under `later_occurrence`, | ||
| 128 | + with its own `note` and `time_difference`. That is Python's `fold=0`/`fold=1` | ||
| 129 | + behaviour, and it is the difference between "01:30" being one time and two. | ||
| 130 | +* `time_difference` is `+3.0h` for whole hours and `+5.75h` / `+13.75h` for the | ||
| 131 | + fractional zones (Kathmandu, Chatham). | ||
| 132 | + | ||
| 133 | +### `get_time_ago` | ||
| 134 | + | ||
| 135 | +```json | ||
| 136 | +{"timezone": "Europe/London", "hours_ago": 5} | ||
| 137 | +``` | ||
| 138 | + | ||
| 139 | +The span is subtracted from the **UTC instant**, not the local wall clock, so | ||
| 140 | +"5 hours ago" across a clock change is five real hours. Every component | ||
| 141 | +(`days_ago`, `hours_ago`, `minutes_ago`, `seconds_ago`) defaults to 0 and must be | ||
| 142 | +non-negative. | ||
| 143 | + | ||
| 144 | +## The page | ||
| 145 | + | ||
| 146 | +`GET /plugin_time` serves a bot-themed World Clock: a live clock in any zone, a | ||
| 147 | +converter with both sides named, an editable world-clock table (persisted per | ||
| 148 | +browser), a time-ago panel and the live tool list. Every value on the page comes | ||
| 149 | +from a tool call, so a node whose system clock is off shows the tool's answer | ||
| 150 | +rather than the browser's. | ||
| 151 | + | ||
| 152 | +Dark and light come from the bot's own tokens and its `localStorage.bot_theme` | ||
| 153 | +key; `?theme=light|dark` and the `{type:'sx-theme'}` postMessage both work, and | ||
| 154 | +`?embed=1` hides the standalone chrome inside a bot modal. Zero `<script src=>`, | ||
| 155 | +zero `<link rel=stylesheet>`, zero CDN — it renders offline and inside a strict | ||
| 156 | +CSP. | ||
| 157 | + | ||
| 158 | +## API actions | ||
| 159 | + | ||
| 160 | +| Action | Arguments | Result | | ||
| 161 | +|---|---|---| | ||
| 162 | +| `current` (or `now`, or `format=json`) | `timezone` | the `get_current_time` object | | ||
| 163 | +| `convert` | `source_timezone`, `time`, `target_timezone` | the `convert_time` object | | ||
| 164 | +| `ago` | `timezone`, `days_ago`, `hours_ago`, `minutes_ago`, `seconds_ago` | the `get_time_ago` object | | ||
| 165 | +| `zones` | — | every embedded key, sorted, plus the node's local zone | | ||
| 166 | +| `tools` | — | the `tools/list` array | | ||
| 167 | +| `info` | — | id, version, kind, zone count, local zone | | ||
| 168 | + | ||
| 169 | +Arguments come from the query string or a JSON body (`POST`), and the API | ||
| 170 | +returns **byte-identical** objects to the MCP tools — `tests/run.sh` asserts | ||
| 171 | +that, because a page that disagrees with the tool is worse than no page. | ||
| 172 | + | ||
| 173 | +## Safety | ||
| 174 | + | ||
| 175 | +`tests/run.sh` asserts the connector sources contain no `popen`, `system`, | ||
| 176 | +`socket`, `connect`, `fork`, `execv`, no `fopen` for write/append, no `remove`, | ||
| 177 | +`unlink`, `rmdir`, `mkdir`, `rename`, `chmod`, and no `curl`/`wget`. Every | ||
| 178 | +`yyjson` string goes through the copying variant, so no document can be built | ||
| 179 | +from a stack buffer that has already been reused. The ASan + UBSan + | ||
| 180 | +`_FORTIFY_SOURCE=2` build runs the whole tool set, the repeated-hour path and the | ||
| 181 | +page with the sanitizers on. | ||
| 182 | + | ||
| 183 | +The binary reads: the system clock, `$TZ`, `/etc/localtime` and `/etc/timezone` | ||
| 184 | +(on POSIX), and the platform's own timezone info (on Windows). It writes nothing. | ||
| 185 | + | ||
| 186 | +## Licence | ||
| 187 | + | ||
| 188 | +MIT. The IANA timezone database it embeds is public domain; the generator and the | ||
| 189 | +table are data, not code lifted from another implementation. |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..259a265 | index 0000000..259a265 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/docs/SECURITY.md | +++ b/docs/SECURITY.md | ||
| @@ -0,0 +1,52 @@ | @@ -0,0 +1,52 @@ | ||
| 1 | +# Security notes | ||
| 2 | + | ||
| 3 | +## What the binary reads | ||
| 4 | + | ||
| 5 | +* the system clock (`time()`), for "now" - overridable with `SX_TIME_NOW` for | ||
| 6 | + deterministic tests; | ||
| 7 | +* `$TZ`, `/etc/localtime` (readlink) and `/etc/timezone` on POSIX, and the | ||
| 8 | + platform timezone information on Windows, to name the node's own zone. | ||
| 9 | + | ||
| 10 | +That is the whole list. Everything else - every offset, abbreviation and DST | ||
| 11 | +rule - comes from the table compiled into the binary. | ||
| 12 | + | ||
| 13 | +## What it does not do | ||
| 14 | + | ||
| 15 | +`tests/run.sh` asserts the connector sources contain none of: | ||
| 16 | + | ||
| 17 | +`popen`, `system`, `socket`, `connect`, `fork`, `execv`, `fopen` for write or | ||
| 18 | +append, `remove`, `unlink`, `rmdir`, `mkdir`, `rename`, `chmod`, `curl`, `wget`. | ||
| 19 | + | ||
| 20 | +So there is no file written, no directory created, no program run and no outbound | ||
| 21 | +request. The plugin is a pure function of (instant, zone name). | ||
| 22 | + | ||
| 23 | +## Authentication | ||
| 24 | + | ||
| 25 | +The page and the JSON API sit behind `require_node_auth()`, which accepts | ||
| 26 | +`X-Bot-Password`, `Authorization: Bearer <pwd>` or `?pwd=` and 401s when | ||
| 27 | +`BOT_PASSWORD` is set and none match. `--meta` and `--mcp` stay open: the node | ||
| 28 | +spawns the stdio server itself, and `/plugins` must be able to discover the | ||
| 29 | +connector. | ||
| 30 | + | ||
| 31 | +A bare `curl http://<node>:8150/plugin_time` on a node with `BOT_PASSWORD` set | ||
| 32 | +answers **401**, which is the first check to run after installing anything. | ||
| 33 | + | ||
| 34 | +## Input handling | ||
| 35 | + | ||
| 36 | +* A timezone argument is validated against the embedded key list - exact, | ||
| 37 | + case-sensitive - after a cheap rejection of `..`, `/`, `\`, `%`, control | ||
| 38 | + characters and anything over 96 bytes. An unknown key is an error naming it; | ||
| 39 | + it is never treated as a path. | ||
| 40 | +* A JSON string whose declared length differs from its C string length (an | ||
| 41 | + embedded NUL) is refused rather than silently truncated to its prefix. | ||
| 42 | +* `time` must be exactly five characters of `HH:MM` with a real hour and minute. | ||
| 43 | +* Every `yyjson` value is emitted with the *copying* constructor. The | ||
| 44 | + non-copying one stores the caller's pointer, and every description and zone | ||
| 45 | + name in this code lives in a stack buffer that is reused - which produces | ||
| 46 | + valid-looking JSON with the wrong text. | ||
| 47 | + | ||
| 48 | +## Build hardening | ||
| 49 | + | ||
| 50 | +`make asan` builds with `-fsanitize=address,undefined -D_FORTIFY_SOURCE=2`, and | ||
| 51 | +the suite drives the whole tool set, the repeated-hour path (the allocation-heavy | ||
| 52 | +one) and the page through it, asserting no sanitizer output. |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..6c269a0 | index 0000000..6c269a0 |

| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..394832b | index 0000000..394832b |

| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..a55bad3 | index 0000000..a55bad3 |

| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..ee0485b | index 0000000..ee0485b |

| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..8e45d4f | index 0000000..8e45d4f |

| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..8e554cc | index 0000000..8e554cc |

| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..b66deb1 | index 0000000..b66deb1 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/docs/usage.md | +++ b/docs/usage.md | ||
| @@ -0,0 +1,105 @@ | @@ -0,0 +1,105 @@ | ||
| 1 | +# Using the time connector | ||
| 2 | + | ||
| 3 | +Three MCP tools and one page, all backed by a timezone database compiled into the | ||
| 4 | +binary. This guide is written for a person setting it up, not for an agent. | ||
| 5 | + | ||
| 6 | +## 1. Install it | ||
| 7 | + | ||
| 8 | +Through SCORPIOX BOT: **Settings -> Plugins -> Marketplace**, find **time** and | ||
| 9 | +pick a target. The prebuilt binary for your node's platform is verified against | ||
| 10 | +its published SHA-256 and run through a `--meta` smoke test before it is moved | ||
| 11 | +into place. | ||
| 12 | + | ||
| 13 | +By hand: | ||
| 14 | + | ||
| 15 | +```sh | ||
| 16 | +cp plugin_time ~/.scorpiox/plugins/plugin_time | ||
| 17 | +chmod +x ~/.scorpiox/plugins/plugin_time | ||
| 18 | +``` | ||
| 19 | + | ||
| 20 | +Windows puts it in `%LOCALAPPDATA%\scorpiox\plugins\plugin_time.exe`; a BotCloud | ||
| 21 | +container uses `/workspace/.scorpiox/plugins/plugin_time`. | ||
| 22 | + | ||
| 23 | +No restart is needed: the node discovers `plugin_*` on every request. | ||
| 24 | + | ||
| 25 | +## 2. Check the zone name you want | ||
| 26 | + | ||
| 27 | +```sh | ||
| 28 | +curl -s -H "X-Bot-Password: $BOT_PASSWORD" \ | ||
| 29 | + "http://127.0.0.1:8150/plugin_time?action=zones" | head -c 200 | ||
| 30 | +``` | ||
| 31 | + | ||
| 32 | +Every key is an exact IANA name. `Europe/Warsaw` is a key; `europe/warsaw` is | ||
| 33 | +not, on any platform, on purpose - a connector that accepts both on macOS and | ||
| 34 | +only one on Linux makes the same call mean different things per node. | ||
| 35 | + | ||
| 36 | +## 3. Ask for the time | ||
| 37 | + | ||
| 38 | +```sh | ||
| 39 | +curl -s -H "X-Bot-Password: $BOT_PASSWORD" \ | ||
| 40 | + "http://127.0.0.1:8150/plugin_time?action=current&timezone=Pacific/Auckland" | ||
| 41 | +``` | ||
| 42 | + | ||
| 43 | +```json | ||
| 44 | +{"ok":true,"timezone":"Pacific/Auckland","datetime":"2026-11-14T13:00:00+13:00", | ||
| 45 | + "day_of_week":"Saturday","is_dst":true,"utc_offset":"+13:00","abbreviation":"NZDT"} | ||
| 46 | +``` | ||
| 47 | + | ||
| 48 | +Omit `timezone` and you get the node's own zone. | ||
| 49 | + | ||
| 50 | +## 4. Convert | ||
| 51 | + | ||
| 52 | +```sh | ||
| 53 | +curl -s -H "X-Bot-Password: $BOT_PASSWORD" \ | ||
| 54 | + "http://127.0.0.1:8150/plugin_time?action=convert&source_timezone=Pacific/Auckland&time=09:00&target_timezone=America/New_York" | ||
| 55 | +``` | ||
| 56 | + | ||
| 57 | +Two things surprise people, and both are deliberate: | ||
| 58 | + | ||
| 59 | +* **A time that does not exist is an error.** On the morning a zone springs | ||
| 60 | + forward, 02:30 never happens; the answer says so and names the date instead of | ||
| 61 | + quietly returning 03:30 or 01:30. | ||
| 62 | +* **A time that happens twice is reported twice.** On the night a zone falls | ||
| 63 | + back, 01:30 happens at +01:00 and again at +00:00. `source`/`target` are the | ||
| 64 | + first; `later_occurrence` is the second, with its own note. | ||
| 65 | + | ||
| 66 | +## 5. Ask what the time was | ||
| 67 | + | ||
| 68 | +```sh | ||
| 69 | +curl -s -H "X-Bot-Password: $BOT_PASSWORD" \ | ||
| 70 | + "http://127.0.0.1:8150/plugin_time?action=ago&timezone=Europe/London&hours_ago=5" | ||
| 71 | +``` | ||
| 72 | + | ||
| 73 | +The span is subtracted from the UTC instant, so five hours across a clock change | ||
| 74 | +is five real hours - not five readings of the wall clock, which would be four or | ||
| 75 | +six. | ||
| 76 | + | ||
| 77 | +## 6. From an agent | ||
| 78 | + | ||
| 79 | +`plugin_time` is a connector, reached through the single `sx-connector-mcp` | ||
| 80 | +router rather than its own MCP server slot: | ||
| 81 | + | ||
| 82 | +```sh | ||
| 83 | +sx-connector-mcp --list # time should be listed | ||
| 84 | +sx-connector-mcp --tools time # the three schemas | ||
| 85 | +sx-connector-mcp --call time convert_time \ | ||
| 86 | + '{"source_timezone":"Pacific/Auckland","time":"09:00","target_timezone":"Asia/Tokyo"}' | ||
| 87 | +``` | ||
| 88 | + | ||
| 89 | +## 7. The page | ||
| 90 | + | ||
| 91 | +Open **Settings -> Plugins -> time -> Open**, or `/plugin_time` on the node | ||
| 92 | +directly. The clock, the converter and the world-clock table all call the tools | ||
| 93 | +every second, so what you see is what an agent gets. Add zones with the picker; | ||
| 94 | +the list is remembered in your browser. | ||
| 95 | + | ||
| 96 | +## 8. Updating the database | ||
| 97 | + | ||
| 98 | +```sh | ||
| 99 | +make tzdata # regenerate src/tzdata.h from /usr/share/zoneinfo | ||
| 100 | +make test # the epochs in the suite are absolute, so a moved zone fails loudly | ||
| 101 | +``` | ||
| 102 | + | ||
| 103 | +Only do that deliberately: a tzdata release can change historical transitions, | ||
| 104 | +and the suite pinning absolute instants is what turns such a change into a | ||
| 105 | +visible failure rather than a silently different answer. |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..4643bd2 | index 0000000..4643bd2 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/plugin.json | +++ b/plugin.json | ||
| @@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
| 1 | +{ | ||
| 2 | + "slug": "time", | ||
| 3 | + "id": "plugin_time", | ||
| 4 | + "name": "time", | ||
| 5 | + "desc": "World time MCP", | ||
| 6 | + "title": "World Time & Timezone Converter (MCP)", | ||
| 7 | + "summary": "Zero-dependency MCP connector for timezones: get_current_time, convert_time and get_time_ago over IANA names, with the same answer on every node. The tz database is embedded in the binary (598 zones, real UTC transitions through 2087 plus each zone's own POSIX rule beyond that), so a Windows node with no tzdata resolves Pacific/Auckland exactly like a Linux one, and europe/warsaw is refused everywhere instead of only on case-sensitive filesystems. A repeated hour at a DST fall-back answers both occurrences, and a wall time skipped at spring-forward is an error naming the date rather than a silently shifted answer. The same three operations back a World Clock, converter and time-ago panel on the plugin page. Zero network requests.", | ||
| 8 | + "kind": "mcp", | ||
| 9 | + "mcp": true, | ||
| 10 | + "daemon": false, | ||
| 11 | + "menus": false, | ||
| 12 | + "verified": "live", | ||
| 13 | + "category": "utilities", | ||
| 14 | + "license": "MIT", | ||
| 15 | + "capabilities": [ | ||
| 16 | + "mcp_stdio_server", | ||
| 17 | + "embedded_iana_tzdata", | ||
| 18 | + "no_network", | ||
| 19 | + "no_state_write" | ||
| 20 | + ], | ||
| 21 | + "trust_notice": "Executes native C code on your node. It reads the node's own timezone name (TZ, /etc/localtime, /etc/timezone, or the platform's offset) and the system clock, and nothing else: no file is written, no directory is scanned, no program is run, and it makes zero outbound network requests. All timezone rules come from the IANA database compiled into this binary.", | ||
| 22 | + "author": "SCORPIOX INC", | ||
| 23 | + "homepage": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-time" | ||
| 24 | +} |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..0dd243b | index 0000000..0dd243b | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/src/bot_theme_plugin.h | +++ b/src/bot_theme_plugin.h | ||
| @@ -0,0 +1,129 @@ | @@ -0,0 +1,129 @@ | ||
| 1 | +/* | ||
| 2 | + * bot_theme_plugin.h -- SCORPIOX BOT theme tokens for node plugins. | ||
| 3 | + * | ||
| 4 | + * A trimmed copy of scorpiox-bot-web/bot_theme.h (the bot's single source of | ||
| 5 | + * truth for its dual theme). A plugin runs inside the bot (Hub modal, Session | ||
| 6 | + * Card modal, Chat floating window) or in its own tab, and in every case it | ||
| 7 | + * must look like part of the bot: same tokens, same dark/light pair, same | ||
| 8 | + * localStorage key, same prepaint order. | ||
| 9 | + * | ||
| 10 | + * Three rules, and nothing else: | ||
| 11 | + * | ||
| 12 | + * 1. Colours come from these tokens only. No plugin-private palette. | ||
| 13 | + * 2. The theme is decided BEFORE first paint, in this order: | ||
| 14 | + * ?theme=light|dark -> localStorage.bot_theme -> prefers-color-scheme | ||
| 15 | + * so a plugin opened from the bot paints in the bot's theme from frame 0. | ||
| 16 | + * 3. When the bot's theme changes it posts {type:'sx-theme',theme:'...'} into | ||
| 17 | + * this window; the plugin follows without a reload. The plugin asks once | ||
| 18 | + * with {type:'sx-theme-request'} at startup, which covers the case where | ||
| 19 | + * it was already open when the bot changed theme. | ||
| 20 | + * | ||
| 21 | + * The bottom of :root carries aliases for the names older plugin CSS already | ||
| 22 | + * uses (--surface, --surface2, --muted, --accent, --accent2, --danger, --warn, | ||
| 23 | + * --ok), so adopting the bot palette is a swap, not a redesign. Because they | ||
| 24 | + * are var() references, one definition covers both themes. | ||
| 25 | + * | ||
| 26 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | ||
| 27 | + */ | ||
| 28 | +#ifndef SX_BOT_THEME_PLUGIN_H | ||
| 29 | +#define SX_BOT_THEME_PLUGIN_H | ||
| 30 | + | ||
| 31 | +/* Dark is the default; light is applied by html[data-theme=light] (chosen by | ||
| 32 | + * ?theme= or the bot) or by prefers-color-scheme when nothing chose. */ | ||
| 33 | +#define SX_BOT_THEME_TOKENS \ | ||
| 34 | +":root{color-scheme:dark;" \ | ||
| 35 | + "--bg:#000000;--bg-2:#08080a;--bg-card:#1c1c1e;" \ | ||
| 36 | + "--surface-2:rgba(255,255,255,.06);--surface-hover:rgba(255,255,255,.05);" \ | ||
| 37 | + "--line:rgba(255,255,255,.08);--line-2:rgba(255,255,255,.16);--border:rgba(255,255,255,.08);" \ | ||
| 38 | + "--text:#f5f5f7;--text-2:#86868b;--dim:#86868b;" \ | ||
| 39 | + "--blue:#0a84ff;--green:#30d158;--orange:#ff9f0a;--red:#ff453a;--gray:#8e8e93;" \ | ||
| 40 | + "--indigo:#5e5ce6;--teal:#64d2ff;--purple:#bf5af2;--yellow:#ffd60a;" \ | ||
| 41 | + "--input-bg:rgba(255,255,255,.04);--modal-bg:#1c1c1e;--menu-bg:#1c1c1e;--code-bg:rgba(255,255,255,.06);" \ | ||
| 42 | + "--mono:'SF Mono',ui-monospace,Menlo,Consolas,'DejaVu Sans Mono',monospace;" \ | ||
| 43 | + "--font:-apple-system,BlinkMacSystemFont,'SF Pro Display','SF Pro Text','Inter','Segoe UI',Roboto,Helvetica,Arial,sans-serif;" \ | ||
| 44 | + "--shadow:0 1px 2px rgba(0,0,0,.35);" \ | ||
| 45 | + "--shadow-lg:0 30px 80px rgba(0,0,0,.65),0 0 0 1px rgba(255,255,255,.04);" \ | ||
| 46 | + "--ease:cubic-bezier(.16,1,.3,1);" \ | ||
| 47 | + "--surface:var(--bg-card);--surface2:var(--surface-2);--muted:var(--text-2);" \ | ||
| 48 | + "--accent:var(--blue);--accent2:var(--green);--accent-dim:#0071e3;" \ | ||
| 49 | + "--danger:var(--red);--err:var(--red);--warn:var(--orange);--ok:var(--green);" \ | ||
| 50 | +"}" \ | ||
| 51 | +"@media (prefers-color-scheme:light){html:not([data-theme=dark]){" \ | ||
| 52 | + "--bg:#f5f5f7;--bg-2:#ffffff;--bg-card:#ffffff;" \ | ||
| 53 | + "--surface-2:rgba(0,0,0,.04);--surface-hover:#ffffff;" \ | ||
| 54 | + "--line:rgba(0,0,0,.06);--line-2:rgba(0,0,0,.12);--border:rgba(0,0,0,.06);" \ | ||
| 55 | + "--text:#1d1d1f;--text-2:#6e6e73;--dim:#6e6e73;" \ | ||
| 56 | + "--blue:#007aff;--green:#34c759;--orange:#ff9500;--red:#ff3b30;--gray:#8e8e93;" \ | ||
| 57 | + "--indigo:#5856d6;--teal:#32ade6;--purple:#af52de;--yellow:#b8860b;" \ | ||
| 58 | + "--accent-dim:#0066d6;" \ | ||
| 59 | + "--input-bg:#f5f5f7;--modal-bg:#ffffff;--menu-bg:#ffffff;--code-bg:rgba(0,0,0,.05);" \ | ||
| 60 | + "--shadow:0 2px 10px rgba(0,0,0,.03);" \ | ||
| 61 | + "--shadow-lg:0 30px 80px rgba(0,0,0,.18),0 0 0 1px rgba(0,0,0,.04);" \ | ||
| 62 | + "color-scheme:light}}" \ | ||
| 63 | +"html[data-theme=light]{" \ | ||
| 64 | + "--bg:#f5f5f7;--bg-2:#ffffff;--bg-card:#ffffff;" \ | ||
| 65 | + "--surface-2:rgba(0,0,0,.04);--surface-hover:#ffffff;" \ | ||
| 66 | + "--line:rgba(0,0,0,.06);--line-2:rgba(0,0,0,.12);--border:rgba(0,0,0,.06);" \ | ||
| 67 | + "--text:#1d1d1f;--text-2:#6e6e73;--dim:#6e6e73;" \ | ||
| 68 | + "--blue:#007aff;--green:#34c759;--orange:#ff9500;--red:#ff3b30;--gray:#8e8e93;" \ | ||
| 69 | + "--indigo:#5856d6;--teal:#32ade6;--purple:#af52de;--yellow:#b8860b;" \ | ||
| 70 | + "--accent-dim:#0066d6;" \ | ||
| 71 | + "--input-bg:#f5f5f7;--modal-bg:#ffffff;--menu-bg:#ffffff;--code-bg:rgba(0,0,0,.05);" \ | ||
| 72 | + "--shadow:0 2px 10px rgba(0,0,0,.03);" \ | ||
| 73 | + "--shadow-lg:0 30px 80px rgba(0,0,0,.18),0 0 0 1px rgba(0,0,0,.04);" \ | ||
| 74 | + "color-scheme:light}" \ | ||
| 75 | +"html{color-scheme:dark}html[data-theme=dark]{color-scheme:dark}" \ | ||
| 76 | +"*{box-sizing:border-box}" \ | ||
| 77 | +"body{margin:0;background:var(--bg);color:var(--text);font-family:var(--font);" \ | ||
| 78 | + "-webkit-font-smoothing:antialiased}" \ | ||
| 79 | +"button{font-family:inherit;letter-spacing:inherit}" \ | ||
| 80 | +"button:active{transform:scale(.98)}" \ | ||
| 81 | +"select option{background:var(--menu-bg);color:var(--text)}" \ | ||
| 82 | +"a{color:var(--blue);text-decoration:none;font-weight:600}" \ | ||
| 83 | +"/* Embed mode (?embed=1): the host owns the title and the theme, so the plugin" \ | ||
| 84 | +" * hides its own back link and its own toggle. */" \ | ||
| 85 | +"html.embedded .embed-hide,html.embedded #themeToggle{display:none!important}" \ | ||
| 86 | +"@media(prefers-reduced-motion:reduce){*{animation:none!important;transition:none!important}}" | ||
| 87 | + | ||
| 88 | +/* Pre-paint: decide the theme before the first frame. Must be the first thing | ||
| 89 | + * in <head> so there is no flash of the wrong theme. Also flags embed mode as | ||
| 90 | + * html.embedded so the embed rules apply before first paint. */ | ||
| 91 | +#define SX_BOT_THEME_PREPAINT \ | ||
| 92 | +"<script>(function(){var t='';" \ | ||
| 93 | +"try{var m=/[?&]theme=(light|dark)/.exec(location.search);if(m)t=m[1];}catch(e){}" \ | ||
| 94 | +"if(!t){try{var s=localStorage.getItem('bot_theme');if(s==='light'||s==='dark')t=s;}catch(e){}}" \ | ||
| 95 | +"if(!t){try{t=(window.matchMedia&&window.matchMedia('(prefers-color-scheme: light)').matches)?'light':'dark';}catch(e){t='dark';}}" \ | ||
| 96 | +"document.documentElement.setAttribute('data-theme',t);" \ | ||
| 97 | +"try{if(/[?&]embed=(1|true)(&|$)/.test(location.search))document.documentElement.classList.add('embedded');}catch(e){}" \ | ||
| 98 | +"try{localStorage.setItem('bot_theme',t);}catch(e){}" \ | ||
| 99 | +"})();</script>\n" | ||
| 100 | + | ||
| 101 | +/* Handoff: follow the bot. The host posts {type:'sx-theme',theme}; we ask once | ||
| 102 | + * at startup in case it changed while this page was already open. Fires the | ||
| 103 | + * same 'bot-theme' CustomEvent the bot pages use, so canvas surfaces can | ||
| 104 | + * re-skin themselves from one listener. */ | ||
| 105 | +#define SX_BOT_THEME_HANDOFF \ | ||
| 106 | +"<script>(function(){" \ | ||
| 107 | +"function apply(t){if(t!=='light'&&t!=='dark')return;" \ | ||
| 108 | +"if(document.documentElement.getAttribute('data-theme')===t)return;" \ | ||
| 109 | +"document.documentElement.setAttribute('data-theme',t);" \ | ||
| 110 | +"try{localStorage.setItem('bot_theme',t);}catch(e){}" \ | ||
| 111 | +"try{window.dispatchEvent(new CustomEvent('bot-theme',{detail:t}));}catch(e){}}" \ | ||
| 112 | +"window.sxApplyTheme=apply;" \ | ||
| 113 | +"window.sxTheme=function(){var t=document.documentElement.getAttribute('data-theme');return (t==='light'||t==='dark')?t:'dark';};" \ | ||
| 114 | +"window.addEventListener('message',function(e){var d=e&&e.data;if(d&&d.type==='sx-theme')apply(d.theme);});" \ | ||
| 115 | +"try{if(window.parent&&window.parent!==window)window.parent.postMessage({type:'sx-theme-request'},'*');}catch(e){}" \ | ||
| 116 | +"})();</script>\n" | ||
| 117 | + | ||
| 118 | +/* Standalone toggle (hidden in embed mode by the CSS above). Only useful when | ||
| 119 | + * the plugin is opened in its own tab; inside the bot the host owns the theme. | ||
| 120 | + * Give it class 'icon-btn'-free styling from the page, or use the helper JS. */ | ||
| 121 | +#define SX_BOT_THEME_TOGGLE_BTN \ | ||
| 122 | +"<button class='embed-hide' id='themeToggle' type='button' title='Toggle appearance'>Theme</button>" | ||
| 123 | + | ||
| 124 | +#define SX_BOT_THEME_TOGGLE_JS \ | ||
| 125 | +"<script>(function(){var b=document.getElementById('themeToggle');if(!b)return;" \ | ||
| 126 | +"b.onclick=function(){window.sxApplyTheme(window.sxTheme()==='dark'?'light':'dark');};" \ | ||
| 127 | +"})();</script>\n" | ||
| 128 | + | ||
| 129 | +#endif /* SX_BOT_THEME_PLUGIN_H */ |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..9442641 | index 0000000..9442641 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/src/plugin_time.c | +++ b/src/plugin_time.c | ||
| @@ -0,0 +1,705 @@ | @@ -0,0 +1,705 @@ | ||
| 1 | +/* | ||
| 2 | + * plugin_time.c - entry point and HTTP front end for the SCORPIOX BOT world | ||
| 3 | + * time connector. | ||
| 4 | + * | ||
| 5 | + * One binary, five modes, exactly as the plugin_<name> spec requires: | ||
| 6 | + * | ||
| 7 | + * 1. --meta / SX_PLUGIN_META=1 -> one line of discovery JSON, exit 0. | ||
| 8 | + * Unauthenticated on purpose: /plugins probes it, and the | ||
| 9 | + * sx-connector-mcp router probes it before it will run anything else. | ||
| 10 | + * 2. --mcp -> MCP JSON-RPC 2.0 over stdio, one request | ||
| 11 | + * per line, one response per line. Through the single sx-connector-mcp | ||
| 12 | + * router this is what makes the plugin a tool provider for SCORPIOX CODE | ||
| 13 | + * with no clang change and no extra MCP server slot. | ||
| 14 | + * 3. --schema -> the tools/list array, for a human. | ||
| 15 | + * 4. ?action=... | ?format=json | POST -> the JSON API the page talks to. | ||
| 16 | + * Same three tools as --mcp, so the page can never drift from the agent. | ||
| 17 | + * 5. GET (default) -> the World Clock & Timezone Converter page. | ||
| 18 | + * | ||
| 19 | + * Modes 4 and 5 sit behind require_node_auth(): a node's :8150 is reachable | ||
| 20 | + * from the LAN. --meta and --mcp stay open so discovery and the router work; | ||
| 21 | + * neither answers a web request. | ||
| 22 | + * | ||
| 23 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | ||
| 24 | + */ | ||
| 25 | + | ||
| 26 | +#if defined(_WIN32) && !defined(_WIN32_WINNT) | ||
| 27 | +# define _WIN32_WINNT 0x0601 | ||
| 28 | +#endif | ||
| 29 | + | ||
| 30 | +#if !defined(_WIN32) | ||
| 31 | +# define _POSIX_C_SOURCE 200809L | ||
| 32 | +# define _DEFAULT_SOURCE 1 | ||
| 33 | +# if defined(__APPLE__) | ||
| 34 | +# define _DARWIN_C_SOURCE 1 | ||
| 35 | +# endif | ||
| 36 | +#endif | ||
| 37 | + | ||
| 38 | +#include <stdio.h> | ||
| 39 | +#include <stdlib.h> | ||
| 40 | +#include <string.h> | ||
| 41 | +#include <ctype.h> | ||
| 42 | + | ||
| 43 | +#if defined(_WIN32) | ||
| 44 | +# define WIN32_LEAN_AND_MEAN | ||
| 45 | +# include <windows.h> | ||
| 46 | +# include <io.h> | ||
| 47 | +# include <fcntl.h> | ||
| 48 | +/* MinGW's CRT starts stdout in TEXT mode and rewrites every \n into \r\n. A | ||
| 49 | + * CGI header block must end with exactly CRLF CRLF; text mode turns that into | ||
| 50 | + * \r\r\n\r\r\n, scorpiox-server never finds the separator, and the whole reply | ||
| 51 | + * (headers included) is served as a body. Force binary stdio before main(). */ | ||
| 52 | +__attribute__((constructor)) | ||
| 53 | +static void sx_plugin_binary_stdio(void) | ||
| 54 | +{ | ||
| 55 | + _setmode(_fileno(stdout), _O_BINARY); | ||
| 56 | + _setmode(_fileno(stdin), _O_BINARY); | ||
| 57 | +} | ||
| 58 | +#else | ||
| 59 | +# include <unistd.h> | ||
| 60 | +#endif | ||
| 61 | + | ||
| 62 | +#include "plugintime.h" | ||
| 63 | +#include "tz.h" | ||
| 64 | +#include "bot_theme_plugin.h" | ||
| 65 | +#include "ui_html.h" | ||
| 66 | + | ||
| 67 | +/* ------------------------------------------------------------------ */ | ||
| 68 | +/* --meta */ | ||
| 69 | +/* ------------------------------------------------------------------ */ | ||
| 70 | + | ||
| 71 | +static int mode_meta(void) | ||
| 72 | +{ | ||
| 73 | + printf("{\"name\":\"%s\",\"desc\":\"%s\",\"kind\":\"mcp\",\"mcp\":true," | ||
| 74 | + "\"daemon\":false,\"menus\":false,\"verified\":\"live\"," | ||
| 75 | + "\"category\":\"utilities\",\"version\":\"%s\"}\n", | ||
| 76 | + PLUGIN_NAME, PLUGIN_DESC, PLUGIN_VER); | ||
| 77 | + fflush(stdout); | ||
| 78 | + return 0; | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +/* ------------------------------------------------------------------ */ | ||
| 82 | +/* MCP over stdio */ | ||
| 83 | +/* ------------------------------------------------------------------ */ | ||
| 84 | + | ||
| 85 | +static void send_raw(const char *json) | ||
| 86 | +{ | ||
| 87 | + fputs(json, stdout); | ||
| 88 | + fputc('\n', stdout); | ||
| 89 | + fflush(stdout); | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | +static void send_result_raw(const char *id_json, const char *result_json) | ||
| 93 | +{ | ||
| 94 | + buf_t b; | ||
| 95 | + buf_init(&b); | ||
| 96 | + buf_puts(&b, "{\"jsonrpc\":\"2.0\",\"id\":"); | ||
| 97 | + buf_puts(&b, id_json ? id_json : "null"); | ||
| 98 | + buf_puts(&b, ",\"result\":"); | ||
| 99 | + buf_puts(&b, result_json ? result_json : "{}"); | ||
| 100 | + buf_puts(&b, "}"); | ||
| 101 | + send_raw(buf_str(&b)); | ||
| 102 | + buf_free(&b); | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +static void send_result(yyjson_val *id, const char *result_json) | ||
| 106 | +{ | ||
| 107 | + char *js = id ? yyjson_val_write(id, 0, NULL) : NULL; | ||
| 108 | + send_result_raw(js ? js : "null", result_json); | ||
| 109 | + free(js); | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | +static void send_error(yyjson_val *id, int code, const char *msg) | ||
| 113 | +{ | ||
| 114 | + buf_t b; | ||
| 115 | + char *js = id ? yyjson_val_write(id, 0, NULL) : NULL; | ||
| 116 | + | ||
| 117 | + buf_init(&b); | ||
| 118 | + buf_puts(&b, "{\"jsonrpc\":\"2.0\",\"id\":"); | ||
| 119 | + /* A parse error has no id to echo; the spec says answer with null, and a | ||
| 120 | + * silent drop makes a broken client look like a hung server. */ | ||
| 121 | + buf_puts(&b, js ? js : "null"); | ||
| 122 | + buf_printf(&b, ",\"error\":{\"code\":%d,\"message\":", code); | ||
| 123 | + json_str(&b, msg); | ||
| 124 | + buf_puts(&b, "}}"); | ||
| 125 | + send_raw(buf_str(&b)); | ||
| 126 | + buf_free(&b); | ||
| 127 | + free(js); | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +static void handle_request(yyjson_val *root, yyjson_val *id, const char *method) | ||
| 131 | +{ | ||
| 132 | + if (strcmp(method, "initialize") == 0) { | ||
| 133 | + buf_t b; | ||
| 134 | + char *local = NULL; | ||
| 135 | + buf_init(&b); | ||
| 136 | + buf_puts(&b, "{\"protocolVersion\":\"" MCP_PROTOCOL "\"," | ||
| 137 | + "\"capabilities\":{\"tools\":{\"listChanged\":false}}," | ||
| 138 | + "\"serverInfo\":{\"name\":\"" SERVER_NAME "\",\"version\":\""); | ||
| 139 | + buf_puts(&b, PLUGIN_VER); | ||
| 140 | + buf_puts(&b, "\"},\"instructions\":"); | ||
| 141 | + { | ||
| 142 | + buf_t d; | ||
| 143 | + buf_init(&d); | ||
| 144 | + buf_puts(&d, "IANA timezone queries and wall-clock conversion for " | ||
| 145 | + "three tools: get_current_time, convert_time and " | ||
| 146 | + "get_time_ago. Times are resolved from an embedded tz " | ||
| 147 | + "database, so every platform answers identically. The " | ||
| 148 | + "server's local timezone is "); | ||
| 149 | + buf_puts(&d, tz_local_name()); | ||
| 150 | + buf_puts(&d, "."); | ||
| 151 | + json_str(&b, buf_str(&d)); | ||
| 152 | + buf_free(&d); | ||
| 153 | + } | ||
| 154 | + buf_puts(&b, "}"); | ||
| 155 | + send_result(id, buf_str(&b)); | ||
| 156 | + buf_free(&b); | ||
| 157 | + free(local); | ||
| 158 | + return; | ||
| 159 | + } | ||
| 160 | + if (strcmp(method, "ping") == 0) { | ||
| 161 | + send_result(id, "{}"); | ||
| 162 | + return; | ||
| 163 | + } | ||
| 164 | + if (strcmp(method, "tools/list") == 0) { | ||
| 165 | + char *tools = tools_list_json(); | ||
| 166 | + buf_t b; | ||
| 167 | + buf_init(&b); | ||
| 168 | + buf_puts(&b, "{\"tools\":"); | ||
| 169 | + buf_puts(&b, tools ? tools : "[]"); | ||
| 170 | + buf_puts(&b, "}"); | ||
| 171 | + send_result(id, buf_str(&b)); | ||
| 172 | + buf_free(&b); | ||
| 173 | + free(tools); | ||
| 174 | + return; | ||
| 175 | + } | ||
| 176 | + if (strcmp(method, "tools/call") == 0) { | ||
| 177 | + yyjson_val *params = yyjson_obj_get(root, "params"); | ||
| 178 | + const char *name = NULL; | ||
| 179 | + yyjson_val *args = NULL; | ||
| 180 | + tool_out_t t; | ||
| 181 | + char *res; | ||
| 182 | + | ||
| 183 | + if (params && yyjson_is_obj(params)) { | ||
| 184 | + yyjson_val *nv = yyjson_obj_get(params, "name"); | ||
| 185 | + if (nv && yyjson_is_str(nv)) name = yyjson_get_str(nv); | ||
| 186 | + args = yyjson_obj_get(params, "arguments"); | ||
| 187 | + if (args && !yyjson_is_obj(args)) args = NULL; | ||
| 188 | + } | ||
| 189 | + if (!name) { | ||
| 190 | + send_error(id, -32602, "tools/call needs params.name"); | ||
| 191 | + return; | ||
| 192 | + } | ||
| 193 | + if (tool_dispatch(name, args, &t) != 0) { | ||
| 194 | + buf_t b; | ||
| 195 | + buf_init(&b); | ||
| 196 | + buf_puts(&b, "Unknown tool: "); | ||
| 197 | + buf_puts(&b, name); | ||
| 198 | + send_error(id, -32602, buf_str(&b)); | ||
| 199 | + buf_free(&b); | ||
| 200 | + tool_free(&t); | ||
| 201 | + return; | ||
| 202 | + } | ||
| 203 | + res = tool_mcp_result(&t); | ||
| 204 | + send_result(id, res ? res : "{\"content\":[],\"isError\":true}"); | ||
| 205 | + free(res); | ||
| 206 | + tool_free(&t); | ||
| 207 | + return; | ||
| 208 | + } | ||
| 209 | + if (strcmp(method, "resources/list") == 0) { | ||
| 210 | + send_result(id, "{\"resources\":[]}"); | ||
| 211 | + return; | ||
| 212 | + } | ||
| 213 | + if (strcmp(method, "resources/templates/list") == 0) { | ||
| 214 | + send_result(id, "{\"resourceTemplates\":[]}"); | ||
| 215 | + return; | ||
| 216 | + } | ||
| 217 | + if (strcmp(method, "prompts/list") == 0) { | ||
| 218 | + send_result(id, "{\"prompts\":[]}"); | ||
| 219 | + return; | ||
| 220 | + } | ||
| 221 | + send_error(id, -32601, "Method not found"); | ||
| 222 | +} | ||
| 223 | + | ||
| 224 | +static int mode_mcp(void) | ||
| 225 | +{ | ||
| 226 | + char *line = (char *)malloc((size_t)STDIN_LINE_MAX); | ||
| 227 | + if (!line) return 1; | ||
| 228 | + | ||
| 229 | + for (;;) { | ||
| 230 | + size_t n = 0; | ||
| 231 | + int c; | ||
| 232 | + | ||
| 233 | + /* Read one line by hand: fgets with a fixed buffer would truncate a | ||
| 234 | + * long arguments object, and a truncated line parses as valid-but- | ||
| 235 | + * wrong JSON instead of failing. */ | ||
| 236 | + for (;;) { | ||
| 237 | + c = fgetc(stdin); | ||
| 238 | + if (c == EOF) break; | ||
| 239 | + if (c == '\n') break; | ||
| 240 | + if (n + 1 < (size_t)STDIN_LINE_MAX) line[n++] = (char)c; | ||
| 241 | + } | ||
| 242 | + line[n] = '\0'; | ||
| 243 | + if (n == 0) { | ||
| 244 | + if (c == EOF) { free(line); return 0; } | ||
| 245 | + continue; | ||
| 246 | + } | ||
| 247 | + { | ||
| 248 | + yyjson_doc *doc = yyjson_read(line, n, 0); | ||
| 249 | + yyjson_val *root, *mv, *id; | ||
| 250 | + const char *method; | ||
| 251 | + | ||
| 252 | + if (!doc) { | ||
| 253 | + send_error(NULL, -32700, "Parse error: not valid JSON"); | ||
| 254 | + continue; | ||
| 255 | + } | ||
| 256 | + root = yyjson_doc_get_root(doc); | ||
| 257 | + if (!root || !yyjson_is_obj(root)) { | ||
| 258 | + yyjson_doc_free(doc); | ||
| 259 | + send_error(NULL, -32600, "Invalid request: expected an object"); | ||
| 260 | + continue; | ||
| 261 | + } | ||
| 262 | + mv = yyjson_obj_get(root, "method"); | ||
| 263 | + method = (mv && yyjson_is_str(mv)) ? yyjson_get_str(mv) : NULL; | ||
| 264 | + id = yyjson_obj_get(root, "id"); | ||
| 265 | + | ||
| 266 | + if (!method) { | ||
| 267 | + yyjson_doc_free(doc); | ||
| 268 | + if (yyjson_obj_get(root, "result") || | ||
| 269 | + yyjson_obj_get(root, "error")) continue; | ||
| 270 | + send_error(id, -32600, "Invalid request: missing method"); | ||
| 271 | + continue; | ||
| 272 | + } | ||
| 273 | + if (!id) { /* notification: no reply */ | ||
| 274 | + yyjson_doc_free(doc); | ||
| 275 | + continue; | ||
| 276 | + } | ||
| 277 | + handle_request(root, id, method); | ||
| 278 | + yyjson_doc_free(doc); | ||
| 279 | + } | ||
| 280 | + } | ||
| 281 | +} | ||
| 282 | + | ||
| 283 | +/* ------------------------------------------------------------------ */ | ||
| 284 | +/* Query parameters */ | ||
| 285 | +/* ------------------------------------------------------------------ */ | ||
| 286 | + | ||
| 287 | +static int get_query_val(const char *qs, const char *key, char *out, size_t outsz) | ||
| 288 | +{ | ||
| 289 | + size_t klen; | ||
| 290 | + | ||
| 291 | + if (!qs || !key || !out || outsz == 0) return 0; | ||
| 292 | + out[0] = '\0'; | ||
| 293 | + klen = strlen(key); | ||
| 294 | + while (*qs) { | ||
| 295 | + if (strncmp(qs, key, klen) == 0 && qs[klen] == '=') { | ||
| 296 | + size_t i = 0; | ||
| 297 | + qs += klen + 1; | ||
| 298 | + while (*qs && *qs != '&' && i + 1 < outsz) { | ||
| 299 | + /* %XX and + decode: a timezone name never needs it, but an | ||
| 300 | + * `America/Argentina/Buenos_Aires` over the wire does not, and | ||
| 301 | + * a page passing `time=09:30` from a form does. */ | ||
| 302 | + if (*qs == '%' && isxdigit((unsigned char)qs[1]) && | ||
| 303 | + isxdigit((unsigned char)qs[2])) { | ||
| 304 | + int hi = isdigit((unsigned char)qs[1]) ? qs[1] - '0' | ||
| 305 | + : (tolower((unsigned char)qs[1]) - 'a' + 10); | ||
| 306 | + int lo = isdigit((unsigned char)qs[2]) ? qs[2] - '0' | ||
| 307 | + : (tolower((unsigned char)qs[2]) - 'a' + 10); | ||
| 308 | + out[i++] = (char)((hi << 4) | lo); | ||
| 309 | + qs += 3; | ||
| 310 | + continue; | ||
| 311 | + } | ||
| 312 | + out[i++] = (*qs == '+') ? ' ' : *qs; | ||
| 313 | + qs++; | ||
| 314 | + } | ||
| 315 | + out[i] = '\0'; | ||
| 316 | + return 1; | ||
| 317 | + } | ||
| 318 | + qs = strchr(qs, '&'); | ||
| 319 | + if (!qs) break; | ||
| 320 | + qs++; | ||
| 321 | + } | ||
| 322 | + return 0; | ||
| 323 | +} | ||
| 324 | + | ||
| 325 | +/* ------------------------------------------------------------------ */ | ||
| 326 | +/* Node authentication */ | ||
| 327 | +/* */ | ||
| 328 | +/* The node's :8150 is LAN-reachable, so the page and the JSON API must */ | ||
| 329 | +/* not answer a bare curl. Three credential forms because three callers */ | ||
| 330 | +/* differ: bot-web proxies with a header, the plugin iframe appends */ | ||
| 331 | +/* ?pwd=, and a script may send a bearer token. --meta and --mcp return */ | ||
| 332 | +/* above this, so discovery and the router stay open. */ | ||
| 333 | +/* ------------------------------------------------------------------ */ | ||
| 334 | + | ||
| 335 | +static void require_node_auth(void) | ||
| 336 | +{ | ||
| 337 | + const char *np = getenv("BOT_PASSWORD"); | ||
| 338 | + const char *xp, *auth, *qs; | ||
| 339 | + char qpwd[256]; | ||
| 340 | + | ||
| 341 | + if (!np || !*np) return; | ||
| 342 | + | ||
| 343 | + xp = getenv("HTTP_X_BOT_PASSWORD"); | ||
| 344 | + if (xp && strcmp(xp, np) == 0) return; | ||
| 345 | + | ||
| 346 | + auth = getenv("HTTP_AUTHORIZATION"); | ||
| 347 | + if (auth) { | ||
| 348 | + while (*auth == ' ') auth++; | ||
| 349 | + if (strncmp(auth, "Bearer ", 7) == 0) { | ||
| 350 | + auth += 7; | ||
| 351 | + while (*auth == ' ') auth++; | ||
| 352 | + if (strcmp(auth, np) == 0) return; | ||
| 353 | + } | ||
| 354 | + } | ||
| 355 | + | ||
| 356 | + qs = getenv("QUERY_STRING"); | ||
| 357 | + if (qs && get_query_val(qs, "pwd", qpwd, sizeof qpwd) && | ||
| 358 | + strcmp(qpwd, np) == 0) return; | ||
| 359 | + | ||
| 360 | + fputs("Status: 401 Unauthorized\r\n" | ||
| 361 | + "Content-Type: application/json; charset=utf-8\r\n" | ||
| 362 | + "Cache-Control: no-store\r\n" | ||
| 363 | + "X-Content-Type-Options: nosniff\r\n\r\n" | ||
| 364 | + "{\"ok\":false,\"error\":\"invalid node password\"}\n", stdout); | ||
| 365 | + exit(0); | ||
| 366 | +} | ||
| 367 | + | ||
| 368 | +/* ------------------------------------------------------------------ */ | ||
| 369 | +/* Responses */ | ||
| 370 | +/* ------------------------------------------------------------------ */ | ||
| 371 | + | ||
| 372 | +static void json_header(void) | ||
| 373 | +{ | ||
| 374 | + fputs("Content-Type: application/json; charset=utf-8\r\n" | ||
| 375 | + "Cache-Control: no-store\r\n" | ||
| 376 | + "X-Content-Type-Options: nosniff\r\n\r\n", stdout); | ||
| 377 | +} | ||
| 378 | + | ||
| 379 | +static void json_raw(const char *json) | ||
| 380 | +{ | ||
| 381 | + json_header(); | ||
| 382 | + fputs(json ? json : "{\"ok\":true}", stdout); | ||
| 383 | + fputc('\n', stdout); | ||
| 384 | + fflush(stdout); | ||
| 385 | +} | ||
| 386 | + | ||
| 387 | +static void json_err(int code, const char *msg) | ||
| 388 | +{ | ||
| 389 | + buf_t b; | ||
| 390 | + const char *status = "Status: 500 Internal Server Error\r\n"; | ||
| 391 | + | ||
| 392 | + if (code == 400) status = "Status: 400 Bad Request\r\n"; | ||
| 393 | + else if (code == 404) status = "Status: 404 Not Found\r\n"; | ||
| 394 | + else if (code == 413) status = "Status: 413 Payload Too Large\r\n"; | ||
| 395 | + | ||
| 396 | + buf_init(&b); | ||
| 397 | + buf_puts(&b, status); | ||
| 398 | + buf_puts(&b, "Content-Type: application/json; charset=utf-8\r\n" | ||
| 399 | + "Cache-Control: no-store\r\n" | ||
| 400 | + "X-Content-Type-Options: nosniff\r\n\r\n"); | ||
| 401 | + buf_puts(&b, "{\"ok\":false,\"error\":"); | ||
| 402 | + json_str(&b, msg ? msg : "error"); | ||
| 403 | + buf_puts(&b, "}\n"); | ||
| 404 | + fputs(buf_str(&b), stdout); | ||
| 405 | + fflush(stdout); | ||
| 406 | + buf_free(&b); | ||
| 407 | +} | ||
| 408 | + | ||
| 409 | +/* Reads the request body when CONTENT_LENGTH says there is one. Never blocks | ||
| 410 | + * on an inherited empty stdin. */ | ||
| 411 | +static char g_body[REQ_BODY_MAX]; | ||
| 412 | +static yyjson_doc *g_body_doc = NULL; | ||
| 413 | + | ||
| 414 | +static int read_body(void) | ||
| 415 | +{ | ||
| 416 | + const char *cl = getenv("CONTENT_LENGTH"); | ||
| 417 | + long len; | ||
| 418 | + size_t got; | ||
| 419 | + | ||
| 420 | + g_body[0] = '\0'; | ||
| 421 | + if (!cl || !*cl) return 0; | ||
| 422 | + len = strtol(cl, NULL, 10); | ||
| 423 | + if (len <= 0) return 0; | ||
| 424 | + if ((size_t)len >= sizeof g_body) return -1; | ||
| 425 | + got = fread(g_body, 1, (size_t)len, stdin); | ||
| 426 | + g_body[got] = '\0'; | ||
| 427 | + return (int)got; | ||
| 428 | +} | ||
| 429 | + | ||
| 430 | +static yyjson_val *body_root(void) | ||
| 431 | +{ | ||
| 432 | + if (!g_body_doc) { | ||
| 433 | + if (!g_body[0]) return NULL; | ||
| 434 | + g_body_doc = yyjson_read(g_body, strlen(g_body), 0); | ||
| 435 | + if (!g_body_doc) return NULL; | ||
| 436 | + } | ||
| 437 | + return yyjson_doc_get_root(g_body_doc); | ||
| 438 | +} | ||
| 439 | + | ||
| 440 | +static int param_str(const char *qs, const char *key, char *out, size_t outsz) | ||
| 441 | +{ | ||
| 442 | + yyjson_val *root, *v; | ||
| 443 | + | ||
| 444 | + if (out && outsz) out[0] = '\0'; | ||
| 445 | + if (qs && get_query_val(qs, key, out, outsz) && out && out[0]) return 1; | ||
| 446 | + root = body_root(); | ||
| 447 | + v = root ? yyjson_obj_get(root, key) : NULL; | ||
| 448 | + if (v && yyjson_is_str(v)) { | ||
| 449 | + snprintf(out, outsz, "%s", yyjson_get_str(v)); | ||
| 450 | + return 1; | ||
| 451 | + } | ||
| 452 | + return 0; | ||
| 453 | +} | ||
| 454 | + | ||
| 455 | +/* ------------------------------------------------------------------ */ | ||
| 456 | +/* HTML page */ | ||
| 457 | +/* ------------------------------------------------------------------ */ | ||
| 458 | + | ||
| 459 | +/* Expand the two control-byte placeholders from src/ui.html into the bot | ||
| 460 | + * theme macros, so the page and the bot palette can never drift apart. */ | ||
| 461 | +static void emit_ui(void) | ||
| 462 | +{ | ||
| 463 | + const char *p = UI_HTML; | ||
| 464 | + | ||
| 465 | + while (*p) { | ||
| 466 | + if (*p == '\001') { | ||
| 467 | + fputs(SX_BOT_THEME_PREPAINT "<style>" SX_BOT_THEME_TOKENS | ||
| 468 | + ":root{--tx:var(--text);--mu:var(--text-2);--ln:var(--line);--ac:var(--blue)}" | ||
| 469 | + "</style>", stdout); | ||
| 470 | + p++; | ||
| 471 | + } else if (*p == '\002') { | ||
| 472 | + fputs(SX_BOT_THEME_HANDOFF, stdout); | ||
| 473 | + p++; | ||
| 474 | + } else { | ||
| 475 | + putchar(*p++); | ||
| 476 | + } | ||
| 477 | + } | ||
| 478 | +} | ||
| 479 | + | ||
| 480 | +static void mode_html(void) | ||
| 481 | +{ | ||
| 482 | + fputs("Content-Type: text/html; charset=utf-8\r\n" | ||
| 483 | + "Cache-Control: no-store\r\n" | ||
| 484 | + "X-Content-Type-Options: nosniff\r\n" | ||
| 485 | + "X-Frame-Options: SAMEORIGIN\r\n" | ||
| 486 | + "Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'; " | ||
| 487 | + "style-src 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; " | ||
| 488 | + "form-action 'none'; base-uri 'none'\r\n\r\n", stdout); | ||
| 489 | + emit_ui(); | ||
| 490 | +} | ||
| 491 | + | ||
| 492 | +/* ------------------------------------------------------------------ */ | ||
| 493 | +/* JSON API */ | ||
| 494 | +/* ------------------------------------------------------------------ */ | ||
| 495 | + | ||
| 496 | +/* Run one tool for an HTTP action. The page sends the same argument names as | ||
| 497 | + * the MCP schema, so one dispatch serves both surfaces. */ | ||
| 498 | +static void api_run(const char *tool, const char *qs) | ||
| 499 | +{ | ||
| 500 | + yyjson_mut_doc *md = yyjson_mut_doc_new(NULL); | ||
| 501 | + yyjson_mut_val *args; | ||
| 502 | + yyjson_doc *adoc; | ||
| 503 | + yyjson_val *av; | ||
| 504 | + tool_out_t t; | ||
| 505 | + char *res; | ||
| 506 | + const char *keys[] = { "timezone", "time", "source_timezone", | ||
| 507 | + "target_timezone", "days_ago", "hours_ago", | ||
| 508 | + "minutes_ago", "seconds_ago" }; | ||
| 509 | + size_t i; | ||
| 510 | + | ||
| 511 | + if (!md) { json_err(500, "out of memory"); return; } | ||
| 512 | + args = yyjson_mut_obj(md); | ||
| 513 | + yyjson_mut_doc_set_root(md, args); | ||
| 514 | + | ||
| 515 | + for (i = 0; i < sizeof keys / sizeof keys[0]; i++) { | ||
| 516 | + yyjson_val *bv; | ||
| 517 | + char sval[512]; | ||
| 518 | + | ||
| 519 | + bv = body_root(); | ||
| 520 | + if (bv && yyjson_is_obj(bv)) { | ||
| 521 | + yyjson_val *v = yyjson_obj_get(bv, keys[i]); | ||
| 522 | + if (v && yyjson_is_str(v)) { | ||
| 523 | + yyjson_mut_obj_add_strcpy(md, args, keys[i], yyjson_get_str(v)); | ||
| 524 | + continue; | ||
| 525 | + } | ||
| 526 | + if (v && yyjson_is_int(v)) { | ||
| 527 | + yyjson_mut_obj_add_int(md, args, keys[i], | ||
| 528 | + (long long)yyjson_get_sint(v)); | ||
| 529 | + continue; | ||
| 530 | + } | ||
| 531 | + } | ||
| 532 | + if (qs && get_query_val(qs, keys[i], sval, sizeof sval) && sval[0]) { | ||
| 533 | + /* The integer fields are numeric arguments; a query string always | ||
| 534 | + * arrives as text, so re-type them here rather than making every | ||
| 535 | + * caller remember which surface wants which type. */ | ||
| 536 | + if (strstr(keys[i], "_ago")) { | ||
| 537 | + char *end = NULL; | ||
| 538 | + long long n = strtoll(sval, &end, 10); | ||
| 539 | + if (end && *end == '\0') | ||
| 540 | + yyjson_mut_obj_add_int(md, args, keys[i], n); | ||
| 541 | + else | ||
| 542 | + yyjson_mut_obj_add_strcpy(md, args, keys[i], sval); | ||
| 543 | + } else { | ||
| 544 | + /* strcpy, not str: `sval` is one stack buffer reused for every | ||
| 545 | + * key in this loop, so a borrowed pointer leaves every argument | ||
| 546 | + * pointing at the last value it held. Silent, and the JSON | ||
| 547 | + * still parses. */ | ||
| 548 | + yyjson_mut_obj_add_strcpy(md, args, keys[i], sval); | ||
| 549 | + } | ||
| 550 | + } | ||
| 551 | + } | ||
| 552 | + | ||
| 553 | + /* The tool implementations read an immutable yyjson_val; serialise the | ||
| 554 | + * mutable object and parse it back so both surfaces share one argument | ||
| 555 | + * shape (and so the HTTP path exercises the same reader as --mcp). */ | ||
| 556 | + { | ||
| 557 | + char *text = yyjson_mut_write(md, 0, NULL); | ||
| 558 | + yyjson_mut_doc_free(md); | ||
| 559 | + adoc = text ? yyjson_read(text, strlen(text), 0) : NULL; | ||
| 560 | + free(text); | ||
| 561 | + } | ||
| 562 | + av = adoc ? yyjson_doc_get_root(adoc) : NULL; | ||
| 563 | + | ||
| 564 | + if (strcmp(tool, "get_current_time") == 0) tool_get_current_time(av, &t); | ||
| 565 | + else if (strcmp(tool, "convert_time") == 0) tool_convert_time(av, &t); | ||
| 566 | + else if (strcmp(tool, "get_time_ago") == 0) tool_get_time_ago(av, &t); | ||
| 567 | + else { if (adoc) yyjson_doc_free(adoc); json_err(400, "unknown tool"); return; } | ||
| 568 | + | ||
| 569 | + res = tool_api_result(&t); | ||
| 570 | + json_raw(res ? res : "{\"ok\":false,\"error\":\"no result\"}"); | ||
| 571 | + free(res); | ||
| 572 | + tool_free(&t); | ||
| 573 | + if (adoc) yyjson_doc_free(adoc); | ||
| 574 | +} | ||
| 575 | + | ||
| 576 | +/* The page's zone picker: every embedded key plus the local one. */ | ||
| 577 | +static void api_zones(void) | ||
| 578 | +{ | ||
| 579 | + buf_t b; | ||
| 580 | + int i; | ||
| 581 | + | ||
| 582 | + buf_init(&b); | ||
| 583 | + buf_puts(&b, "{\"ok\":true,\"count\":"); | ||
| 584 | + buf_printf(&b, "%d", tz_count()); | ||
| 585 | + buf_puts(&b, ",\"local\":"); | ||
| 586 | + json_str(&b, tz_local_name()); | ||
| 587 | + buf_puts(&b, ",\"zones\":["); | ||
| 588 | + for (i = 0; i < tz_count(); i++) { | ||
| 589 | + if (i) buf_putc(&b, ','); | ||
| 590 | + json_str(&b, tz_name_at(i)); | ||
| 591 | + } | ||
| 592 | + buf_puts(&b, "]}"); | ||
| 593 | + json_raw(buf_str(&b)); | ||
| 594 | + buf_free(&b); | ||
| 595 | +} | ||
| 596 | + | ||
| 597 | +static int api_handle(void) | ||
| 598 | +{ | ||
| 599 | + const char *qs = getenv("QUERY_STRING"); | ||
| 600 | + const char *method = getenv("REQUEST_METHOD"); | ||
| 601 | + char action[64]; | ||
| 602 | + int is_get; | ||
| 603 | + | ||
| 604 | + if (!qs) qs = ""; | ||
| 605 | + is_get = !method || strcmp(method, "GET") == 0; | ||
| 606 | + | ||
| 607 | + require_node_auth(); | ||
| 608 | + | ||
| 609 | + if (!is_get && read_body() < 0) { | ||
| 610 | + json_err(413, "request body too large"); | ||
| 611 | + return 0; | ||
| 612 | + } | ||
| 613 | + | ||
| 614 | + /* Two separate buffers: parsing `format` into `action` would clobber the | ||
| 615 | + * action we just parsed and silently answer the default one. */ | ||
| 616 | + action[0] = '\0'; | ||
| 617 | + if (!get_query_val(qs, "action", action, sizeof action) || !action[0]) | ||
| 618 | + param_str(qs, "action", action, sizeof action); | ||
| 619 | + | ||
| 620 | + if (!action[0]) { | ||
| 621 | + if (strstr(qs, "format=json")) snprintf(action, sizeof action, "current"); | ||
| 622 | + else { mode_html(); return 0; } | ||
| 623 | + } | ||
| 624 | + | ||
| 625 | + if (strcmp(action, "current") == 0 || strcmp(action, "now") == 0) | ||
| 626 | + api_run("get_current_time", qs); | ||
| 627 | + else if (strcmp(action, "convert") == 0) | ||
| 628 | + api_run("convert_time", qs); | ||
| 629 | + else if (strcmp(action, "ago") == 0) | ||
| 630 | + api_run("get_time_ago", qs); | ||
| 631 | + else if (strcmp(action, "zones") == 0) | ||
| 632 | + api_zones(); | ||
| 633 | + else if (strcmp(action, "tools") == 0) { | ||
| 634 | + char *t = tools_list_json(); | ||
| 635 | + buf_t b; | ||
| 636 | + buf_init(&b); | ||
| 637 | + buf_puts(&b, "{\"ok\":true,\"tools\":"); | ||
| 638 | + buf_puts(&b, t ? t : "[]"); | ||
| 639 | + buf_puts(&b, "}"); | ||
| 640 | + json_raw(buf_str(&b)); | ||
| 641 | + buf_free(&b); | ||
| 642 | + free(t); | ||
| 643 | + } else if (strcmp(action, "info") == 0) { | ||
| 644 | + buf_t b; | ||
| 645 | + buf_init(&b); | ||
| 646 | + buf_puts(&b, "{\"ok\":true,\"id\":\"" PLUGIN_ID "\",\"version\":\"" | ||
| 647 | + PLUGIN_VER "\",\"kind\":\"mcp\",\"mcp\":true,\"zones\":"); | ||
| 648 | + buf_printf(&b, "%d", tz_count()); | ||
| 649 | + buf_puts(&b, ",\"local\":"); | ||
| 650 | + json_str(&b, tz_local_name()); | ||
| 651 | + buf_puts(&b, "}"); | ||
| 652 | + json_raw(buf_str(&b)); | ||
| 653 | + buf_free(&b); | ||
| 654 | + } else { | ||
| 655 | + json_err(400, "unknown action"); | ||
| 656 | + } | ||
| 657 | + if (g_body_doc) { yyjson_doc_free(g_body_doc); g_body_doc = NULL; } | ||
| 658 | + return 0; | ||
| 659 | +} | ||
| 660 | + | ||
| 661 | +/* ------------------------------------------------------------------ */ | ||
| 662 | +/* main */ | ||
| 663 | +/* ------------------------------------------------------------------ */ | ||
| 664 | + | ||
| 665 | +static void print_help(void) | ||
| 666 | +{ | ||
| 667 | + int i; | ||
| 668 | + printf("%s %s - SCORPIOX BOT world time MCP connector\n\n" | ||
| 669 | + "modes:\n" | ||
| 670 | + " --meta print plugin discovery metadata and exit\n" | ||
| 671 | + " --mcp serve MCP JSON-RPC 2.0 over stdin/stdout\n" | ||
| 672 | + " --schema print the tools/list array\n" | ||
| 673 | + " --version print version\n" | ||
| 674 | + " (no args) CGI: World Clock page + ?action= API\n\n" | ||
| 675 | + "tools: ", PLUGIN_ID, PLUGIN_VER); | ||
| 676 | + for (i = 0; i < TOOL_COUNT; i++) | ||
| 677 | + printf("%s%s", i ? ", " : "", TOOL_NAMES[i]); | ||
| 678 | + printf("\nzones: %d embedded IANA zones; local is %s\n", | ||
| 679 | + tz_count(), tz_local_name()); | ||
| 680 | +} | ||
| 681 | + | ||
| 682 | +int main(int argc, char **argv) | ||
| 683 | +{ | ||
| 684 | + int i; | ||
| 685 | + | ||
| 686 | + for (i = 1; i < argc; i++) { | ||
| 687 | + if (strcmp(argv[i], "--meta") == 0) return mode_meta(); | ||
| 688 | + if (strcmp(argv[i], "--mcp") == 0) return mode_mcp(); | ||
| 689 | + if (strcmp(argv[i], "--version") == 0) { | ||
| 690 | + printf("%s %s\n", PLUGIN_ID, PLUGIN_VER); | ||
| 691 | + return 0; | ||
| 692 | + } | ||
| 693 | + if (strcmp(argv[i], "--schema") == 0) { | ||
| 694 | + char *t = tools_list_json(); | ||
| 695 | + printf("%s\n", t ? t : "[]"); | ||
| 696 | + free(t); | ||
| 697 | + return 0; | ||
| 698 | + } | ||
| 699 | + if (strcmp(argv[i], "--help") == 0) { print_help(); return 0; } | ||
| 700 | + } | ||
| 701 | + if (getenv("SX_PLUGIN_META")) return mode_meta(); | ||
| 702 | + if (getenv("SX_PLUGIN_MCP")) return mode_mcp(); | ||
| 703 | + | ||
| 704 | + return api_handle(); | ||
| 705 | +} |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..0aba4dd | index 0000000..0aba4dd | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/src/plugintime.h | +++ b/src/plugintime.h | ||
| @@ -0,0 +1,112 @@ | @@ -0,0 +1,112 @@ | ||
| 1 | +/* | ||
| 2 | + * plugintime.h - shared declarations for plugin_time. | ||
| 3 | + * | ||
| 4 | + * NOT time.h: gcc searches -I directories for #include <...> as well, so a | ||
| 5 | + * local time.h silently shadows the C library's <time.h> and every struct tm | ||
| 6 | + * in the tree stops existing. | ||
| 7 | + * | ||
| 8 | + * plugin_time is a SCORPIOX BOT connector plugin that reimplements the | ||
| 9 | + * reference MCP "time" server in pure C99: IANA timezone queries and | ||
| 10 | + * wall-clock conversions, served as MCP tools over stdio and as a JSON API | ||
| 11 | + * behind a browser UI. | ||
| 12 | + * | ||
| 13 | + * plugin_time.c main(), --meta, --mcp loop, CGI dispatch, auth, HTML | ||
| 14 | + * timeutil.c the three tools, ISO formatting, tool schemas | ||
| 15 | + * tz.c the embedded IANA table: keys, offsets, wall resolution | ||
| 16 | + * | ||
| 17 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | ||
| 18 | + */ | ||
| 19 | +#ifndef SX_PLUGIN_TIME_H | ||
| 20 | +#define SX_PLUGIN_TIME_H | ||
| 21 | + | ||
| 22 | +#include <stddef.h> | ||
| 23 | +#include <stdint.h> | ||
| 24 | +#include "yyjson.h" | ||
| 25 | + | ||
| 26 | +#define PLUGIN_SLUG "time" | ||
| 27 | +#define PLUGIN_ID "plugin_time" | ||
| 28 | +#define PLUGIN_VER "1.0.0" | ||
| 29 | +#define PLUGIN_NAME "time" | ||
| 30 | +#define PLUGIN_DESC "World time MCP" | ||
| 31 | + | ||
| 32 | +#define MCP_PROTOCOL "2025-06-18" | ||
| 33 | +#define SERVER_NAME "time-server" | ||
| 34 | + | ||
| 35 | +#define TZ_NAME_MAX 96 | ||
| 36 | +#define STDIN_LINE_MAX (1024 * 1024) | ||
| 37 | +#define REQ_BODY_MAX (1024 * 1024) | ||
| 38 | + | ||
| 39 | +/* ------------------------------------------------------------------ */ | ||
| 40 | +/* Growable byte buffer (timeutil.c) */ | ||
| 41 | +/* ------------------------------------------------------------------ */ | ||
| 42 | + | ||
| 43 | +typedef struct { | ||
| 44 | + char *p; | ||
| 45 | + size_t n; | ||
| 46 | + size_t cap; | ||
| 47 | +} buf_t; | ||
| 48 | + | ||
| 49 | +void buf_init(buf_t *b); | ||
| 50 | +int buf_need(buf_t *b, size_t extra); | ||
| 51 | +void buf_add(buf_t *b, const char *s, size_t n); | ||
| 52 | +void buf_puts(buf_t *b, const char *s); | ||
| 53 | +void buf_putc(buf_t *b, int c); | ||
| 54 | +void buf_printf(buf_t *b, const char *fmt, ...); | ||
| 55 | +const char *buf_str(const buf_t *b); | ||
| 56 | +void buf_free(buf_t *b); | ||
| 57 | + | ||
| 58 | +/* JSON string escape into a buf_t (safe against control characters). */ | ||
| 59 | +void json_str(buf_t *b, const char *s); | ||
| 60 | + | ||
| 61 | +/* ------------------------------------------------------------------ */ | ||
| 62 | +/* Time formatting */ | ||
| 63 | +/* ------------------------------------------------------------------ */ | ||
| 64 | + | ||
| 65 | +/* Local wall time for a UTC instant in `offset` (east of UTC, seconds). */ | ||
| 66 | +void time_split(int64_t utc, int offset, int *year, int *month, int *day, | ||
| 67 | + int *hour, int *min, int *sec, int *weekday); | ||
| 68 | +/* ISO-8601 with seconds precision and a numeric offset, e.g. | ||
| 69 | + * 2026-11-14T13:21:10+13:00. `out` needs ~40 bytes. */ | ||
| 70 | +void time_iso(int64_t utc, int offset, char *out, size_t outsz); | ||
| 71 | +/* "+13:00", "-05:00", "+05:45", "+00:00". */ | ||
| 72 | +void time_offset_str(int offset, char *out, size_t outsz); | ||
| 73 | +/* "Friday" */ | ||
| 74 | +const char *time_weekday_name(int weekday); | ||
| 75 | +/* The current UTC instant; SX_TIME_NOW pins it for deterministic tests. */ | ||
| 76 | +int64_t time_now_utc(void); | ||
| 77 | + | ||
| 78 | +/* "+3.0h" / "+5.75h" / "-0.5h" - the reference server's format. */ | ||
| 79 | +void time_diff_str(int source_off, int target_off, char *out, size_t outsz); | ||
| 80 | + | ||
| 81 | +/* ------------------------------------------------------------------ */ | ||
| 82 | +/* Tool results */ | ||
| 83 | +/* ------------------------------------------------------------------ */ | ||
| 84 | + | ||
| 85 | +typedef struct { | ||
| 86 | + yyjson_mut_doc *md; /* owns root */ | ||
| 87 | + yyjson_mut_val *root; | ||
| 88 | + char *error; /* error text when is_error */ | ||
| 89 | + int is_error; | ||
| 90 | +} tool_out_t; | ||
| 91 | + | ||
| 92 | +void tool_out_init(tool_out_t *t); | ||
| 93 | +void tool_err(tool_out_t *t, const char *fmt, ...); | ||
| 94 | +void tool_free(tool_out_t *t); | ||
| 95 | + | ||
| 96 | +void tool_get_current_time(yyjson_val *args, tool_out_t *t); | ||
| 97 | +void tool_convert_time(yyjson_val *args, tool_out_t *t); | ||
| 98 | +void tool_get_time_ago(yyjson_val *args, tool_out_t *t); | ||
| 99 | + | ||
| 100 | +/* The full tools/list array as JSON text (caller frees). */ | ||
| 101 | +char *tools_list_json(void); | ||
| 102 | +int tool_dispatch(const char *name, yyjson_val *args, tool_out_t *out); | ||
| 103 | +/* MCP result: content[] + structuredContent + isError. Caller frees. */ | ||
| 104 | +char *tool_mcp_result(tool_out_t *t); | ||
| 105 | +/* HTTP API result: {"ok":true,...} or {"ok":false,"error":"..."}. Caller frees. */ | ||
| 106 | +char *tool_api_result(tool_out_t *t); | ||
| 107 | + | ||
| 108 | +/* The three tool names, for --help and the page's tool list. */ | ||
| 109 | +extern const char *const TOOL_NAMES[]; | ||
| 110 | +extern const int TOOL_COUNT; | ||
| 111 | + | ||
| 112 | +#endif /* SX_PLUGIN_TIME_H */ |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..bcbfcb9 | index 0000000..bcbfcb9 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/src/timeutil.c | +++ b/src/timeutil.c | ||
| @@ -0,0 +1,825 @@ | @@ -0,0 +1,825 @@ | ||
| 1 | +/* | ||
| 2 | + * timeutil.c - the tools of plugin_time and the formatting they share. | ||
| 3 | + * | ||
| 4 | + * Three tools, matching the reference `mcp_server_time` server: | ||
| 5 | + * | ||
| 6 | + * get_current_time -> {timezone, datetime, day_of_week, is_dst} | ||
| 7 | + * convert_time -> {source, target, time_difference, later_occurrence} | ||
| 8 | + * get_time_ago -> {timezone, datetime, day_of_week, is_dst} | ||
| 9 | + * | ||
| 10 | + * The reference server's semantics are reproduced, not approximated: | ||
| 11 | + * | ||
| 12 | + * - an omitted timezone defaults to the node's own zone, and the tool | ||
| 13 | + * descriptions name it so a model can default to it; | ||
| 14 | + * - an unknown key is a tool error whose text starts "Invalid timezone:"; | ||
| 15 | + * - `time` is HH:MM in 24-hour form, and anything else is | ||
| 16 | + * "Invalid time format. Expected HH:MM [24-hour format]"; | ||
| 17 | + * - a wall time skipped by a forward clock change is an error naming the | ||
| 18 | + * date, not a silently shifted answer; | ||
| 19 | + * - a wall time repeated by a backward clock change answers the FIRST | ||
| 20 | + * occurrence in source/target and reports the second under | ||
| 21 | + * `later_occurrence`, which is the behaviour of Python's fold=0/fold=1; | ||
| 22 | + * - `time_difference` is "+3.0h", and a fractional zone like Nepal's +05:45 | ||
| 23 | + * prints "+5.75h" (two decimals, trailing zeros stripped). | ||
| 24 | + * | ||
| 25 | + * Nothing in here reads the clock directly: now_utc() honours SX_TIME_NOW | ||
| 26 | + * (an ISO-8601 UTC instant) so the suite can pin a date and prove a DST edge | ||
| 27 | + * with the same answer on every platform. | ||
| 28 | + * | ||
| 29 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | ||
| 30 | + */ | ||
| 31 | +#if defined(_WIN32) && !defined(_WIN32_WINNT) | ||
| 32 | +# define _WIN32_WINNT 0x0601 | ||
| 33 | +#endif | ||
| 34 | + | ||
| 35 | +#if !defined(_WIN32) | ||
| 36 | +# define _POSIX_C_SOURCE 200809L | ||
| 37 | +# define _DEFAULT_SOURCE 1 | ||
| 38 | +#endif | ||
| 39 | + | ||
| 40 | +#include <stdio.h> | ||
| 41 | +#include <stdlib.h> | ||
| 42 | +#include <string.h> | ||
| 43 | +#include <stdarg.h> | ||
| 44 | +#include <ctype.h> | ||
| 45 | +#include <time.h> | ||
| 46 | + | ||
| 47 | +#include "plugintime.h" | ||
| 48 | +#include "tz.h" | ||
| 49 | + | ||
| 50 | +const char *const TOOL_NAMES[] = { | ||
| 51 | + "get_current_time", "convert_time", "get_time_ago" | ||
| 52 | +}; | ||
| 53 | +const int TOOL_COUNT = (int)(sizeof TOOL_NAMES / sizeof TOOL_NAMES[0]); | ||
| 54 | + | ||
| 55 | +/* ------------------------------------------------------------------ */ | ||
| 56 | +/* Growable buffer */ | ||
| 57 | +/* ------------------------------------------------------------------ */ | ||
| 58 | + | ||
| 59 | +void buf_init(buf_t *b) | ||
| 60 | +{ | ||
| 61 | + b->p = NULL; | ||
| 62 | + b->n = 0; | ||
| 63 | + b->cap = 0; | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +int buf_need(buf_t *b, size_t extra) | ||
| 67 | +{ | ||
| 68 | + size_t want = b->n + extra + 1; | ||
| 69 | + char *np; | ||
| 70 | + size_t cap; | ||
| 71 | + | ||
| 72 | + if (want <= b->cap) return 0; | ||
| 73 | + cap = b->cap ? b->cap : 256; | ||
| 74 | + while (cap < want) { | ||
| 75 | + if (cap > (size_t)-1 / 2) return -1; | ||
| 76 | + cap *= 2; | ||
| 77 | + } | ||
| 78 | + np = (char *)realloc(b->p, cap); | ||
| 79 | + if (!np) return -1; | ||
| 80 | + b->p = np; | ||
| 81 | + b->cap = cap; | ||
| 82 | + return 0; | ||
| 83 | +} | ||
| 84 | + | ||
| 85 | +void buf_add(buf_t *b, const char *s, size_t n) | ||
| 86 | +{ | ||
| 87 | + if (buf_need(b, n) != 0) return; | ||
| 88 | + if (n) memcpy(b->p + b->n, s, n); | ||
| 89 | + b->n += n; | ||
| 90 | + b->p[b->n] = '\0'; | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +void buf_puts(buf_t *b, const char *s) | ||
| 94 | +{ | ||
| 95 | + if (s) buf_add(b, s, strlen(s)); | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +void buf_putc(buf_t *b, int c) | ||
| 99 | +{ | ||
| 100 | + char ch = (char)c; | ||
| 101 | + buf_add(b, &ch, 1); | ||
| 102 | +} | ||
| 103 | + | ||
| 104 | +void buf_printf(buf_t *b, const char *fmt, ...) | ||
| 105 | +{ | ||
| 106 | + va_list ap; | ||
| 107 | + int need; | ||
| 108 | + char tmp[512]; | ||
| 109 | + | ||
| 110 | + va_start(ap, fmt); | ||
| 111 | + need = vsnprintf(tmp, sizeof tmp, fmt, ap); | ||
| 112 | + va_end(ap); | ||
| 113 | + if (need < 0) return; | ||
| 114 | + if ((size_t)need < sizeof tmp) { | ||
| 115 | + buf_add(b, tmp, (size_t)need); | ||
| 116 | + return; | ||
| 117 | + } | ||
| 118 | + if (buf_need(b, (size_t)need) != 0) return; | ||
| 119 | + va_start(ap, fmt); | ||
| 120 | + vsnprintf(b->p + b->n, (size_t)need + 1, fmt, ap); | ||
| 121 | + va_end(ap); | ||
| 122 | + b->n += (size_t)need; | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +const char *buf_str(const buf_t *b) | ||
| 126 | +{ | ||
| 127 | + return b->p ? b->p : ""; | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +void buf_free(buf_t *b) | ||
| 131 | +{ | ||
| 132 | + free(b->p); | ||
| 133 | + buf_init(b); | ||
| 134 | +} | ||
| 135 | + | ||
| 136 | +void json_str(buf_t *b, const char *s) | ||
| 137 | +{ | ||
| 138 | + buf_putc(b, '"'); | ||
| 139 | + if (s) { | ||
| 140 | + for (; *s; s++) { | ||
| 141 | + unsigned char c = (unsigned char)*s; | ||
| 142 | + switch (c) { | ||
| 143 | + case '"': buf_puts(b, "\\\""); break; | ||
| 144 | + case '\\': buf_puts(b, "\\\\"); break; | ||
| 145 | + case '\n': buf_puts(b, "\\n"); break; | ||
| 146 | + case '\r': buf_puts(b, "\\r"); break; | ||
| 147 | + case '\t': buf_puts(b, "\\t"); break; | ||
| 148 | + default: | ||
| 149 | + if (c < 0x20) buf_printf(b, "\\u%04x", c); | ||
| 150 | + else buf_putc(b, (char)c); | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + } | ||
| 154 | + buf_putc(b, '"'); | ||
| 155 | +} | ||
| 156 | + | ||
| 157 | +/* ------------------------------------------------------------------ */ | ||
| 158 | +/* Formatting */ | ||
| 159 | +/* ------------------------------------------------------------------ */ | ||
| 160 | + | ||
| 161 | +void time_split(int64_t utc, int offset, int *year, int *month, int *day, | ||
| 162 | + int *hour, int *min, int *sec, int *weekday) | ||
| 163 | +{ | ||
| 164 | + int64_t local = utc + offset; | ||
| 165 | + int64_t days = local / 86400; | ||
| 166 | + int64_t rem = local % 86400; | ||
| 167 | + int y, m, d; | ||
| 168 | + | ||
| 169 | + /* Floor division: a negative instant must round toward the past, not | ||
| 170 | + * toward zero, or a timestamp before 1970 gets the wrong date. */ | ||
| 171 | + if (rem < 0) { | ||
| 172 | + rem += 86400; | ||
| 173 | + days -= 1; | ||
| 174 | + } | ||
| 175 | + tz_civil_from_days(days, &y, &m, &d); | ||
| 176 | + if (year) *year = y; | ||
| 177 | + if (month) *month = m; | ||
| 178 | + if (day) *day = d; | ||
| 179 | + if (hour) *hour = (int)(rem / 3600); | ||
| 180 | + if (min) *min = (int)((rem % 3600) / 60); | ||
| 181 | + if (sec) *sec = (int)(rem % 60); | ||
| 182 | + if (weekday) *weekday = tz_weekday(days); | ||
| 183 | +} | ||
| 184 | + | ||
| 185 | +void time_offset_str(int offset, char *out, size_t outsz) | ||
| 186 | +{ | ||
| 187 | + int a = offset < 0 ? -offset : offset; | ||
| 188 | + char sign = offset < 0 ? '-' : '+'; | ||
| 189 | + | ||
| 190 | + snprintf(out, outsz, "%c%02d:%02d", sign, a / 3600, (a % 3600) / 60); | ||
| 191 | +} | ||
| 192 | + | ||
| 193 | +void time_iso(int64_t utc, int offset, char *out, size_t outsz) | ||
| 194 | +{ | ||
| 195 | + int y, m, d, hh, mm, ss; | ||
| 196 | + char off[16]; | ||
| 197 | + | ||
| 198 | + time_split(utc, offset, &y, &m, &d, &hh, &mm, &ss, NULL); | ||
| 199 | + time_offset_str(offset, off, sizeof off); | ||
| 200 | + snprintf(out, outsz, "%04d-%02d-%02dT%02d:%02d:%02d%s", y, m, d, hh, mm, ss, off); | ||
| 201 | +} | ||
| 202 | + | ||
| 203 | +static const char *const WEEKDAYS[7] = { | ||
| 204 | + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" | ||
| 205 | +}; | ||
| 206 | + | ||
| 207 | +const char *time_weekday_name(int weekday) | ||
| 208 | +{ | ||
| 209 | + if (weekday < 0 || weekday > 6) return WEEKDAYS[0]; | ||
| 210 | + return WEEKDAYS[weekday]; | ||
| 211 | +} | ||
| 212 | + | ||
| 213 | +void time_diff_str(int source_off, int target_off, char *out, size_t outsz) | ||
| 214 | +{ | ||
| 215 | + int diff = target_off - source_off; | ||
| 216 | + double hours = (double)diff / 3600.0; | ||
| 217 | + char tmp[32]; | ||
| 218 | + | ||
| 219 | + /* The reference prints one decimal when the difference is a whole number | ||
| 220 | + * of hours, otherwise two decimals with trailing zeros stripped. */ | ||
| 221 | + if (diff % 3600 == 0) { | ||
| 222 | + snprintf(out, outsz, "%+.1fh", hours); | ||
| 223 | + return; | ||
| 224 | + } | ||
| 225 | + snprintf(tmp, sizeof tmp, "%+.2f", hours); | ||
| 226 | + { | ||
| 227 | + size_t n = strlen(tmp); | ||
| 228 | + while (n > 0 && tmp[n - 1] == '0') tmp[--n] = '\0'; | ||
| 229 | + if (n > 0 && tmp[n - 1] == '.') tmp[--n] = '\0'; | ||
| 230 | + } | ||
| 231 | + snprintf(out, outsz, "%sh", tmp); | ||
| 232 | +} | ||
| 233 | + | ||
| 234 | +/* ------------------------------------------------------------------ */ | ||
| 235 | +/* "now" */ | ||
| 236 | +/* ------------------------------------------------------------------ */ | ||
| 237 | + | ||
| 238 | +static int parse_iso_utc(const char *s, int64_t *out) | ||
| 239 | +{ | ||
| 240 | + int y = 0, mo = 0, d = 0, hh = 0, mm = 0, ss = 0; | ||
| 241 | + int n = 0; | ||
| 242 | + | ||
| 243 | + if (!s || !*s) return -1; | ||
| 244 | + if (sscanf(s, "%4d-%2d-%2dT%2d:%2d:%2d%n", &y, &mo, &d, &hh, &mm, &ss, &n) < 6) | ||
| 245 | + return -1; | ||
| 246 | + if (s[n] && s[n] != 'Z' && s[n] != 'z' && s[n] != '+' && s[n] != '-') return -1; | ||
| 247 | + if (mo < 1 || mo > 12 || d < 1 || d > 31) return -1; | ||
| 248 | + if (hh < 0 || hh > 23 || mm < 0 || mm > 59 || ss < 0 || ss > 60) return -1; | ||
| 249 | + *out = tz_days_from_civil(y, mo, d) * 86400 + hh * 3600 + mm * 60 + ss; | ||
| 250 | + return 0; | ||
| 251 | +} | ||
| 252 | + | ||
| 253 | +/* The current UTC instant. SX_TIME_NOW ("2026-03-29T01:30:00Z") pins it so a | ||
| 254 | + * test can assert a DST edge without waiting for one; an unparseable value is | ||
| 255 | + * ignored rather than fatal, because a broken env var must not take the | ||
| 256 | + * connector down. */ | ||
| 257 | +int64_t time_now_utc(void) | ||
| 258 | +{ | ||
| 259 | + const char *env = getenv("SX_TIME_NOW"); | ||
| 260 | + int64_t pinned; | ||
| 261 | + | ||
| 262 | + if (env && *env && parse_iso_utc(env, &pinned) == 0) return pinned; | ||
| 263 | + return (int64_t)time(NULL); | ||
| 264 | +} | ||
| 265 | + | ||
| 266 | +/* ------------------------------------------------------------------ */ | ||
| 267 | +/* Result plumbing */ | ||
| 268 | +/* ------------------------------------------------------------------ */ | ||
| 269 | + | ||
| 270 | +void tool_out_init(tool_out_t *t) | ||
| 271 | +{ | ||
| 272 | + memset(t, 0, sizeof *t); | ||
| 273 | +} | ||
| 274 | + | ||
| 275 | +void tool_err(tool_out_t *t, const char *fmt, ...) | ||
| 276 | +{ | ||
| 277 | + char tmp[768]; | ||
| 278 | + va_list ap; | ||
| 279 | + | ||
| 280 | + va_start(ap, fmt); | ||
| 281 | + vsnprintf(tmp, sizeof tmp, fmt, ap); | ||
| 282 | + va_end(ap); | ||
| 283 | + if (!t->error) { | ||
| 284 | + size_t n = strlen(tmp); | ||
| 285 | + t->error = (char *)malloc(n + 1); | ||
| 286 | + if (t->error) memcpy(t->error, tmp, n + 1); | ||
| 287 | + } | ||
| 288 | + t->is_error = 1; | ||
| 289 | +} | ||
| 290 | + | ||
| 291 | +void tool_free(tool_out_t *t) | ||
| 292 | +{ | ||
| 293 | + if (!t) return; | ||
| 294 | + if (t->md) yyjson_mut_doc_free(t->md); | ||
| 295 | + free(t->error); | ||
| 296 | + memset(t, 0, sizeof *t); | ||
| 297 | +} | ||
| 298 | + | ||
| 299 | +static void tool_start(tool_out_t *t) | ||
| 300 | +{ | ||
| 301 | + if (t->md) return; | ||
| 302 | + t->md = yyjson_mut_doc_new(NULL); | ||
| 303 | + if (!t->md) { tool_err(t, "out of memory"); return; } | ||
| 304 | + t->root = yyjson_mut_obj(t->md); | ||
| 305 | + if (!t->root) { | ||
| 306 | + yyjson_mut_doc_free(t->md); | ||
| 307 | + t->md = NULL; | ||
| 308 | + tool_err(t, "out of memory"); | ||
| 309 | + return; | ||
| 310 | + } | ||
| 311 | + yyjson_mut_doc_set_root(t->md, t->root); | ||
| 312 | +} | ||
| 313 | + | ||
| 314 | +/* COPY, not reference: yyjson_mut_obj_add_str stores the caller's pointer, so a | ||
| 315 | + * stack buffer (every description and timezone name in this file) is reused and | ||
| 316 | + * the document serialises garbage. Silent, valid-looking JSON - pinned by the | ||
| 317 | + * "description is not the timezone text" assertion in tests/run.sh. */ | ||
| 318 | +static void add_str(yyjson_mut_doc *md, yyjson_mut_val *obj, | ||
| 319 | + const char *k, const char *v) | ||
| 320 | +{ | ||
| 321 | + yyjson_mut_obj_add_strcpy(md, obj, k, v ? v : ""); | ||
| 322 | +} | ||
| 323 | + | ||
| 324 | +static void add_bool(yyjson_mut_doc *md, yyjson_mut_val *obj, | ||
| 325 | + const char *k, int v) | ||
| 326 | +{ | ||
| 327 | + yyjson_mut_obj_add_bool(md, obj, k, v ? true : false); | ||
| 328 | +} | ||
| 329 | + | ||
| 330 | +/* Fill a TimeResult object. The two single-result tools fill the root object | ||
| 331 | + * directly; convert_time nests one per side, so they are the same four fields | ||
| 332 | + * everywhere and a caller never has to unwrap a "result" key. */ | ||
| 333 | +static void time_result_fill(yyjson_mut_doc *md, yyjson_mut_val *o, | ||
| 334 | + const tz_zone *z, int64_t utc, const char *name) | ||
| 335 | +{ | ||
| 336 | + int off, dst = 0; | ||
| 337 | + char iso[64], abbr[24]; | ||
| 338 | + | ||
| 339 | + off = tz_offset(z, utc, &dst, abbr, sizeof abbr); | ||
| 340 | + time_iso(utc, off, iso, sizeof iso); | ||
| 341 | + | ||
| 342 | + add_str(md, o, "timezone", name); | ||
| 343 | + add_str(md, o, "datetime", iso); | ||
| 344 | + { | ||
| 345 | + int wd = 0; | ||
| 346 | + time_split(utc, off, NULL, NULL, NULL, NULL, NULL, NULL, &wd); | ||
| 347 | + add_str(md, o, "day_of_week", time_weekday_name(wd)); | ||
| 348 | + } | ||
| 349 | + add_bool(md, o, "is_dst", dst); | ||
| 350 | + /* Two fields the reference does not carry: a caller reading the text | ||
| 351 | + * block should not have to re-derive the numeric offset or the zone | ||
| 352 | + * abbreviation, and neither changes the meaning of the standard four. */ | ||
| 353 | + { | ||
| 354 | + char offs[16]; | ||
| 355 | + time_offset_str(off, offs, sizeof offs); | ||
| 356 | + add_str(md, o, "utc_offset", offs); | ||
| 357 | + add_str(md, o, "abbreviation", abbr); | ||
| 358 | + } | ||
| 359 | +} | ||
| 360 | + | ||
| 361 | +static yyjson_mut_val *time_result_obj(yyjson_mut_doc *md, const tz_zone *z, | ||
| 362 | + int64_t utc, const char *name) | ||
| 363 | +{ | ||
| 364 | + yyjson_mut_val *o = yyjson_mut_obj(md); | ||
| 365 | + time_result_fill(md, o, z, utc, name); | ||
| 366 | + return o; | ||
| 367 | +} | ||
| 368 | + | ||
| 369 | +/* Resolve a timezone argument; fills `name` (the default when absent). */ | ||
| 370 | +static const tz_zone *zone_arg(yyjson_val *args, const char *key, | ||
| 371 | + char *name, size_t namesz, tool_out_t *t) | ||
| 372 | +{ | ||
| 373 | + yyjson_val *v = args ? yyjson_obj_get(args, key) : NULL; | ||
| 374 | + | ||
| 375 | + if (v && yyjson_is_str(v)) { | ||
| 376 | + const char *s = yyjson_get_str(v); | ||
| 377 | + /* A string whose declared length is longer than the C string means an | ||
| 378 | + * embedded NUL: refuse it rather than resolving a prefix. */ | ||
| 379 | + if (strlen(s) != yyjson_get_len(v)) { | ||
| 380 | + tool_err(t, "Invalid timezone: the name contains a NUL byte"); | ||
| 381 | + return NULL; | ||
| 382 | + } | ||
| 383 | + if (!*s) { | ||
| 384 | + tool_err(t, "Invalid timezone: the name is empty"); | ||
| 385 | + return NULL; | ||
| 386 | + } | ||
| 387 | + snprintf(name, namesz, "%s", s); | ||
| 388 | + } else if (v && !yyjson_is_null(v)) { | ||
| 389 | + tool_err(t, "%s must be a string (an IANA timezone name)", key); | ||
| 390 | + return NULL; | ||
| 391 | + } else { | ||
| 392 | + snprintf(name, namesz, "%s", tz_local_name()); | ||
| 393 | + } | ||
| 394 | + { | ||
| 395 | + const tz_zone *z = tz_find(name); | ||
| 396 | + if (!z) { | ||
| 397 | + tool_err(t, "Invalid timezone: No time zone found with key %s", name); | ||
| 398 | + return NULL; | ||
| 399 | + } | ||
| 400 | + return z; | ||
| 401 | + } | ||
| 402 | +} | ||
| 403 | + | ||
| 404 | +static int int_arg(yyjson_val *args, const char *key, int dflt, int lo, int hi, | ||
| 405 | + tool_out_t *t) | ||
| 406 | +{ | ||
| 407 | + yyjson_val *v = args ? yyjson_obj_get(args, key) : NULL; | ||
| 408 | + long long n; | ||
| 409 | + | ||
| 410 | + if (!v || yyjson_is_null(v)) return dflt; | ||
| 411 | + if (yyjson_is_int(v)) { | ||
| 412 | + n = (long long)yyjson_get_sint(v); | ||
| 413 | + } else if (yyjson_is_str(v)) { | ||
| 414 | + char *end = NULL; | ||
| 415 | + n = strtoll(yyjson_get_str(v), &end, 10); | ||
| 416 | + if (!end || *end) { | ||
| 417 | + tool_err(t, "%s must be an integer (whole number)", key); | ||
| 418 | + return dflt; | ||
| 419 | + } | ||
| 420 | + } else { | ||
| 421 | + tool_err(t, "%s must be an integer (whole number)", key); | ||
| 422 | + return dflt; | ||
| 423 | + } | ||
| 424 | + if (n < lo || n > hi) { | ||
| 425 | + tool_err(t, "%s must be between %d and %d", key, lo, hi); | ||
| 426 | + return dflt; | ||
| 427 | + } | ||
| 428 | + return (int)n; | ||
| 429 | +} | ||
| 430 | + | ||
| 431 | +/* ------------------------------------------------------------------ */ | ||
| 432 | +/* get_current_time */ | ||
| 433 | +/* ------------------------------------------------------------------ */ | ||
| 434 | + | ||
| 435 | +void tool_get_current_time(yyjson_val *args, tool_out_t *t) | ||
| 436 | +{ | ||
| 437 | + char name[TZ_NAME_MAX + 1]; | ||
| 438 | + const tz_zone *z; | ||
| 439 | + int64_t now = time_now_utc(); | ||
| 440 | + | ||
| 441 | + tool_out_init(t); | ||
| 442 | + z = zone_arg(args, "timezone", name, sizeof name, t); | ||
| 443 | + if (!z) return; | ||
| 444 | + | ||
| 445 | + tool_start(t); | ||
| 446 | + if (t->is_error) return; | ||
| 447 | + time_result_fill(t->md, t->root, z, now, name); | ||
| 448 | +} | ||
| 449 | + | ||
| 450 | +/* ------------------------------------------------------------------ */ | ||
| 451 | +/* get_time_ago */ | ||
| 452 | +/* ------------------------------------------------------------------ */ | ||
| 453 | + | ||
| 454 | +void tool_get_time_ago(yyjson_val *args, tool_out_t *t) | ||
| 455 | +{ | ||
| 456 | + char name[TZ_NAME_MAX + 1]; | ||
| 457 | + const tz_zone *z; | ||
| 458 | + int d, h, m, s; | ||
| 459 | + int64_t span, past; | ||
| 460 | + | ||
| 461 | + tool_out_init(t); | ||
| 462 | + z = zone_arg(args, "timezone", name, sizeof name, t); | ||
| 463 | + if (!z) return; | ||
| 464 | + | ||
| 465 | + d = int_arg(args, "days_ago", 0, 0, 365000, t); | ||
| 466 | + if (t->is_error) return; | ||
| 467 | + h = int_arg(args, "hours_ago", 0, 0, 100000000, t); | ||
| 468 | + if (t->is_error) return; | ||
| 469 | + m = int_arg(args, "minutes_ago", 0, 0, 100000000, t); | ||
| 470 | + if (t->is_error) return; | ||
| 471 | + s = int_arg(args, "seconds_ago", 0, 0, 100000000, t); | ||
| 472 | + if (t->is_error) return; | ||
| 473 | + | ||
| 474 | + /* The span is subtracted from the UTC instant, not the local wall clock: | ||
| 475 | + * "5 hours ago" across a clock change is five real hours back. */ | ||
| 476 | + span = (int64_t)d * 86400 + (int64_t)h * 3600 + (int64_t)m * 60 + (int64_t)s; | ||
| 477 | + past = time_now_utc() - span; | ||
| 478 | + | ||
| 479 | + tool_start(t); | ||
| 480 | + if (t->is_error) return; | ||
| 481 | + time_result_fill(t->md, t->root, z, past, name); | ||
| 482 | +} | ||
| 483 | + | ||
| 484 | +/* ------------------------------------------------------------------ */ | ||
| 485 | +/* convert_time */ | ||
| 486 | +/* ------------------------------------------------------------------ */ | ||
| 487 | + | ||
| 488 | +/* "HH:MM" -> seconds after midnight. Rejects anything else, including a | ||
| 489 | + * trailing colon, a 24th hour or a 60th minute. */ | ||
| 490 | +static int parse_hhmm(const char *s, int *out) | ||
| 491 | +{ | ||
| 492 | + /* Exactly HH:MM. sscanf("%2d:%2d") would also accept "9:00" and " 9:00", | ||
| 493 | + * and the reference server rejects both, so the shape is checked by hand | ||
| 494 | + * before a number is read. */ | ||
| 495 | + if (!s || strlen(s) != 5) return -1; | ||
| 496 | + if (!isdigit((unsigned char)s[0]) || !isdigit((unsigned char)s[1])) return -1; | ||
| 497 | + if (s[2] != ':') return -1; | ||
| 498 | + if (!isdigit((unsigned char)s[3]) || !isdigit((unsigned char)s[4])) return -1; | ||
| 499 | + { | ||
| 500 | + int h = (s[0] - '0') * 10 + (s[1] - '0'); | ||
| 501 | + int m = (s[3] - '0') * 10 + (s[4] - '0'); | ||
| 502 | + if (h > 23 || m > 59) return -1; | ||
| 503 | + *out = h * 3600 + m * 60; | ||
| 504 | + } | ||
| 505 | + return 0; | ||
| 506 | +} | ||
| 507 | + | ||
| 508 | +void tool_convert_time(yyjson_val *args, tool_out_t *t) | ||
| 509 | +{ | ||
| 510 | + char src_name[TZ_NAME_MAX + 1]; | ||
| 511 | + char dst_name[TZ_NAME_MAX + 1]; | ||
| 512 | + const tz_zone *zs, *zd; | ||
| 513 | + yyjson_val *tv; | ||
| 514 | + const char *time_str = NULL; | ||
| 515 | + int mins; | ||
| 516 | + int64_t now, today, wall; | ||
| 517 | + tz_cand_t cand[TZ_MAX_CANDIDATES]; | ||
| 518 | + int ncand; | ||
| 519 | + int i; | ||
| 520 | + int off_s, off_t; | ||
| 521 | + char diff[32]; | ||
| 522 | + int so, mo, dd; | ||
| 523 | + | ||
| 524 | + tool_out_init(t); | ||
| 525 | + | ||
| 526 | + tv = args ? yyjson_obj_get(args, "time") : NULL; | ||
| 527 | + if (!tv || !yyjson_is_str(tv)) { | ||
| 528 | + tool_err(t, "Missing required argument: time"); | ||
| 529 | + return; | ||
| 530 | + } | ||
| 531 | + time_str = yyjson_get_str(tv); | ||
| 532 | + if (strlen(time_str) != yyjson_get_len(tv)) { | ||
| 533 | + tool_err(t, "Invalid time format. Expected HH:MM [24-hour format]"); | ||
| 534 | + return; | ||
| 535 | + } | ||
| 536 | + if (parse_hhmm(time_str, &mins) != 0) { | ||
| 537 | + tool_err(t, "Invalid time format. Expected HH:MM [24-hour format]"); | ||
| 538 | + return; | ||
| 539 | + } | ||
| 540 | + | ||
| 541 | + zs = zone_arg(args, "source_timezone", src_name, sizeof src_name, t); | ||
| 542 | + if (!zs) return; | ||
| 543 | + zd = zone_arg(args, "target_timezone", dst_name, sizeof dst_name, t); | ||
| 544 | + if (!zd) return; | ||
| 545 | + | ||
| 546 | + /* "Today" in the source zone, at the requested wall time. */ | ||
| 547 | + now = time_now_utc(); | ||
| 548 | + off_s = tz_offset(zs, now, NULL, NULL, 0); | ||
| 549 | + time_split(now, off_s, NULL, NULL, NULL, NULL, NULL, NULL, NULL); | ||
| 550 | + { | ||
| 551 | + int y, m, d; | ||
| 552 | + time_split(now, off_s, &y, &m, &d, NULL, NULL, NULL, NULL); | ||
| 553 | + today = tz_days_from_civil(y, m, d); | ||
| 554 | + so = y; mo = m; dd = d; | ||
| 555 | + } | ||
| 556 | + wall = today * 86400 + mins; | ||
| 557 | + | ||
| 558 | + ncand = tz_resolve_wall(zs, wall, cand, TZ_MAX_CANDIDATES); | ||
| 559 | + if (ncand == 0) { | ||
| 560 | + char date[32]; | ||
| 561 | + time_split(wall, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL); | ||
| 562 | + snprintf(date, sizeof date, "%04d-%02d-%02d", so, mo, dd); | ||
| 563 | + tool_err(t, "Invalid time: %s does not exist in %s on %s " | ||
| 564 | + "(skipped by a clock change)", time_str, src_name, date); | ||
| 565 | + return; | ||
| 566 | + } | ||
| 567 | + | ||
| 568 | + tool_start(t); | ||
| 569 | + if (t->is_error) return; | ||
| 570 | + | ||
| 571 | + { | ||
| 572 | + yyjson_mut_val *src_obj = time_result_obj(t->md, zs, cand[0].utc, src_name); | ||
| 573 | + yyjson_mut_val *dst_obj = time_result_obj(t->md, zd, cand[0].utc, dst_name); | ||
| 574 | + | ||
| 575 | + off_s = tz_offset(zs, cand[0].utc, NULL, NULL, 0); | ||
| 576 | + off_t = tz_offset(zd, cand[0].utc, NULL, NULL, 0); | ||
| 577 | + time_diff_str(off_s, off_t, diff, sizeof diff); | ||
| 578 | + | ||
| 579 | + yyjson_mut_obj_add_val(t->md, t->root, "source", src_obj); | ||
| 580 | + yyjson_mut_obj_add_val(t->md, t->root, "target", dst_obj); | ||
| 581 | + add_str(t->md, t->root, "time_difference", diff); | ||
| 582 | + | ||
| 583 | + /* A repeated hour has two instants. The reference reports both rather | ||
| 584 | + * than presenting the first as the only answer. */ | ||
| 585 | + if (ncand > 1) { | ||
| 586 | + yyjson_mut_val *later = yyjson_mut_obj(t->md); | ||
| 587 | + yyjson_mut_val *lsrc, *ldst; | ||
| 588 | + char note[512]; | ||
| 589 | + char src_iso[64], later_iso[64]; | ||
| 590 | + char d2[32]; | ||
| 591 | + int o_s; | ||
| 592 | + | ||
| 593 | + o_s = tz_offset(zs, cand[0].utc, NULL, NULL, 0); | ||
| 594 | + time_iso(cand[0].utc, o_s, src_iso, sizeof src_iso); | ||
| 595 | + o_s = tz_offset(zs, cand[1].utc, NULL, NULL, 0); | ||
| 596 | + time_iso(cand[1].utc, o_s, later_iso, sizeof later_iso); | ||
| 597 | + | ||
| 598 | + snprintf(note, sizeof note, | ||
| 599 | + "%s occurs twice in %s on %04d-%02d-%02d because a clock " | ||
| 600 | + "change repeats it: first at %s, then at %s. source and " | ||
| 601 | + "target are the first occurrence; this is the second.", | ||
| 602 | + time_str, src_name, so, mo, dd, src_iso, later_iso); | ||
| 603 | + add_str(t->md, later, "note", note); | ||
| 604 | + | ||
| 605 | + lsrc = time_result_obj(t->md, zs, cand[1].utc, src_name); | ||
| 606 | + ldst = time_result_obj(t->md, zd, cand[1].utc, dst_name); | ||
| 607 | + off_s = tz_offset(zs, cand[1].utc, NULL, NULL, 0); | ||
| 608 | + off_t = tz_offset(zd, cand[1].utc, NULL, NULL, 0); | ||
| 609 | + time_diff_str(off_s, off_t, d2, sizeof d2); | ||
| 610 | + | ||
| 611 | + yyjson_mut_obj_add_val(t->md, later, "source", lsrc); | ||
| 612 | + yyjson_mut_obj_add_val(t->md, later, "target", ldst); | ||
| 613 | + add_str(t->md, later, "time_difference", d2); | ||
| 614 | + yyjson_mut_obj_add_val(t->md, t->root, "later_occurrence", later); | ||
| 615 | + } else { | ||
| 616 | + yyjson_mut_obj_add_null(t->md, t->root, "later_occurrence"); | ||
| 617 | + } | ||
| 618 | + } | ||
| 619 | + (void)i; | ||
| 620 | + (void)off_s; | ||
| 621 | + (void)off_t; | ||
| 622 | +} | ||
| 623 | + | ||
| 624 | +/* ------------------------------------------------------------------ */ | ||
| 625 | +/* Schemas and dispatch */ | ||
| 626 | +/* ------------------------------------------------------------------ */ | ||
| 627 | + | ||
| 628 | +char *tools_list_json(void) | ||
| 629 | +{ | ||
| 630 | + buf_t b; | ||
| 631 | + const char *local = tz_local_name(); | ||
| 632 | + yyjson_mut_doc *md; | ||
| 633 | + yyjson_mut_val *arr, *tool, *schema, *props, *req, *item; | ||
| 634 | + | ||
| 635 | + buf_init(&b); | ||
| 636 | + md = yyjson_mut_doc_new(NULL); | ||
| 637 | + if (!md) return NULL; | ||
| 638 | + arr = yyjson_mut_arr(md); | ||
| 639 | + yyjson_mut_doc_set_root(md, arr); | ||
| 640 | + | ||
| 641 | + /* get_current_time */ | ||
| 642 | + tool = yyjson_mut_obj(md); | ||
| 643 | + add_str(md, tool, "name", "get_current_time"); | ||
| 644 | + { | ||
| 645 | + char desc[512]; | ||
| 646 | + snprintf(desc, sizeof desc, | ||
| 647 | + "Get current time in a specific timezone"); | ||
| 648 | + add_str(md, tool, "description", desc); | ||
| 649 | + } | ||
| 650 | + schema = yyjson_mut_obj(md); | ||
| 651 | + add_str(md, schema, "type", "object"); | ||
| 652 | + props = yyjson_mut_obj(md); | ||
| 653 | + item = yyjson_mut_obj(md); | ||
| 654 | + { | ||
| 655 | + char td[512]; | ||
| 656 | + add_str(md, item, "type", "string"); | ||
| 657 | + snprintf(td, sizeof td, "IANA timezone name (e.g., 'America/New_York', " | ||
| 658 | + "'Europe/London'). Optional: defaults to the server's local " | ||
| 659 | + "timezone, '%s', when omitted.", local); | ||
| 660 | + add_str(md, item, "description", td); | ||
| 661 | + add_str(md, item, "default", local); | ||
| 662 | + } | ||
| 663 | + yyjson_mut_obj_add_val(md, props, "timezone", item); | ||
| 664 | + yyjson_mut_obj_add_val(md, schema, "properties", props); | ||
| 665 | + req = yyjson_mut_arr(md); | ||
| 666 | + yyjson_mut_obj_add_val(md, tool, "inputSchema", schema); | ||
| 667 | + yyjson_mut_obj_add_bool(md, tool, "readOnlyHint", true); | ||
| 668 | + yyjson_mut_arr_append(arr, tool); | ||
| 669 | + (void)req; | ||
| 670 | + | ||
| 671 | + /* convert_time */ | ||
| 672 | + tool = yyjson_mut_obj(md); | ||
| 673 | + add_str(md, tool, "name", "convert_time"); | ||
| 674 | + add_str(md, tool, "description", "Convert time between timezones"); | ||
| 675 | + schema = yyjson_mut_obj(md); | ||
| 676 | + add_str(md, schema, "type", "object"); | ||
| 677 | + props = yyjson_mut_obj(md); | ||
| 678 | + { | ||
| 679 | + char td[512]; | ||
| 680 | + item = yyjson_mut_obj(md); | ||
| 681 | + add_str(md, item, "type", "string"); | ||
| 682 | + snprintf(td, sizeof td, "Source IANA timezone name (e.g., " | ||
| 683 | + "'America/New_York', 'Europe/London'). Optional: defaults to " | ||
| 684 | + "the server's local timezone, '%s', when omitted.", local); | ||
| 685 | + add_str(md, item, "description", td); | ||
| 686 | + add_str(md, item, "default", local); | ||
| 687 | + yyjson_mut_obj_add_val(md, props, "source_timezone", item); | ||
| 688 | + | ||
| 689 | + item = yyjson_mut_obj(md); | ||
| 690 | + add_str(md, item, "type", "string"); | ||
| 691 | + add_str(md, item, "description", "Time to convert in 24-hour format (HH:MM)"); | ||
| 692 | + yyjson_mut_obj_add_val(md, props, "time", item); | ||
| 693 | + | ||
| 694 | + item = yyjson_mut_obj(md); | ||
| 695 | + add_str(md, item, "type", "string"); | ||
| 696 | + snprintf(td, sizeof td, "Target IANA timezone name (e.g., 'Asia/Tokyo', " | ||
| 697 | + "'America/Los_Angeles'). Optional: defaults to the server's " | ||
| 698 | + "local timezone, '%s', when omitted.", local); | ||
| 699 | + add_str(md, item, "description", td); | ||
| 700 | + add_str(md, item, "default", local); | ||
| 701 | + yyjson_mut_obj_add_val(md, props, "target_timezone", item); | ||
| 702 | + } | ||
| 703 | + yyjson_mut_obj_add_val(md, schema, "properties", props); | ||
| 704 | + req = yyjson_mut_arr(md); | ||
| 705 | + yyjson_mut_arr_add_str(md, req, "time"); | ||
| 706 | + yyjson_mut_obj_add_val(md, schema, "required", req); | ||
| 707 | + yyjson_mut_obj_add_val(md, tool, "inputSchema", schema); | ||
| 708 | + yyjson_mut_obj_add_bool(md, tool, "readOnlyHint", true); | ||
| 709 | + yyjson_mut_arr_append(arr, tool); | ||
| 710 | + | ||
| 711 | + /* get_time_ago */ | ||
| 712 | + tool = yyjson_mut_obj(md); | ||
| 713 | + add_str(md, tool, "name", "get_time_ago"); | ||
| 714 | + add_str(md, tool, "description", | ||
| 715 | + "Get the time a given number of days, hours, minutes and seconds " | ||
| 716 | + "ago in a specific timezone"); | ||
| 717 | + schema = yyjson_mut_obj(md); | ||
| 718 | + add_str(md, schema, "type", "object"); | ||
| 719 | + props = yyjson_mut_obj(md); | ||
| 720 | + { | ||
| 721 | + char td[512]; | ||
| 722 | + item = yyjson_mut_obj(md); | ||
| 723 | + add_str(md, item, "type", "string"); | ||
| 724 | + snprintf(td, sizeof td, "IANA timezone name (e.g., 'America/New_York', " | ||
| 725 | + "'Europe/London'). Optional: defaults to the server's local " | ||
| 726 | + "timezone, '%s', when omitted.", local); | ||
| 727 | + add_str(md, item, "description", td); | ||
| 728 | + add_str(md, item, "default", local); | ||
| 729 | + yyjson_mut_obj_add_val(md, props, "timezone", item); | ||
| 730 | + | ||
| 731 | + item = yyjson_mut_obj(md); | ||
| 732 | + add_str(md, item, "type", "integer"); | ||
| 733 | + add_str(md, item, "description", "Whole days before now"); | ||
| 734 | + yyjson_mut_obj_add_int(md, item, "minimum", 0); | ||
| 735 | + yyjson_mut_obj_add_val(md, props, "days_ago", item); | ||
| 736 | + | ||
| 737 | + item = yyjson_mut_obj(md); | ||
| 738 | + add_str(md, item, "type", "integer"); | ||
| 739 | + add_str(md, item, "description", "Whole hours before now"); | ||
| 740 | + yyjson_mut_obj_add_int(md, item, "minimum", 0); | ||
| 741 | + yyjson_mut_obj_add_val(md, props, "hours_ago", item); | ||
| 742 | + | ||
| 743 | + item = yyjson_mut_obj(md); | ||
| 744 | + add_str(md, item, "type", "integer"); | ||
| 745 | + add_str(md, item, "description", "Whole minutes before now"); | ||
| 746 | + yyjson_mut_obj_add_int(md, item, "minimum", 0); | ||
| 747 | + yyjson_mut_obj_add_val(md, props, "minutes_ago", item); | ||
| 748 | + | ||
| 749 | + item = yyjson_mut_obj(md); | ||
| 750 | + add_str(md, item, "type", "integer"); | ||
| 751 | + add_str(md, item, "description", "Whole seconds before now"); | ||
| 752 | + yyjson_mut_obj_add_int(md, item, "minimum", 0); | ||
| 753 | + yyjson_mut_obj_add_val(md, props, "seconds_ago", item); | ||
| 754 | + } | ||
| 755 | + yyjson_mut_obj_add_val(md, schema, "properties", props); | ||
| 756 | + yyjson_mut_obj_add_val(md, tool, "inputSchema", schema); | ||
| 757 | + yyjson_mut_obj_add_bool(md, tool, "readOnlyHint", true); | ||
| 758 | + yyjson_mut_arr_append(arr, tool); | ||
| 759 | + | ||
| 760 | + { | ||
| 761 | + char *text = yyjson_mut_write(md, 0, NULL); | ||
| 762 | + yyjson_mut_doc_free(md); | ||
| 763 | + return text; | ||
| 764 | + } | ||
| 765 | +} | ||
| 766 | + | ||
| 767 | +int tool_dispatch(const char *name, yyjson_val *args, tool_out_t *out) | ||
| 768 | +{ | ||
| 769 | + if (!name) return -1; | ||
| 770 | + if (strcmp(name, "get_current_time") == 0) { tool_get_current_time(args, out); return 0; } | ||
| 771 | + if (strcmp(name, "convert_time") == 0) { tool_convert_time(args, out); return 0; } | ||
| 772 | + if (strcmp(name, "get_time_ago") == 0) { tool_get_time_ago(args, out); return 0; } | ||
| 773 | + return -1; | ||
| 774 | +} | ||
| 775 | + | ||
| 776 | +char *tool_mcp_result(tool_out_t *t) | ||
| 777 | +{ | ||
| 778 | + buf_t b; | ||
| 779 | + | ||
| 780 | + buf_init(&b); | ||
| 781 | + if (t->is_error) { | ||
| 782 | + buf_puts(&b, "{\"content\":[{\"type\":\"text\",\"text\":"); | ||
| 783 | + json_str(&b, t->error ? t->error : "error"); | ||
| 784 | + buf_puts(&b, "}],\"isError\":true}"); | ||
| 785 | + return b.p; | ||
| 786 | + } | ||
| 787 | + { | ||
| 788 | + char *payload = t->md ? yyjson_mut_write(t->md, 0, NULL) : NULL; | ||
| 789 | + buf_puts(&b, "{\"content\":[{\"type\":\"text\",\"text\":"); | ||
| 790 | + json_str(&b, payload ? payload : "{}"); | ||
| 791 | + buf_puts(&b, "}],\"isError\":false"); | ||
| 792 | + if (payload) { | ||
| 793 | + buf_puts(&b, ",\"structuredContent\":"); | ||
| 794 | + buf_puts(&b, payload); | ||
| 795 | + } | ||
| 796 | + buf_puts(&b, "}"); | ||
| 797 | + free(payload); | ||
| 798 | + } | ||
| 799 | + return b.p; | ||
| 800 | +} | ||
| 801 | + | ||
| 802 | +char *tool_api_result(tool_out_t *t) | ||
| 803 | +{ | ||
| 804 | + buf_t b; | ||
| 805 | + | ||
| 806 | + buf_init(&b); | ||
| 807 | + if (t->is_error) { | ||
| 808 | + buf_puts(&b, "{\"ok\":false,\"error\":"); | ||
| 809 | + json_str(&b, t->error ? t->error : "error"); | ||
| 810 | + buf_puts(&b, "}"); | ||
| 811 | + return b.p; | ||
| 812 | + } | ||
| 813 | + { | ||
| 814 | + char *payload = t->md ? yyjson_mut_write(t->md, 0, NULL) : NULL; | ||
| 815 | + buf_puts(&b, "{\"ok\":true"); | ||
| 816 | + if (payload && payload[0] == '{' && payload[1] != '}') { | ||
| 817 | + buf_putc(&b, ','); | ||
| 818 | + buf_puts(&b, payload + 1); | ||
| 819 | + } else { | ||
| 820 | + buf_putc(&b, '}'); | ||
| 821 | + } | ||
| 822 | + free(payload); | ||
| 823 | + } | ||
| 824 | + return b.p; | ||
| 825 | +} |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..2d507c4 | index 0000000..2d507c4 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/src/tz.c | +++ b/src/tz.c | ||
| @@ -0,0 +1,738 @@ | @@ -0,0 +1,738 @@ | ||
| 1 | +/* | ||
| 2 | + * tz.c - IANA timezone resolution over the embedded table (src/tzdata.h). | ||
| 3 | + * | ||
| 4 | + * The design mirrors what the OS does, in three steps: | ||
| 5 | + * | ||
| 6 | + * 1. Look the zone up in the exact-key table. Never a filesystem path, so | ||
| 7 | + * `europe/warsaw` is refused on every platform instead of only on the | ||
| 8 | + * case-sensitive ones. | ||
| 9 | + * 2. Answer a UTC instant from the embedded transition list, falling back | ||
| 10 | + * to the zone's own POSIX TZ footer string beyond the last embedded | ||
| 11 | + * transition - exactly what zoneinfo itself does after it runs out of | ||
| 12 | + * the compiled table. | ||
| 13 | + * 3. For a wall-clock instant the caller supplies (the `convert_time` | ||
| 14 | + * tool), find the UTC instants that map to it. A forward clock change | ||
| 15 | + * makes a wall time that does not exist (0 candidates); a backward | ||
| 16 | + * change repeats an hour (2 candidates). Guessing one silently is how a | ||
| 17 | + * converter reports a time that never happened. | ||
| 18 | + * | ||
| 19 | + * Nothing here reads the clock: every function is a pure function of its | ||
| 20 | + * arguments, so a test can pin a date in 2035 and get the same answer on | ||
| 21 | + * Linux, macOS and Windows. | ||
| 22 | + * | ||
| 23 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | ||
| 24 | + */ | ||
| 25 | +#if defined(_WIN32) && !defined(_WIN32_WINNT) | ||
| 26 | +# define _WIN32_WINNT 0x0601 | ||
| 27 | +#endif | ||
| 28 | + | ||
| 29 | +#if !defined(_WIN32) | ||
| 30 | +# define _POSIX_C_SOURCE 200809L | ||
| 31 | +# define _DEFAULT_SOURCE 1 | ||
| 32 | +# if defined(__APPLE__) | ||
| 33 | +# define _DARWIN_C_SOURCE 1 | ||
| 34 | +# endif | ||
| 35 | +#endif | ||
| 36 | + | ||
| 37 | +#include <stdio.h> | ||
| 38 | +#include <stdlib.h> | ||
| 39 | +#include <string.h> | ||
| 40 | +#include <time.h> | ||
| 41 | +#include <ctype.h> | ||
| 42 | + | ||
| 43 | +#if defined(_WIN32) | ||
| 44 | +# define WIN32_LEAN_AND_MEAN | ||
| 45 | +# include <windows.h> | ||
| 46 | +#else | ||
| 47 | +# include <unistd.h> | ||
| 48 | +# include <limits.h> | ||
| 49 | +#endif | ||
| 50 | + | ||
| 51 | +#include "tz.h" | ||
| 52 | +#include "tzdata.h" | ||
| 53 | + | ||
| 54 | +/* ------------------------------------------------------------------ */ | ||
| 55 | +/* The zone record */ | ||
| 56 | +/* */ | ||
| 57 | +/* tz_zone is the generated struct plus the name pointer, so callers */ | ||
| 58 | +/* never index the string pool themselves. */ | ||
| 59 | +/* ------------------------------------------------------------------ */ | ||
| 60 | + | ||
| 61 | +struct tz_zone { | ||
| 62 | + const char *name; | ||
| 63 | + const tz_zone_t *rec; | ||
| 64 | + int index; | ||
| 65 | +}; | ||
| 66 | + | ||
| 67 | +static tz_zone *g_zones = NULL; | ||
| 68 | +static int g_nzones = 0; | ||
| 69 | + | ||
| 70 | +static void zones_build(void) | ||
| 71 | +{ | ||
| 72 | + int i; | ||
| 73 | + if (g_zones) return; | ||
| 74 | + g_zones = (tz_zone *)calloc((size_t)TZ_ZONE_COUNT, sizeof *g_zones); | ||
| 75 | + if (!g_zones) return; | ||
| 76 | + for (i = 0; i < TZ_ZONE_COUNT; i++) { | ||
| 77 | + g_zones[i].name = TZ_NAME_POOL + TZ_ZONES[i].name; | ||
| 78 | + g_zones[i].rec = &TZ_ZONES[i]; | ||
| 79 | + g_zones[i].index = i; | ||
| 80 | + } | ||
| 81 | + g_nzones = TZ_ZONE_COUNT; | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +int tz_count(void) | ||
| 85 | +{ | ||
| 86 | + zones_build(); | ||
| 87 | + return g_nzones; | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +const char *tz_name_at(int index) | ||
| 91 | +{ | ||
| 92 | + zones_build(); | ||
| 93 | + if (index < 0 || index >= g_nzones) return NULL; | ||
| 94 | + return g_zones[index].name; | ||
| 95 | +} | ||
| 96 | + | ||
| 97 | +const tz_zone *tz_find(const char *name) | ||
| 98 | +{ | ||
| 99 | + int lo, hi; | ||
| 100 | + | ||
| 101 | + if (!name || !*name) return NULL; | ||
| 102 | + /* An IANA key can never contain these: refused before any search so a | ||
| 103 | + * path, a traversal or a `..` cannot be mistaken for a zone. */ | ||
| 104 | + if (strstr(name, "..") || strchr(name, '\\') || strchr(name, '\n') || | ||
| 105 | + strchr(name, '\r') || strchr(name, '%')) | ||
| 106 | + return NULL; | ||
| 107 | + if (name[0] == '/' || name[0] == '.' || name[0] == ' ') return NULL; | ||
| 108 | + if (strlen(name) > 96) return NULL; | ||
| 109 | + | ||
| 110 | + zones_build(); | ||
| 111 | + if (!g_zones) return NULL; | ||
| 112 | + | ||
| 113 | + /* The generated table is sorted by key, so this is a plain binary search | ||
| 114 | + * with a case-sensitive compare - the whole point of the exact match. */ | ||
| 115 | + lo = 0; | ||
| 116 | + hi = g_nzones - 1; | ||
| 117 | + while (lo <= hi) { | ||
| 118 | + int mid = lo + (hi - lo) / 2; | ||
| 119 | + int cmp = strcmp(name, g_zones[mid].name); | ||
| 120 | + if (cmp == 0) return &g_zones[mid]; | ||
| 121 | + if (cmp < 0) hi = mid - 1; | ||
| 122 | + else lo = mid + 1; | ||
| 123 | + } | ||
| 124 | + return NULL; | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +/* ------------------------------------------------------------------ */ | ||
| 128 | +/* Civil date arithmetic (proleptic Gregorian, days since 1970-01-01) */ | ||
| 129 | +/* ------------------------------------------------------------------ */ | ||
| 130 | + | ||
| 131 | +int64_t tz_days_from_civil(int y, int m, int d) | ||
| 132 | +{ | ||
| 133 | + int64_t era, yoe, doy, doe; | ||
| 134 | + | ||
| 135 | + y -= (m <= 2); | ||
| 136 | + era = (y >= 0 ? y : y - 399) / 400; | ||
| 137 | + yoe = y - era * 400; /* 0..399 */ | ||
| 138 | + doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; /* 0..365 */ | ||
| 139 | + doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; /* 0..146096 */ | ||
| 140 | + return era * 146097 + doe - 719468; | ||
| 141 | +} | ||
| 142 | + | ||
| 143 | +void tz_civil_from_days(int64_t days, int *y, int *m, int *d) | ||
| 144 | +{ | ||
| 145 | + int64_t z, era, doe, yoe, doy, mp; | ||
| 146 | + int yy, mm, dd; | ||
| 147 | + | ||
| 148 | + z = days + 719468; | ||
| 149 | + era = (z >= 0 ? z : z - 146096) / 146097; | ||
| 150 | + doe = z - era * 146097; | ||
| 151 | + yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; | ||
| 152 | + yy = (int)(yoe + era * 400); | ||
| 153 | + doy = doe - (365 * yoe + yoe / 4 - yoe / 100); | ||
| 154 | + mp = (5 * doy + 2) / 153; | ||
| 155 | + dd = (int)(doy - (153 * mp + 2) / 5 + 1); | ||
| 156 | + mm = (int)(mp + (mp < 10 ? 3 : -9)); | ||
| 157 | + if (y) *y = yy + (mm <= 2); | ||
| 158 | + if (m) *m = mm; | ||
| 159 | + if (d) *d = dd; | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +int tz_weekday(int64_t days) | ||
| 163 | +{ | ||
| 164 | + /* 1970-01-01 was a Thursday (4). */ | ||
| 165 | + int64_t w = (days + 4) % 7; | ||
| 166 | + if (w < 0) w += 7; | ||
| 167 | + return (int)w; | ||
| 168 | +} | ||
| 169 | + | ||
| 170 | +/* ------------------------------------------------------------------ */ | ||
| 171 | +/* POSIX TZ footer strings */ | ||
| 172 | +/* */ | ||
| 173 | +/* Beyond the last embedded transition the tz database defers to the */ | ||
| 174 | +/* zone's own rule string, e.g. */ | ||
| 175 | +/* NZST-12NZDT,M9.5.0,M4.1.0/3 (Auckland) */ | ||
| 176 | +/* EST5EDT,M3.2.0,M11.1.0 (New York) */ | ||
| 177 | +/* <-02>2<-01>,M3.5.0/-1,M10.5.0/0 (Godthab) */ | ||
| 178 | +/* <+00>0<+01>,0/0,J365/25 (Casablanca, Ramadan) */ | ||
| 179 | +/* */ | ||
| 180 | +/* Offsets in the string are WEST positive, so the east-of-UTC offset we */ | ||
| 181 | +/* store everywhere else is the negation. */ | ||
| 182 | +/* ------------------------------------------------------------------ */ | ||
| 183 | + | ||
| 184 | +typedef struct { | ||
| 185 | + int kind; /* 0 = Jn, 1 = n (zero-based), 2 = Mm.w.d */ | ||
| 186 | + int n; /* Jn / n value, or the day-of-week for rules kind 2 */ | ||
| 187 | + int week; /* kind 2: 1..5 (5 = last) */ | ||
| 188 | + int month; /* kind 2 */ | ||
| 189 | + int time; /* seconds after local midnight, may be <0 or >86400 */ | ||
| 190 | +} rule_date; | ||
| 191 | + | ||
| 192 | +typedef struct { | ||
| 193 | + int ok; | ||
| 194 | + int std_off; /* east of UTC */ | ||
| 195 | + int dst_off; /* east of UTC */ | ||
| 196 | + int has_dst; | ||
| 197 | + rule_date start, end; | ||
| 198 | +} posix_tz; | ||
| 199 | + | ||
| 200 | +static const char *pz_name(const char *s, char *out, size_t outsz) | ||
| 201 | +{ | ||
| 202 | + size_t n = 0; | ||
| 203 | + if (*s == '<') { | ||
| 204 | + s++; | ||
| 205 | + while (*s && *s != '>') { | ||
| 206 | + if (n + 1 < outsz) out[n++] = *s; | ||
| 207 | + s++; | ||
| 208 | + } | ||
| 209 | + if (*s == '>') s++; | ||
| 210 | + } else { | ||
| 211 | + while (*s && isalpha((unsigned char)*s)) { | ||
| 212 | + if (n + 1 < outsz) out[n++] = *s; | ||
| 213 | + s++; | ||
| 214 | + } | ||
| 215 | + } | ||
| 216 | + out[n] = '\0'; | ||
| 217 | + return s; | ||
| 218 | +} | ||
| 219 | + | ||
| 220 | +static const char *pz_offset(const char *s, int *out, int *found) | ||
| 221 | +{ | ||
| 222 | + int sign = 1, h = 0, m = 0, sec = 0; | ||
| 223 | + *found = 0; | ||
| 224 | + if (*s == '+' || *s == '-') { | ||
| 225 | + if (*s == '-') sign = -1; | ||
| 226 | + s++; | ||
| 227 | + } | ||
| 228 | + if (!isdigit((unsigned char)*s) && *s != ':') return s; | ||
| 229 | + while (isdigit((unsigned char)*s)) { h = h * 10 + (*s - '0'); s++; } | ||
| 230 | + if (*s == ':') { | ||
| 231 | + s++; | ||
| 232 | + while (isdigit((unsigned char)*s)) { m = m * 10 + (*s - '0'); s++; } | ||
| 233 | + if (*s == ':') { | ||
| 234 | + s++; | ||
| 235 | + while (isdigit((unsigned char)*s)) { sec = sec * 10 + (*s - '0'); s++; } | ||
| 236 | + } | ||
| 237 | + } | ||
| 238 | + *out = sign * (h * 3600 + m * 60 + sec); | ||
| 239 | + *found = 1; | ||
| 240 | + return s; | ||
| 241 | +} | ||
| 242 | + | ||
| 243 | +static const char *pz_time_of_day(const char *s, int *out) | ||
| 244 | +{ | ||
| 245 | + int sign = 1, h = 0, m = 0, sec = 0; | ||
| 246 | + if (*s == '+' || *s == '-') { | ||
| 247 | + if (*s == '-') sign = -1; | ||
| 248 | + s++; | ||
| 249 | + } | ||
| 250 | + while (isdigit((unsigned char)*s)) { h = h * 10 + (*s - '0'); s++; } | ||
| 251 | + if (*s == ':') { | ||
| 252 | + s++; | ||
| 253 | + while (isdigit((unsigned char)*s)) { m = m * 10 + (*s - '0'); s++; } | ||
| 254 | + if (*s == ':') { | ||
| 255 | + s++; | ||
| 256 | + while (isdigit((unsigned char)*s)) { sec = sec * 10 + (*s - '0'); s++; } | ||
| 257 | + } | ||
| 258 | + } | ||
| 259 | + *out = sign * (h * 3600 + m * 60 + sec); | ||
| 260 | + return s; | ||
| 261 | +} | ||
| 262 | + | ||
| 263 | +static const char *pz_rule(const char *s, rule_date *r) | ||
| 264 | +{ | ||
| 265 | + memset(r, 0, sizeof *r); | ||
| 266 | + r->time = 2 * 3600; /* POSIX default: 02:00 local */ | ||
| 267 | + if (*s == 'M') { | ||
| 268 | + int m = 0, w = 0, d = 0; | ||
| 269 | + s++; | ||
| 270 | + while (isdigit((unsigned char)*s)) { m = m * 10 + (*s - '0'); s++; } | ||
| 271 | + if (*s == '.') s++; | ||
| 272 | + while (isdigit((unsigned char)*s)) { w = w * 10 + (*s - '0'); s++; } | ||
| 273 | + if (*s == '.') s++; | ||
| 274 | + while (isdigit((unsigned char)*s)) { d = d * 10 + (*s - '0'); s++; } | ||
| 275 | + r->kind = 2; r->month = m; r->week = w; r->n = d; | ||
| 276 | + } else if (*s == 'J') { | ||
| 277 | + int n = 0; | ||
| 278 | + s++; | ||
| 279 | + while (isdigit((unsigned char)*s)) { n = n * 10 + (*s - '0'); s++; } | ||
| 280 | + r->kind = 0; r->n = n; | ||
| 281 | + } else { | ||
| 282 | + int n = 0; | ||
| 283 | + while (isdigit((unsigned char)*s)) { n = n * 10 + (*s - '0'); s++; } | ||
| 284 | + r->kind = 1; r->n = n; | ||
| 285 | + } | ||
| 286 | + if (*s == '/') s = pz_time_of_day(s + 1, &r->time); | ||
| 287 | + return s; | ||
| 288 | +} | ||
| 289 | + | ||
| 290 | +static int posix_parse(const char *s, posix_tz *p) | ||
| 291 | +{ | ||
| 292 | + int found = 0; | ||
| 293 | + char nm[24]; | ||
| 294 | + | ||
| 295 | + memset(p, 0, sizeof *p); | ||
| 296 | + if (!s || !*s || *s == '\n') return -1; | ||
| 297 | + | ||
| 298 | + s = pz_name(s, nm, sizeof nm); | ||
| 299 | + if (!nm[0]) return -1; | ||
| 300 | + s = pz_offset(s, &p->std_off, &found); | ||
| 301 | + if (!found) return -1; | ||
| 302 | + p->std_off = -p->std_off; /* west -> east */ | ||
| 303 | + | ||
| 304 | + if (*s && *s != ',' && *s != '\n') { | ||
| 305 | + s = pz_name(s, nm, sizeof nm); | ||
| 306 | + if (!nm[0]) return -1; | ||
| 307 | + p->has_dst = 1; | ||
| 308 | + if (*s == '+' || *s == '-' || isdigit((unsigned char)*s)) { | ||
| 309 | + int v = 0; | ||
| 310 | + s = pz_offset(s, &v, &found); | ||
| 311 | + if (!found) return -1; | ||
| 312 | + p->dst_off = -v; | ||
| 313 | + } else { | ||
| 314 | + p->dst_off = p->std_off + 3600; /* POSIX default +1h */ | ||
| 315 | + } | ||
| 316 | + } | ||
| 317 | + if (p->has_dst) { | ||
| 318 | + if (*s != ',') return -1; /* a DST zone without rules | ||
| 319 | + * is not in the shipped | ||
| 320 | + * database; refuse rather | ||
| 321 | + * than guess */ | ||
| 322 | + s = pz_rule(s + 1, &p->start); | ||
| 323 | + if (*s != ',') return -1; | ||
| 324 | + s = pz_rule(s + 1, &p->end); | ||
| 325 | + } | ||
| 326 | + p->ok = 1; | ||
| 327 | + return 0; | ||
| 328 | +} | ||
| 329 | + | ||
| 330 | +/* The date a rule names in `year`, as days since the epoch. */ | ||
| 331 | +static int64_t rule_day(const rule_date *r, int year) | ||
| 332 | +{ | ||
| 333 | + int64_t jan1 = tz_days_from_civil(year, 1, 1); | ||
| 334 | + | ||
| 335 | + if (r->kind == 0) { /* Jn: 1..365, no Feb 29 */ | ||
| 336 | + int leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); | ||
| 337 | + int doy = r->n - 1; | ||
| 338 | + if (leap && r->n >= 60) doy += 1; | ||
| 339 | + return jan1 + doy; | ||
| 340 | + } | ||
| 341 | + if (r->kind == 1) { /* n: 0..365, Feb 29 counts */ | ||
| 342 | + return jan1 + r->n; | ||
| 343 | + } | ||
| 344 | + { | ||
| 345 | + /* Mm.w.d: day d (0=Sunday) of week w of month m; w=5 means last. */ | ||
| 346 | + int64_t first = tz_days_from_civil(year, r->month, 1); | ||
| 347 | + int wd = tz_weekday(first); | ||
| 348 | + int delta = (r->n - wd + 7) % 7; | ||
| 349 | + int64_t day = first + delta + (int64_t)(r->week - 1) * 7; | ||
| 350 | + int y2, m2, d2; | ||
| 351 | + tz_civil_from_days(day, &y2, &m2, &d2); | ||
| 352 | + while (m2 != r->month) { /* week 5 spilled over */ | ||
| 353 | + day -= 7; | ||
| 354 | + tz_civil_from_days(day, &y2, &m2, &d2); | ||
| 355 | + } | ||
| 356 | + return day; | ||
| 357 | + } | ||
| 358 | +} | ||
| 359 | + | ||
| 360 | +/* The UTC instant `year`'s rule fires at, given the offset in force before it. */ | ||
| 361 | +static int64_t rule_utc(const rule_date *r, int year, int before_east) | ||
| 362 | +{ | ||
| 363 | + return rule_day(r, year) * 86400 + r->time - before_east; | ||
| 364 | +} | ||
| 365 | + | ||
| 366 | +static int posix_is_dst(const posix_tz *p, int64_t utc) | ||
| 367 | +{ | ||
| 368 | + /* The rule names two instants per year; DST is in force after a start | ||
| 369 | + * event and before the next end event. Sorting the events of the | ||
| 370 | + * surrounding years and taking the last one at or before `utc` handles a | ||
| 371 | + * northern rule, a southern rule (start after end within a year) and a | ||
| 372 | + * rule whose changeovers sit at 24:00 or -01:00 without a special case | ||
| 373 | + * for any of them. The earlier "does the window contain utc" form | ||
| 374 | + * answered DST forever after a southern zone's start date, because | ||
| 375 | + * "utc >= start of last season" stays true for the rest of the year. */ | ||
| 376 | + struct { int64_t t; int on; } ev[9]; | ||
| 377 | + int n = 0, i, j, k; | ||
| 378 | + int year, month, day; | ||
| 379 | + | ||
| 380 | + if (!p->has_dst) return 0; | ||
| 381 | + tz_civil_from_days((utc + p->std_off) / 86400, &year, &month, &day); | ||
| 382 | + | ||
| 383 | + for (k = -1; k <= 1; k++) { | ||
| 384 | + int y = year + k; | ||
| 385 | + if (n < 8) { ev[n].t = rule_utc(&p->start, y, p->std_off); ev[n++].on = 1; } | ||
| 386 | + if (n < 8) { ev[n].t = rule_utc(&p->end, y, p->dst_off); ev[n++].on = 0; } | ||
| 387 | + } | ||
| 388 | + for (i = 1; i < n; i++) { /* insertion sort, n <= 9 */ | ||
| 389 | + for (j = i; j > 0 && ev[j - 1].t > ev[j].t; j--) { | ||
| 390 | + int64_t t = ev[j].t; | ||
| 391 | + int on = ev[j].on; | ||
| 392 | + ev[j].t = ev[j - 1].t; | ||
| 393 | + ev[j].on = ev[j - 1].on; | ||
| 394 | + ev[j - 1].t = t; | ||
| 395 | + ev[j - 1].on = on; | ||
| 396 | + } | ||
| 397 | + } | ||
| 398 | + for (i = n - 1; i >= 0; i--) if (ev[i].t <= utc) return ev[i].on; | ||
| 399 | + return 0; | ||
| 400 | +} | ||
| 401 | +/* ------------------------------------------------------------------ */ | ||
| 402 | +/* Offset lookup */ | ||
| 403 | +/* ------------------------------------------------------------------ */ | ||
| 404 | + | ||
| 405 | +static int zone_abbr(const tz_zone *z, uint16_t off, char *abbr, size_t sz) | ||
| 406 | +{ | ||
| 407 | + const char *a = TZ_ABBR_POOL + off; | ||
| 408 | + if (abbr && sz) { | ||
| 409 | + size_t n = strlen(a); | ||
| 410 | + if (n >= sz) n = sz - 1; | ||
| 411 | + memcpy(abbr, a, n); | ||
| 412 | + abbr[n] = '\0'; | ||
| 413 | + } | ||
| 414 | + (void)z; | ||
| 415 | + return 0; | ||
| 416 | +} | ||
| 417 | + | ||
| 418 | +int tz_offset(const tz_zone *z, int64_t utc, int *is_dst, | ||
| 419 | + char *abbr, size_t abbrsz) | ||
| 420 | +{ | ||
| 421 | + const tz_zone_t *r; | ||
| 422 | + int64_t rel; | ||
| 423 | + int lo, hi, hit; | ||
| 424 | + posix_tz pz; | ||
| 425 | + | ||
| 426 | + if (!z) return 0; | ||
| 427 | + r = z->rec; | ||
| 428 | + | ||
| 429 | + if (utc < TZ_WINDOW_EPOCH) { | ||
| 430 | + if (is_dst) *is_dst = r->base_dst; | ||
| 431 | + zone_abbr(z, r->base_abbr, abbr, abbrsz); | ||
| 432 | + return r->base_off; | ||
| 433 | + } | ||
| 434 | + rel = utc - TZ_WINDOW_EPOCH; | ||
| 435 | + | ||
| 436 | + /* Last transition at or before `rel` inside this zone's slice. */ | ||
| 437 | + lo = 0; | ||
| 438 | + hi = (int)r->count - 1; | ||
| 439 | + hit = -1; | ||
| 440 | + while (lo <= hi) { | ||
| 441 | + int mid = lo + (hi - lo) / 2; | ||
| 442 | + uint32_t t = TZ_TR_T[r->first + (uint32_t)mid]; | ||
| 443 | + if ((int64_t)t <= rel) { hit = mid; lo = mid + 1; } | ||
| 444 | + else { hi = mid - 1; } | ||
| 445 | + } | ||
| 446 | + if (hit >= 0) { | ||
| 447 | + uint32_t i = r->first + (uint32_t)hit; | ||
| 448 | + /* A query PAST this zone's last embedded transition must go to the | ||
| 449 | + * POSIX rule below, not to the last row: the last row is whatever | ||
| 450 | + * state the tz database stopped generating transitions in, and for | ||
| 451 | + * most zones that is standard time in 2037. Returning it made every | ||
| 452 | + * zone answer "no DST" from 2038 on - valid JSON, wrong answer, | ||
| 453 | + * forever. */ | ||
| 454 | + int past_last = (r->count > 0 && | ||
| 455 | + (int64_t)TZ_TR_T[r->first + r->count - 1] < rel); | ||
| 456 | + if (!(r->has_rule && past_last)) { | ||
| 457 | + if (is_dst) *is_dst = TZ_TR_DST[i] ? 1 : 0; | ||
| 458 | + zone_abbr(z, TZ_TR_ABBR[i], abbr, abbrsz); | ||
| 459 | + return TZ_TR_OFF[i]; | ||
| 460 | + } | ||
| 461 | + } | ||
| 462 | + | ||
| 463 | + /* Before this zone's first embedded transition but after the window | ||
| 464 | + * start: the base state still holds. */ | ||
| 465 | + if (r->count == 0 || rel < (int64_t)TZ_TR_T[r->first]) { | ||
| 466 | + /* Only fall through to the POSIX rule when we are past the zone's | ||
| 467 | + * last transition (a zone whose slice is empty uses base forever). */ | ||
| 468 | + if (r->count == 0) { | ||
| 469 | + if (is_dst) *is_dst = r->base_dst; | ||
| 470 | + zone_abbr(z, r->base_abbr, abbr, abbrsz); | ||
| 471 | + return r->base_off; | ||
| 472 | + } | ||
| 473 | + } | ||
| 474 | + | ||
| 475 | + /* Past the last embedded transition: the zone's own POSIX rule decides, | ||
| 476 | + * which is exactly how a host with less tzdata behaves. */ | ||
| 477 | + if (r->has_rule && posix_parse(TZ_RULE_POOL + r->rule, &pz) == 0) { | ||
| 478 | + int dst = posix_is_dst(&pz, utc); | ||
| 479 | + if (is_dst) *is_dst = dst; | ||
| 480 | + if (abbr && abbrsz) { | ||
| 481 | + /* Reuse the abbreviation of the most recent embedded transition | ||
| 482 | + * that had the same DST state; that is the name the zone uses. */ | ||
| 483 | + int i; | ||
| 484 | + const char *best = NULL; | ||
| 485 | + for (i = (int)r->count - 1; i >= 0; i--) { | ||
| 486 | + uint32_t j = r->first + (uint32_t)i; | ||
| 487 | + if ((TZ_TR_DST[j] ? 1 : 0) == dst) { | ||
| 488 | + best = TZ_ABBR_POOL + TZ_TR_ABBR[j]; | ||
| 489 | + break; | ||
| 490 | + } | ||
| 491 | + } | ||
| 492 | + if (!best) best = TZ_ABBR_POOL + r->base_abbr; | ||
| 493 | + { | ||
| 494 | + size_t n = strlen(best); | ||
| 495 | + if (n >= abbrsz) n = abbrsz - 1; | ||
| 496 | + memcpy(abbr, best, n); | ||
| 497 | + abbr[n] = '\0'; | ||
| 498 | + } | ||
| 499 | + } | ||
| 500 | + return dst ? pz.dst_off : pz.std_off; | ||
| 501 | + } | ||
| 502 | + | ||
| 503 | + if (is_dst) *is_dst = r->base_dst; | ||
| 504 | + zone_abbr(z, r->base_abbr, abbr, abbrsz); | ||
| 505 | + return r->base_off; | ||
| 506 | +} | ||
| 507 | + | ||
| 508 | +/* ------------------------------------------------------------------ */ | ||
| 509 | +/* Wall clock -> UTC */ | ||
| 510 | +/* */ | ||
| 511 | +/* Every distinct offset in force anywhere near `wall` is a candidate; */ | ||
| 512 | +/* an offset is valid only if applying it lands on an instant whose */ | ||
| 513 | +/* offset is that same value. That test is what distinguishes the two */ | ||
| 514 | +/* readings of a repeated hour from a time that never happened. */ | ||
| 515 | +/* ------------------------------------------------------------------ */ | ||
| 516 | + | ||
| 517 | +static int cand_push(tz_cand_t *out, int n, int max, | ||
| 518 | + int64_t utc, int off, int dst) | ||
| 519 | +{ | ||
| 520 | + int i; | ||
| 521 | + for (i = 0; i < n; i++) if (out[i].utc == utc) return n; | ||
| 522 | + if (n >= max) return n; | ||
| 523 | + out[n].utc = utc; | ||
| 524 | + out[n].offset = off; | ||
| 525 | + out[n].is_dst = dst; | ||
| 526 | + return n + 1; | ||
| 527 | +} | ||
| 528 | + | ||
| 529 | +static int cand_cmp(const void *a, const void *b) | ||
| 530 | +{ | ||
| 531 | + int64_t x = ((const tz_cand_t *)a)->utc; | ||
| 532 | + int64_t y = ((const tz_cand_t *)b)->utc; | ||
| 533 | + return (x < y) ? -1 : (x > y) ? 1 : 0; | ||
| 534 | +} | ||
| 535 | + | ||
| 536 | +int tz_resolve_wall(const tz_zone *z, int64_t wall, tz_cand_t *out, int outmax) | ||
| 537 | +{ | ||
| 538 | + const tz_zone_t *r; | ||
| 539 | + int offs[TZ_MAX_CANDIDATES * 4]; | ||
| 540 | + int noffs = 0; | ||
| 541 | + int n = 0, i; | ||
| 542 | + int64_t probes[7]; | ||
| 543 | + int p; | ||
| 544 | + | ||
| 545 | + if (!z || !out || outmax <= 0) return 0; | ||
| 546 | + r = z->rec; | ||
| 547 | + | ||
| 548 | + /* Offsets in force just before and just after the wall time, plus every | ||
| 549 | + * transition within a two-day margin of it (a shift can be more than an | ||
| 550 | + * hour, and a rule can move the change across midnight). */ | ||
| 551 | + probes[0] = wall - 3 * 86400; | ||
| 552 | + probes[1] = wall - 86400; | ||
| 553 | + probes[2] = wall - 3600; | ||
| 554 | + probes[3] = wall; | ||
| 555 | + probes[4] = wall + 3600; | ||
| 556 | + probes[5] = wall + 86400; | ||
| 557 | + probes[6] = wall + 3 * 86400; | ||
| 558 | + for (p = 0; p < 7; p++) { | ||
| 559 | + int dst = 0; | ||
| 560 | + int o = tz_offset(z, probes[p], &dst, NULL, 0); | ||
| 561 | + int dup = 0; | ||
| 562 | + for (i = 0; i < noffs; i++) if (offs[i] == o) { dup = 1; break; } | ||
| 563 | + if (!dup && noffs < (int)(sizeof offs / sizeof offs[0])) offs[noffs++] = o; | ||
| 564 | + } | ||
| 565 | + { | ||
| 566 | + int64_t lo = wall - 3 * 86400, hi = wall + 3 * 86400; | ||
| 567 | + int64_t rel_lo = lo - TZ_WINDOW_EPOCH, rel_hi = hi - TZ_WINDOW_EPOCH; | ||
| 568 | + int j; | ||
| 569 | + for (j = 0; j < (int)r->count; j++) { | ||
| 570 | + uint32_t k = r->first + (uint32_t)j; | ||
| 571 | + int64_t t = (int64_t)TZ_TR_T[k]; | ||
| 572 | + if (t < rel_lo) continue; | ||
| 573 | + if (t > rel_hi) break; | ||
| 574 | + { | ||
| 575 | + int o = TZ_TR_OFF[k]; | ||
| 576 | + int dup = 0; | ||
| 577 | + for (i = 0; i < noffs; i++) if (offs[i] == o) { dup = 1; break; } | ||
| 578 | + if (!dup && noffs < (int)(sizeof offs / sizeof offs[0])) | ||
| 579 | + offs[noffs++] = o; | ||
| 580 | + } | ||
| 581 | + } | ||
| 582 | + } | ||
| 583 | + | ||
| 584 | + for (i = 0; i < noffs; i++) { | ||
| 585 | + int64_t utc = wall - offs[i]; | ||
| 586 | + int dst = 0; | ||
| 587 | + int o = tz_offset(z, utc, &dst, NULL, 0); | ||
| 588 | + if (o == offs[i]) n = cand_push(out, n, outmax, utc, o, dst); | ||
| 589 | + } | ||
| 590 | + qsort(out, (size_t)n, sizeof *out, cand_cmp); | ||
| 591 | + return n; | ||
| 592 | +} | ||
| 593 | + | ||
| 594 | +/* ------------------------------------------------------------------ */ | ||
| 595 | +/* The node's own zone */ | ||
| 596 | +/* ------------------------------------------------------------------ */ | ||
| 597 | + | ||
| 598 | +static char g_local_name[128]; | ||
| 599 | +static int g_local_done = 0; | ||
| 600 | + | ||
| 601 | +static int name_ok(const char *s) | ||
| 602 | +{ | ||
| 603 | + return s && *s && tz_find(s) != NULL; | ||
| 604 | +} | ||
| 605 | + | ||
| 606 | +#if !defined(_WIN32) | ||
| 607 | +/* /etc/localtime is a symlink into the zoneinfo tree on most Linux hosts and | ||
| 608 | + * on macOS; the key is the tail of the link target after "zoneinfo/". */ | ||
| 609 | +static int name_from_localtime_link(void) | ||
| 610 | +{ | ||
| 611 | + static const char *paths[2] = { "/etc/localtime", | ||
| 612 | + "/var/db/timezone/localtime" }; | ||
| 613 | + int i; | ||
| 614 | + for (i = 0; i < 2; i++) { | ||
| 615 | + char buf[PATH_MAX]; | ||
| 616 | + ssize_t n = readlink(paths[i], buf, sizeof buf - 1); | ||
| 617 | + const char *p; | ||
| 618 | + if (n <= 0) continue; | ||
| 619 | + buf[n] = '\0'; | ||
| 620 | + p = strstr(buf, "zoneinfo/"); | ||
| 621 | + if (p && name_ok(p + 9)) { | ||
| 622 | + snprintf(g_local_name, sizeof g_local_name, "%s", p + 9); | ||
| 623 | + return 1; | ||
| 624 | + } | ||
| 625 | + p = strstr(buf, "zoneinfo.default/"); | ||
| 626 | + if (p && name_ok(p + 16)) { | ||
| 627 | + snprintf(g_local_name, sizeof g_local_name, "%s", p + 16); | ||
| 628 | + return 1; | ||
| 629 | + } | ||
| 630 | + } | ||
| 631 | + return 0; | ||
| 632 | +} | ||
| 633 | + | ||
| 634 | +/* Debian/Ubuntu keep the key in /etc/timezone as plain text. */ | ||
| 635 | +static int name_from_etc_timezone(void) | ||
| 636 | +{ | ||
| 637 | + char buf[128]; | ||
| 638 | + FILE *f = fopen("/etc/timezone", "r"); | ||
| 639 | + size_t n; | ||
| 640 | + | ||
| 641 | + if (!f) return 0; | ||
| 642 | + n = fread(buf, 1, sizeof buf - 1, f); | ||
| 643 | + fclose(f); | ||
| 644 | + if (n == 0) return 0; | ||
| 645 | + buf[n] = '\0'; | ||
| 646 | + while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r' || | ||
| 647 | + buf[n - 1] == ' ' || buf[n - 1] == '\t')) buf[--n] = '\0'; | ||
| 648 | + if (!name_ok(buf)) return 0; | ||
| 649 | + snprintf(g_local_name, sizeof g_local_name, "%s", buf); | ||
| 650 | + return 1; | ||
| 651 | +} | ||
| 652 | +#endif | ||
| 653 | + | ||
| 654 | +/* Offset-only fallback: the current local offset, DST state and - on POSIX - | ||
| 655 | + * the abbreviation, matched against the embedded table. An offset alone cannot | ||
| 656 | + * distinguish Europe/Paris (CET) from Africa/Lagos (WAT) in winter, so the | ||
| 657 | + * abbreviation is the discriminator where the platform reports one. A | ||
| 658 | + * "Region/City" key beats a bare legacy key (UTC, GMT, EST) so the answer is | ||
| 659 | + * stable and useful; the table is sorted, so it is deterministic. */ | ||
| 660 | +static int name_from_offset(void) | ||
| 661 | +{ | ||
| 662 | + int64_t now = (int64_t)time(NULL); | ||
| 663 | + int off; | ||
| 664 | + int dst; | ||
| 665 | + int i; | ||
| 666 | + int fallback = -1; | ||
| 667 | + char abbr[24]; | ||
| 668 | + | ||
| 669 | + abbr[0] = '\0'; | ||
| 670 | + | ||
| 671 | +#if defined(_WIN32) | ||
| 672 | + { | ||
| 673 | + TIME_ZONE_INFORMATION tz; | ||
| 674 | + DWORD kind = GetTimeZoneInformation(&tz); | ||
| 675 | + long bias = tz.Bias + ((kind == TIME_ZONE_ID_DAYLIGHT) | ||
| 676 | + ? tz.DaylightBias : tz.StandardBias); | ||
| 677 | + off = (int)(-bias * 60); | ||
| 678 | + dst = (kind == TIME_ZONE_ID_DAYLIGHT); | ||
| 679 | + } | ||
| 680 | +#else | ||
| 681 | + { | ||
| 682 | + time_t t = (time_t)now; | ||
| 683 | + struct tm lt; | ||
| 684 | + if (!localtime_r(&t, <)) return 0; | ||
| 685 | + off = (int)lt.tm_gmtoff; | ||
| 686 | + dst = lt.tm_isdst > 0; | ||
| 687 | + if (lt.tm_zone) snprintf(abbr, sizeof abbr, "%s", lt.tm_zone); | ||
| 688 | + } | ||
| 689 | +#endif | ||
| 690 | + for (i = 0; i < tz_count(); i++) { | ||
| 691 | + const tz_zone *z = tz_find(tz_name_at(i)); | ||
| 692 | + const char *nm = tz_name_at(i); | ||
| 693 | + char za[24]; | ||
| 694 | + int d = 0; | ||
| 695 | + int o; | ||
| 696 | + | ||
| 697 | + if (!z) continue; | ||
| 698 | + o = tz_offset(z, now, &d, za, sizeof za); | ||
| 699 | + if (o != off || d != dst) continue; | ||
| 700 | + if (abbr[0] && strcmp(za, abbr) != 0) continue; | ||
| 701 | + if (!strchr(nm, '/')) { | ||
| 702 | + if (fallback < 0) fallback = i; | ||
| 703 | + continue; | ||
| 704 | + } | ||
| 705 | + fallback = i; | ||
| 706 | + break; | ||
| 707 | + } | ||
| 708 | + if (fallback >= 0) { | ||
| 709 | + snprintf(g_local_name, sizeof g_local_name, "%s", tz_name_at(fallback)); | ||
| 710 | + return 1; | ||
| 711 | + } | ||
| 712 | + return 0; | ||
| 713 | +} | ||
| 714 | + | ||
| 715 | +const char *tz_local_name(void) | ||
| 716 | +{ | ||
| 717 | + const char *env; | ||
| 718 | + | ||
| 719 | + if (g_local_done) return g_local_name; | ||
| 720 | + g_local_done = 1; | ||
| 721 | + | ||
| 722 | + env = getenv("TZ"); | ||
| 723 | + if (env && *env) { | ||
| 724 | + if (env[0] == ':') env++; | ||
| 725 | + if (name_ok(env)) { | ||
| 726 | + snprintf(g_local_name, sizeof g_local_name, "%s", env); | ||
| 727 | + return g_local_name; | ||
| 728 | + } | ||
| 729 | + } | ||
| 730 | +#if !defined(_WIN32) | ||
| 731 | + if (name_from_localtime_link()) return g_local_name; | ||
| 732 | + if (name_from_etc_timezone()) return g_local_name; | ||
| 733 | +#endif | ||
| 734 | + if (name_from_offset()) return g_local_name; | ||
| 735 | + | ||
| 736 | + snprintf(g_local_name, sizeof g_local_name, "UTC"); | ||
| 737 | + return g_local_name; | ||
| 738 | +} |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..cd2cedd | index 0000000..cd2cedd | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/src/tz.h | +++ b/src/tz.h | ||
| @@ -0,0 +1,74 @@ | @@ -0,0 +1,74 @@ | ||
| 1 | +/* | ||
| 2 | + * tz.h - IANA timezone resolution for plugin_time. | ||
| 3 | + * | ||
| 4 | + * The connector cannot read the host's tz database: a Windows node has none, | ||
| 5 | + * macOS's is case-insensitive, and a slim container may have a different tzdata | ||
| 6 | + * version, all of which would make the same tool call answer differently per | ||
| 7 | + * node. So the table is embedded (src/tzdata.h, generated by | ||
| 8 | + * tools/gen_tzdata.py) and every platform resolves identically: | ||
| 9 | + * | ||
| 10 | + * tz_find() exact, case-sensitive IANA key -> zone record (or NULL) | ||
| 11 | + * tz_offset() UTC instant -> offset east of UTC, DST flag, abbreviation | ||
| 12 | + * tz_resolve_wall() a wall-clock local time -> the UTC instants it can mean | ||
| 13 | + * (two of them on a DST fall-back, none in a spring gap) | ||
| 14 | + * tz_local_name() the node's own timezone name, best effort | ||
| 15 | + * | ||
| 16 | + * Times are int64 seconds since the Unix epoch; nothing here reads the system | ||
| 17 | + * clock, so every function is deterministic and testable. | ||
| 18 | + * | ||
| 19 | + * MIT License. Copyright (c) 2026 SCORPIOX INC. | ||
| 20 | + */ | ||
| 21 | +#ifndef SX_PLUGIN_TIME_TZ_H | ||
| 22 | +#define SX_PLUGIN_TIME_TZ_H | ||
| 23 | + | ||
| 24 | +#include <stddef.h> | ||
| 25 | +#include <stdint.h> | ||
| 26 | + | ||
| 27 | +/* A wall-clock resolution can legitimately have two answers (a repeated hour | ||
| 28 | + * at a DST fall-back) or zero (an hour skipped at spring-forward). */ | ||
| 29 | +#define TZ_MAX_CANDIDATES 4 | ||
| 30 | + | ||
| 31 | +typedef struct { | ||
| 32 | + int64_t utc; | ||
| 33 | + int offset; /* east of UTC, seconds */ | ||
| 34 | + int is_dst; | ||
| 35 | +} tz_cand_t; | ||
| 36 | + | ||
| 37 | +/* The zone record, opaque outside tz.c. */ | ||
| 38 | +typedef struct tz_zone tz_zone; | ||
| 39 | + | ||
| 40 | +/* Exact IANA key lookup. Case sensitive on every platform on purpose: see the | ||
| 41 | + * header comment in tools/gen_tzdata.py. Returns NULL for an unknown key. */ | ||
| 42 | +const tz_zone *tz_find(const char *name); | ||
| 43 | + | ||
| 44 | +/* Number of embedded zones and their keys (sorted), for the picker UI. */ | ||
| 45 | +int tz_count(void); | ||
| 46 | +const char *tz_name_at(int index); | ||
| 47 | + | ||
| 48 | +/* Offset east of UTC in force at `utc`. `abbr` (may be NULL) receives the | ||
| 49 | + * zone abbreviation, truncated to `abbrsz` including the terminator. */ | ||
| 50 | +int tz_offset(const tz_zone *z, int64_t utc, int *is_dst, | ||
| 51 | + char *abbr, size_t abbrsz); | ||
| 52 | + | ||
| 53 | +/* Every UTC instant that has the given wall-clock time in `z`, earliest | ||
| 54 | + * first. `wall` is the local time read as if it were UTC (i.e. it already | ||
| 55 | + * includes the date and the hh:mm:ss). Returns the count: | ||
| 56 | + * 0 = the wall time does not exist (skipped by a forward clock change) | ||
| 57 | + * 1 = unambiguous | ||
| 58 | + * 2 = the hour repeated (a backward clock change); [0] is the first | ||
| 59 | + * occurrence, [1] the second - which is what Python's fold=0/1 gives. | ||
| 60 | + */ | ||
| 61 | +int tz_resolve_wall(const tz_zone *z, int64_t wall, tz_cand_t *out, int outmax); | ||
| 62 | + | ||
| 63 | +/* The node's own IANA zone name, best effort: $TZ, then the platform's own | ||
| 64 | + * configuration, then an offset match against the embedded table. Never | ||
| 65 | + * returns NULL; falls back to "UTC". The result is always a name tz_find() | ||
| 66 | + * accepts. */ | ||
| 67 | +const char *tz_local_name(void); | ||
| 68 | + | ||
| 69 | +/* Civil-date helpers (proleptic Gregorian, no leap seconds). */ | ||
| 70 | +int64_t tz_days_from_civil(int y, int m, int d); | ||
| 71 | +void tz_civil_from_days(int64_t days, int *y, int *m, int *d); | ||
| 72 | +int tz_weekday(int64_t days); /* 0 = Sunday */ | ||
| 73 | + | ||
| 74 | +#endif /* SX_PLUGIN_TIME_TZ_H */ |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..0b7b41a | index 0000000..0b7b41a | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/src/tzdata.h | +++ b/src/tzdata.h | ||
| @@ -0,0 +1,3494 @@ | @@ -0,0 +1,3494 @@ | ||
| 1 | +/* | ||
| 2 | + * tzdata.h - generated by tools/gen_tzdata.py from the compiled IANA | ||
| 3 | + * tz database (version 2025b). DO NOT EDIT. | ||
| 4 | + * | ||
| 5 | + * 598 zones, 10341 UTC transitions from 2015-01-01 onwards. | ||
| 6 | + * | ||
| 7 | + * Four parallel arrays index the transitions so the records stay | ||
| 8 | + * padding-free on every ABI: seconds since the window start, the UTC | ||
| 9 | + * offset east of Greenwich in seconds, the abbreviation pool offset, | ||
| 10 | + * and the DST flag. | ||
| 11 | + */ | ||
| 12 | +#ifndef SX_PLUGIN_TIME_TZDATA_H | ||
| 13 | +#define SX_PLUGIN_TIME_TZDATA_H | ||
| 14 | + | ||
| 15 | +#include <stdint.h> | ||
| 16 | + | ||
| 17 | +#define TZ_WINDOW_EPOCH 1420070400 | ||
| 18 | +#define TZ_ZONE_COUNT 598 | ||
| 19 | +#define TZ_TRANS_COUNT 10341 | ||
| 20 | + | ||
| 21 | +typedef struct { | ||
| 22 | + uint32_t name; /* offset into TZ_NAME_POOL */ | ||
| 23 | + uint32_t rule; /* offset into TZ_RULE_POOL */ | ||
| 24 | + int32_t base_off; /* east-of-UTC seconds before the window */ | ||
| 25 | + uint32_t first; /* index of this zone's first transition */ | ||
| 26 | + uint16_t count; /* transitions in this zone's slice */ | ||
| 27 | + uint16_t base_abbr; /* offset into TZ_ABBR_POOL */ | ||
| 28 | + uint8_t base_dst; | ||
| 29 | + uint8_t has_rule; | ||
| 30 | +} tz_zone_t; | ||
| 31 | + | ||
| 32 | +static const char TZ_NAME_POOL[] = | ||
| 33 | + "Africa/Abidjan\0Africa/Accra\0Africa/Addis_Ababa\0Africa/Algiers\0Africa/Asmara\0Africa/Asmera\0Africa/Bamako\0Africa/Bangui\0Africa/Banjul\0Africa/Bissau\0Africa/Blantyre\0Africa/Brazzaville" | ||
| 34 | + "\0Africa/Bujumbura\0Africa/Cairo\0Africa/Casablanca\0Africa/Ceuta\0Africa/Conakry\0Africa/Dakar\0Africa/Dar_es_Salaam\0Africa/Djibouti\0Africa/Douala\0Africa/El_Aaiun\0Africa/Freetown\0Africa/" | ||
| 35 | + "Gaborone\0Africa/Harare\0Africa/Johannesburg\0Africa/Juba\0Africa/Kampala\0Africa/Khartoum\0Africa/Kigali\0Africa/Kinshasa\0Africa/Lagos\0Africa/Libreville\0Africa/Lome\0Africa/Luanda\0Africa/" | ||
| 36 | + "Lubumbashi\0Africa/Lusaka\0Africa/Malabo\0Africa/Maputo\0Africa/Maseru\0Africa/Mbabane\0Africa/Mogadishu\0Africa/Monrovia\0Africa/Nairobi\0Africa/Ndjamena\0Africa/Niamey\0Africa/Nouakchott\0Af" | ||
| 37 | + "rica/Ouagadougou\0Africa/Porto-Novo\0Africa/Sao_Tome\0Africa/Timbuktu\0Africa/Tripoli\0Africa/Tunis\0Africa/Windhoek\0America/Adak\0America/Anchorage\0America/Anguilla\0America/Antigua\0Ameri" | ||
| 38 | + "ca/Araguaina\0America/Argentina/Buenos_Aires\0America/Argentina/Catamarca\0America/Argentina/ComodRivadavia\0America/Argentina/Cordoba\0America/Argentina/Jujuy\0America/Argentina/La_Rioj" | ||
| 39 | + "a\0America/Argentina/Mendoza\0America/Argentina/Rio_Gallegos\0America/Argentina/Salta\0America/Argentina/San_Juan\0America/Argentina/San_Luis\0America/Argentina/Tucuman\0America/Argentina" | ||
| 40 | + "/Ushuaia\0America/Aruba\0America/Asuncion\0America/Atikokan\0America/Atka\0America/Bahia\0America/Bahia_Banderas\0America/Barbados\0America/Belem\0America/Belize\0America/Blanc-Sablon\0Americ" | ||
| 41 | + "a/Boa_Vista\0America/Bogota\0America/Boise\0America/Buenos_Aires\0America/Cambridge_Bay\0America/Campo_Grande\0America/Cancun\0America/Caracas\0America/Catamarca\0America/Cayenne\0America/Ca" | ||
| 42 | + "yman\0America/Chicago\0America/Chihuahua\0America/Ciudad_Juarez\0America/Coral_Harbour\0America/Cordoba\0America/Costa_Rica\0America/Coyhaique\0America/Creston\0America/Cuiaba\0America/Curac" | ||
| 43 | + "ao\0America/Danmarkshavn\0America/Dawson\0America/Dawson_Creek\0America/Denver\0America/Detroit\0America/Dominica\0America/Edmonton\0America/Eirunepe\0America/El_Salvador\0America/Ensenada\0A" | ||
| 44 | + "merica/Fort_Nelson\0America/Fort_Wayne\0America/Fortaleza\0America/Glace_Bay\0America/Godthab\0America/Goose_Bay\0America/Grand_Turk\0America/Grenada\0America/Guadeloupe\0America/Guatemala\0" | ||
| 45 | + "America/Guayaquil\0America/Guyana\0America/Halifax\0America/Havana\0America/Hermosillo\0America/Indiana/Indianapolis\0America/Indiana/Knox\0America/Indiana/Marengo\0America/Indiana/Petersb" | ||
| 46 | + "urg\0America/Indiana/Tell_City\0America/Indiana/Vevay\0America/Indiana/Vincennes\0America/Indiana/Winamac\0America/Indianapolis\0America/Inuvik\0America/Iqaluit\0America/Jamaica\0America/Ju" | ||
| 47 | + "juy\0America/Juneau\0America/Kentucky/Louisville\0America/Kentucky/Monticello\0America/Knox_IN\0America/Kralendijk\0America/La_Paz\0America/Lima\0America/Los_Angeles\0America/Louisville\0Ame" | ||
| 48 | + "rica/Lower_Princes\0America/Maceio\0America/Managua\0America/Manaus\0America/Marigot\0America/Martinique\0America/Matamoros\0America/Mazatlan\0America/Mendoza\0America/Menominee\0America/Mer" | ||
| 49 | + "ida\0America/Metlakatla\0America/Mexico_City\0America/Miquelon\0America/Moncton\0America/Monterrey\0America/Montevideo\0America/Montreal\0America/Montserrat\0America/Nassau\0America/New_York" | ||
| 50 | + "\0America/Nipigon\0America/Nome\0America/Noronha\0America/North_Dakota/Beulah\0America/North_Dakota/Center\0America/North_Dakota/New_Salem\0America/Nuuk\0America/Ojinaga\0America/Panama\0Ame" | ||
| 51 | + "rica/Pangnirtung\0America/Paramaribo\0America/Phoenix\0America/Port-au-Prince\0America/Port_of_Spain\0America/Porto_Acre\0America/Porto_Velho\0America/Puerto_Rico\0America/Punta_Arenas\0Ame" | ||
| 52 | + "rica/Rainy_River\0America/Rankin_Inlet\0America/Recife\0America/Regina\0America/Resolute\0America/Rio_Branco\0America/Rosario\0America/Santa_Isabel\0America/Santarem\0America/Santiago\0Ameri" | ||
| 53 | + "ca/Santo_Domingo\0America/Sao_Paulo\0America/Scoresbysund\0America/Shiprock\0America/Sitka\0America/St_Barthelemy\0America/St_Johns\0America/St_Kitts\0America/St_Lucia\0America/St_Thomas\0Am" | ||
| 54 | + "erica/St_Vincent\0America/Swift_Current\0America/Tegucigalpa\0America/Thule\0America/Thunder_Bay\0America/Tijuana\0America/Toronto\0America/Tortola\0America/Vancouver\0America/Virgin\0Americ" | ||
| 55 | + "a/Whitehorse\0America/Winnipeg\0America/Yakutat\0America/Yellowknife\0Antarctica/Casey\0Antarctica/Davis\0Antarctica/DumontDUrville\0Antarctica/Macquarie\0Antarctica/Mawson\0Antarctica/McMu" | ||
| 56 | + "rdo\0Antarctica/Palmer\0Antarctica/Rothera\0Antarctica/South_Pole\0Antarctica/Syowa\0Antarctica/Troll\0Antarctica/Vostok\0Arctic/Longyearbyen\0Asia/Aden\0Asia/Almaty\0Asia/Amman\0Asia/Anadyr\0" | ||
| 57 | + "Asia/Aqtau\0Asia/Aqtobe\0Asia/Ashgabat\0Asia/Ashkhabad\0Asia/Atyrau\0Asia/Baghdad\0Asia/Bahrain\0Asia/Baku\0Asia/Bangkok\0Asia/Barnaul\0Asia/Beirut\0Asia/Bishkek\0Asia/Brunei\0Asia/Calcutta\0Asi" | ||
| 58 | + "a/Chita\0Asia/Choibalsan\0Asia/Chongqing\0Asia/Chungking\0Asia/Colombo\0Asia/Dacca\0Asia/Damascus\0Asia/Dhaka\0Asia/Dili\0Asia/Dubai\0Asia/Dushanbe\0Asia/Famagusta\0Asia/Gaza\0Asia/Harbin\0Asia/" | ||
| 59 | + "Hebron\0Asia/Ho_Chi_Minh\0Asia/Hong_Kong\0Asia/Hovd\0Asia/Irkutsk\0Asia/Istanbul\0Asia/Jakarta\0Asia/Jayapura\0Asia/Jerusalem\0Asia/Kabul\0Asia/Kamchatka\0Asia/Karachi\0Asia/Kashgar\0Asia/Kathm" | ||
| 60 | + "andu\0Asia/Katmandu\0Asia/Khandyga\0Asia/Kolkata\0Asia/Krasnoyarsk\0Asia/Kuala_Lumpur\0Asia/Kuching\0Asia/Kuwait\0Asia/Macao\0Asia/Macau\0Asia/Magadan\0Asia/Makassar\0Asia/Manila\0Asia/Muscat\0A" | ||
| 61 | + "sia/Nicosia\0Asia/Novokuznetsk\0Asia/Novosibirsk\0Asia/Omsk\0Asia/Oral\0Asia/Phnom_Penh\0Asia/Pontianak\0Asia/Pyongyang\0Asia/Qatar\0Asia/Qostanay\0Asia/Qyzylorda\0Asia/Rangoon\0Asia/Riyadh\0As" | ||
| 62 | + "ia/Saigon\0Asia/Sakhalin\0Asia/Samarkand\0Asia/Seoul\0Asia/Shanghai\0Asia/Singapore\0Asia/Srednekolymsk\0Asia/Taipei\0Asia/Tashkent\0Asia/Tbilisi\0Asia/Tehran\0Asia/Tel_Aviv\0Asia/Thimbu\0Asia/" | ||
| 63 | + "Thimphu\0Asia/Tokyo\0Asia/Tomsk\0Asia/Ujung_Pandang\0Asia/Ulaanbaatar\0Asia/Ulan_Bator\0Asia/Urumqi\0Asia/Ust-Nera\0Asia/Vientiane\0Asia/Vladivostok\0Asia/Yakutsk\0Asia/Yangon\0Asia/Yekaterinb" | ||
| 64 | + "urg\0Asia/Yerevan\0Atlantic/Azores\0Atlantic/Bermuda\0Atlantic/Canary\0Atlantic/Cape_Verde\0Atlantic/Faeroe\0Atlantic/Faroe\0Atlantic/Jan_Mayen\0Atlantic/Madeira\0Atlantic/Reykjavik\0Atlantic" | ||
| 65 | + "/South_Georgia\0Atlantic/St_Helena\0Atlantic/Stanley\0Australia/ACT\0Australia/Adelaide\0Australia/Brisbane\0Australia/Broken_Hill\0Australia/Canberra\0Australia/Currie\0Australia/Darwin\0Au" | ||
| 66 | + "stralia/Eucla\0Australia/Hobart\0Australia/LHI\0Australia/Lindeman\0Australia/Lord_Howe\0Australia/Melbourne\0Australia/NSW\0Australia/North\0Australia/Perth\0Australia/Queensland\0Australia" | ||
| 67 | + "/South\0Australia/Sydney\0Australia/Tasmania\0Australia/Victoria\0Australia/West\0Australia/Yancowinna\0Brazil/Acre\0Brazil/DeNoronha\0Brazil/East\0Brazil/West\0CET\0CST6CDT\0Canada/Atlantic\0C" | ||
| 68 | + "anada/Central\0Canada/Eastern\0Canada/Mountain\0Canada/Newfoundland\0Canada/Pacific\0Canada/Saskatchewan\0Canada/Yukon\0Chile/Continental\0Chile/EasterIsland\0Cuba\0EET\0EST\0EST5EDT\0Egypt\0Eir" | ||
| 69 | + "e\0Etc/GMT\0Etc/GMT+0\0Etc/GMT+1\0Etc/GMT+10\0Etc/GMT+11\0Etc/GMT+12\0Etc/GMT+2\0Etc/GMT+3\0Etc/GMT+4\0Etc/GMT+5\0Etc/GMT+6\0Etc/GMT+7\0Etc/GMT+8\0Etc/GMT+9\0Etc/GMT-0\0Etc/GMT-1\0Etc/GMT-10\0Etc/GM" | ||
| 70 | + "T-11\0Etc/GMT-12\0Etc/GMT-13\0Etc/GMT-14\0Etc/GMT-2\0Etc/GMT-3\0Etc/GMT-4\0Etc/GMT-5\0Etc/GMT-6\0Etc/GMT-7\0Etc/GMT-8\0Etc/GMT-9\0Etc/GMT0\0Etc/Greenwich\0Etc/UCT\0Etc/UTC\0Etc/Universal\0Etc/Zulu\0" | ||
| 71 | + "Europe/Amsterdam\0Europe/Andorra\0Europe/Astrakhan\0Europe/Athens\0Europe/Belfast\0Europe/Belgrade\0Europe/Berlin\0Europe/Bratislava\0Europe/Brussels\0Europe/Bucharest\0Europe/Budapest\0Europ" | ||
| 72 | + "e/Busingen\0Europe/Chisinau\0Europe/Copenhagen\0Europe/Dublin\0Europe/Gibraltar\0Europe/Guernsey\0Europe/Helsinki\0Europe/Isle_of_Man\0Europe/Istanbul\0Europe/Jersey\0Europe/Kaliningrad\0Euro" | ||
| 73 | + "pe/Kiev\0Europe/Kirov\0Europe/Kyiv\0Europe/Lisbon\0Europe/Ljubljana\0Europe/London\0Europe/Luxembourg\0Europe/Madrid\0Europe/Malta\0Europe/Mariehamn\0Europe/Minsk\0Europe/Monaco\0Europe/Moscow" | ||
| 74 | + "\0Europe/Nicosia\0Europe/Oslo\0Europe/Paris\0Europe/Podgorica\0Europe/Prague\0Europe/Riga\0Europe/Rome\0Europe/Samara\0Europe/San_Marino\0Europe/Sarajevo\0Europe/Saratov\0Europe/Simferopol\0Eur" | ||
| 75 | + "ope/Skopje\0Europe/Sofia\0Europe/Stockholm\0Europe/Tallinn\0Europe/Tirane\0Europe/Tiraspol\0Europe/Ulyanovsk\0Europe/Uzhgorod\0Europe/Vaduz\0Europe/Vatican\0Europe/Vienna\0Europe/Vilnius\0Euro" | ||
| 76 | + "pe/Volgograd\0Europe/Warsaw\0Europe/Zagreb\0Europe/Zaporozhye\0Europe/Zurich\0Factory\0GB\0GB-Eire\0GMT\0GMT+0\0GMT-0\0GMT0\0Greenwich\0HST\0Hongkong\0Iceland\0Indian/Antananarivo\0Indian/Chagos\0In" | ||
| 77 | + "dian/Christmas\0Indian/Cocos\0Indian/Comoro\0Indian/Kerguelen\0Indian/Mahe\0Indian/Maldives\0Indian/Mauritius\0Indian/Mayotte\0Indian/Reunion\0Iran\0Israel\0Jamaica\0Japan\0Kwajalein\0Libya\0MET\0" | ||
| 78 | + "MST\0MST7MDT\0Mexico/BajaNorte\0Mexico/BajaSur\0Mexico/General\0NZ\0NZ-CHAT\0Navajo\0PRC\0PST8PDT\0Pacific/Apia\0Pacific/Auckland\0Pacific/Bougainville\0Pacific/Chatham\0Pacific/Chuuk\0Pacific/Ea" | ||
| 79 | + "ster\0Pacific/Efate\0Pacific/Enderbury\0Pacific/Fakaofo\0Pacific/Fiji\0Pacific/Funafuti\0Pacific/Galapagos\0Pacific/Gambier\0Pacific/Guadalcanal\0Pacific/Guam\0Pacific/Honolulu\0Pacific/Johns" | ||
| 80 | + "ton\0Pacific/Kanton\0Pacific/Kiritimati\0Pacific/Kosrae\0Pacific/Kwajalein\0Pacific/Majuro\0Pacific/Marquesas\0Pacific/Midway\0Pacific/Nauru\0Pacific/Niue\0Pacific/Norfolk\0Pacific/Noumea\0Pac" | ||
| 81 | + "ific/Pago_Pago\0Pacific/Palau\0Pacific/Pitcairn\0Pacific/Pohnpei\0Pacific/Ponape\0Pacific/Port_Moresby\0Pacific/Rarotonga\0Pacific/Saipan\0Pacific/Samoa\0Pacific/Tahiti\0Pacific/Tarawa\0Pacif" | ||
| 82 | + "ic/Tongatapu\0Pacific/Truk\0Pacific/Wake\0Pacific/Wallis\0Pacific/Yap\0Poland\0Portugal\0ROC\0ROK\0Singapore\0Turkey\0UCT\0US/Alaska\0US/Aleutian\0US/Arizona\0US/Central\0US/East-Indiana\0US/Easter" | ||
| 83 | + "n\0US/Hawaii\0US/Indiana-Starke\0US/Michigan\0US/Mountain\0US/Pacific\0US/Samoa\0UTC\0Universal\0W-SU\0WET\0Zulu\0" | ||
| 84 | + ; | ||
| 85 | + | ||
| 86 | +static const char TZ_ABBR_POOL[] = | ||
| 87 | + "GMT\0EAT\0CET\0WAT\0CAT\0EEST\0EET\0+01\0+00\0CEST\0SAST\0HDT\0HST\0AKDT\0AKST\0AST\0-03\0-04\0EST\0CDT\0CST\0-05\0MDT\0MST\0-0430\0PDT\0PST\0EDT\0ADT\0-02\0-01\0NDT\0NST\0+11\0+08\0+07\0+10\0AEST\0AEDT\0+05\0NZST\0NZDT\0+" | ||
| 88 | + "03\0+02\0+06\0+12\0+04\0IST\0+09\0+0530\0HKT\0WIB\0WIT\0IDT\0+0430\0PKT\0+0545\0WITA\0KST\0+0630\0+0330\0JST\0WEST\0WET\0ACST\0ACDT\0+0845\0+1030\0AWST\0-06\0-10\0-11\0-12\0-07\0-08\0-09\0+13\0+14\0UTC\0BST\0MSK\0-00\0ME" | ||
| 89 | + "ST\0MET\0+1245\0+1345\0ChST\0-0930\0SST\0+1130\0" | ||
| 90 | + ; | ||
| 91 | + | ||
| 92 | +static const char TZ_RULE_POOL[] = | ||
| 93 | + "GMT0\0EAT-3\0CET-1\0WAT-1\0CAT-2\0EET-2EEST,M4.5.5/0,M10.5.4/24\0<+00>0<+01>,0/0,J365/25\0CET-1CEST,M3.5.0,M10.5.0/3\0SAST-2\0EET-2\0HST10HDT,M3.2.0,M11.1.0\0AKST9AKDT,M3.2.0,M11.1.0\0AST4\0<-0" | ||
| 94 | + "3>3\0EST5\0CST6\0<-04>4\0<-05>5\0MST7MDT,M3.2.0,M11.1.0\0CST6CDT,M3.2.0,M11.1.0\0MST7\0EST5EDT,M3.2.0,M11.1.0\0PST8PDT,M3.2.0,M11.1.0\0AST4ADT,M3.2.0,M11.1.0\0<-02>2<-01>,M3.5.0/-1,M10.5.0/0\0" | ||
| 95 | + "CST5CDT,M3.2.0/0,M11.1.0/1\0<-03>3<-02>,M3.2.0,M11.1.0\0<-02>2\0<-04>4<-03>,M9.1.6/24,M4.1.6/24\0NST3:30NDT,M3.2.0,M11.1.0\0<+08>-8\0<+07>-7\0<+10>-10\0AEST-10AEDT,M10.1.0,M4.1.0/3\0<+05>-5" | ||
| 96 | + "\0NZST-12NZDT,M9.5.0,M4.1.0/3\0<+03>-3\0<+00>0<+02>-2,M3.5.0/1,M10.5.0/3\0<+12>-12\0<+04>-4\0EET-2EEST,M3.5.0/0,M10.5.0/0\0<+06>-6\0IST-5:30\0<+09>-9\0CST-8\0<+0530>-5:30\0EET-2EEST,M3.5.0/3,M" | ||
| 97 | + "10.5.0/4\0EET-2EEST,M3.4.4/50,M10.4.4/50\0HKT-8\0WIB-7\0WIT-9\0IST-2IDT,M3.4.4/26,M10.5.0\0<+0430>-4:30\0PKT-5\0<+0545>-5:45\0<+11>-11\0WITA-8\0PST-8\0KST-9\0<+0630>-6:30\0<+0330>-3:30\0JST-9\0<-0" | ||
| 98 | + "1>1<+00>,M3.5.0/0,M10.5.0/1\0WET0WEST,M3.5.0/1,M10.5.0\0<-01>1\0ACST-9:30ACDT,M10.1.0,M4.1.0/3\0AEST-10\0ACST-9:30\0<+0845>-8:45\0<+1030>-10:30<+11>-11,M10.1.0,M4.1.0\0AWST-8\0<-06>6<-05>,M" | ||
| 99 | + "9.1.6/22,M4.1.6/22\0GMT0IST,M3.5.0/1,M10.5.0\0<-10>10\0<-11>11\0<-12>12\0<-06>6\0<-07>7\0<-08>8\0<-09>9\0<+01>-1\0<+13>-13\0<+14>-14\0<+02>-2\0UTC0\0GMT0BST,M3.5.0/1,M10.5.0\0EET-2EEST,M3.5.0,M10" | ||
| 100 | + ".5.0/3\0MSK-3\0<-00>0\0HST10\0MET-1MEST,M3.5.0,M10.5.0/3\0<+1245>-12:45<+1345>,M9.5.0/2:45,M4.1.0/3:45\0ChST-10\0<-0930>9:30\0SST11\0<+11>-11<+12>,M10.1.0,M4.1.0/3\0" | ||
| 101 | + ; | ||
| 102 | + | ||
| 103 | +static const uint32_t TZ_TR_T[TZ_TRANS_COUNT] = { | ||
| 104 | + 262562400,278283600,294012000,310338000,325461600,341787600,356911200,373237200,388965600, | ||
| 105 | + 404686800,420415200,436136400,451864800,467586000,483314400,499640400, | ||
| 106 | + 514764000,531090000,546818400,562539600,578268000,593989200,609717600,625438800,641167200, | ||
| 107 | + 656888400,672616800,688942800,704066400,720392400,7524000,14176800, | ||
| 108 | + 17200800,25668000,38973600,45021600,48045600,57722400,70423200,75261600,78890400,89172000, | ||
| 109 | + 101872800,106106400,109130400,136951200,139975200,167191200, | ||
| 110 | + 170820000,198036000,201060000,228276000,231904800,259120800,262144800,289965600,292989600, | ||
| 111 | + 320205600,323834400,351050400,354074400,381895200,384919200,412135200, | ||
| 112 | + 415764000,442980000,446004000,473220000,476848800,504064800,507088800,534909600,537933600, | ||
| 113 | + 565149600,568778400,595994400,599018400,626234400,629863200,657079200, | ||
| 114 | + 660708000,687924000,690948000,718164000,721792800,749008800,752032800,779853600,782877600, | ||
| 115 | + 810093600,813722400,840938400,843962400,871178400,874807200,902023200, | ||
| 116 | + 905652000,932868000,935892000,963108000,966736800,993952800,996976800,1024797600,1027821600, | ||
| 117 | + 1055037600,1058666400,1085882400,1088906400,1116122400,1119751200,1146967200, | ||
| 118 | + 1150596000,1177812000,1180836000,1208052000,1211680800,1238896800,1241920800,1269741600, | ||
| 119 | + 1272765600,1299981600,1303610400,1330826400,1333850400,1361066400,1364695200,1391911200, | ||
| 120 | + 1395540000,1422756000,1425780000,1452996000,1456624800,1483840800,1486864800,1514685600, | ||
| 121 | + 1517709600,1544925600,1548554400,1575770400,1578794400,1606010400,1609639200,1636855200, | ||
| 122 | + 1640484000,1667700000,1670724000,1697940000,1701568800,1728784800,1731808800,1759629600, | ||
| 123 | + 1762653600,1789869600,1793498400,1820714400,1823738400,1850954400,1854583200,1881799200, | ||
| 124 | + 1885428000,1912644000,1915668000,1942884000,1946512800,1973728800,1976752800,2004573600, | ||
| 125 | + 2007597600,2034813600,2038442400,2065658400,2068682400,2095898400,2099527200,2126743200, | ||
| 126 | + 2129767200,2157588000,2160612000,2187828000,2191456800,2218672800,2221696800,2249517600, | ||
| 127 | + 2252541600,2279757600,2283386400,7520400,25664400,38970000,57718800,70419600, | ||
| 128 | + 89168400,101869200,120618000,133923600,152067600,165373200,183517200,196822800,215571600, | ||
| 129 | + 228272400,247021200,259722000,278470800,291776400,309920400,323226000, | ||
| 130 | + 341370000,354675600,372819600,386125200,404874000,417574800,436323600,449024400,467773200, | ||
| 131 | + 481078800,499222800,512528400,530672400,543978000,562726800,575427600, | ||
| 132 | + 594176400,606877200,625626000,638326800,657075600,670381200,688525200,701830800,719974800, | ||
| 133 | + 7524000,14176800,17200800,25668000,38973600,45021600,48045600, | ||
| 134 | + 57722400,70423200,75261600,78890400,89172000,101872800,106106400,109130400,136951200, | ||
| 135 | + 139975200,167191200,170820000,198036000,201060000,228276000,231904800, | ||
| 136 | + 259120800,262144800,289965600,292989600,320205600,323834400,351050400,354074400,381895200, | ||
| 137 | + 384919200,412135200,415764000,442980000,446004000,473220000,476848800, | ||
| 138 | + 504064800,507088800,534909600,537933600,565149600,568778400,595994400,599018400,626234400, | ||
| 139 | + 629863200,657079200,660708000,687924000,690948000,718164000,721792800, | ||
| 140 | + 749008800,752032800,779853600,782877600,810093600,813722400,840938400,843962400,871178400, | ||
| 141 | + 874807200,902023200,905652000,932868000,935892000,963108000,966736800, | ||
| 142 | + 993952800,996976800,1024797600,1027821600,1055037600,1058666400,1085882400,1088906400, | ||
| 143 | + 1116122400,1119751200,1146967200,1150596000,1177812000,1180836000,1208052000,1211680800, | ||
| 144 | + 1238896800,1241920800,1269741600,1272765600,1299981600,1303610400,1330826400,1333850400, | ||
| 145 | + 1361066400,1364695200,1391911200,1395540000,1422756000,1425780000,1452996000,1456624800, | ||
| 146 | + 1483840800,1486864800,1514685600,1517709600,1544925600,1548554400,1575770400,1578794400, | ||
| 147 | + 1606010400,1609639200,1636855200,1640484000,1667700000,1670724000,1697940000,1701568800, | ||
| 148 | + 1728784800,1731808800,1759629600,1762653600,1789869600,1793498400,1820714400,1823738400, | ||
| 149 | + 1850954400,1854583200,1881799200,1885428000,1912644000,1915668000,1942884000,1946512800, | ||
| 150 | + 1973728800,1976752800,2004573600,2007597600,2034813600,2038442400,2065658400,2068682400, | ||
| 151 | + 2095898400,2099527200,2126743200,2129767200,2157588000,2160612000,2187828000,2191456800, | ||
| 152 | + 2218672800,2221696800,2249517600,2252541600,2279757600,2283386400,192056400,89413200, | ||
| 153 | + 94698000,126234000,8121600,21430800,39571200,52880400,71020800,84330000, | ||
| 154 | + 88725600,5745600,26305200,37800000,58359600,69249600,89809200,100699200,121258800,132148800, | ||
| 155 | + 152708400,163598400,184158000,195652800,216212400,227102400, | ||
| 156 | + 247662000,258552000,279111600,290001600,310561200,321451200,342010800,352900800,373460400, | ||
| 157 | + 384955200,405514800,416404800,436964400,447854400,468414000,479304000, | ||
| 158 | + 499863600,510753600,531313200,542808000,563367600,574257600,594817200,605707200,626266800, | ||
| 159 | + 637156800,657716400,668606400,689166000,700056000,720615600,5742000, | ||
| 160 | + 26301600,37796400,58356000,69246000,89805600,100695600,121255200,132145200,152704800, | ||
| 161 | + 163594800,184154400,195649200,216208800,227098800,247658400,258548400, | ||
| 162 | + 279108000,289998000,310557600,321447600,342007200,352897200,373456800,384951600,405511200, | ||
| 163 | + 416401200,436960800,447850800,468410400,479300400,499860000,510750000, | ||
| 164 | + 531309600,542804400,563364000,574254000,594813600,605703600,626263200,637153200,657712800, | ||
| 165 | + 668602800,689162400,700052400,720612000,727413247,727413247,727413247, | ||
| 166 | + 727413247,727413247,727413247,727413247,727413247,727413247,727413247,727413247,727413247, | ||
| 167 | + 727413247,727413247,6922800,23860800,38977200,55310400,70426800, | ||
| 168 | + 86760000,101876400,118814400,133326000,150264000,164775600,181713600,196830000,213163200, | ||
| 169 | + 228279600,244612800,259729200,276062400,291178800,308116800,308890800, | ||
| 170 | + 727413247,5745600,26305200,37800000,58359600,69249600,89809200,100699200,121258800, | ||
| 171 | + 132148800,152708400,163598400,184158000,195652800,216212400,227102400, | ||
| 172 | + 247662000,258552000,279111600,290001600,310561200,321451200,342010800,352900800,373460400, | ||
| 173 | + 384955200,405514800,416404800,436964400,447854400,468414000,479304000, | ||
| 174 | + 499863600,510753600,531313200,542808000,563367600,574257600,594817200,605707200,626266800, | ||
| 175 | + 637156800,657716400,668606400,689166000,700056000,720615600,727413247, | ||
| 176 | + 8150400,25686000,39600000,57740400,71049600,89190000,102499200,120639600,134553600, | ||
| 177 | + 152089200,166003200,183538800,197452800,215593200,228902400,247042800, | ||
| 178 | + 727413247,727413247,727413247,5734800,26294400,37789200,58348800,69238800,89798400, | ||
| 179 | + 100688400,121248000,132138000,152697600,163587600,184147200,195642000, | ||
| 180 | + 216201600,227091600,247651200,258541200,279100800,289990800,310550400,321440400,342000000, | ||
| 181 | + 352890000,373449600,384944400,405504000,416394000,436953600,447843600, | ||
| 182 | + 468403200,479293200,499852800,510742800,531302400,542797200,563356800,574246800,594806400, | ||
| 183 | + 605696400,626256000,637146000,657705600,668595600,689155200,700045200, | ||
| 184 | + 720604800,727413247,5734800,26294400,37789200,58348800,69238800,89798400,100688400, | ||
| 185 | + 121248000,132138000,152697600,163587600,184147200,195642000,216201600, | ||
| 186 | + 227091600,247651200,258541200,279100800,289990800,310550400,321440400,342000000,352890000, | ||
| 187 | + 373449600,384944400,405504000,416394000,436953600,447843600,468403200, | ||
| 188 | + 479293200,499852800,510742800,531302400,542797200,563356800,574246800,594806400,605696400, | ||
| 189 | + 626256000,637146000,657705600,668595600,689155200,700045200,720604800, | ||
| 190 | + 4503600,25070400,35953200,56520000,67402800,87969600,98852400,121233600,130302000,727413247, | ||
| 191 | + 2707200,42015600,727413247,727413247,727413247,5731200,26290800,37785600,58345200,69235200, | ||
| 192 | + 89794800,100684800,121244400,132134400,152694000,163584000,184143600,195638400,216198000, | ||
| 193 | + 227088000,247647600,258537600,279097200,289987200,310546800,321436800,341996400,352886400, | ||
| 194 | + 373446000,384940800,405500400,416390400,436950000,447840000,468399600,479289600,499849200, | ||
| 195 | + 510739200,531298800,542793600,563353200,574243200,594802800,605692800,626252400,637142400, | ||
| 196 | + 657702000,668592000,689151600,700041600,720601200,8154000,25689600,39603600, | ||
| 197 | + 57744000,71053200,89193600,102502800,120643200,134557200,152092800,166006800,183542400, | ||
| 198 | + 197456400,215596800,228906000,247046400,5734800,26294400,37789200, | ||
| 199 | + 58348800,69238800,89798400,100688400,121248000,132138000,152697600,163587600,184147200, | ||
| 200 | + 195642000,216201600,227091600,247046400,249717600,258541200,279100800, | ||
| 201 | + 289990800,310550400,321440400,342000000,352890000,373449600,384944400,405504000,416394000, | ||
| 202 | + 436953600,447843600,468403200,479293200,499852800,510742800,531302400, | ||
| 203 | + 542797200,563356800,574246800,594806400,605696400,626256000,637146000,657705600,668595600, | ||
| 204 | + 689155200,700045200,720604800,727413247,43210800,51076800,74660400, | ||
| 205 | + 82526400,106110000,113976000,134535600,147844800,165985200,179294400,197434800,210744000, | ||
| 206 | + 228884400,242798400,260334000,273643200,292388400,305697600,322369200, | ||
| 207 | + 727413247,4503600,25070400,35953200,56520000,67402800,87969600,98852400,121233600,130302000, | ||
| 208 | + 727413247,5738400,26298000,37792800,58352400,69242400,89802000,100692000,121251600, | ||
| 209 | + 132141600,152701200,163591200,184143600,5734800,26294400,37789200,58348800,69238800, | ||
| 210 | + 89798400,100688400,121248000,132138000,152697600,163587600,184147200,195642000,216201600, | ||
| 211 | + 227091600,247651200,258541200,279100800,289990800,310550400,321440400,342000000,352890000, | ||
| 212 | + 373449600,384944400,405504000,416394000,436953600,447843600,468403200,479293200,499852800, | ||
| 213 | + 510742800,531302400,542797200,563356800,574246800,594806400,605696400,626256000,637146000, | ||
| 214 | + 657705600,668595600,689155200,700045200,720604800,5727600,26287200,37782000,58341600, | ||
| 215 | + 69231600,89791200,100681200,121240800,132130800,152690400,163580400, | ||
| 216 | + 184140000,195634800,216194400,227084400,247644000,258534000,279093600,289983600,310543200, | ||
| 217 | + 321433200,341992800,352882800,373442400,384937200,405496800,416386800, | ||
| 218 | + 436946400,447836400,468396000,479286000,499845600,510735600,531295200,542790000,563349600, | ||
| 219 | + 574239600,594799200,605689200,626248800,637138800,657698400,668588400, | ||
| 220 | + 689148000,700038000,720597600,5734800,26294400,37789200,58348800,69238800,89798400, | ||
| 221 | + 100688400,121248000,132138000,152697600,163587600,184147200,195642000, | ||
| 222 | + 216201600,227091600,247651200,258541200,279100800,289990800,310550400,321440400,342000000, | ||
| 223 | + 352890000,373449600,384944400,405504000,416394000,436953600,447843600, | ||
| 224 | + 468403200,479293200,499852800,510742800,531302400,542797200,563356800,574246800,594806400, | ||
| 225 | + 605696400,626256000,637146000,657705600,668595600,689155200,700045200, | ||
| 226 | + 720604800,727413247,5738400,26298000,37792800,58352400,69242400,89802000,100692000, | ||
| 227 | + 121251600,132141600,152701200,163591200,184150800,195645600,216205200, | ||
| 228 | + 227095200,247654800,258544800,279104400,289994400,310554000,321444000,342003600,352893600, | ||
| 229 | + 373453200,384948000,405507600,416397600,436957200,447847200,468406800, | ||
| 230 | + 479296800,499856400,510746400,531306000,542800800,563360400,574250400,594810000,605700000, | ||
| 231 | + 626259600,637149600,657709200,668599200,689158800,700048800,720608400, | ||
| 232 | + 5738400,5727600,26287200,37782000,58341600,69231600,89791200,100681200,121240800,132130800, | ||
| 233 | + 152690400,163580400,184140000,195634800,216194400,227084400, | ||
| 234 | + 247644000,258534000,279093600,289983600,310543200,321433200,341992800,352882800,373442400, | ||
| 235 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000, | ||
| 236 | + 499845600,510735600,531295200,542790000,563349600,574239600,594799200,605689200,626248800, | ||
| 237 | + 637138800,657698400,668588400,689148000,700038000,720597600,727413247, | ||
| 238 | + 5724000,26283600,37778400,58338000,69228000,89787600,100677600,121237200,132127200, | ||
| 239 | + 152686800,163576800,184136400,195631200,216190800,227080800,247640400, | ||
| 240 | + 258530400,279090000,289980000,310539600,321429600,341989200,352879200,373438800,384933600, | ||
| 241 | + 405493200,416383200,436942800,447832800,468392400,479282400,499842000, | ||
| 242 | + 510732000,531291600,542786400,563346000,574236000,594795600,605685600,626245200,637135200, | ||
| 243 | + 657694800,668584800,689144400,700034400,720594000,7520400,25664400, | ||
| 244 | + 38970000,57718800,70419600,89168400,101869200,120618000,133923600,152067600,165373200, | ||
| 245 | + 183517200,196822800,215571600,228272400,247021200,259722000,291776400, | ||
| 246 | + 309920400,323226000,341370000,354675600,372819600,386125200,404874000,417574800,436323600, | ||
| 247 | + 449024400,467773200,481078800,499222800,512528400,530672400,543978000, | ||
| 248 | + 562726800,575427600,594176400,606877200,625626000,638326800,657075600,670381200,688525200, | ||
| 249 | + 701830800,719974800,727413247,5724000,26283600,37778400,58338000, | ||
| 250 | + 69228000,89787600,100677600,121237200,132127200,152686800,163576800,184136400,195631200, | ||
| 251 | + 216190800,227080800,247640400,258530400,279090000,289980000,310539600, | ||
| 252 | + 321429600,341989200,352879200,373438800,384933600,405493200,416383200,436942800,447832800, | ||
| 253 | + 468392400,479282400,499842000,510732000,531291600,542786400,563346000, | ||
| 254 | + 574236000,594795600,605685600,626245200,637135200,657694800,668584800,689144400,700034400, | ||
| 255 | + 720594000,5727600,100681200,121240800,132130800,152690400,163580400, | ||
| 256 | + 184140000,195634800,216194400,227084400,247644000,258534000,279093600,289983600,310543200, | ||
| 257 | + 321433200,341992800,352882800,373442400,384937200,405496800,416386800, | ||
| 258 | + 436946400,447836400,468396000,479286000,499845600,510735600,531295200,542790000,563349600, | ||
| 259 | + 574239600,594799200,605689200,626248800,637138800,657698400,668588400, | ||
| 260 | + 689148000,700038000,720597600,727413247,727413247,5724000,26283600,37778400,58338000, | ||
| 261 | + 69228000,89787600,100677600,121237200,132127200,152686800,163576800, | ||
| 262 | + 184136400,195631200,216190800,227080800,247640400,258530400,279090000,289980000,310539600, | ||
| 263 | + 321429600,341989200,352879200,373438800,384933600,405493200,416383200, | ||
| 264 | + 436942800,447832800,468392400,479282400,499842000,510732000,531291600,542786400,563346000, | ||
| 265 | + 574236000,594795600,605685600,626245200,637135200,657694800,668584800, | ||
| 266 | + 689144400,700034400,720594000,5720400,26283600,37774800,58338000,69224400,89787600, | ||
| 267 | + 100674000,121237200,132123600,152686800,163573200,184136400,195627600, | ||
| 268 | + 216190800,227077200,247640400,258526800,279090000,289976400,310539600,321426000,341989200, | ||
| 269 | + 352875600,373438800,384930000,405493200,416379600,436942800,447829200, | ||
| 270 | + 468392400,479278800,499842000,510728400,531291600,542782800,563346000,574232400,594795600, | ||
| 271 | + 605682000,626245200,637131600,657694800,668581200,689144400,700030800, | ||
| 272 | + 720594000,5727600,26287200,37782000,58341600,69231600,89791200,100681200,121240800, | ||
| 273 | + 132130800,152690400,163580400,184140000,195634800,216194400,227084400, | ||
| 274 | + 247644000,258534000,279093600,289983600,310543200,321433200,341992800,352882800,373442400, | ||
| 275 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000, | ||
| 276 | + 499845600,510735600,531295200,542790000,563349600,574239600,594799200,605689200,626248800, | ||
| 277 | + 637138800,657698400,668588400,689148000,700038000,720597600,5731200, | ||
| 278 | + 26290800,37785600,58345200,69235200,89794800,100684800,121244400,132134400,152694000, | ||
| 279 | + 163584000,184143600,195638400,216198000,227088000,247647600,258537600, | ||
| 280 | + 279097200,289987200,310546800,321436800,341996400,352886400,373446000,384940800,405500400, | ||
| 281 | + 416390400,436950000,447840000,468399600,479289600,499849200,510739200, | ||
| 282 | + 531298800,542793600,563353200,574243200,594802800,605692800,626252400,637142400,657702000, | ||
| 283 | + 668592000,689151600,700041600,720601200,5727600,26287200,37782000, | ||
| 284 | + 58341600,69231600,89791200,100681200,121240800,132130800,152690400,163580400,184140000, | ||
| 285 | + 195634800,216194400,227084400,247644000,258534000,279093600,289983600, | ||
| 286 | + 310543200,321433200,341992800,352882800,373442400,384937200,405496800,416386800,436946400, | ||
| 287 | + 447836400,468396000,479286000,499845600,510735600,531295200,542790000, | ||
| 288 | + 563349600,574239600,594799200,605689200,626248800,637138800,657698400,668588400,689148000, | ||
| 289 | + 700038000,720597600,5727600,26287200,37782000,58341600,69231600, | ||
| 290 | + 89791200,100681200,121240800,132130800,152690400,163580400,184140000,195634800,216194400, | ||
| 291 | + 227084400,247644000,258534000,279093600,289983600,310543200,321433200, | ||
| 292 | + 341992800,352882800,373442400,384937200,405496800,416386800,436946400,447836400,468396000, | ||
| 293 | + 479286000,499845600,510735600,531295200,542790000,563349600,574239600, | ||
| 294 | + 594799200,605689200,626248800,637138800,657698400,668588400,689148000,700038000,720597600, | ||
| 295 | + 5731200,26290800,37785600,58345200,69235200,89794800,100684800, | ||
| 296 | + 121244400,132134400,152694000,163584000,184143600,195638400,216198000,227088000,247647600, | ||
| 297 | + 258537600,279097200,289987200,310546800,321436800,341996400,352886400, | ||
| 298 | + 373446000,384940800,405500400,416390400,436950000,447840000,468399600,479289600,499849200, | ||
| 299 | + 510739200,531298800,542793600,563353200,574243200,594802800,605692800, | ||
| 300 | + 626252400,637142400,657702000,668592000,689151600,700041600,720601200,5727600,26287200, | ||
| 301 | + 37782000,58341600,69231600,89791200,100681200,121240800,132130800, | ||
| 302 | + 152690400,163580400,184140000,195634800,216194400,227084400,247644000,258534000,279093600, | ||
| 303 | + 289983600,310543200,321433200,341992800,352882800,373442400,384937200, | ||
| 304 | + 405496800,416386800,436946400,447836400,468396000,479286000,499845600,510735600,531295200, | ||
| 305 | + 542790000,563349600,574239600,594799200,605689200,626248800,637138800, | ||
| 306 | + 657698400,668588400,689148000,700038000,720597600,5727600,26287200,37782000,58341600, | ||
| 307 | + 69231600,89791200,100681200,121240800,132130800,152690400,163580400, | ||
| 308 | + 184140000,195634800,216194400,227084400,247644000,258534000,279093600,289983600,310543200, | ||
| 309 | + 321433200,341992800,352882800,373442400,384937200,405496800,416386800, | ||
| 310 | + 436946400,447836400,468396000,479286000,499845600,510735600,531295200,542790000,563349600, | ||
| 311 | + 574239600,594799200,605689200,626248800,637138800,657698400,668588400, | ||
| 312 | + 689148000,700038000,720597600,5727600,26287200,37782000,58341600,69231600,89791200, | ||
| 313 | + 100681200,121240800,132130800,152690400,163580400,184140000,195634800, | ||
| 314 | + 216194400,227084400,247644000,258534000,279093600,289983600,310543200,321433200,341992800, | ||
| 315 | + 352882800,373442400,384937200,405496800,416386800,436946400,447836400, | ||
| 316 | + 468396000,479286000,499845600,510735600,531295200,542790000,563349600,574239600,594799200, | ||
| 317 | + 605689200,626248800,637138800,657698400,668588400,689148000,700038000, | ||
| 318 | + 720597600,5727600,26287200,37782000,58341600,69231600,89791200,100681200,121240800, | ||
| 319 | + 132130800,152690400,163580400,184140000,195634800,216194400,227084400, | ||
| 320 | + 247644000,258534000,279093600,289983600,310543200,321433200,341992800,352882800,373442400, | ||
| 321 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000, | ||
| 322 | + 499845600,510735600,531295200,542790000,563349600,574239600,594799200,605689200,626248800, | ||
| 323 | + 637138800,657698400,668588400,689148000,700038000,720597600,5734800, | ||
| 324 | + 26294400,37789200,58348800,69238800,89798400,100688400,121248000,132138000,152697600, | ||
| 325 | + 163587600,184147200,195642000,216201600,227091600,247651200,258541200, | ||
| 326 | + 279100800,289990800,310550400,321440400,342000000,352890000,373449600,384944400,405504000, | ||
| 327 | + 416394000,436953600,447843600,468403200,479293200,499852800,510742800, | ||
| 328 | + 531302400,542797200,563356800,574246800,594806400,605696400,626256000,637146000,657705600, | ||
| 329 | + 668595600,689155200,700045200,720604800,5727600,26287200,37782000, | ||
| 330 | + 58341600,69231600,89791200,100681200,121240800,132130800,152690400,163580400,184140000, | ||
| 331 | + 195634800,216194400,227084400,247644000,258534000,279093600,289983600, | ||
| 332 | + 310543200,321433200,341992800,352882800,373442400,384937200,405496800,416386800,436946400, | ||
| 333 | + 447836400,468396000,479286000,499845600,510735600,531295200,542790000, | ||
| 334 | + 563349600,574239600,594799200,605689200,626248800,637138800,657698400,668588400,689148000, | ||
| 335 | + 700038000,720597600,727413247,5742000,26301600,37796400,58356000, | ||
| 336 | + 69246000,89805600,100695600,121255200,132145200,152704800,163594800,184154400,195649200, | ||
| 337 | + 216208800,227098800,247658400,258548400,279108000,289998000,310557600, | ||
| 338 | + 321447600,342007200,352897200,373456800,384951600,405511200,416401200,436960800,447850800, | ||
| 339 | + 468410400,479300400,499860000,510750000,531309600,542804400,563364000, | ||
| 340 | + 574254000,594813600,605703600,626263200,637153200,657712800,668602800,689162400,700052400, | ||
| 341 | + 720612000,5727600,26287200,37782000,58341600,69231600,89791200, | ||
| 342 | + 100681200,121240800,132130800,152690400,163580400,184140000,195634800,216194400,227084400, | ||
| 343 | + 247644000,258534000,279093600,289983600,310543200,321433200,341992800, | ||
| 344 | + 352882800,373442400,384937200,405496800,416386800,436946400,447836400,468396000,479286000, | ||
| 345 | + 499845600,510735600,531295200,542790000,563349600,574239600,594799200, | ||
| 346 | + 605689200,626248800,637138800,657698400,668588400,689148000,700038000,720597600,5727600, | ||
| 347 | + 26287200,37782000,58341600,69231600,89791200,100681200,121240800, | ||
| 348 | + 132130800,152690400,163580400,184140000,195634800,216194400,227084400,247644000,258534000, | ||
| 349 | + 279093600,289983600,310543200,321433200,341992800,352882800,373442400, | ||
| 350 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000,499845600,510735600, | ||
| 351 | + 531295200,542790000,563349600,574239600,594799200,605689200,626248800, | ||
| 352 | + 637138800,657698400,668588400,689148000,700038000,720597600,5731200,26290800,37785600, | ||
| 353 | + 58345200,69235200,89794800,100684800,121244400,132134400,152694000, | ||
| 354 | + 163584000,184143600,195638400,216198000,227088000,247647600,258537600,279097200,289987200, | ||
| 355 | + 310546800,321436800,341996400,352886400,373446000,384940800,405500400, | ||
| 356 | + 416390400,436950000,447840000,468399600,479289600,499849200,510739200,531298800,542793600, | ||
| 357 | + 563353200,574243200,594802800,605692800,626252400,637142400,657702000, | ||
| 358 | + 668592000,689151600,700041600,720601200,727413247,727413247,5738400,26298000,37792800, | ||
| 359 | + 58352400,69242400,89802000,100692000,121251600,132141600,152701200, | ||
| 360 | + 163591200,184150800,195645600,216205200,227095200,247654800,258544800,279104400,289994400, | ||
| 361 | + 310554000,321444000,342003600,352893600,373453200,384948000,405507600, | ||
| 362 | + 416397600,436957200,447847200,468406800,479296800,499856400,510746400,531306000,542800800, | ||
| 363 | + 563360400,574250400,594810000,605700000,626259600,637149600,657709200, | ||
| 364 | + 668599200,689158800,700048800,720608400,5727600,26287200,37782000,58341600,69231600, | ||
| 365 | + 89791200,100681200,121240800,132130800,152690400,163580400,184140000, | ||
| 366 | + 195634800,216194400,227084400,247644000,258534000,279093600,289983600,310543200,321433200, | ||
| 367 | + 341992800,352882800,373442400,384937200,405496800,416386800,436946400, | ||
| 368 | + 447836400,468396000,479286000,499845600,510735600,531295200,542790000,563349600,574239600, | ||
| 369 | + 594799200,605689200,626248800,637138800,657698400,668588400,689148000, | ||
| 370 | + 700038000,720597600,727413247,727413247,5731200,26290800,37785600,58345200,69235200, | ||
| 371 | + 89794800,100684800,121244400,132134400,152694000,163584000,184143600, | ||
| 372 | + 195638400,216198000,227088000,247647600,258537600,279097200,289987200,310546800,321436800, | ||
| 373 | + 341996400,352886400,373446000,384940800,405500400,416390400,436950000, | ||
| 374 | + 447840000,468399600,479289600,499849200,510739200,531298800,542793600,563353200,574243200, | ||
| 375 | + 594802800,605692800,626252400,637142400,657702000,668592000,689151600, | ||
| 376 | + 700041600,720601200,8154000,25689600,39603600,57744000,71053200,89193600,102502800, | ||
| 377 | + 120643200,134557200,152092800,166006800,183542400,197456400,215596800, | ||
| 378 | + 228906000,247046400,727413247,5731200,26290800,37785600,58345200,69235200,89794800, | ||
| 379 | + 100684800,121244400,132134400,152694000,163584000,184143600,195638400, | ||
| 380 | + 216198000,227088000,247647600,258537600,279097200,289987200,310546800,321436800,341996400, | ||
| 381 | + 352886400,373446000,384940800,405500400,416390400,436950000,447840000, | ||
| 382 | + 468399600,479289600,499849200,510739200,531298800,542793600,563353200,574243200,594802800, | ||
| 383 | + 605692800,626252400,637142400,657702000,668592000,689151600,700041600, | ||
| 384 | + 720601200,8150400,25686000,39600000,57740400,71049600,89190000,102499200,120639600, | ||
| 385 | + 134553600,152089200,166003200,183538800,197452800,215593200,228902400, | ||
| 386 | + 247042800,26301600,37796400,58356000,69246000,89805600,100695600,121255200,127908000, | ||
| 387 | + 132145200,152704800,163594800,184154400,195649200,216208800,227098800, | ||
| 388 | + 247658400,258548400,279108000,289998000,310557600,321447600,342007200,352897200,373456800, | ||
| 389 | + 384951600,405511200,416401200,436960800,447850800,468410400,479300400, | ||
| 390 | + 499860000,510750000,531309600,542804400,563364000,574254000,594813600,605703600,626263200, | ||
| 391 | + 637153200,657712800,668602800,689162400,700052400,720612000,8150400, | ||
| 392 | + 25686000,39600000,57740400,71049600,89190000,102499200,120639600,134553600,152089200, | ||
| 393 | + 166003200,183538800,197452800,215593200,228902400,247042800,5720400, | ||
| 394 | + 26280000,37774800,58334400,69224400,89784000,100674000,121233600,132123600,152683200, | ||
| 395 | + 163573200,184132800,195627600,216187200,227077200,247636800,258526800, | ||
| 396 | + 279086400,289976400,310536000,321426000,341985600,352875600,373435200,384930000,405489600, | ||
| 397 | + 416379600,436939200,447829200,468388800,479278800,499838400,510728400, | ||
| 398 | + 531288000,542782800,563342400,574232400,594792000,605682000,626241600,637131600,657691200, | ||
| 399 | + 668581200,689140800,700030800,720590400,727413247,5724000,26283600, | ||
| 400 | + 37778400,58338000,69228000,89787600,100677600,121237200,132127200,152686800,163576800, | ||
| 401 | + 184136400,195631200,216190800,227080800,247640400,258530400,279090000, | ||
| 402 | + 289980000,310539600,321429600,341989200,352879200,373438800,384933600,405493200,416383200, | ||
| 403 | + 436942800,447832800,468392400,479282400,499842000,510732000,531291600, | ||
| 404 | + 542786400,563346000,574236000,594795600,605685600,626245200,637135200,657694800,668584800, | ||
| 405 | + 689144400,700034400,720594000,8150400,25686000,39600000,57740400, | ||
| 406 | + 71049600,89190000,102499200,120639600,134553600,152089200,166003200,183538800,197452800, | ||
| 407 | + 215593200,228902400,247042800,5716800,727413247,5727600,26287200, | ||
| 408 | + 37782000,58341600,69231600,89791200,100681200,121240800,132130800,152690400,163580400, | ||
| 409 | + 184140000,195634800,216194400,227084400,247644000,258534000,279093600, | ||
| 410 | + 289983600,310543200,321433200,341992800,352882800,373442400,384937200,405496800,416386800, | ||
| 411 | + 436946400,447836400,468396000,479286000,499845600,510735600,531295200, | ||
| 412 | + 542790000,563349600,574239600,594799200,605689200,626248800,637138800,657698400,668588400, | ||
| 413 | + 689148000,700038000,720597600,5727600,26287200,37782000,58341600, | ||
| 414 | + 69231600,89791200,100681200,121240800,132130800,152690400,163580400,184140000,195634800, | ||
| 415 | + 216194400,227084400,247644000,258534000,279093600,289983600,310543200, | ||
| 416 | + 321433200,341992800,352882800,373442400,384937200,405496800,416386800,436946400,447836400, | ||
| 417 | + 468396000,479286000,499845600,510735600,531295200,542790000,563349600, | ||
| 418 | + 574239600,594799200,605689200,626248800,637138800,657698400,668588400,689148000,700038000, | ||
| 419 | + 720597600,5727600,26287200,37782000,58341600,69231600,89791200, | ||
| 420 | + 100681200,121240800,132130800,152690400,163580400,184140000,195634800,216194400,227084400, | ||
| 421 | + 247644000,258534000,279093600,289983600,310543200,321433200,341992800, | ||
| 422 | + 352882800,373442400,384937200,405496800,416386800,436946400,447836400,468396000,479286000, | ||
| 423 | + 499845600,510735600,531295200,542790000,563349600,574239600,594799200, | ||
| 424 | + 605689200,626248800,637138800,657698400,668588400,689148000,700038000,720597600,5727600, | ||
| 425 | + 26287200,37782000,58341600,69231600,89791200,100681200,121240800, | ||
| 426 | + 132130800,152690400,163580400,184140000,195634800,216194400,227084400,247644000,258534000, | ||
| 427 | + 279093600,289983600,310543200,321433200,341992800,352882800,373442400, | ||
| 428 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000,499845600,510735600, | ||
| 429 | + 531295200,542790000,563349600,574239600,594799200,605689200,626248800, | ||
| 430 | + 637138800,657698400,668588400,689148000,700038000,720597600,5742000,26301600,37796400, | ||
| 431 | + 58356000,69246000,89805600,100695600,121255200,132145200,152704800, | ||
| 432 | + 163594800,184154400,195649200,216208800,227098800,247658400,258548400,279108000,289998000, | ||
| 433 | + 310557600,321447600,342007200,352897200,373456800,384951600,405511200, | ||
| 434 | + 416401200,436960800,447850800,468410400,479300400,499860000,510750000,531309600,542804400, | ||
| 435 | + 563364000,574254000,594813600,605703600,626263200,637153200,657712800, | ||
| 436 | + 668602800,689162400,700052400,720612000,727413247,5731200,26290800,37785600,58345200, | ||
| 437 | + 69235200,89794800,100684800,121244400,132134400,152694000,163584000, | ||
| 438 | + 184143600,195638400,216198000,227088000,247647600,258537600,279097200,289987200,310546800, | ||
| 439 | + 321436800,341996400,352886400,373446000,384940800,405500400,416390400, | ||
| 440 | + 436950000,447840000,468399600,479289600,499849200,510739200,531298800,542793600,563353200, | ||
| 441 | + 574243200,594802800,605692800,626252400,637142400,657702000,668592000, | ||
| 442 | + 689151600,700041600,720601200,5731200,26290800,37785600,58345200,69235200,89794800, | ||
| 443 | + 100684800,121244400,132134400,152694000,163584000,184143600,195638400, | ||
| 444 | + 216198000,227088000,247647600,258537600,279097200,289987200,310546800,321436800,341996400, | ||
| 445 | + 352886400,373446000,384940800,405500400,416390400,436950000,447840000, | ||
| 446 | + 468399600,479289600,499849200,510739200,531298800,542793600,563353200,574243200,594802800, | ||
| 447 | + 605692800,626252400,637142400,657702000,668592000,689151600,700041600, | ||
| 448 | + 720601200,5731200,26290800,37785600,58345200,69235200,89794800,100684800,121244400, | ||
| 449 | + 132134400,152694000,163584000,184143600,195638400,216198000,227088000, | ||
| 450 | + 247647600,258537600,279097200,289987200,310546800,321436800,341996400,352886400,373446000, | ||
| 451 | + 384940800,405500400,416390400,436950000,447840000,468399600,479289600, | ||
| 452 | + 499849200,510739200,531298800,542793600,563353200,574243200,594802800,605692800,626252400, | ||
| 453 | + 637142400,657702000,668592000,689151600,700041600,720601200,7520400, | ||
| 454 | + 25664400,38970000,57718800,70419600,89168400,101869200,120618000,133923600,152067600, | ||
| 455 | + 165373200,183517200,196822800,215571600,228272400,247021200,259722000, | ||
| 456 | + 291776400,309920400,323226000,341370000,354675600,372819600,386125200,404874000,417574800, | ||
| 457 | + 436323600,449024400,467773200,481078800,499222800,512528400,530672400, | ||
| 458 | + 543978000,562726800,575427600,594176400,606877200,625626000,638326800,657075600,670381200, | ||
| 459 | + 688525200,701830800,719974800,727413247,5734800,26294400,37789200, | ||
| 460 | + 58348800,69238800,89798400,100688400,121248000,132138000,152697600,163587600,184147200, | ||
| 461 | + 195642000,216201600,227091600,247046400,258537600,279097200,289987200, | ||
| 462 | + 310546800,321436800,341996400,352886400,373446000,384940800,405500400,416390400,436950000, | ||
| 463 | + 447840000,468399600,479289600,499849200,510739200,531298800,542793600, | ||
| 464 | + 563353200,574243200,594802800,605692800,626252400,637142400,657702000,668592000,689151600, | ||
| 465 | + 700041600,720601200,5727600,26287200,37782000,58341600,69231600, | ||
| 466 | + 89791200,100681200,121240800,132130800,152690400,163580400,184140000,195634800,216194400, | ||
| 467 | + 227084400,247644000,258534000,279093600,289983600,310543200,321433200, | ||
| 468 | + 341992800,352882800,373442400,384937200,405496800,416386800,436946400,447836400,468396000, | ||
| 469 | + 479286000,499845600,510735600,531295200,542790000,563349600,574239600, | ||
| 470 | + 594799200,605689200,626248800,637138800,657698400,668588400,689148000,700038000,720597600, | ||
| 471 | + 727413247,5727600,26287200,69231600,89791200,100681200,121240800, | ||
| 472 | + 132130800,152690400,163580400,184140000,195634800,216194400,227084400,247644000,258534000, | ||
| 473 | + 279093600,289983600,310543200,321433200,341992800,352882800,373442400, | ||
| 474 | + 384937200,405496800,416386800,436946400,447836400,468396000,479286000,499845600,510735600, | ||
| 475 | + 531295200,542790000,563349600,574239600,594799200,605689200,626248800, | ||
| 476 | + 637138800,657698400,668588400,689148000,700038000,720597600,727413247,727413247,43210800, | ||
| 477 | + 51076800,60750000,727413247,5731200,26290800,37785600,58345200, | ||
| 478 | + 69235200,89794800,100684800,121244400,132134400,152694000,163584000,184143600,195638400, | ||
| 479 | + 216198000,227088000,247647600,258537600,279097200,289987200,310546800, | ||
| 480 | + 321436800,341996400,352886400,373446000,384940800,405500400,416390400,436950000,447840000, | ||
| 481 | + 468399600,479289600,499849200,510739200,531298800,542793600,563353200, | ||
| 482 | + 574243200,594802800,605692800,626252400,637142400,657702000,668592000,689151600,700041600, | ||
| 483 | + 720601200,5731200,26290800,37785600,58345200,69235200,89794800, | ||
| 484 | + 100684800,121244400,132134400,152694000,163584000,184143600,195638400,216198000,227088000, | ||
| 485 | + 247647600,258537600,279097200,289987200,310546800,321436800,341996400, | ||
| 486 | + 352886400,373446000,384940800,405500400,416390400,436950000,447840000,468399600,479289600, | ||
| 487 | + 499849200,510739200,531298800,542793600,563353200,574243200,594802800, | ||
| 488 | + 605692800,626252400,637142400,657702000,668592000,689151600,700041600,720601200,727413247, | ||
| 489 | + 5731200,26290800,37785600,58345200,69235200,89794800,100684800, | ||
| 490 | + 121244400,132134400,152694000,163584000,184143600,195638400,216198000,227088000,247647600, | ||
| 491 | + 258537600,279097200,289987200,310546800,321436800,341996400,352886400, | ||
| 492 | + 373446000,384940800,405500400,416390400,436950000,447840000,468399600,479289600,499849200, | ||
| 493 | + 510739200,531298800,542793600,563353200,574243200,594802800,605692800, | ||
| 494 | + 626252400,637142400,657702000,668592000,689151600,700041600,720601200,727413247,727413247, | ||
| 495 | + 5738400,26298000,37792800,58352400,69242400,89802000,100692000, | ||
| 496 | + 121251600,132141600,152701200,163591200,184150800,195645600,216205200,227095200,247654800, | ||
| 497 | + 258544800,279104400,289994400,310554000,321444000,342003600,352893600, | ||
| 498 | + 373453200,384948000,405507600,416397600,436957200,447847200,468406800,479296800,499856400, | ||
| 499 | + 510746400,531306000,542800800,563360400,574250400,594810000,605700000, | ||
| 500 | + 626259600,637149600,657709200,668599200,689158800,700048800,720608400,727413247,43210800, | ||
| 501 | + 51076800,74660400,82526400,106110000,113976000,134535600,147844800, | ||
| 502 | + 165985200,179294400,197434800,210744000,228884400,242798400,260334000,273643200,292388400, | ||
| 503 | + 305697600,323838000,337147200,355287600,368596800,386737200,400046400, | ||
| 504 | + 418186800,431496000,450241200,462945600,481690800,495000000,513140400,526449600,544590000, | ||
| 505 | + 557899200,576039600,589348800,607489200,620798400,639543600,652248000, | ||
| 506 | + 670993200,684302400,702442800,715752000,727413247,4500000,25066800,35949600,56516400, | ||
| 507 | + 67399200,87966000,98848800,121230000,130298400,727413247,7520400, | ||
| 508 | + 25664400,38970000,57718800,70419600,89168400,101869200,120618000,133923600,152067600, | ||
| 509 | + 165373200,183517200,196822800,215571600,228272400,247021200,259722000, | ||
| 510 | + 278470800,291776400,309920400,323226000,341370000,354675600,372819600,386125200,404874000, | ||
| 511 | + 417574800,436323600,449024400,467773200,481078800,499222800,512528400, | ||
| 512 | + 530672400,543978000,562726800,575427600,594176400,606877200,625626000,638326800,657075600, | ||
| 513 | + 670381200,688525200,701830800,719974800,727413247,5734800,26294400, | ||
| 514 | + 37789200,58348800,69238800,89798400,100688400,121248000,132138000,152697600,163587600, | ||
| 515 | + 184147200,195642000,216201600,227091600,247651200,258541200,279100800, | ||
| 516 | + 289990800,310550400,321440400,342000000,352890000,373449600,384944400,405504000,416394000, | ||
| 517 | + 436953600,447843600,468403200,479293200,499852800,510742800,531302400, | ||
| 518 | + 542797200,563356800,574246800,594806400,605696400,626256000,637146000,657705600,668595600, | ||
| 519 | + 689155200,700045200,720604800,5742000,26301600,37796400,58356000, | ||
| 520 | + 69246000,89805600,100695600,121255200,132145200,152704800,163594800,184154400,195649200, | ||
| 521 | + 216208800,227098800,247658400,258548400,279108000,289998000,310557600, | ||
| 522 | + 321447600,342007200,352897200,373456800,384951600,405511200,416401200,436960800,447850800, | ||
| 523 | + 468410400,479300400,499860000,510750000,531309600,542804400,563364000, | ||
| 524 | + 574254000,594813600,605703600,626263200,637153200,657712800,668602800,689162400,700052400, | ||
| 525 | + 720612000,5722200,26281800,37776600,58336200,69226200,89785800, | ||
| 526 | + 100675800,121235400,132125400,152685000,163575000,184134600,195629400,216189000,227079000, | ||
| 527 | + 247638600,258528600,279088200,289978200,310537800,321427800,341987400, | ||
| 528 | + 352877400,373437000,384931800,405491400,416381400,436941000,447831000,468390600,479280600, | ||
| 529 | + 499840200,510730200,531289800,542784600,563344200,574234200,594793800, | ||
| 530 | + 605683800,626243400,637133400,657693000,668583000,689142600,700032600,720592200,5724000, | ||
| 531 | + 26283600,37778400,58338000,69228000,89787600,100677600,121237200, | ||
| 532 | + 132127200,152686800,163576800,184136400,195631200,216190800,227080800,247640400,258530400, | ||
| 533 | + 279090000,289980000,310539600,321429600,341989200,352879200,373438800, | ||
| 534 | + 384933600,405493200,416383200,436942800,447832800,468392400,479282400,499842000,510732000, | ||
| 535 | + 531291600,542786400,563346000,574236000,594795600,605685600,626245200, | ||
| 536 | + 637135200,657694800,668584800,689144400,700034400,720594000,5727600,26287200,37782000, | ||
| 537 | + 58341600,69231600,89791200,100681200,121240800,132130800,152690400, | ||
| 538 | + 163580400,184140000,195634800,216194400,227084400,247644000,258534000,279093600,289983600, | ||
| 539 | + 310543200,321433200,341992800,352882800,373442400,384937200,405496800, | ||
| 540 | + 416386800,436946400,447836400,468396000,479286000,499845600,510735600,531295200,542790000, | ||
| 541 | + 563349600,574239600,594799200,605689200,626248800,637138800,657698400, | ||
| 542 | + 668588400,689148000,700038000,720597600,5738400,26298000,37792800,58352400,69242400, | ||
| 543 | + 89802000,100692000,121251600,132141600,152701200,163591200,184150800, | ||
| 544 | + 195645600,216205200,227095200,247654800,258544800,279104400,289994400,310554000,321444000, | ||
| 545 | + 342003600,352893600,373453200,384948000,405507600,416397600,436957200, | ||
| 546 | + 447847200,468406800,479296800,499856400,510746400,531306000,542800800,563360400,574250400, | ||
| 547 | + 594810000,605700000,626259600,637149600,657709200,668599200,689158800, | ||
| 548 | + 700048800,720608400,5727600,26287200,37782000,58341600,69231600,89791200,100681200, | ||
| 549 | + 121240800,132130800,152690400,163580400,184140000,195634800,216194400, | ||
| 550 | + 227084400,247644000,258534000,279093600,289983600,310543200,321433200,341992800,352882800, | ||
| 551 | + 373442400,384937200,405496800,416386800,436946400,447836400,468396000, | ||
| 552 | + 479286000,499845600,510735600,531295200,542790000,563349600,574239600,594799200,605689200, | ||
| 553 | + 626248800,637138800,657698400,668588400,689148000,700038000,720597600, | ||
| 554 | + 5738400,26298000,37792800,58352400,69242400,89802000,100692000,121251600,132141600, | ||
| 555 | + 152701200,163591200,184150800,195645600,216205200,227095200,247654800, | ||
| 556 | + 258544800,279104400,289994400,310554000,321444000,342003600,352893600,373453200,384948000, | ||
| 557 | + 405507600,416397600,436957200,447847200,468406800,479296800,499856400, | ||
| 558 | + 510746400,531306000,542800800,563360400,574250400,594810000,605700000,626259600,637149600, | ||
| 559 | + 657709200,668599200,689158800,700048800,720608400,5738400,26298000, | ||
| 560 | + 37792800,58352400,69242400,89802000,100692000,121251600,132141600,152701200,163591200, | ||
| 561 | + 184143600,5731200,26290800,37785600,58345200,69235200,89794800, | ||
| 562 | + 100684800,121244400,132134400,152694000,163584000,184143600,195638400,216198000,227088000, | ||
| 563 | + 247647600,258537600,279097200,289987200,310546800,321436800,341996400, | ||
| 564 | + 352886400,373446000,384940800,405500400,416390400,436950000,447840000,468399600,479289600, | ||
| 565 | + 499849200,510739200,531298800,542793600,563353200,574243200,594802800, | ||
| 566 | + 605692800,626252400,637142400,657702000,668592000,689151600,700041600,720601200,5742000, | ||
| 567 | + 26301600,37796400,58356000,69246000,89805600,100695600,121255200, | ||
| 568 | + 132145200,152704800,163594800,184154400,195649200,216208800,227098800,247658400,258548400, | ||
| 569 | + 279108000,289998000,310557600,321447600,342007200,352897200,373456800, | ||
| 570 | + 384951600,405511200,416401200,436960800,447850800,468410400,479300400,499860000,510750000, | ||
| 571 | + 531309600,542804400,563364000,574254000,594813600,605703600,626263200, | ||
| 572 | + 637153200,657712800,668602800,689162400,700052400,720612000,5734800,26294400,37789200, | ||
| 573 | + 58348800,69238800,89798400,100688400,121248000,132138000,152697600, | ||
| 574 | + 163587600,184147200,195642000,216201600,227091600,247651200,258541200,279100800,289990800, | ||
| 575 | + 310550400,321440400,342000000,352890000,373449600,384944400,405504000, | ||
| 576 | + 416394000,436953600,447843600,468403200,479293200,499852800,510742800,531302400,542797200, | ||
| 577 | + 563356800,574246800,594806400,605696400,626256000,637146000,657705600, | ||
| 578 | + 668595600,689155200,700045200,720604800,56995200,100630800,118785600,132681600,150058800, | ||
| 579 | + 163526400,181670460,195570000,213120060,227019600,244569660,258220800, | ||
| 580 | + 727413247,727413247,727413247,8092800,23817600,39542400,55267200,70992000,86716800, | ||
| 581 | + 102441600,118771200,134496000,150220800,165945600,181670400,197395200, | ||
| 582 | + 213120000,228844800,244569600,260294400,276019200,292348800,308073600,323798400,339523200, | ||
| 583 | + 355248000,370972800,386697600,402422400,418147200,433872000,449596800, | ||
| 584 | + 465926400,481651200,497376000,513100800,528825600,544550400,560275200,576000000,591724800, | ||
| 585 | + 607449600,623174400,638899200,655228800,670953600,686678400,702403200, | ||
| 586 | + 718128000,727413247,8085600,23205600,39535200,54655200,70984800,86104800,102434400, | ||
| 587 | + 118159200,134488800,149608800,165938400,181058400,197388000,212508000, | ||
| 588 | + 228837600,243957600,260287200,275407200,292341600,307461600,323791200,338911200,355240800, | ||
| 589 | + 370360800,386690400,401810400,418140000,433260000,449589600,465314400, | ||
| 590 | + 481644000,496764000,513093600,528213600,544543200,559663200,575992800,591112800,607442400, | ||
| 591 | + 622562400,638892000,654616800,670946400,686066400,702396000,717516000, | ||
| 592 | + 43210800,51076800,60750000,727413247,727413247,8085600,23205600,39535200,54655200,70984800, | ||
| 593 | + 86104800,102434400,118159200,134488800,149608800,165938400, | ||
| 594 | + 181058400,197388000,212508000,228837600,243957600,260287200,275407200,292341600,307461600, | ||
| 595 | + 323791200,338911200,355240800,370360800,386690400,401810400,418140000, | ||
| 596 | + 433260000,449589600,465314400,481644000,496764000,513093600,528213600,544543200,559663200, | ||
| 597 | + 575992800,591112800,607442400,622562400,638892000,654616800,670946400, | ||
| 598 | + 686066400,702396000,717516000,727413247,7520400,25664400,38970000,57718800,70419600, | ||
| 599 | + 89168400,101869200,120618000,133923600,152067600,165373200,183517200, | ||
| 600 | + 196822800,215571600,228272400,247021200,259722000,278470800,291776400,309920400,323226000, | ||
| 601 | + 341370000,354675600,372819600,386125200,404874000,417574800,436323600, | ||
| 602 | + 449024400,467773200,481078800,499222800,512528400,530672400,543978000,562726800,575427600, | ||
| 603 | + 594176400,606877200,625626000,638326800,657075600,670381200,688525200, | ||
| 604 | + 701830800,719974800,727413247,282769200,727413247,7520400,25664400,38970000,57718800, | ||
| 605 | + 70419600,89168400,101869200,120618000,133923600,152067600,165373200, | ||
| 606 | + 183517200,196822800,215571600,228272400,247021200,259722000,278470800,291776400,309920400, | ||
| 607 | + 323226000,341370000,354675600,372819600,386125200,404874000,417574800, | ||
| 608 | + 436323600,449024400,467773200,481078800,499222800,512528400,530672400,543978000,562726800, | ||
| 609 | + 575427600,594176400,606877200,625626000,638326800,657075600,670381200, | ||
| 610 | + 688525200,701830800,719974800,727413247,289159200,727413247,7336800,26085600,39391200, | ||
| 611 | + 57535200,70840800,88984800,102290400,120434400,133740000,151884000, | ||
| 612 | + 165189600,183938400,196639200,215388000,225669600,246837600,727413247,727413247,727413247, | ||
| 613 | + 727413247,727413247,727413247,727413247,727413247,727413247,7516800, | ||
| 614 | + 25660800,727413247,727413247,38952000,727413247,7509600,25650000,38959200,57704400,70408800, | ||
| 615 | + 89154000,101858400,120603600,133912800,152053200,165362400, | ||
| 616 | + 183502800,196812000,215557200,228261600,247006800,259711200,278456400,291765600,309906000, | ||
| 617 | + 323215200,341355600,354664800,372805200,386114400,404859600,417564000, | ||
| 618 | + 436309200,449013600,467758800,481068000,499208400,512517600,530658000,543967200,562712400, | ||
| 619 | + 575416800,594162000,606866400,625611600,638316000,657061200,670370400, | ||
| 620 | + 688510800,701820000,719960400,727413247,727413247,38944800,727413247,7408800,23122800, | ||
| 621 | + 38858400,54572400,727413247,727413247,727413247,7336800,26082000, | ||
| 622 | + 38786400,57531600,70840800,88981200,102290400,120430800,133740000,151880400,165189600, | ||
| 623 | + 183934800,196639200,215384400,228088800,246834000,727413247,727413247, | ||
| 624 | + 727413247,727413247,727413247,7520400,25664400,38970000,53211600,89168400,101869200, | ||
| 625 | + 120618000,133923600,152067600,165373200,183517200,196822800,215571600, | ||
| 626 | + 228272400,247021200,259722000,278470800,291776400,309920400,323226000,341370000,354675600, | ||
| 627 | + 372819600,386125200,404874000,417574800,436323600,449024400,467773200, | ||
| 628 | + 481078800,499222800,512528400,530672400,543978000,562726800,575427600,594176400,606877200, | ||
| 629 | + 625626000,638326800,657075600,670381200,688525200,701830800,719974800, | ||
| 630 | + 7423200,25480800,38876400,57621600,70326000,89071200,101775600,120520800,133740000, | ||
| 631 | + 151966800,165276000,183420000,196725600,215388000,228261600,246927600, | ||
| 632 | + 262656000,278377200,293500800,309826800,324345600,341276400,354585600,372726000,386035200, | ||
| 633 | + 404780400,417484800,436230000,448934400,467679600,480988800,499129200, | ||
| 634 | + 512438400,530578800,543888000,562633200,575337600,594082800,606787200,625532400,638236800, | ||
| 635 | + 656982000,670291200,687826800,701740800,718671600,733190400,748911600, | ||
| 636 | + 764640000,779756400,796089600,809996400,814233600,814834800,828144000,840841200,844473600, | ||
| 637 | + 846284400,859593600,871686000,875318400,877734000,891043200,901926000, | ||
| 638 | + 906163200,909183600,922492800,932770800,936403200,941238000,953942400,963615600,967248000, | ||
| 639 | + 972687600,985392000,993855600,998092800,1004137200,1017446400,1024700400, | ||
| 640 | + 1028332800,1035586800,1048896000,1054940400,1059177600,1067036400,1080345600,1085785200, | ||
| 641 | + 1089417600,1099090800,1111795200,1116630000,1120262400,1130540400,1143244800,1146870000, | ||
| 642 | + 1151107200,1161990000,1175299200,1177714800,1181347200,1193439600,1206748800,1207954800, | ||
| 643 | + 1212192000,1224889200,1238198400,1238799600,1243036800,1256338800,1273276800,1288393200, | ||
| 644 | + 1304121600,1319842800,1334361600,1351292400,1365206400,1382742000,1396051200,1414191600, | ||
| 645 | + 1427500800,1446246000,1458950400,1477695600,1490400000,1509145200,1521849600,1540594800, | ||
| 646 | + 1553904000,1572044400,1585353600,1603494000,1616803200,1635548400,1648252800,1666998000, | ||
| 647 | + 1679702400,1697842800,1711756800,1728687600,1743206400,1759532400,1774656000,1789772400, | ||
| 648 | + 1806105600,1820617200,1824249600,1824850800,1837555200,1851462000,1855094400,1856300400, | ||
| 649 | + 1869004800,1881702000,1885939200,1887750000,1901059200,1912546800,1916179200,1919199600, | ||
| 650 | + 1932508800,1942786800,1947024000,1950649200,1963958400,1973631600,1977868800,1982703600, | ||
| 651 | + 1995408000,2004476400,2008108800,2014153200,2026857600,2034716400,2038953600,2045602800, | ||
| 652 | + 2058912000,2065561200,2069193600,2077052400,2090361600,2096406000,2100038400,2108502000, | ||
| 653 | + 2121811200,2126646000,2130883200,2139951600,2153260800,2157490800,2161123200,2172006000, | ||
| 654 | + 2184710400,2187730800,2191968000,2203455600,2216160000,2218575600,2222812800,2234905200, | ||
| 655 | + 2248214400,2249420400,2253052800,2266354800,7423200,25480800,38876400,57621600, | ||
| 656 | + 70326000,89071200,101775600,120520800,133740000,151966800,165276000,183420000,196725600, | ||
| 657 | + 215388000,228261600,246927600,262656000,278377200,293500800,309826800, | ||
| 658 | + 324345600,341276400,354585600,372726000,386035200,404780400,417484800,436230000,448934400, | ||
| 659 | + 467679600,480988800,499129200,512438400,530578800,543888000,562633200, | ||
| 660 | + 575337600,594082800,606787200,625532400,638236800,656982000,670291200,687826800,701740800, | ||
| 661 | + 718671600,733190400,748911600,764640000,779756400,796089600,809996400, | ||
| 662 | + 814233600,814834800,828144000,840841200,844473600,846284400,859593600,871686000,875318400, | ||
| 663 | + 877734000,891043200,901926000,906163200,909183600,922492800,932770800, | ||
| 664 | + 936403200,941238000,953942400,963615600,967248000,972687600,985392000,993855600,998092800, | ||
| 665 | + 1004137200,1017446400,1024700400,1028332800,1035586800,1048896000,1054940400, | ||
| 666 | + 1059177600,1067036400,1080345600,1085785200,1089417600,1099090800,1111795200,1116630000, | ||
| 667 | + 1120262400,1130540400,1143244800,1146870000,1151107200,1161990000,1175299200,1177714800, | ||
| 668 | + 1181347200,1193439600,1206748800,1207954800,1212192000,1224889200,1238198400,1238799600, | ||
| 669 | + 1243036800,1256338800,1273276800,1288393200,1304121600,1319842800,1334361600,1351292400, | ||
| 670 | + 1365206400,1382742000,1396051200,1414191600,1427500800,1446246000,1458950400,1477695600, | ||
| 671 | + 1490400000,1509145200,1521849600,1540594800,1553904000,1572044400,1585353600,1603494000, | ||
| 672 | + 1616803200,1635548400,1648252800,1666998000,1679702400,1697842800,1711756800,1728687600, | ||
| 673 | + 1743206400,1759532400,1774656000,1789772400,1806105600,1820617200,1824249600,1824850800, | ||
| 674 | + 1837555200,1851462000,1855094400,1856300400,1869004800,1881702000,1885939200,1887750000, | ||
| 675 | + 1901059200,1912546800,1916179200,1919199600,1932508800,1942786800,1947024000,1950649200, | ||
| 676 | + 1963958400,1973631600,1977868800,1982703600,1995408000,2004476400,2008108800,2014153200, | ||
| 677 | + 2026857600,2034716400,2038953600,2045602800,2058912000,2065561200,2069193600,2077052400, | ||
| 678 | + 2090361600,2096406000,2100038400,2108502000,2121811200,2126646000,2130883200,2139951600, | ||
| 679 | + 2153260800,2157490800,2161123200,2172006000,2184710400,2187730800,2191968000,2203455600, | ||
| 680 | + 2216160000,2218575600,2222812800,2234905200,2248214400,2249420400,2253052800,2266354800, | ||
| 681 | + 727413247,7412400,23126400,38862000,54576000,727413247,727413247,7520400, | ||
| 682 | + 26874000,38970000,53125200,727413247,7344000,25657200,38793600,57711600,70243200,89161200, | ||
| 683 | + 101692800,120610800,133747200,152060400,165196800,183510000, | ||
| 684 | + 196646400,215564400,228096000,247014000,259545600,278463600,291600000,309913200,323049600, | ||
| 685 | + 341362800,354499200,372812400,385948800,404866800,417398400,436316400, | ||
| 686 | + 448848000,467766000,480902400,499215600,512352000,530665200,543801600,562719600,575251200, | ||
| 687 | + 594169200,606700800,625618800,638150400,657068400,670204800,688518000, | ||
| 688 | + 701654400,719967600,727413247,727413247,727413247,727413247,727413247,727413247,727413247, | ||
| 689 | + 727413247,727413247,727413247,41356800,727413247,727413247,7520400, | ||
| 690 | + 25664400,38970000,57718800,70419600,89168400,101869200,120618000,133923600,152067600, | ||
| 691 | + 165373200,183517200,196822800,215571600,228272400,247021200,259722000, | ||
| 692 | + 278470800,291776400,309920400,323226000,341370000,354675600,372819600,386125200,404874000, | ||
| 693 | + 417574800,436323600,449024400,467773200,481078800,499222800,512528400, | ||
| 694 | + 530672400,543978000,562726800,575427600,594176400,606877200,625626000,638326800,657075600, | ||
| 695 | + 670381200,688525200,701830800,719974800,727413247,49233600,727413247, | ||
| 696 | + 727413247,727413247,727413247,19494000,105375600,727413247,289159200,727413247,125258400, | ||
| 697 | + 727413247,727413247,727413247,727413247,38937600,727413247,727413247, | ||
| 698 | + 727413247,727413247,727413247,727413247,6899400,22793400,38435400,54329400,70057800, | ||
| 699 | + 85951800,101593800,117487800,133129800,149023800,164665800,180559800, | ||
| 700 | + 196288200,212182200,227824200,243718200,727413247,7344000,25657200,38793600,57711600, | ||
| 701 | + 70243200,89161200,101692800,120610800,133747200,152060400,165196800, | ||
| 702 | + 183510000,196646400,215564400,228096000,247014000,259545600,278463600,291600000,309913200, | ||
| 703 | + 323049600,341362800,354499200,372812400,385948800,404866800,417398400, | ||
| 704 | + 436316400,448848000,467766000,480902400,499215600,512352000,530665200,543801600,562719600, | ||
| 705 | + 575251200,594169200,606700800,625618800,638150400,657068400,670204800, | ||
| 706 | + 688518000,701654400,719967600,727413247,727413247,44395200,727413247,7408800,23122800, | ||
| 707 | + 38858400,54572400,727413247,7408800,23122800,38858400,54572400, | ||
| 708 | + 727413247,727413247,727413247,727413247,727413247,727413247,727413247,727413247,727413247, | ||
| 709 | + 7520400,25664400,38970000,57718800,70419600,89168400,101869200, | ||
| 710 | + 120618000,133923600,152067600,165373200,183517200,196822800,215571600,228272400,247021200, | ||
| 711 | + 259722000,278470800,291776400,309920400,323226000,341370000,354675600, | ||
| 712 | + 372819600,386125200,404874000,417574800,436323600,449024400,467773200,481078800,499222800, | ||
| 713 | + 512528400,530672400,543978000,562726800,575427600,594176400,606877200, | ||
| 714 | + 625626000,638326800,657075600,670381200,688525200,701830800,719974800,727413247,5724000, | ||
| 715 | + 26283600,37778400,58338000,69228000,89787600,100677600,121237200, | ||
| 716 | + 132127200,152686800,163576800,184136400,195631200,216190800,227080800,247640400,258530400, | ||
| 717 | + 279090000,289980000,310539600,321429600,341989200,352879200,373438800, | ||
| 718 | + 384933600,405493200,416383200,436942800,447832800,468392400,479282400,499842000,510732000, | ||
| 719 | + 531291600,542786400,563346000,574236000,594795600,605685600,626245200, | ||
| 720 | + 637135200,657694800,668584800,689144400,700034400,720594000,7520400,25664400,38970000, | ||
| 721 | + 57718800,70419600,89168400,101869200,120618000,133923600,152067600, | ||
| 722 | + 165373200,183517200,196822800,215571600,228272400,247021200,259722000,278470800,291776400, | ||
| 723 | + 309920400,323226000,341370000,354675600,372819600,386125200,404874000, | ||
| 724 | + 417574800,436323600,449024400,467773200,481078800,499222800,512528400,530672400,543978000, | ||
| 725 | + 562726800,575427600,594176400,606877200,625626000,638326800,657075600, | ||
| 726 | + 670381200,688525200,701830800,719974800,727413247,7520400,25664400,38970000,57718800, | ||
| 727 | + 70419600,89168400,101869200,120618000,133923600,152067600,165373200, | ||
| 728 | + 183517200,196822800,215571600,228272400,247021200,259722000,278470800,291776400,309920400, | ||
| 729 | + 323226000,341370000,354675600,372819600,386125200,404874000,417574800, | ||
| 730 | + 436323600,449024400,467773200,481078800,499222800,512528400,530672400,543978000,562726800, | ||
| 731 | + 575427600,594176400,606877200,625626000,638326800,657075600,670381200, | ||
| 732 | + 688525200,701830800,719974800,7520400,25664400,38970000,57718800,70419600,89168400, | ||
| 733 | + 101869200,120618000,133923600,152067600,165373200,183517200,196822800, | ||
| 734 | + 215571600,228272400,247021200,259722000,278470800,291776400,309920400,323226000,341370000, | ||
| 735 | + 354675600,372819600,386125200,404874000,417574800,436323600,449024400, | ||
| 736 | + 467773200,481078800,499222800,512528400,530672400,543978000,562726800,575427600,594176400, | ||
| 737 | + 606877200,625626000,638326800,657075600,670381200,688525200,701830800, | ||
| 738 | + 719974800,7520400,25664400,38970000,57718800,70419600,89168400,101869200,120618000, | ||
| 739 | + 133923600,152067600,165373200,183517200,196822800,215571600,228272400, | ||
| 740 | + 247021200,259722000,278470800,291776400,309920400,323226000,341370000,354675600,372819600, | ||
| 741 | + 386125200,404874000,417574800,436323600,449024400,467773200,481078800, | ||
| 742 | + 499222800,512528400,530672400,543978000,562726800,575427600,594176400,606877200,625626000, | ||
| 743 | + 638326800,657075600,670381200,688525200,701830800,719974800,7520400, | ||
| 744 | + 25664400,38970000,57718800,70419600,89168400,101869200,120618000,133923600,152067600, | ||
| 745 | + 165373200,183517200,196822800,215571600,228272400,247021200,259722000, | ||
| 746 | + 278470800,291776400,309920400,323226000,341370000,354675600,372819600,386125200,404874000, | ||
| 747 | + 417574800,436323600,449024400,467773200,481078800,499222800,512528400, | ||
| 748 | + 530672400,543978000,562726800,575427600,594176400,606877200,625626000,638326800,657075600, | ||
| 749 | + 670381200,688525200,701830800,719974800,727413247,727413247,8092800, | ||
| 750 | + 23817600,39542400,55267200,70992000,86716800,102441600,118771200,134496000,150220800, | ||
| 751 | + 165945600,181670400,197395200,213120000,228844800,244569600,260294400, | ||
| 752 | + 276019200,292348800,308073600,323798400,339523200,355248000,370972800,386697600,402422400, | ||
| 753 | + 418147200,433872000,449596800,465926400,481651200,497376000,513100800, | ||
| 754 | + 528825600,544550400,560275200,576000000,591724800,607449600,623174400,638899200,655228800, | ||
| 755 | + 670953600,686678400,702403200,718128000,8094600,23819400,39544200, | ||
| 756 | + 55269000,70993800,86718600,102443400,118773000,134497800,150222600,165947400,181672200, | ||
| 757 | + 197397000,213121800,228846600,244571400,260296200,276021000,292350600, | ||
| 758 | + 308075400,323800200,339525000,355249800,370974600,386699400,402424200,418149000,433873800, | ||
| 759 | + 449598600,465928200,481653000,497377800,513102600,528827400,544552200, | ||
| 760 | + 560277000,576001800,591726600,607451400,623176200,638901000,655230600,670955400,686680200, | ||
| 761 | + 702405000,718129800,8094600,23819400,39544200,55269000,70993800, | ||
| 762 | + 86718600,102443400,118773000,134497800,150222600,165947400,181672200,197397000,213121800, | ||
| 763 | + 228846600,244571400,260296200,276021000,292350600,308075400,323800200, | ||
| 764 | + 339525000,355249800,370974600,386699400,402424200,418149000,433873800,449598600,465928200, | ||
| 765 | + 481653000,497377800,513102600,528827400,544552200,560277000,576001800, | ||
| 766 | + 591726600,607451400,623176200,638901000,655230600,670955400,686680200,702405000,718129800, | ||
| 767 | + 8092800,23817600,39542400,55267200,70992000,86716800,102441600, | ||
| 768 | + 118771200,134496000,150220800,165945600,181670400,197395200,213120000,228844800,244569600, | ||
| 769 | + 260294400,276019200,292348800,308073600,323798400,339523200,355248000, | ||
| 770 | + 370972800,386697600,402422400,418147200,433872000,449596800,465926400,481651200,497376000, | ||
| 771 | + 513100800,528825600,544550400,560275200,576000000,591724800,607449600, | ||
| 772 | + 623174400,638899200,655228800,670953600,686678400,702403200,718128000,8092800,23817600, | ||
| 773 | + 39542400,55267200,70992000,86716800,102441600,118771200,134496000, | ||
| 774 | + 150220800,165945600,181670400,197395200,213120000,228844800,244569600,260294400,276019200, | ||
| 775 | + 292348800,308073600,323798400,339523200,355248000,370972800,386697600, | ||
| 776 | + 402422400,418147200,433872000,449596800,465926400,481651200,497376000,513100800,528825600, | ||
| 777 | + 544550400,560275200,576000000,591724800,607449600,623174400,638899200, | ||
| 778 | + 655228800,670953600,686678400,702403200,718128000,727413247,8092800,23817600,39542400, | ||
| 779 | + 55267200,70992000,86716800,102441600,118771200,134496000,150220800, | ||
| 780 | + 165945600,181670400,197395200,213120000,228844800,244569600,260294400,276019200,292348800, | ||
| 781 | + 308073600,323798400,339523200,355248000,370972800,386697600,402422400, | ||
| 782 | + 418147200,433872000,449596800,465926400,481651200,497376000,513100800,528825600,544550400, | ||
| 783 | + 560275200,576000000,591724800,607449600,623174400,638899200,655228800, | ||
| 784 | + 670953600,686678400,702403200,718128000,8089200,23815800,39538800,55265400,70988400, | ||
| 785 | + 86715000,102438000,118769400,134492400,150219000,165942000,181668600, | ||
| 786 | + 197391600,213118200,228841200,244567800,260290800,276017400,292345200,308071800,323794800, | ||
| 787 | + 339521400,355244400,370971000,386694000,402420600,418143600,433870200, | ||
| 788 | + 449593200,465924600,481647600,497374200,513097200,528823800,544546800,560273400,575996400, | ||
| 789 | + 591723000,607446000,623172600,638895600,655227000,670950000,686676600, | ||
| 790 | + 702399600,718126200,727413247,8089200,23815800,39538800,55265400,70988400,86715000, | ||
| 791 | + 102438000,118769400,134492400,150219000,165942000,181668600,197391600, | ||
| 792 | + 213118200,228841200,244567800,260290800,276017400,292345200,308071800,323794800,339521400, | ||
| 793 | + 355244400,370971000,386694000,402420600,418143600,433870200,449593200, | ||
| 794 | + 465924600,481647600,497374200,513097200,528823800,544546800,560273400,575996400,591723000, | ||
| 795 | + 607446000,623172600,638895600,655227000,670950000,686676600,702399600, | ||
| 796 | + 718126200,727413247,8092800,23817600,39542400,55267200,70992000,86716800,102441600, | ||
| 797 | + 118771200,134496000,150220800,165945600,181670400,197395200,213120000, | ||
| 798 | + 228844800,244569600,260294400,276019200,292348800,308073600,323798400,339523200,355248000, | ||
| 799 | + 370972800,386697600,402422400,418147200,433872000,449596800,465926400, | ||
| 800 | + 481651200,497376000,513100800,528825600,544550400,560275200,576000000,591724800,607449600, | ||
| 801 | + 623174400,638899200,655228800,670953600,686678400,702403200,718128000, | ||
| 802 | + 8092800,23817600,39542400,55267200,70992000,86716800,102441600,118771200,134496000, | ||
| 803 | + 150220800,165945600,181670400,197395200,213120000,228844800,244569600, | ||
| 804 | + 260294400,276019200,292348800,308073600,323798400,339523200,355248000,370972800,386697600, | ||
| 805 | + 402422400,418147200,433872000,449596800,465926400,481651200,497376000, | ||
| 806 | + 513100800,528825600,544550400,560275200,576000000,591724800,607449600,623174400,638899200, | ||
| 807 | + 655228800,670953600,686678400,702403200,718128000,8094600,23819400, | ||
| 808 | + 39544200,55269000,70993800,86718600,102443400,118773000,134497800,150222600,165947400, | ||
| 809 | + 181672200,197397000,213121800,228846600,244571400,260296200,276021000, | ||
| 810 | + 292350600,308075400,323800200,339525000,355249800,370974600,386699400,402424200,418149000, | ||
| 811 | + 433873800,449598600,465928200,481653000,497377800,513102600,528827400, | ||
| 812 | + 544552200,560277000,576001800,591726600,607451400,623176200,638901000,655230600,670955400, | ||
| 813 | + 686680200,702405000,718129800,8092800,23817600,39542400,55267200, | ||
| 814 | + 70992000,86716800,102441600,118771200,134496000,150220800,165945600,181670400,197395200, | ||
| 815 | + 213120000,228844800,244569600,260294400,276019200,292348800,308073600, | ||
| 816 | + 323798400,339523200,355248000,370972800,386697600,402422400,418147200,433872000,449596800, | ||
| 817 | + 465926400,481651200,497376000,513100800,528825600,544550400,560275200, | ||
| 818 | + 576000000,591724800,607449600,623174400,638899200,655228800,670953600,686678400,702403200, | ||
| 819 | + 718128000,8092800,23817600,39542400,55267200,70992000,86716800, | ||
| 820 | + 102441600,118771200,134496000,150220800,165945600,181670400,197395200,213120000,228844800, | ||
| 821 | + 244569600,260294400,276019200,292348800,308073600,323798400,339523200, | ||
| 822 | + 355248000,370972800,386697600,402422400,418147200,433872000,449596800,465926400,481651200, | ||
| 823 | + 497376000,513100800,528825600,544550400,560275200,576000000,591724800, | ||
| 824 | + 607449600,623174400,638899200,655228800,670953600,686678400,702403200,718128000,8092800, | ||
| 825 | + 23817600,39542400,55267200,70992000,86716800,102441600,118771200, | ||
| 826 | + 134496000,150220800,165945600,181670400,197395200,213120000,228844800,244569600,260294400, | ||
| 827 | + 276019200,292348800,308073600,323798400,339523200,355248000,370972800, | ||
| 828 | + 386697600,402422400,418147200,433872000,449596800,465926400,481651200,497376000,513100800, | ||
| 829 | + 528825600,544550400,560275200,576000000,591724800,607449600,623174400, | ||
| 830 | + 638899200,655228800,670953600,686678400,702403200,718128000,8094600,23819400,39544200, | ||
| 831 | + 55269000,70993800,86718600,102443400,118773000,134497800,150222600, | ||
| 832 | + 165947400,181672200,197397000,213121800,228846600,244571400,260296200,276021000,292350600, | ||
| 833 | + 308075400,323800200,339525000,355249800,370974600,386699400,402424200, | ||
| 834 | + 418149000,433873800,449598600,465928200,481653000,497377800,513102600,528827400,544552200, | ||
| 835 | + 560277000,576001800,591726600,607451400,623176200,638901000,655230600, | ||
| 836 | + 670955400,686680200,702405000,718129800,727413247,727413247,4500000,25066800,35949600, | ||
| 837 | + 56516400,67399200,87966000,98848800,121230000,130298400,727413247, | ||
| 838 | + 727413247,7520400,25664400,38970000,57718800,70419600,89168400,101869200,120618000, | ||
| 839 | + 133923600,152067600,165373200,183517200,196822800,215571600,228272400, | ||
| 840 | + 247021200,259722000,278470800,291776400,309920400,323226000,341370000,354675600,372819600, | ||
| 841 | + 386125200,404874000,417574800,436323600,449024400,467773200,481078800, | ||
| 842 | + 499222800,512528400,530672400,543978000,562726800,575427600,594176400,606877200,625626000, | ||
| 843 | + 638326800,657075600,670381200,688525200,701830800,719974800,5731200, | ||
| 844 | + 26290800,37785600,58345200,69235200,89794800,100684800,121244400,132134400,152694000, | ||
| 845 | + 163584000,184143600,195638400,216198000,227088000,247647600,258537600, | ||
| 846 | + 279097200,289987200,310546800,321436800,341996400,352886400,373446000,384940800,405500400, | ||
| 847 | + 416390400,436950000,447840000,468399600,479289600,499849200,510739200, | ||
| 848 | + 531298800,542793600,563353200,574243200,594802800,605692800,626252400,637142400,657702000, | ||
| 849 | + 668592000,689151600,700041600,720601200,5724000,26283600,37778400, | ||
| 850 | + 58338000,69228000,89787600,100677600,121237200,132127200,152686800,163576800,184136400, | ||
| 851 | + 195631200,216190800,227080800,247640400,258530400,279090000,289980000, | ||
| 852 | + 310539600,321429600,341989200,352879200,373438800,384933600,405493200,416383200,436942800, | ||
| 853 | + 447832800,468392400,479282400,499842000,510732000,531291600,542786400, | ||
| 854 | + 563346000,574236000,594795600,605685600,626245200,637135200,657694800,668584800,689144400, | ||
| 855 | + 700034400,720594000,5731200,26290800,37785600,58345200,69235200, | ||
| 856 | + 89794800,100684800,121244400,132134400,152694000,163584000,184143600,195638400,216198000, | ||
| 857 | + 227088000,247647600,258537600,279097200,289987200,310546800,321436800, | ||
| 858 | + 341996400,352886400,373446000,384940800,405500400,416390400,436950000,447840000,468399600, | ||
| 859 | + 479289600,499849200,510739200,531298800,542793600,563353200,574243200, | ||
| 860 | + 594802800,605692800,626252400,637142400,657702000,668592000,689151600,700041600,720601200, | ||
| 861 | + 5727600,26287200,37782000,58341600,69231600,89791200,100681200, | ||
| 862 | + 121240800,132130800,152690400,163580400,184140000,195634800,216194400,227084400,247644000, | ||
| 863 | + 258534000,279093600,289983600,310543200,321433200,341992800,352882800, | ||
| 864 | + 373442400,384937200,405496800,416386800,436946400,447836400,468396000,479286000,499845600, | ||
| 865 | + 510735600,531295200,542790000,563349600,574239600,594799200,605689200, | ||
| 866 | + 626248800,637138800,657698400,668588400,689148000,700038000,720597600,5734800,26294400, | ||
| 867 | + 37789200,58348800,69238800,89798400,100688400,121248000,132138000, | ||
| 868 | + 152697600,163587600,184147200,195642000,216201600,227091600,247651200,258541200,279100800, | ||
| 869 | + 289990800,310550400,321440400,342000000,352890000,373449600,384944400, | ||
| 870 | + 405504000,416394000,436953600,447843600,468403200,479293200,499852800,510742800,531302400, | ||
| 871 | + 542797200,563356800,574246800,594806400,605696400,626256000,637146000, | ||
| 872 | + 657705600,668595600,689155200,700045200,720604800,5722200,26281800,37776600,58336200, | ||
| 873 | + 69226200,89785800,100675800,121235400,132125400,152685000,163575000, | ||
| 874 | + 184134600,195629400,216189000,227079000,247638600,258528600,279088200,289978200,310537800, | ||
| 875 | + 321427800,341987400,352877400,373437000,384931800,405491400,416381400, | ||
| 876 | + 436941000,447831000,468390600,479280600,499840200,510730200,531289800,542784600,563344200, | ||
| 877 | + 574234200,594793800,605683800,626243400,637133400,657693000,668583000, | ||
| 878 | + 689142600,700032600,720592200,5738400,26298000,37792800,58352400,69242400,89802000, | ||
| 879 | + 100692000,121251600,132141600,152701200,163591200,184150800,195645600, | ||
| 880 | + 216205200,227095200,247654800,258544800,279104400,289994400,310554000,321444000,342003600, | ||
| 881 | + 352893600,373453200,384948000,405507600,416397600,436957200,447847200, | ||
| 882 | + 468406800,479296800,499856400,510746400,531306000,542800800,563360400,574250400,594810000, | ||
| 883 | + 605700000,626259600,637149600,657709200,668599200,689158800,700048800, | ||
| 884 | + 720608400,5738400,26298000,37792800,58352400,69242400,89802000,100692000,121251600, | ||
| 885 | + 132141600,152701200,163591200,184143600,43210800,51076800,74660400, | ||
| 886 | + 82526400,106110000,113976000,134535600,147844800,165985200,179294400,197434800,210744000, | ||
| 887 | + 228884400,242798400,260334000,273643200,292388400,305697600,323838000, | ||
| 888 | + 337147200,355287600,368596800,386737200,400046400,418186800,431496000,450241200,462945600, | ||
| 889 | + 481690800,495000000,513140400,526449600,544590000,557899200,576039600, | ||
| 890 | + 589348800,607489200,620798400,639543600,652248000,670993200,684302400,702442800,715752000, | ||
| 891 | + 727413247,43210800,51076800,74660400,82526400,106110000,113976000, | ||
| 892 | + 134535600,147844800,165985200,179294400,197434800,210744000,228884400,242798400,260334000, | ||
| 893 | + 273643200,292388400,305697600,323838000,337147200,355287600,368596800, | ||
| 894 | + 386737200,400046400,418186800,431496000,450241200,462945600,481690800,495000000,513140400, | ||
| 895 | + 526449600,544590000,557899200,576039600,589348800,607489200,620798400, | ||
| 896 | + 639543600,652248000,670993200,684302400,702442800,715752000,727413247,5720400,26283600, | ||
| 897 | + 37774800,58338000,69224400,89787600,100674000,121237200,132123600, | ||
| 898 | + 152686800,163573200,184136400,195627600,216190800,227077200,247640400,258526800,279090000, | ||
| 899 | + 289976400,310539600,321426000,341989200,352875600,373438800,384930000, | ||
| 900 | + 405493200,416379600,436942800,447829200,468392400,479278800,499842000,510728400,531291600, | ||
| 901 | + 542782800,563346000,574232400,594795600,605682000,626245200,637131600, | ||
| 902 | + 657694800,668581200,689144400,700030800,720594000,7520400,25664400,38970000,57718800, | ||
| 903 | + 70419600,89168400,101869200,120618000,133923600,152067600,165373200, | ||
| 904 | + 183517200,196822800,215571600,228272400,247021200,259722000,278470800,291776400,309920400, | ||
| 905 | + 323226000,341370000,354675600,372819600,386125200,404874000,417574800, | ||
| 906 | + 4363 |