← All lessons/Time & History
23
Time & History

Time Range Queries

Query records that were written within a specific time window.

Prerequisite: Lesson 22 complete

What you'll learn

  • type: time_range with from_hlc and to_hlc (both inclusive)
  • Converting datetime to HLC: int(dt.timestamp()*1000)*65536
  • time_range with filter — combined time + field filtering
  • Billing period helper: first second of month to first second of next month
  • type: count with time_range filter — period totals
Challenge

Query all orders from the past 24 hours. Count them using a count query with the same time range filter.

Interactive Walkthrough

What you'll learn

Query records that were written within a specific time window.

Time range query

`bash # Convert timestamps to HLC START_HLC=$(python3 -c "import datetime; d=datetime.datetime(2026,7,1); print(int(d.timestamp()*1000)*65536)") END_HLC=$(python3 -c "import datetime; d=datetime.datetime(2026,7,8); print(int(d.timestamp()*1000)*65536)")

curl -s -X POST http://localhost:7475/v1/agents/events/query \ -H "Content-Type: application/json" \ -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \ -d "{ \"type\": \"time_range\", \"from_hlc\": $START_HLC, \"to_hlc\": $END_HLC, \"limit\": 1000 }" | python3 -m json.tool `

Time range with filter

curl -s -X POST http://localhost:7475/v1/agents/orders/query \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  -d "{
    \"type\":     \"time_range\",
    \"from_hlc\": $START_HLC,
    \"to_hlc\":   $END_HLC,
    \"limit\":    1000,
    \"filter\":   {\"field\": \"status\", \"op\": \"eq\", \"value\": \"paid\"}
  }" | python3 -m json.tool

Common billing period patterns

`python import datetime

def billing_period_hlc(year: int, month: int): """Returns (from_hlc, to_hlc) for a calendar month.""" start = datetime.datetime(year, month, 1) if month == 12: end = datetime.datetime(year + 1, 1, 1) else: end = datetime.datetime(year, month + 1, 1) return ( int(start.timestamp() * 1000) * 65536, int(end.timestamp() * 1000) * 65536 - 1 )

from_hlc, to_hlc = billing_period_hlc(2026, 7) `

Challenge

Using an orders agent, query all orders from the past 24 hours. Then count them with a count query using the same time range filter.

---

← Previous
Lesson 22: Time Travel Deep Dive
Next →
Lesson 24: Chain Verification