Auto-update bind 9 zone database serial numbers

This post is helpful for configuring emacs to automatically update the “Serial” in bind zone databases. I wanted to do the same in Vim, but the solutions in the comments of that post didn’t work for me. Here’s a version that does. It’s yours for free under the Affero GPL v3 (or any later version, at your preference):

function s:BindZoneSettings()
    function s:UpdateBindZoneSerial(date, num)
        if (strftime("%Y%m%d") == a:date)
            return a:date . a:num+1
        return strftime("%Y%m%d") . '01'
    function s:ReplaceBindZoneSerialLine()
        :%s/\(2[0-9]\{7}\)\([0-9]\{2}\)\(\s*;\s*Serial\)/\=UpdateBindZoneSerial(submatch(1), submatch(2)) . submatch(3)/g
    autocmd BufWritePre /etc/bind/db.* call ReplaceBindZoneSerialLine()