Created
April 9, 2018 22:34
-
-
Save sieste/932e81ba9d2eb9babf2f40496d0a80dd to your computer and use it in GitHub Desktop.
vimscript function to format a string that describes a date ('now', 'in 2 days 2pm', 'next week'), using pythons `dateparser` module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" vimscript function takes input string `inp`, hands it to python function `parse` (module `dateparser`), | |
" and outputs the parsed date in format `fmt`; return `inp` if string could not be parsed to valid date | |
" Example: | |
" :echo ParseDate('tomorrow', '%Y %m %d') | |
function! ParseDate(inp, fmt) | |
python << endpython | |
import vim, dateparser as dp | |
inp = vim.eval('a:inp') | |
fmt = vim.eval('a:fmt') | |
inp_parsed = dp.parse(inp, languages=['en','de'], | |
settings={'PREFER_DATES_FROM': 'future'}) | |
out = inp | |
if type(inp_parsed).__name__ == 'datetime': | |
out = inp_parsed.strftime(fmt) | |
vim.command("let s:ans = '%s'" % out) | |
endpython | |
return s:ans | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment