本文介绍用于处理日期的 Bicep 函数。
dateTimeAdd
dateTimeAdd(base, duration, [format])
将持续时间添加到基值。 需要 ISO 8601 格式。
命名空间: sys。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
基础 | 是的 | 字符串 | 添加的起始日期/时间值。 使用 ISO 8601 时间戳格式。 |
持续时间 | 是的 | 字符串 | 要添加到基数的时间值。 它可以是负值。 使用 ISO 8601 持续时间格式。 |
格式 | 否 | 字符串 | 日期时间结果的输出格式。 如果未提供,则使用基值的格式。 使用 标准格式字符串 或 自定义格式字符串。 |
返回值
将持续时间值添加到基值的日期/时间值。
注解
该 dateTimeAdd
函数不考虑跨年, P1Y 应解释为 P365D,而 P1M 应解释为 P30D。 以下 Bicep 文件显示了一些示例:
output addOneYearNonLeap string = dateTimeAdd('2023-01-01 00:00:00Z', 'P1Y') //2024-01-01T00:00:00Z
output addOneYearLeap string = dateTimeAdd('2024-01-01 00:00:00Z', 'P1Y') //2024-12-31T00:00:00Z
output addOneMonthNonLeap string = dateTimeAdd('2023-02-01 00:00:00Z', 'P1M') //2023-03-03T00:00:00Z
output addOneMonthLeap string = dateTimeAdd('2024-02-01 00:00:00Z', 'P1M') //2023-03-02T00:00:00Z
在前面的示例中,将 2023 年视为非跃升年,将一年加到一年的结果为 2024-01-01T00:00:00Z。 相反,在 2024 年的开始日期中添加一年,即跃年,结果为 2024-12-31T00:00:00Z,而不是 2025-01-01T00:00:00Z,因为一个跃年包括 366 天而不是 365 天。 此外,在 2 月的第一天增加一个月时,跳跃和非跨越年份之间的区别变得明显,导致月结果不同。
例子
下面的示例演示了添加时间值的不同方式。
param baseTime string = utcNow('u')
var add3Years = dateTimeAdd(baseTime, 'P3Y')
var subtract9Days = dateTimeAdd(baseTime, '-P9D')
var add1Hour = dateTimeAdd(baseTime, 'PT1H')
output add3YearsOutput string = add3Years
output subtract9DaysOutput string = subtract9Days
output add1HourOutput string = add1Hour
使用基时间 2020-04-07 14:53:14Z
部署上述示例时,输出为:
名称 | 类型 | 价值 |
---|---|---|
add3YearsOutput | 字符串 | 2023/4/7 下午 2:53:14 |
subtract9DaysOutput | 字符串 | 2020/3/29 下午 2:53:14 |
add1HourOutput | 字符串 | 2020/4/7 下午 3:53:14 |
下一个示例演示如何设置自动化计划的开始时间。
param omsAutomationAccountName string = 'demoAutomation'
param scheduleName string = 'demSchedule1'
param baseTime string = utcNow('u')
var startTime = dateTimeAdd(baseTime, 'PT1H')
...
resource scheduler 'Microsoft.Automation/automationAccounts/schedules@2023-11-01' = {
name: concat(omsAutomationAccountName, '/', scheduleName)
properties: {
description: 'Demo Scheduler'
startTime: startTime
interval: 1
frequency: 'Hour'
}
}
dateTimeFromEpoch
dateTimeFromEpoch(epochTime)
将纪元时间整数值转换为 ISO 8601 日期/时间。
命名空间: sys。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
epochTime | 是的 | 整数 (int) | 要转换为日期/时间字符串的纪元时间。 |
返回值
ISO 8601 日期/时间字符串。
注解
此函数需要 Bicep CLI 0.5.X 或更高版本。
示例:
以下示例显示了纪元时间函数的输出值。
param convertedEpoch int = dateTimeToEpoch(dateTimeAdd(utcNow(), 'P1Y'))
var convertedDatetime = dateTimeFromEpoch(convertedEpoch)
output epochValue int = convertedEpoch
output datetimeValue string = convertedDatetime
输出为:
名称 | 类型 | 价值 |
---|---|---|
datetimeValue | 字符串 | 2023-05-02T15:16:13Z |
epochValue | int (整数) | 1683040573 |
dateTimeToEpoch
dateTimeToEpoch(dateTime)
将 ISO 8601 日期/时间字符串转换为纪元时间整数值。
命名空间: sys。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
dateTime | 是的 | 字符串 | 要转换为纪元时间的日期/时间字符串。 |
返回值
一个整数,表示 1970 年 1 月 1 日午夜后的秒数。
注解
此函数需要 Bicep CLI 0.5.X 或更高版本。
例子
以下示例显示了纪元时间函数的输出值。
param convertedEpoch int = dateTimeToEpoch(dateTimeAdd(utcNow(), 'P1Y'))
var convertedDatetime = dateTimeFromEpoch(convertedEpoch)
output epochValue int = convertedEpoch
output datetimeValue string = convertedDatetime
输出为:
名称 | 类型 | 价值 |
---|---|---|
datetimeValue | 字符串 | 2023-05-02T15:16:13Z |
epochValue | int (整数) | 1683040573 |
下一个示例使用纪元时间值设置密钥保管库中的密钥的过期时间。
@description('The location into which the resources should be deployed.')
param location string = resourceGroup().location
@description('The Tenant Id that should be used throughout the deployment.')
param tenantId string = subscription().tenantId
@description('The name of the existing User Assigned Identity.')
param userAssignedIdentityName string
@description('The name of the resource group for the User Assigned Identity.')
param userAssignedIdentityResourceGroupName string
@description('The name of the Key Vault.')
param keyVaultName string = 'vault-${uniqueString(resourceGroup().id)}'
@description('Name of the key in the Key Vault')
param keyVaultKeyName string = 'cmkey'
@description('Expiration time of the key')
param keyExpiration int = dateTimeToEpoch(dateTimeAdd(utcNow(), 'P1Y'))
@description('The name of the Storage Account')
param storageAccountName string = 'storage${uniqueString(resourceGroup().id)}'
resource userAssignedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' existing = {
scope: resourceGroup(userAssignedIdentityResourceGroupName)
name: userAssignedIdentityName
}
resource keyVault 'Microsoft.KeyVault/vaults@2021-10-01' = {
name: keyVaultName
location: location
properties: {
sku: {
name: 'standard'
family: 'A'
}
enableSoftDelete: true
enablePurgeProtection: true
enabledForDiskEncryption: true
tenantId: tenantId
accessPolicies: [
{
tenantId: tenantId
permissions: {
keys: [
'unwrapKey'
'wrapKey'
'get'
]
}
objectId: userAssignedIdentity.properties.principalId
}
]
}
}
resource kvKey 'Microsoft.KeyVault/vaults/keys@2021-10-01' = {
parent: keyVault
name: keyVaultKeyName
properties: {
attributes: {
enabled: true
exp: keyExpiration
}
keySize: 4096
kty: 'RSA'
}
}
resource storage 'Microsoft.Storage/storageAccounts@2021-04-01' = {
name: storageAccountName
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${userAssignedIdentity.id}': {}
}
}
properties: {
accessTier: 'Hot'
supportsHttpsTrafficOnly: true
minimumTlsVersion: 'TLS1_2'
encryption: {
identity: {
userAssignedIdentity: userAssignedIdentity.id
}
services: {
blob: {
enabled: true
}
}
keySource: 'Microsoft.Keyvault'
keyvaultproperties: {
keyname: kvKey.name
keyvaulturi: endsWith(keyVault.properties.vaultUri,'/') ? substring(keyVault.properties.vaultUri,0,length(keyVault.properties.vaultUri)-1) : keyVault.properties.vaultUri
}
}
}
}
utcNow
utcNow(format)
以指定格式返回当前 (UTC) 日期/时间值。 如果未提供任何格式,则使用 ISO 8601 (yyyyMMddTHHmmssZ
) 格式。
此函数只能在参数的默认值中使用。
命名空间: sys。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
格式 | 否 | 字符串 | 要转换为字符串的 URI 编码值。 使用 标准格式字符串 或 自定义格式字符串。 |
注解
只能在表达式中对参数的默认值使用此函数。 在 Bicep 文件中的其他任何位置使用此函数都会返回错误。 不允许在 Bicep 文件的其他部分使用该函数,因为每次调用该函数,都会返回不同的值。 使用相同的参数部署同一 Bicep 文件不能可靠地生成相同的结果。
如果使用 此选项将错误回滚 到较早成功的部署,并且前面的部署包含使用 utcNow 的参数,则不会重新评估该参数。 而是在回滚部署中自动重复使用以前部署中的参数值。
请小心重新部署依赖于 utcNow 函数的 Bicep 文件作为默认值。 如果重新部署且不提供参数的值,则会重新评估该函数。 若要更新现有的资源而不是新建资源,请传入以前部署中的参数值。
返回值
当前 UTC 日期/时间值。
例子
以下示例显示了日期/时间值的不同格式。
param utcValue string = utcNow()
param utcShortValue string = utcNow('d')
param utcCustomValue string = utcNow('M d')
output utcOutput string = utcValue
output utcShortOutput string = utcShortValue
output utcCustomOutput string = utcCustomValue
上述示例的输出根据每个部署的不同而异,但类似于:
名称 | 类型 | 价值 |
---|---|---|
utcOutput | 字符串 | 20190305T175318Z |
utcShortOutput | 字符串 | 03/05/2019 |
utcCustomOutput | 字符串 | 3 5 |
下一个示例演示如何在设置标记值时使用函数中的值。
param utcShort string = utcNow('d')
param rgName string
resource myRg 'Microsoft.Resources/resourceGroups@2024-03-01' = {
name: rgName
location: 'chinanorth2'
tags: {
createdDate: utcShort
}
}
output utcShortOutput string = utcShort
后续步骤
- 有关 Bicep 文件中各部分的说明,请参阅了解 Bicep 文件的结构和语法。