mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The compiler emits its runtime helpers by name and the VM defines them by name, so the two are one contract. It was renamed on one side only: all three engines -- the Python VM, the TypeScript VM, and the JS emitter -- already said `__printScriptValue`, while every committed snapshot still said `__printHogValue`. The snapshots had not been regenerated since. That was not only cosmetic. `catch.script` and `catch2.script` call `HogError(...)`, an STL name that became `ScriptError`, so both had been failing; and `stl.stdout` recorded `__hogInterval__` for a marker the engines stopped writing two renames ago. So: drop the language's name from the helpers, since inside its own runtime every value is a Script value and the infix distinguishes nothing. Both sides move here, together, and the snapshots are regenerated from the compiler rather than edited to agree. Two names could not simply lose the infix, and the collision is the interesting part: __toScriptDate/DateTime -> __newDate/__newDateTime, not __toDate, which already exists and is a different function -- the STL builtin that PARSES a foreign input, where these build a value from its parts. They join `__newError`, which was already named that way. ScriptError stays. Its remaining word is `Error`, also taken, and also a different function: `Error(message)` fixes the type, `ScriptError( type, message)` takes it. Leaving the families orthogonal: `__new*` builds, `__to*` converts, `__is*` tests, `__x__` marks, `__print*` renders. Verified with the two engines this ABI spans, python-VM output against compiled-JS output per test: 31 pass / 4 fail, against 29 / 6 on the commit before this one. The two it fixes are catch and catch2. The four that remain fail identically on both trees and are untouched by this. `insightsqlx.js` is deliberately NOT regenerated and still spells the old names: its source does not compile on either tree (`BytecodeCompiler has no method visit_insights_qlxtag`), and editing a snapshot by hand to agree with a rename is the exact failure this commit exists to undo. Co-authored-by: Hanzo Dev <[email protected]>
172 lines
4.2 KiB
Python
172 lines
4.2 KiB
Python
import datetime
|
|
from typing import Optional
|
|
|
|
import pytz
|
|
|
|
from common.scriptvm.python.objects import is_script_date, is_script_datetime
|
|
|
|
|
|
def to_script_date(year: int, month: int, day: int):
|
|
return {
|
|
"__date__": True,
|
|
"year": year,
|
|
"month": month,
|
|
"day": day,
|
|
}
|
|
|
|
|
|
def to_script_datetime(timestamp: int | float | dict, zone: Optional[str] = None):
|
|
if isinstance(timestamp, dict) and is_script_date(timestamp):
|
|
dt = datetime.datetime(
|
|
year=timestamp["year"], month=timestamp["month"], day=timestamp["day"], tzinfo=pytz.timezone(zone or "UTC")
|
|
)
|
|
return {
|
|
"__dateTime__": True,
|
|
"dt": dt.timestamp(),
|
|
"zone": (dt.tzinfo.tzname(None) if dt.tzinfo else None) or "UTC",
|
|
}
|
|
return {
|
|
"__dateTime__": True,
|
|
"dt": timestamp,
|
|
"zone": zone or "UTC",
|
|
}
|
|
|
|
|
|
# Exported functions
|
|
|
|
|
|
def now(zone: Optional[str] = None):
|
|
return to_script_datetime(datetime.datetime.now().timestamp(), zone)
|
|
|
|
|
|
def toUnixTimestamp(date, timezone: Optional[str] = None):
|
|
if isinstance(date, dict) and is_script_datetime(date):
|
|
return date["dt"]
|
|
if isinstance(date, dict) and is_script_date(date):
|
|
return datetime.datetime(
|
|
year=date["year"], month=date["month"], day=date["day"], tzinfo=pytz.timezone(timezone or "UTC")
|
|
).timestamp()
|
|
|
|
date = datetime.datetime.fromisoformat(date)
|
|
if timezone:
|
|
date = date.astimezone(pytz.timezone(timezone))
|
|
return date.timestamp()
|
|
|
|
|
|
def fromUnixTimestamp(timestamp: int | float):
|
|
return to_script_datetime(timestamp)
|
|
|
|
|
|
def toUnixTimestampMilli(date, timezone: Optional[str] = None):
|
|
return int(toUnixTimestamp(date, timezone) * 1000)
|
|
|
|
|
|
def fromUnixTimestampMilli(timestamp: int):
|
|
return fromUnixTimestamp(float(timestamp) / 1000.0)
|
|
|
|
|
|
def toTimeZone(date: dict, timezone: str):
|
|
if not is_script_datetime(date):
|
|
raise ValueError("Expected a DateTime")
|
|
return {
|
|
**date,
|
|
"zone": timezone,
|
|
}
|
|
|
|
|
|
def toDate(input):
|
|
if isinstance(input, int) or isinstance(input, float):
|
|
dt = datetime.datetime.fromtimestamp(input)
|
|
else:
|
|
dt = datetime.datetime.fromisoformat(input)
|
|
return {
|
|
"__date__": True,
|
|
"year": dt.year,
|
|
"month": dt.month,
|
|
"day": dt.day,
|
|
}
|
|
|
|
|
|
def toDateTime(input):
|
|
if isinstance(input, int) or isinstance(input, float):
|
|
dt = float(input)
|
|
else:
|
|
dt = datetime.datetime.fromisoformat(input).timestamp()
|
|
return {
|
|
"__dateTime__": True,
|
|
"dt": dt,
|
|
"zone": "UTC",
|
|
}
|
|
|
|
|
|
def date_string_to_seconds(input: str) -> Optional[float]:
|
|
"""Epoch seconds for a date-like string, parsed the same way `toDateTime` would, else None."""
|
|
try:
|
|
return datetime.datetime.fromisoformat(input).timestamp()
|
|
except ValueError:
|
|
return None
|
|
|
|
|
|
# From Datastore to Python
|
|
token_translations = {
|
|
"a": "%a",
|
|
"b": "%b",
|
|
"c": "%m",
|
|
"C": "%y",
|
|
"d": "%d",
|
|
"D": "%m/%d/%y",
|
|
"e": "%d",
|
|
"f": "%f",
|
|
"F": "%Y-%m-%d",
|
|
"g": "%y",
|
|
"G": "%Y",
|
|
"h": "%I",
|
|
"H": "%H",
|
|
"i": "%M",
|
|
"I": "%I",
|
|
"j": "%j",
|
|
"k": "%H",
|
|
"l": "%I",
|
|
"m": "%m",
|
|
"M": "%B",
|
|
"n": "\n",
|
|
"p": "%p",
|
|
# 'Q': '%Q',
|
|
"r": "%I:%M %p",
|
|
"R": "%H:%M",
|
|
"s": "%S",
|
|
"S": "%S",
|
|
"t": "\t",
|
|
"T": "%H:%M:%S",
|
|
"u": "%u",
|
|
"V": "%V",
|
|
"w": "%w",
|
|
"W": "%A",
|
|
"y": "%y",
|
|
"Y": "%Y",
|
|
"z": "%z",
|
|
"%": "%%",
|
|
}
|
|
|
|
|
|
def formatDateTime(input: dict, format: str, zone: Optional[str] = None) -> str:
|
|
if not is_script_datetime(input):
|
|
raise ValueError("Expected a DateTime")
|
|
format_string = ""
|
|
acc = ""
|
|
i = 0
|
|
while i < len(format):
|
|
if format[i] == "%":
|
|
if acc:
|
|
format_string += acc
|
|
acc = ""
|
|
i += 1
|
|
if i < len(format) and format[i] in token_translations:
|
|
format_string += token_translations[format[i]]
|
|
else:
|
|
acc += format[i]
|
|
i += 1
|
|
if acc:
|
|
format_string += acc
|
|
return datetime.datetime.fromtimestamp(input["dt"], pytz.timezone(zone or input["zone"])).strftime(format_string)
|