

# 時間型政策支援
<a name="policy-time-based"></a>

AgentCore 中的政策透過`context.system.now`日期時間值支援 Cedar 政策中的時間型限制。這可讓您根據特定日期、時間或時間範圍強制執行政策。

**Topics**
+ [運作方式](#policy-time-based-how)
+ [絕對日期和時間範圍限制](#policy-time-absolute)
+ [每日週期性時間限制](#policy-time-daily)
+ [合併日期和時間限制](#policy-time-combined)
+ [時區處理](#policy-time-timezone)
+ [使用自然語言產生以時間為基礎的政策](#policy-time-natural-language)

## 運作方式
<a name="policy-time-based-how"></a>

在政策評估期間，目前 UTC 時間戳記會做為評估內容的一部分提供：

```
// Current datetime in UTC
context.system.now
```

您可以使用 Cedar 的日期時間函數來建立以時間為基礎的條件：
+  `datetime("YYYY-MM-DDTHH:MM:SSZ")` — 建立日期時間值
+  `duration("Xh")` — 建立持續時間 （小時、分鐘、秒）
+  `.toTime()` — 從日期時間擷取一天中的時間
+ 比較運算子：`<`、`⇐`、`>`、`>=`、 `==`

## 絕對日期和時間範圍限制
<a name="policy-time-absolute"></a>

在特定行事曆期間內強制執行政策。

### 範例：促銷期間政策
<a name="policy-time-absolute-example"></a>

```
permit(
  principal,
  action == AgentCore::Action::"RefundToolTarget___refund",
  resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-east-1:123456789012:gateway/my-gateway"
)
when {
  context.system.now >= datetime("2025-01-01T00:00:00Z") &&
  context.system.now < datetime("2025-01-31T23:59:59Z")
};
```

 **使用案例：**僅允許在 2025 年 1 月期間退款。

## 每日週期性時間限制
<a name="policy-time-daily"></a>

根據每日重複的時間強制執行政策。

### 範例：上班時間政策
<a name="policy-time-daily-example"></a>

```
permit(
  principal,
  action == AgentCore::Action::"RefundToolTarget___refund",
  resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-east-1:123456789012:gateway/my-gateway"
)
when {
  duration("9h") <= context.system.now.toTime() &&
  context.system.now.toTime() <= duration("17h")
};
```

 **使用案例：**僅允許在上班時間 （每天 UTC 上午 9 點至下午 5 點） 退款。

## 合併日期和時間限制
<a name="policy-time-combined"></a>

結合絕對日期與每日時間限制。

### 範例：具有每日小時數的有限時間提升
<a name="policy-time-combined-example"></a>

```
permit(
  principal,
  action == AgentCore::Action::"DiscountToolTarget___apply_discount",
  resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-east-1:123456789012:gateway/my-gateway"
)
when {
  // Valid dates: Feb 1-28, 2025
  context.system.now >= datetime("2025-02-01T00:00:00Z") &&
  context.system.now < datetime("2025-03-01T00:00:00Z") &&
  // Valid hours: 9am-9pm UTC daily
  duration("9h") <= context.system.now.toTime() &&
  context.system.now.toTime() <= duration("21h")
};
```

 **使用案例：**僅允許在 2025 年 2 月期間，每天上午 9 點到下午 9 點 UTC 使用折扣工具。

## 時區處理
<a name="policy-time-timezone"></a>

所有日期時間值都必須是 UTC。政策引擎不支援時區轉換或時區感知政策。

在政策中指定時間時，請務必使用 UTC。如果您的業務在不同的時區營運，請在建立政策之前將本機時間轉換為 UTC。

## 使用自然語言產生以時間為基礎的政策
<a name="policy-time-natural-language"></a>

政策撰寫服務可以從自然語言描述產生以時間為基礎的政策。

### 範例：產生營業時間政策
<a name="policy-time-nl-example"></a>

```
aws bedrock-agentcore-control start-policy-generation \
  --policy-engine-id MyEngine-abc123 \
  --name BusinessHoursOnly \
  --content '{
    "rawText": "Allow refunds only during business hours 9am to 5pm UTC"
  }' \
  --resource '{
    "arn": "arn:aws:bedrock-agentcore:us-east-1:123456789012:gateway/MyGateway-xyz789"
  }'
```

產生的政策：

```
permit(
  principal,
  action == AgentCore::Action::"RefundToolTarget___refund",
  resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-east-1:123456789012:gateway/MyGateway-xyz789"
)
when {
  duration("9h") <= context.system.now.toTime() &&
  context.system.now.toTime() <= duration("17h")
};
```

### 自然語言的最佳實務
<a name="policy-time-nl-best-practices"></a>
+  **明確了解時間** — 使用「9am to 5pm UTC」而不是「上班時間」
+  **一律指定 UTC** — 包含 "UTC" 以避免模棱兩可的情況
+  **針對日期使用 ISO 格式** — 使用 "2025-01-01" 而非 "1 月 1 日"
+  **提供特定時間範圍** — 避免含糊的詞彙，例如「白天」或「下班後」

自然語言提示的良好範例：

```
"Allow refunds only between 9am and 5pm UTC"
"Allow payments except between 2am and 4am UTC daily"
"Allow discounts only from 2025-02-01 to 2025-02-28"
"Permit high-value transactions between 8am and 8pm UTC"
```