Skip to content

An open-source, faster Devin alternative

TypeScript

Accept bare ISO dates in usage `from` / `to`

Igor Zalutski2542b92f6d51
diff --git a/docs/api-reference/usage/get-sandbox-usage.mdx b/docs/api-reference/usage/get-sandbox-usage.mdx
index 721d6408..fda4f967 100644
--- a/docs/api-reference/usage/get-sandbox-usage.mdx
+++ b/docs/api-reference/usage/get-sandbox-usage.mdx
@@ -15,11 +15,13 @@ resident memory consumed by the sandbox process, sampled every 60s.
</ParamField>
<ParamField query="from" type="string">
- RFC3339 lower bound. Default: now minus 1 hour.
+ Lower bound. Accepts either a date (`2026-05-27`, treated as UTC
+ midnight) or a full RFC3339 timestamp. Default: now minus 1 hour.
</ParamField>
<ParamField query="to" type="string">
- RFC3339 upper bound. Default: now. Window must be ≤ 30 days.
+ Upper bound. Same format as `from`. Default: now. Window must be ≤
+ 30 days.
</ParamField>
## Response
diff --git a/docs/api-reference/usage/get-usage.mdx b/docs/api-reference/usage/get-usage.mdx
index 52f8b618..7f21f19b 100644
--- a/docs/api-reference/usage/get-usage.mdx
+++ b/docs/api-reference/usage/get-usage.mdx
@@ -32,12 +32,13 @@ billing pipeline uses, so the numbers here match the invoice.
</ParamField>
<ParamField query="from" type="string">
- RFC3339 inclusive lower bound. Default: now minus 30 days. Max
- window: 90 days.
+ Inclusive lower bound. Accepts either a date (`2026-05-27`, treated
+ as UTC midnight) or a full RFC3339 timestamp. Default: now minus
+ 30 days. Max window: 90 days.
</ParamField>
<ParamField query="to" type="string">
- RFC3339 exclusive upper bound. Default: now.
+ Exclusive upper bound. Same format as `from`. Default: now.
</ParamField>
<ParamField query="filter[tag:<key>]" type="string">
diff --git a/docs/sandboxes/usage.mdx b/docs/sandboxes/usage.mdx
index 807d89c8..489aa2fd 100644
--- a/docs/sandboxes/usage.mdx
+++ b/docs/sandboxes/usage.mdx
@@ -85,12 +85,14 @@ falls out of `points[]` directly: `allocatedMemoryMb` is the step,
## Request
```http
-GET /api/sandboxes/sb-abc/usage?from=2026-05-27T00:00:00Z&to=2026-05-27T01:00:00Z
+GET /api/sandboxes/sb-abc/usage?from=2026-05-27&to=2026-05-28
```
-Path carries the sandbox ID. `from` and `to` are optional ISO-8601
-timestamps — omit them for the last hour, max window is **30 days**.
-Full param reference: the [API page](/api-reference/usage/get-sandbox-usage).
+Path carries the sandbox ID. `from` and `to` accept either a date
+(`2026-05-27`, interpreted as UTC midnight) or a full timestamp
+(`2026-05-27T15:30:00Z`). Both are optional — omit for the last
+hour. Max window is **30 days**. Full param reference: the
+[API page](/api-reference/usage/get-sandbox-usage).
## Response
diff --git a/internal/api/usage.go b/internal/api/usage.go
index 35421135..0c72babc 100644
--- a/internal/api/usage.go
+++ b/internal/api/usage.go
@@ -25,6 +25,22 @@ const (
usageHandlerTimeout = 10 * time.Second
)
+// parseUsageTimestamp accepts either a full RFC3339 timestamp
+// (`2026-05-27T15:30:00Z`) or a bare ISO date (`2026-05-27`). Dates
+// are interpreted as UTC midnight — the conventional "start of day"
+// semantics for usage windows. Anything in between (date + time
+// without a timezone) is rejected because the interpretation isn't
+// obvious.
+func parseUsageTimestamp(s string) (time.Time, error) {
+ if t, err := time.Parse(time.RFC3339, s); err == nil {
+ return t, nil
+ }
+ if t, err := time.Parse("2006-01-02", s); err == nil {
+ return t.UTC(), nil
+ }
+ return time.Time{}, fmt.Errorf("must be an ISO date (YYYY-MM-DD) or RFC3339 timestamp")
+}
+
// parseUsageQuery reads from / to / groupBy / filter[...] / sort /
// limit / cursor from the echo request. Returns a fully-validated
// UsageQuery suitable for handing to the store.
@@ -38,18 +54,18 @@ func parseUsageQuery(c echo.Context) (db.UsageQuery, error) {
now := time.Now().UTC()
if s := c.QueryParam("from"); s != "" {
- t, err := time.Parse(time.RFC3339, s)
+ t, err := parseUsageTimestamp(s)
if err != nil {
- return q, fmt.Errorf("`from` must be RFC3339: %w", err)
+ return q, fmt.Errorf("`from` %w", err)
}
q.From = t
} else {
q.From = now.Add(-usageDefaultWindow)
}
if s := c.QueryParam("to"); s != "" {
- t, err := time.Parse(time.RFC3339, s)
+ t, err := parseUsageTimestamp(s)
if err != nil {
- return q, fmt.Errorf("`to` must be RFC3339: %w", err)
+ return q, fmt.Errorf("`to` %w", err)
}
q.To = t
} else {
@@ -304,7 +320,7 @@ const sandboxUsageDefaultWindow = time.Hour
// once usage_collector.go starts populating cpu_usec.
func (s *Server) getSandboxUsage(c echo.Context) error {
sandboxID := c.Param("id")
- if err := s.ownsSandbox(c, sandboxID); err != nil {
+ if ok, err := s.ownsSandbox(c, sandboxID); err != nil || !ok {
return err
}
@@ -313,16 +329,16 @@ func (s *Server) getSandboxUsage(c echo.Context) error {
now := time.Now().UTC()
from, to := now.Add(-sandboxUsageDefaultWindow), now
if s := c.QueryParam("from"); s != "" {
- t, err := time.Parse(time.RFC3339, s)
+ t, err := parseUsageTimestamp(s)
if err != nil {
- return c.JSON(http.StatusBadRequest, map[string]string{"error": "`from` must be RFC3339"})
+ return c.JSON(http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("`from` %s", err)})
}
from = t
}
if s := c.QueryParam("to"); s != "" {
- t, err := time.Parse(time.RFC3339, s)
+ t, err := parseUsageTimestamp(s)
if err != nil {
- return c.JSON(http.StatusBadRequest, map[string]string{"error": "`to` must be RFC3339"})
+ return c.JSON(http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("`to` %s", err)})
}
to = t
}
diff --git a/internal/api/usage_test.go b/internal/api/usage_test.go
new file mode 100644
index 00000000..644d8443
--- /dev/null
+++ b/internal/api/usage_test.go
@@ -0,0 +1,67 @@
+package api
+
+import (
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestParseUsageTimestamp(t *testing.T) {
+ tests := []struct {
+ name string
+ in string
+ want time.Time
+ wantErr string
+ }{
+ {
+ name: "RFC3339 with Z",
+ in: "2026-05-27T15:30:00Z",
+ want: time.Date(2026, 5, 27, 15, 30, 0, 0, time.UTC),
+ },
+ {
+ name: "RFC3339 with offset",
+ in: "2026-05-27T15:30:00+02:00",
+ want: time.Date(2026, 5, 27, 13, 30, 0, 0, time.UTC),
+ },
+ {
+ name: "date only — UTC midnight",
+ in: "2026-05-27",
+ want: time.Date(2026, 5, 27, 0, 0, 0, 0, time.UTC),
+ },
+ {
+ name: "date + time without timezone — rejected",
+ in: "2026-05-27T15:30:00",
+ wantErr: "must be an ISO date",
+ },
+ {
+ name: "free-form string",
+ in: "yesterday",
+ wantErr: "must be an ISO date",
+ },
+ {
+ name: "empty string",
+ in: "",
+ wantErr: "must be an ISO date",
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := parseUsageTimestamp(tt.in)
+ if tt.wantErr != "" {
+ if err == nil {
+ t.Fatalf("expected error containing %q, got nil", tt.wantErr)
+ }
+ if !strings.Contains(err.Error(), tt.wantErr) {
+ t.Errorf("error = %q, want substring %q", err.Error(), tt.wantErr)
+ }
+ return
+ }
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if !got.Equal(tt.want) {
+ t.Errorf("got %v, want %v", got, tt.want)
+ }
+ })
+ }
+}