Time Functions¶
Functions for calculating policy dates and time-based adjustments.
$CalculatePolicyDates¶
Calculates policy inception and expiry dates based on business rules and the QuotePublicData environment variable. This function reads quote information from the environment, calculates appropriate dates considering business rules, and updates both the environment variables and QuotePublicData with the results.
Signature
Parameters
- None (uses
QuotePublicDatafrom environment)
Returns
- null
Environment Variables Used
QuoteType-"New Business","MTA"or"Renewals"(case-insensitive). Any other value, or a missingQuoteTypeorTimeZone, leaves the dates unchangedTimeZone- Windows/IANA time zone ID of the policy (e.g."GMT Standard Time")PolicyPeriod-"Annual"(default) or"Custom". A custom period requires an expiry dateUseServerSideTime- If true, usesInceptionDate/ExpiryDate(UTC). Otherwise usesInceptionDateLocalTime/ExpiryDateLocalTimeInceptionDate,ExpiryDate- Requested dates in UTCInceptionDateLocalTime,ExpiryDateLocalTime- Requested dates in the policy's local timeIsTest,IsAmendment,IsImported,ImportedPrice- Relax the start-date rules for test, amended and imported quotesMTAChanges- Used by the MTA strategyQuotePublicData- Updated with the results
Environment Variables Updated
InceptionDate- Calculated policy inception date (ISO 8601 format, UTC)ExpiryDate- Calculated policy expiry date (ISO 8601 format, UTC)PolicyDurationInDays- Number of days in the policy period, measured in the policy's local time (only set when greater than 0)QuotePublicData- Updated with calculated dates
Errors
Throws a PolicyDateException when:
TimeZoneis missing- a date is not in a date format
PolicyPeriodis unknown- a custom period has no expiry date
- for MTAs, the expiry date is before the inception date
Example
// QuotePublicData must be initialized with quote details first
// (typically done by the quoting system)
// Calculate policy dates
$CalculatePolicyDates();
// Access the calculated dates
inception = InceptionDate; // ISO 8601 format string
expiry = ExpiryDate; // ISO 8601 format string
duration = PolicyDurationInDays; // Number
// Dates are also available in QuotePublicData
policyData = QuotePublicData;
$Log("Policy runs from: " + policyData.inceptionDate + " to " + policyData.expiryDate);
Policy Date Business Rules¶
The $CalculatePolicyDates function uses strategies based on your product configuration to handle:
1. Start Date Validation¶
- Ensures start date is not in the past (beyond grace period)
- May enforce minimum future date (e.g., next day minimum)
- Handles same-day inception if allowed
// The function automatically handles date validation
// based on the requested date in QuotePublicData
$CalculatePolicyDates();
// Dates are validated and adjusted according to business rules
inception = $ToDate(InceptionDate);
2. Expiry Date Calculation¶
- Annual: the inception date is converted to the policy's local time, then one year is added and one day taken away. Expiry is the end of that day (23:59:59) in local time, converted back to UTC. Because the calculation runs in local time, expiry is always 23:59:59 local even when the policy year crosses a clock change.
- Custom: the supplied expiry date is used.
- MTA:
- If
MTAChangescontainsInceptionDate, inception is taken fromInceptionDate, and an annual policy gets a new expiry using the annual rule. - If
MTAChangescontainsExpiryDateLocalTime, a custom-period expiry is taken fromExpiryDateLocalTime. - Otherwise the existing
InceptionDateandExpiryDateare kept. - An expiry before inception throws.
- Handles leap years and month-end dates.
// For a 12-month UK policy starting 2024-03-15 (local time)
$CalculatePolicyDates();
// InceptionDate: 2024-03-15T00:00:00Z
// ExpiryDate: 2025-03-14T23:59:59Z
// PolicyDurationInDays: 365
Policy duration and clock changes
PolicyDurationInDays is the number of local-time days between inception and expiry, rounded up. It is worked out in local time because in UTC a policy that spans a clock change gains or loses an hour. For example, a single-trip policy over the autumn clock change measured in UTC would be rounded up to one extra day and charged for it.
3. Renewal Alignment¶
- May align expiry to standard renewal dates
- Handles pro-rata adjustments for non-standard terms
Working with Policy Dates¶
Calculating Policy Duration¶
// Calculate policy dates first
$CalculatePolicyDates();
// Duration is automatically available
daysInPolicy = PolicyDurationInDays;
// Or calculate manually from dates
inception = $ToDate(InceptionDate);
expiry = $ToDate(ExpiryDate);
calculatedDays = $DifferenceInDays(expiry, inception);
Pro-Rata Calculations¶
For mid-term adjustments, calculate remaining time:
// Policy dates must be calculated first
$CalculatePolicyDates();
policyInception = $ToDate(InceptionDate);
policyExpiry = $ToDate(ExpiryDate);
MTADate = $Today();
daysRemaining = $DifferenceInDays(policyExpiry, MTADate);
daysCovered = $DifferenceInDays(MTADate, policyInception);
totalDays = PolicyDurationInDays;
proRataFactor = daysRemaining / totalDays;
// Apply pro-rata to premium changes
adjustedPremium = premiumChange * proRataFactor;
Age at Inception¶
Calculate customer age at policy start:
// Calculate policy dates
$CalculatePolicyDates();
policyStart = $ToDate(InceptionDate);
ageAtInception = $CalculateAge(policyStart, customerDOB);
// Use age for rating
if (ageAtInception < 25) {
$AddFactorToPricingMatrix("Young Driver", 1.50, true, 0);
}
Integration with Quote Calculations¶
// Complete new business flow with policy dates
function ProcessNewBusiness() {
// QuotePublicData should already contain quote details
// Calculate policy dates from QuotePublicData
$CalculatePolicyDates();
// Access calculated dates
inception = $ToDate(InceptionDate);
expiry = $ToDate(ExpiryDate);
duration = PolicyDurationInDays;
// Calculate age at inception
ageAtInception = $CalculateAge(inception, Customer.DateOfBirth);
// Build pricing based on inception date
$SetCurrencyToPricingMatrix("GBP");
// Age-based pricing
basePremium = 400.00;
if (ageAtInception < 25) {
basePremium = 600.00;
} else if (ageAtInception > 70) {
basePremium = 550.00;
}
$AddItemToPricingMatrix("Base Premium", basePremium, 1.0);
// Calculate premium
$CalculatePremiumAdditive("NewBusiness");
return {
"InceptionDate": inception,
"ExpiryDate": expiry,
"Duration": duration,
"Premium": PricingMatrix.Premium,
"AgeAtInception": ageAtInception
};
}
quote = ProcessNewBusiness();
Renewal Date Calculations¶
// Calculate renewal date (day before expiry + 1 year)
function CalculateRenewalDates() {
// Current policy dates
currentExpiry = $ToDate(ExpiryDate);
renewalInception = $AddDays(currentExpiry, 1);
renewalExpiry = $AddYears(renewalInception, 1);
renewalExpiry = $AddDays(renewalExpiry, -1); // Day before
return {
"InceptionDate": renewalInception,
"ExpiryDate": renewalExpiry
};
}
// Use for renewal quotes
renewalDates = CalculateRenewalDates();
MTA Date Validation¶
// Validate MTA date is within policy period
function ValidateMTADate(mtaDate) {
inception = $ToDate(InceptionDate);
expiry = $ToDate(ExpiryDate);
if (mtaDate < inception) {
return {
"IsValid": false,
"Message": "MTA date cannot be before policy inception"
};
}
if (mtaDate > expiry) {
return {
"IsValid": false,
"Message": "MTA date cannot be after policy expiry"
};
}
return { "IsValid": true };
}
// Use in MTA workflow
$CalculatePolicyDates(); // Ensure dates are calculated
MTADate = $ToDate(userInput.MTADate);
validation = ValidateMTADate(MTADate);
if (!validation.IsValid) {
$Referral("MTA-DATE", validation.Message, "Level 1");
} else {
$CalculateMTAPremium(15.00);
}
Cooling-Off Period¶
// Check if cancellation is within cooling-off period
function IsWithinCoolingOff(cancellationDate) {
inception = $ToDate(InceptionDate);
daysSinceInception = $DifferenceInDays(cancellationDate, inception);
return daysSinceInception <= 14; // 14-day cooling-off
}
// Use for cancellation
$CalculatePolicyDates(); // Ensure dates are available
CancellationDate = $Today();
withinCoolingOff = IsWithinCoolingOff(CancellationDate);
if (withinCoolingOff) {
$CalculateFullCancellation(0.00, true); // Full refund
} else {
$CalculateProRataCancellation(25.00, false); // Pro-rata with fee
}
Best Practices¶
1. Calculate Dates Early¶
// Calculate policy dates at the start of your quote flow
$CalculatePolicyDates();
// Dates are now available throughout the script
inception = $ToDate(InceptionDate);
expiry = $ToDate(ExpiryDate);
2. Access Dates from Environment¶
// After calculating policy dates, access them from environment variables
$CalculatePolicyDates();
inception = InceptionDate; // ISO 8601 string
expiry = ExpiryDate; // ISO 8601 string
duration = PolicyDurationInDays; // Number
// Or from QuotePublicData
quoteData = QuotePublicData;
formattedInception = quoteData.inceptionDateFormatted;
3. Convert to Date Objects for Calculations¶
// Environment variables contain ISO 8601 strings
// Convert to Date objects for date arithmetic
inceptionDate = $ToDate(InceptionDate);
expiryDate = $ToDate(ExpiryDate);
// Now you can perform date calculations
daysBetween = $DifferenceInDays(expiryDate, inceptionDate);
4. Handle Time Zones via QuotePublicData¶
// QuotePublicData contains timezone information
quoteData = QuotePublicData;
timeZone = quoteData.timeZoneId;
// Use timezone-aware age calculations when needed
inceptionDate = $ToDate(InceptionDate);
ageAtInception = $CalculateAge(
inceptionDate,
Customer.DateOfBirth,
timeZone
);
Environment Variables¶
| Variable | Type | Description |
|---|---|---|
QuoteType (input) |
String | "New Business", "MTA" or "Renewals" |
TimeZone (input) |
String | Policy time zone ID |
PolicyPeriod (input) |
String | "Annual" (default) or "Custom" |
UseServerSideTime (input) |
Bool | Use UTC dates rather than local-time dates |
InceptionDateLocalTime / ExpiryDateLocalTime (input) |
String | Requested dates in local time |
InceptionDate (in/out) |
String | Inception date (ISO 8601, UTC) |
ExpiryDate (in/out) |
String | Expiry date (ISO 8601, UTC) |
PolicyDurationInDays |
Number | Number of local-time days in policy period |
QuotePublicData (output) |
Object | Updated with calculated dates and formatting |
Next Steps¶
- Quote Calculation Functions - Use policy dates in calculations
- Examples - See policy dates in MTA workflow