Tutorial 4: Creative Temporal Patterns

Open inAnthropic

Series: Time Processing Overview | Tutorial 1: Dates | Tutorial 2: Durations | Tutorial 3: Intervals | Tutorial 4

What you will learn

By the end of this tutorial you will be able to:

  • Exploit the three modes of WOQL predicates: compute, validate, and classify
  • Compose generators with filters to build business day calendars
  • Use and to chain temporal predicates into multi-step reasoning
  • Build fiscal calendar structures and verify them with Allen's algebra
  • Detect gaps and overlaps between periods
  • Coordinate deadlines across multiple entities with range_min / range_max

Prerequisites

  • Completed Tutorials 1, 2, and 3
  • A running TerminusDB instance

The Big Idea: Three Modes of Every Predicate

Most programming languages have functions that go one way: input → output. WOQL predicates are multi-directional. The same predicate can:

  1. Compute — give it inputs, get outputs
  2. Validate — give it all arguments, get yes/no
  3. Generate — give it partial information, get the missing piece, used to Classify here.

You have already seen this with date_duration (Tutorial 2) and interval_relation_typed (Tutorial 3). This tutorial shows you how to compose these modes to solve problems that would require complex procedural code in other systems.


Part 1: Business Day Calendars

Financial systems need to know which dates are business days. WOQL has no built-in "is_business_day" predicate — but you can build one by composing sequence (generator) with weekday (classifier) and less/lte (filter).

Step 1: Generate all dates in January, then filter to weekdays

Step 1: Business days in January 2025

Generate all dates in Jan, get each weekday number (Mon=1..Sun=7), keep only Mon-Fri.

Ctrl+Enter to run

What happened: sequence generated all 31 dates. weekday classified each one. lte(dow, 5) filtered out Saturday (6) and Sunday (7). The result is every business day in January 2025.

Step 2: Count business days in a month

Step 2: Count business days

How many business days in January 2025? Converts dates to strings before collecting, then counts.

Ctrl+Enter to run

Step 3: Find month-end dates that fall on weekdays

Step 3: Business-day month-ends

Month-end dates that are also business days. If a month ends on a weekend, it's excluded.

Ctrl+Enter to run

Part 2: Fiscal Calendar Construction

Build a complete fiscal calendar structure and verify it in a single query.

Step 4: Define and validate four quarters

Step 4: Fiscal quarters — construct and validate

Build Q1-Q4 as intervals, then verify each adjacent pair meets. One result = valid partition.

Ctrl+Enter to run

If you get one result, the quarters are correctly defined. Zero results means something is wrong — try changing a date to introduce a gap and see what happens.

Step 5: Quarter day-counts

Step 5: Day-count per quarter

How many days in each quarter? Note they're not all equal.

Ctrl+Enter to run

Part 3: Filing Deadlines and Coordination

Step 6: Compute a filing deadline

SEC 10-Q filings are due within 40 days after the fiscal quarter ends. The "as of" date (inclusive end of the period) is the starting point:

Step 6: Filing deadline

40 days after the as-of date (March 31) for a Q1 filing.

Ctrl+Enter to run

Step 7: Find earliest and latest deadlines

When multiple subsidiaries have different filing dates, find the reporting window:

Step 7: Filing window — earliest and latest

Four subsidiaries, four deadlines. What's the consolidated window?

Ctrl+Enter to run

Step 8: Check which is the last business day before the deadline

Step 8: Last business day before deadline

Collect all business days into a list, then pick the maximum.

Ctrl+Enter to run

group_by collects business days into a list, range_max picks the latest. One row, one answer.


Part 4: Overlap and Gap Detection

Step 9: Detect overlapping periods

Step 9: Overlap detection

Does the audit period overlap with the reporting period? Check the relation.

Ctrl+Enter to run

Any result containing overlaps, overlapped_by, starts, started_by, during, contains, finishes, finished_by, or equals means the periods share at least some time.

Step 10: Validate no overlap between adjacent assignments

Step 10: No-overlap check

'meets' means zero gap AND zero overlap. The cleanest adjacency.

Ctrl+Enter to run

One result = clean handover. Zero results = something is wrong (gap or overlap).


Part 5: Rolling Windows and Sequence Composition

Step 11: Generate monthly reporting periods

Step 11: Monthly periods — start and end

For each month in H1, get both the first and last day.

Ctrl+Enter to run

What happened: sequence generated each gYearMonth. month_start_date and month_end_date converted each to concrete dates. You now have a complete monthly calendar.

Step 12: Monthly intervals for Allen's algebra

Step 12: Monthly intervals

Construct an interval for each month in Q1 using start + P1M duration.

Ctrl+Enter to run

Part 6: Multi-Step Temporal Reasoning

This is where WOQL's composability truly shines. Chain multiple temporal predicates to answer complex questions.

Step 13: "What is the last business day of each month in H1 2025?"

This requires generating month-end dates, checking each for weekday, and if it's a weekend, stepping backward until you find a weekday. Here's a simplified version that shows the month-ends and their weekdays:

Step 13: Month-end weekdays

Which month-ends are weekdays (1-5) and which are weekends (6-7)?

Ctrl+Enter to run

Inspect the results: any dow of 6 (Saturday) or 7 (Sunday) means that month's last business day is actually the Friday before.

Step 14: Duration + interval + relation in one query

"Given a start date and a project duration, construct the project interval, then check if it overlaps with the Q2 reporting period."

Step 14: Multi-step reasoning

Build a project interval from start+duration, classify its relation to Q2.

Ctrl+Enter to run

Design Patterns Summary

PatternPredicatesUse case
Generate + Filtersequence + weekday + lteBusiness day calendars
Construct + Validateinterval + interval_relation_typed("meets")Partition verification
Compute + Generatedate_duration + interval_relation_typed(v.rel)Temporal reasoning
Generate + Transformsequence(ym) + month_start_dateMonthly calendars
Aggregate + Comparerange_min / range_maxDeadline coordination
Chain + Composeinterval_start_duration + interval_relation_typedMulti-step analysis

Self-Check

  1. How would you find all Fridays in March 2025?
  2. What Allen relation would you check to verify two shifts have no gap?
  3. If interval_relation_typed(v.rel, audit, reporting) returns "overlapped_by", what does that mean?
  4. How would you find the next business day after a computed deadline?
  5. Why is WOQL better suited for temporal reasoning than a typical SQL query?

What You Learned

ConceptKey Point
Three modesCompute, validate, classify — same predicate, different binding patterns
Generate + Filtersequence + weekday = business day calendar
Partition verificationChain meets checks to prove no gaps or overlaps
Multi-step reasoningand composes any predicates — duration, interval, relation
Deadline coordinationrange_min / range_max across entity lists
Gap/overlap detectionClassify with interval_relation_typed — check the relation name

Where to go next

Was this helpful?