5

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.

Xavier T.
  • 40,509
  • 10
  • 68
  • 97
Dave Ray
  • 39,616
  • 7
  • 83
  • 82

2 Answers2

2

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.

Prince Goulash
  • 15,295
  • 2
  • 46
  • 47
  • yep. I looked at it. It's very powerful, but not obvious how I would extract this little piece of functionality :) – Dave Ray Sep 07 '12 at 22:43
2

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
Dave Ray
  • 39,616
  • 7
  • 83
  • 82