Skip to content

Instantly share code, notes, and snippets.

@romanrabodzei
Created August 19, 2024 20:32
Show Gist options
  • Save romanrabodzei/184fb4c0b2c0c7dc36c270cd51cc1faf to your computer and use it in GitHub Desktop.
Save romanrabodzei/184fb4c0b2c0c7dc36c270cd51cc1faf to your computer and use it in GitHub Desktop.
Azure Bicep Function to calculate the StartDateTime (Azure Update Manager maintenance configuration)
@description('Custom start date for maintenance window. If not provided, current start date will be used. Format: yyyy-MM-dd')
param maintenanceStartDate string = ''
param currentStartDate string = utcNow('yyyy-MM-dd 00:00')
/* Due to limitations of the Bicep language, the error "StartDateTime should be after 15 minutes of creation" may occur.
To avoid this error, the start date is calculated as the next day. The following code is used to calculate the next day.
If the date is 28, the next day will be 01 next month. February has only 28 days, and 28 was taken just because of it. */
func calculateStartDate(currentStartDate string) object => {
day: int(take(skip(currentStartDate, 8), 2)) < 28 ? string(int(take(skip(currentStartDate, 8), 2)) + 1) : '01'
month: int(take(skip(currentStartDate, 8), 2)) < 28
? int(take(skip(currentStartDate, 5), 2)) <= 12 ? take(skip(currentStartDate, 5), 2) : '01'
: int(take(skip(currentStartDate, 5), 2)) + 1 < 9
? '0${string(int(take(skip(currentStartDate, 5), 2)) + 1)}'
: int(take(skip(currentStartDate, 5), 2)) + 1 > 12 ? '01' : string(int(take(skip(currentStartDate, 5), 2)) + 1)
year: int(take(skip(currentStartDate, 5), 2)) < 12
? string(take(currentStartDate, 4))
: string(int(take(currentStartDate, 4)) + 1)
}
func newStartDate(currentStartDate string) string =>
'${calculateStartDate(currentStartDate).year}-${calculateStartDate(currentStartDate).month}-${calculateStartDate(currentStartDate).day} 00:00'
output maintenanceStartTime string = maintenanceStartDate == ''
? newStartDate(currentStartDate)
: '${maintenanceStartDate} 00:00'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment