PC05-L04 · independent challenge

Active time period

Evaluate a period with a required start and an optional end.

Practical CEL4 test casesDocumented
What will I practise?

You can use short-circuit evaluation to protect an optional end value.

How is it checked?

Your one expression must produce the expected result for all four JSON inputs.

When am I done?

When all tests pass and you can explain every condition in your expression.

Word problem

A period is active after its required start and before its optional end. A missing end means the period remains open.

Exact requirements

  1. The current instant must be at or after start.
  2. A missing end is accepted.
  3. A present end is exclusive.
  4. Do not read or parse end when it is missing.

Required result

Write one CEL expression. It must return the expected value and type for all four prepared JSON inputs. Do not change the inputs to make an incomplete expression pass.

Recommended procedure

  1. Translate each numbered requirement into one small boolean condition.
  2. Run the starter expression and inspect which cases fail.
  3. Add one condition at a time and run the complete suite again.
  4. Mark the challenge complete only when you can explain every operator and every boundary.

Solve the challenge · browser test suite

Write one expression that passes every case

Edit the CEL expression, then run it against all prepared JSON inputs. A solution is complete only when every case passes.

4 test cases

Hint: When the end is missing, the second part must be true without trying to parse it.

Tip: press Ctrl/⌘ + Enter to run all tests.
Open-ended periodNot run yet
Expected: true · bool
Show JSON input
{
  "now": "2026-07-23T10:00:00Z",
  "subscription": {
    "start": "2026-07-01T00:00:00Z"
  }
}
Inside closed periodNot run yet
Expected: true · bool
Show JSON input
{
  "now": "2026-07-23T10:00:00Z",
  "subscription": {
    "start": "2026-07-01T00:00:00Z",
    "end": "2026-08-01T00:00:00Z"
  }
}
Before startNot run yet
Expected: false · bool
Show JSON input
{
  "now": "2026-06-30T23:59:59Z",
  "subscription": {
    "start": "2026-07-01T00:00:00Z"
  }
}
Exact endNot run yet
Expected: false · bool
Show JSON input
{
  "now": "2026-08-01T00:00:00Z",
  "subscription": {
    "start": "2026-07-01T00:00:00Z",
    "end": "2026-08-01T00:00:00Z"
  }
}
Test result

Not run yet

The same expression runs against every case. Test data is fixed so that boundary and failure scenarios cannot be skipped.

Explained solution

Show the complete expression
timestamp(now) >= timestamp(subscription.start) && (!has(subscription.end) || timestamp(now) < timestamp(subscription.end))

Why the solution works

  1. The first comparison enforces the required start.
  2. The parenthesized second part has an open-ended and a closed-ended path.
  3. !has(subscription.end) resolves the open-ended path.
  4. Short-circuit || protects the timestamp conversion when end is absent.

What the test cases prove

  • Open-ended period: expected true (bool).
  • Inside closed period: expected true (bool).
  • Before start: expected false (bool).
  • Exact end: expected false (bool).

The cases are intentionally different. A solution that passes only the first case has not yet implemented the complete rule.

Common mistake

Do not optimise the expression for the first successful input. A requirement is implemented only when its positive case, negative case, and boundary case all produce the stated result.

Knowledge check

Can you point to the exact part of the solution that makes each false test case fail? If two different failures depend on the same condition, explain why.

Key takeaway

You can use short-circuit evaluation to protect an optional end value.

Sources

  • CEL-DEV — official CEL overview.
  • CEL-LANG — official CEL language definition for operators, types, macros, presence, strings, and time values used in this challenge.