# Triggered Policies

Triggered policies in our API measure the time between when a policy is triggered and when a job starts. The system supports four different time type measurements, each serving different business needs. Understanding how these time types work is crucial for setting up appropriate notification and escalation policies.

{% hint style="info" %}
The "trigger" time for short notice policies is determined by the request's **Requested At** timestamp. See [Requested At Timestamp](https://docs.octoo.com/general/requests/requested-timestamp) for details on how this value is set and how it can be backdated by admins.
{% endhint %}

**Assuming the following business hours for all time calculations below:**

* Monday through Friday: 8:00 AM - 5:00 PM
* Weekends: Closed (Saturday & Sunday)
* Holidays: Closed (when observed)

Each partner can set their own business hours, but the default is as above.

## 1. Calendar Hours

Definition: Measures the actual elapsed time in hours/minutes between trigger and job start, regardless of business hours, weekends, or holidays.

How it works:

* Counts every minute continuously
* Returns the total number of minutes between trigger and start time
* Rounds up to the nearest minute

Examples:

| Scenario           | Triggered At                | Job Starts At                      | Calendar Hours Result    |
| ------------------ | --------------------------- | ---------------------------------- | ------------------------ |
| Same day           | Friday 2:00 PM              | Friday 4:00 PM                     | 120 minutes (2 hours)    |
| Over weekend       | Friday 2:00 PM              | Monday 2:00 PM                     | 4,320 minutes (72 hours) |
| Over holiday       | Thursday 2:00 PM (July 1st) | Monday 2:00 PM (July 5th, holiday) | 5,760 minutes (96 hours) |
| Late night trigger | Thursday 10:00 PM           | Friday 2:00 PM                     | 960 minutes (16 hours)   |

## 2. Calendar Days

Definition: Counts the number of complete calendar days between trigger and job start.

How it works:

* Only counts full days that fall completely between the trigger and start dates
* Does not count the trigger day or start day themselves
* Returns 0 if triggered and started on the same day

Examples:

| Scenario       | Triggered At      | Job Starts At  | Calendar Days Result       |
| -------------- | ----------------- | -------------- | -------------------------- |
| Same day       | Friday 9:00 AM    | Friday 5:00 PM | 0 days                     |
| Next day       | Thursday 2:00 PM  | Friday 2:00 PM | 0 days                     |
| Two days later | Wednesday 2:00 PM | Friday 2:00 PM | 1 day (Thursday)           |
| Over weekend   | Friday 2:00 PM    | Monday 2:00 PM | 2 days (Saturday & Sunday) |
| Week span      | Saturday 2:00 PM  | Monday 2:00 PM | 1 day (Sunday)             |

## 3. Business Hours

Definition: Measures working hours between trigger and job start, only counting time during business hours.

How it works:

* Only counts minutes during business hours (8 AM - 5 PM, Monday-Friday)
* If triggered on the same day as the job starts, returns 0
* If triggered outside business hours, calculation starts from the next business hour
* If job starts outside business hours, calculation ends at the last business hour
* Returns total business minutes (displayed as hours in policies)

Important Business Hour Rules:

* Business day starts at 8:00 AM sharp
* Business day ends at 5:00 PM sharp
* Triggers before 8:00 AM are treated as starting at 8:00 AM
* Triggers after 5:00 PM are treated as starting the next business day at 8:00 AM

Examples:

| Scenario              | Triggered At      | Job Starts At  | Business Hours Result    |
| --------------------- | ----------------- | -------------- | ------------------------ |
| Same business day     | Friday 10:00 AM   | Friday 2:00 PM | 0 minutes                |
| Next business day     | Thursday 2:00 PM  | Friday 2:00 PM | 1,440 minutes (24 hours) |
| After hours trigger   | Thursday 10:00 PM | Friday 2:00 PM | 0 minutes                |
| Weekend trigger       | Saturday 2:00 PM  | Monday 2:00 PM | 0 minutes                |
| Early morning trigger | Wednesday 2:00 AM | Friday 2:00 PM | 4,320 minutes (72 hours) |
| Cross weekend         | Friday 2:00 PM    | Monday 2:00 PM | 1,440 minutes (24 hours) |
| Before business hours | Thursday 2:00 AM  | Friday 2:00 PM | 2,880 minutes (48 hours) |

## 4. Business Days

Definition: Counts the number of complete business days between trigger and job start.

How it works:

* Only counts full business days (8 AM - 5 PM periods)
* A trigger before 8:00 AM counts that day as a full business day
* A trigger after 8:00 AM does not count that day as a full business day
* Does not count weekends or holidays
* Returns 0 if triggered and started on the same day

Examples:

| Scenario                  | Triggered At      | Job Starts At            | Business Days Result |
| ------------------------- | ----------------- | ------------------------ | -------------------- |
| Same day                  | Friday 10:00 AM   | Friday 5:00 PM           | 0 days               |
| Early trigger, same week  | Wednesday 2:00 AM | Friday 2:00 PM           | 2 days (Wed & Thu)   |
| Normal trigger, same week | Wednesday 9:00 AM | Friday 2:00 PM           | 1 day (Thursday)     |
| Over weekend              | Friday 2:00 PM    | Monday 2:00 PM           | 0 days               |
| Early Friday trigger      | Friday 7:00 AM    | Monday 2:00 PM           | 1 day (Friday)       |
| Weekend trigger           | Saturday 2:00 PM  | Monday 2:00 PM           | 0 days               |
| Holiday Monday            | Thursday 2:00 PM  | Monday 2:00 PM (holiday) | 1 day (Friday)       |
| Multiple days             | Monday 9:00 AM    | Wednesday 2:00 PM        | 1 day (Tuesday)      |

## Special Considerations

**Holidays**

* Holidays are treated as non-business days
* Calendar time types continue counting through holidays
* Business time types skip holidays entirely

**Weekend Handling**

* Calendar Types: Count weekends normally
* Business Types: Skip weekends completely

## Edge Cases

1. Same-Day Triggers: Business hours/days always return 0 if the trigger and start are on the same calendar day
2. After-Hours Adjustments:

* Triggers after 5:00 PM are treated as starting the next business day at 8:00 AM
* Triggers before 8:00 AM may count that day as a full business day (for business\_days type)

3. Cross-Weekend Policies:

* Friday 2:00 PM trigger for Monday 2:00 PM start:
  * Calendar: 72 hours
  * Business: 24 hours (only counts Friday 2-5 PM and Monday 8 AM-2 PM)

4. 48-Hour Threshold:

* Many business hour calculations use a 48-hour business time threshold
* This equals 2 full business days or 6 full 8-hour business periods
