Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse a date in VimScript

Tags:

vim

viml

Given a date string of the form "2012-09-31", I'd like a vimscript function to parse it so I can calculate relative dates. Pure vimscript or python solutions are welcome.

like image 432
Dave Ray Avatar asked Oct 07 '22 17:10

Dave Ray


2 Answers

For normal mode manipulation of dates there is the superb SpeedDating plugin from Tim Pope. In principle it should be possible to extract some of the internal functions of this plugin to use in other scripts, but I don't know how easy that is in practice.

like image 84
Prince Goulash Avatar answered Oct 10 '22 02:10

Prince Goulash


For what it's worth, here's my current solution. Better suggestions or improvements are welcome.

function! AdjustDate(date, offset)
python << EOF
import vim
import datetime

result = datetime.datetime.strptime(vim.eval("a:date"), "%Y-%m-%d") + \
        datetime.timedelta(days=int(vim.eval("a:offset")))
vim.command("let l:result = '" + result.strftime("%Y-%m-%d") + "'")
EOF
return result
endfunction
like image 43
Dave Ray Avatar answered Oct 10 '22 02:10

Dave Ray