Class DateTimeSubtractTime
- Namespace
- YndigoBlue.Velocity.Functions
- Assembly
- YndigoBlue.Velocity.dll
Represents a function that subtracts a Time-of-day value from a DateTime as a duration from midnight, returning a new DateTime.
public class DateTimeSubtractTime : Function, ICheckItem, IDefaultItem, IFilterItem, IElement
- Inheritance
-
DateTimeSubtractTime
- Implements
Remarks
DateTimeSubtractTime(datetime, time) → datetime shifted back by the time duration.
The time argument is treated as a duration rather than an absolute clock time: a value of
02:30:00 subtracts two hours and thirty minutes from the datetime regardless of its existing
time component.
Both arguments may be column references or literal values. To add a time duration instead, use DateTimeAddTime.
Examples
Recover the original datetime by subtracting a time offset column from an adjusted datetime:
var schema = manager.LoadSchema("scheduling");
var appointments = schema["appointments"];
var query = new Query()
.Select([ appointments["id"], new Expression("base_datetime", new DateTimeSubtractTime(appointments["adjusted_at"], appointments["offset_time"])) ])
.From(appointments);
var results = manager.Retrieve(query);
Use the column + TimeOnly constructor to subtract a fixed time-of-day offset from each datetime:
var schema = manager.LoadSchema("scheduling");
var appointments = schema["appointments"];
Column adjustedAtColumn = appointments["adjusted_at"];
// Remove the 08:30 offset that was added to all appointment datetimes
var startTime = new TimeOnly(8, 30, 0);
var query = new Query()
.Select([ appointments["id"], new Expression("original_at", new DateTimeSubtractTime(adjustedAtColumn, startTime)) ])
.From(appointments);
var results = manager.Retrieve(query);
Use the DateTime + TimeOnly constructor to subtract a fixed time duration from a literal datetime:
var adjustedDate = new DateTime(2024, 6, 1, 14, 0, 0);
var shift = new TimeOnly(14, 0, 0); // 14:00 = 14 hours
// Returns 2024-06-01 00:00:00
var expr = new DateTimeSubtractTime(adjustedDate, shift);
Constructors
- DateTimeSubtractTime(DateTime, TimeOnly)
Initializes a new instance of DateTimeSubtractTime with a DateTime literal and a TimeOnly literal.
- DateTimeSubtractTime(DateTime, Column)
Initializes a new instance of DateTimeSubtractTime with a DateTime literal and a time column.
- DateTimeSubtractTime(IElement, IElement)
Initializes a new instance of DateTimeSubtractTime.
- DateTimeSubtractTime(Column, TimeOnly)
Initializes a new instance of DateTimeSubtractTime with a DateTime column and a TimeOnly literal.
- DateTimeSubtractTime(Column, Column)
Initializes a new instance of DateTimeSubtractTime with both arguments as columns.