You can use inTimeRange in OCL when you need to test whether a time of day falls within a specified time range.
Purpose
inTimeRange is a Boolean operator: it returns true or false. Use it to compare a TimeSpan value, such as the time-of-day portion of a DateTime, with two TimeSpan range values.
For example, an order date such as 2026-03-09 14:30 is a DateTime. Its TimeOfDay value is 14:30:00, which is a TimeSpan and can be supplied to inTimeRange.
Syntax
self.[DateTimeProperty].TimeOfDay.inTimeRange(TimeSpan.FromHours(X), TimeSpan.FromHours(Y))
| Part | Meaning |
|---|---|
self.[DateTimeProperty]
|
The DateTime property that you want to test.
|
.TimeOfDay
|
Extracts the duration since midnight as a TimeSpan. This makes the comparison independent of the calendar date.
|
TimeSpan.FromHours(X)
|
Creates a TimeSpan range boundary by calling the .NET TimeSpan.FromHours method through the CLR.
|
inTimeRange(...)
|
Tests the extracted TimeSpan against the two supplied range boundaries and returns a Boolean value.
|
Use a DateTime property's time of day
Follow these steps when the property you have is a DateTime:
- Start with the
DateTimeproperty. - Call
TimeOfDayto obtain aTimeSpan. - Define both range boundaries as
TimeSpanvalues. - Use the Boolean result in an expression or assign it to a variable.
vResult := self.OrderDate.TimeOfDay.inTimeRange(TimeSpan.FromHours(0), TimeSpan.FromHours(0))
In this example:
self.OrderDateis aDateTime.self.OrderDate.TimeOfDayis the correspondingTimeSpan.- Both boundaries are created as
TimeSpanvalues. vResultreceives the Boolean result.
The := operator assigns the result so that later OCL logic or a user-interface expression can use vResult.
Avoid DateTime and TimeSpan type mismatches
Do not compare a full DateTime directly with TimeSpan boundaries. They are different types. A comparison such as the following supplies a TimeSpan to inTimeRange because TimeOfDay performs the required extraction:
self.OrderDate.TimeOfDay.inTimeRange(TimeSpan.FromHours(8), TimeSpan.FromHours(17))
This expression tests the order's time of day using boundaries representing 8 and 17 hours after midnight. It does not compare the date portion of OrderDate.
Notes
- The operator is CLR-bound; its time comparisons use standard .NET time values, including ticks and milliseconds.
- The result is always Boolean. For general logical composition of Boolean expressions, see OCL Boolean Operators.
- Confirm the boundary behavior for equal start and end values and for ranges that cross midnight before using those cases in business rules.
