你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Bicep 的日期函数

本文介绍用于处理日期的 Bicep 函数。

dateTimeAdd

dateTimeAdd(base, duration, [format])

向基础值加上一个持续时间。 需要 ISO 8601 格式。

命名空间:sys

参数

参数 必选 类型​​ 说明
base string 用于相加的起始日期/时间值。 使用 ISO 8601 时间戳格式
duration string 要与 base 相加的时间值。 它可以是负值。 使用 ISO 8601 持续时间格式
format string 日期时间结果的输出格式。 如果未提供,则将使用 base 值的格式。 使用标准格式字符串自定义格式字符串

返回值

将 duration 值与 base 值相加后得到的日期/时间值。

备注

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 部署了前面的示例,则输出为:

名称 类型 Value
add3YearsOutput String 4/7/2023 2:53:14 PM
subtract9DaysOutput String 3/29/2020 2:53:14 PM
add1HourOutput String 4/7/2020 3:53:14 PM

下一示例展示了如何设置自动化计划的开始时间。

param omsAutomationAccountName string = 'demoAutomation'
param scheduleName string = 'demSchedule1'
param baseTime string = utcNow('u')

var startTime = dateTimeAdd(baseTime, 'PT1H')

...

resource scheduler 'Microsoft.Automation/automationAccounts/schedules@2022-08-08' = {
  name: concat(omsAutomationAccountName, '/', scheduleName)
  properties: {
    description: 'Demo Scheduler'
    startTime: startTime
    interval: 1
    frequency: 'Hour'
  }
}

dateTimeFromEpoch

dateTimeFromEpoch(epochTime)

将纪元时间整数值转换为 ISO 8601 日期/时间。

命名空间:sys

参数

参数 必选 类型​​ 说明
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

参数

参数 必选 类型​​ 说明
dateTime string 要转换为纪元时间的日期/时间字符串。

返回值

一个整数,表示从 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

参数

参数 必选 类型​​ 说明
format string 要转换为字符串的 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 string 20190305T175318Z
utcShortOutput string 03/05/2019
utcCustomOutput string 3 5

以下示例演示在设置标记值时如何使用函数中的值。

param utcShort string = utcNow('d')
param rgName string

resource myRg 'Microsoft.Resources/resourceGroups@2022-09-01' = {
  name: rgName
  location: 'westeurope'
  tags: {
    createdDate: utcShort
  }
}

output utcShortOutput string = utcShort

后续步骤