Duration
The duration
data type in Filtrera represents a timespan. It is a special type because it encompasses a class of types, each representing a different unit of time. This reference guide provides a detailed specification of the duration
data type, its syntax, and usage for users already familiar with the concept.
Duration Types
The different duration types in Filtrera are:
years
months
days
weeks
hours
minutes
seconds
milliseconds
(orms
)ticks
The type notation duration
(which can be used for parameters) is a union of all these duration types.
Syntax
Literal duration values are defined by <number> <type>
. The unit is always plural, even for a single value.
Examples
from 1 yearsfrom 2 monthsfrom 10 daysfrom 1 weeksfrom 5 hoursfrom 30 minutesfrom 45 secondsfrom 500 millisecondsfrom 1000 msfrom 10000 ticks
In these examples:
1 years
represents a duration of one year.2 months
represents a duration of two months.10 days
represents a duration of ten days.1 weeks
represents a duration of one week.5 hours
represents a duration of five hours.30 minutes
represents a duration of thirty minutes.45 seconds
represents a duration of forty-five seconds.500 milliseconds
and500 ms
both represent a duration of five hundred milliseconds.10000 ticks
represents a duration of ten thousand ticks.
Type Notation for duration
When defining parameters or variables, you can use the type notation duration
to represent any of the duration types.
Example
param timeout: durationlet message = $'The timeout duration is {timeout}.'from message
In this example, timeout
is defined as a duration
type, and message
incorporates timeout
.
Arithmetic Operations with Durations
Durations can be added and subtracted if they are of the same unit. For more details on arithmetic operations, refer to the Arithmetic article.
Example
let duration1 = 5 dayslet duration2 = 3 dayslet totalDuration = duration1 + duration2let remainingDuration = duration1 - duration2
from totalDurationfrom remainingDuration
In this example, totalDuration
is 8 days
, and remainingDuration
is 2 days
.
Literal Types
As with all other primitive types, duration types can be literal:
param myParam: 4 days|5 days
Practical Usage
Example: Calculating Future Dates
let startDate = 2023-01-01let duration = 10 dayslet endDate = startDate + duration
from endDate
In this example, startDate
is an instant
, duration
is 10 days
, and endDate
is calculated by adding duration
to startDate
.
Example: Using Different Duration Types
let projectDuration = 6 monthslet meetingLength = 1 hourslet responseTime = 30 minutes
let message = $'The project duration is {projectDuration}, each meeting lasts {meetingLength}, and the expected response time is {responseTime}.'from message
In this example, projectDuration
, meetingLength
, and responseTime
are different duration types, and message
incorporates all of them.
Summary
The duration
data type in Filtrera allows you to work with timespans effectively. By understanding its syntax and usage, including the different duration types and their literals, you can accurately represent and manipulate durations. The type notation duration
serves as a union of all duration types, providing flexibility in parameter and variable definitions. Additionally, durations of the same unit can be added and subtracted. For more details on arithmetic operations with durations, refer to the Arithmetic article. Leveraging these features enables you to create precise and reliable Filtrera programs that handle timespans with ease.