From bb5cb79bd1736fc33f5959fafed207ca1dd0db6d Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 28 Dec 2019 23:43:08 +1300 Subject: Fix and refactor HTML timestamp updating --- vim/autoload/html.vim | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'vim') diff --git a/vim/autoload/html.vim b/vim/autoload/html.vim index 7ac4f9d3..72f29bb4 100644 --- a/vim/autoload/html.vim +++ b/vim/autoload/html.vim @@ -21,19 +21,48 @@ function! html#TidyBuffer() abort call winrestview(view) endfunction +let s:formats = { + \ 'human': '%a, %d %b %Y %T %Z', + \ 'machine': '%Y-%m-%dT%H:%M:%S.000Z', + \} + +function! s:Timestamp(time) abort + if exists('$TZ') + let tz = $TZ + endif + let $TZ = 'UTC' + let time = localtime() + let timestamp = {} + for key in keys(s:formats) + let timestamp[key] = strftime(s:formats[key], time) + endfor + if exists('tz') + let $TZ = tz + endif + return timestamp +endfunction + +let s:pattern = '\m\C' + \.'Last updated: ' + \.'' + " Update a timestamp function! html#TimestampUpdate() abort if !&modified return endif - let cv = winsaveview() - call cursor(1,1) - let li = search('\m\C^\s*Last updated: .\+$', 'n') - if li - let date = substitute(system('date -u'), '\n$', '', '') - let line = getline(li) - call setline(li, substitute(line, '\S.*', - \ 'Last updated: '.date.'', '')) + let lnum = search(s:pattern, 'nw') + if !lnum + return endif - call winrestview(cv) + let timestamp = s:Timestamp(localtime()) + let update = 'Last updated: ' + \.'' + let line = getline(lnum) + let line = substitute(line, s:pattern, update, '') + call setline(lnum, line) endfunction -- cgit v1.2.3