

# Time-based 政策支持
<a name="policy-time-based"></a>

中的策略通过`context.system.now`日期时间值 AgentCore 支持 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")
};
```

 **用例：**仅允许在工作时间（世界标准时间每天上午 9 点至下午 5 点）内退款。

## 合并的日期和时间限制
<a name="policy-time-combined"></a>

将绝对日期与每日时间限制相结合。

### 示例：使用每日工时进行 Limited-time 促销
<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 点之间，才允许使用折扣工具。

## 时区处理
<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>
+  **明确时间** — 使用 “世界标准时间上午 9 点至下午 5 点” 而不是 “工作时间”
+  **始终指定 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"
```