Bicep의 날짜 함수

이 문서에서는 날짜 작업을 위한 Bicep 함수를 설명합니다.

dateTimeAdd

dateTimeAdd(base, duration, [format])

기본 값에 기간을 추가합니다. ISO 8601 형식이 예상됩니다.

네임스페이스: sys.

매개 변수

매개 변수 필수 Type 설명
base string 더하기의 시작 날짜/시간 값. ISO 8601 타임스탬프 형식을 사용합니다.
duration string 베이스에 추가할 시간 값. 음수가 될 수 있습니다. ISO 8601 기간 형식을 사용합니다.
format 아니요 string 날짜/시간 결과의 출력 형식입니다. 제공되지 않는 경우, 기준 값의 형식이 사용됩니다. 표준 형식 문자열 또는 사용자 지정 형식 문자열을 사용합니다.

반환 값

기준 값에 기간 값을 더하여 생성되는 날짜/시간 값입니다.

설명

dateTimeAdd 함수는 윤년을 고려하지 않으며 P1YP365D로 해석되지만 P1MP30D로 해석되어야 합니다. 다음 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년을 비윤년으로 간주하면 연도의 첫째 날에 1년을 추가한 결과는 2024-01-01T00:00:00Z가 됩니다. 반대로 윤년인 2024년의 시작일에 1년을 추가하면 윤년이 365일이 아닌 366일로 구성된다는 점을 감안할 때 2025-01-01T00:00:00Z가 아니라 2024-12-31T00:00:00Z가 됩니다. 또한 2월 첫째 날에 1달을 더 추가하면 윤년과 비윤년의 차이가 명백해지며, 이로 인해 날짜 결과가 달라집니다.

예제

다음 예제에서는 시간 값을 더하는 여러 가지 방법을 보여 줍니다.

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 문자열 4/7/2023 2:53:14 PM
subtract9DaysOutput 문자열 3/29/2020 2:53:14 PM
add1HourOutput 문자열 4/7/2020 3:53:14 PM

다음 예제에서는 Automation 일정의 시작 시간을 설정하는 방법을 보여 줍니다.

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)

epoch 시간 정수 값을 ISO 8601 날짜/시간으로 변환합니다.

네임스페이스: sys.

매개 변수

매개 변수 필수 Type 설명
epochTime int 날짜/시간 문자열로 변환할 epoch 시간입니다.

반환 값

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 정수 1683040573

dateTimeToEpoch

dateTimeToEpoch(dateTime)

ISO 8601 날짜/시간 문자열을 epoch 시간 정수 값으로 변환합니다.

네임스페이스: sys.

매개 변수

매개 변수 필수 Type 설명
dateTime string epoch 시간으로 변환할 날짜/시간 문자열입니다.

반환 값

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 정수 1683040573

다음 예제에서는 epoch 시간 값을 사용하여 키 자격 증명 모음의 키 만료를 설정합니다.

@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.

매개 변수

매개 변수 필수 Type 설명
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 2019/03/05
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

다음 단계