Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move xolox#misc#timer#format_timespan() → xolox#misc#format#timestamp()
- Loading branch information
Showing
5 changed files
with
72 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
" Human friendly string formatting for Vim. | ||
" | ||
" Author: Peter Odding <peter@peterodding.com> | ||
" Last Change: June 2, 2013 | ||
" URL: http://peterodding.com/code/vim/misc/ | ||
|
||
function! xolox#misc#format#timestamp(ts) " {{{1 | ||
" Format a time stamp (a string containing a formatted floating point | ||
" number) into a human friendly format, for example 70 seconds is phrased as | ||
" "1 minute and 10 seconds". | ||
let seconds = a:ts + 0 | ||
" Fast common case with extra precision from reltime(). | ||
if seconds < 5 | ||
let extract = matchstr(a:ts, '^\d\+\(\.0*[1-9][1-9]\?\)\?') | ||
if extract =~ '[123456789]' | ||
return extract . ' second' . (extract != '1' ? 's' : '') | ||
endif | ||
endif | ||
" Generic but slow code. | ||
let result = [] | ||
for [name, size] in [['day', 60 * 60 * 24], ['hour', 60 * 60], ['minute', 60], ['second', 1]] | ||
if seconds >= size | ||
let counter = seconds / size | ||
let seconds = seconds % size | ||
let suffix = counter != 1 ? 's' : '' | ||
call add(result, printf('%i %s%s', counter, name, suffix)) | ||
endif | ||
endfor | ||
" Format the resulting text? | ||
if len(result) == 1 | ||
return result[0] | ||
else | ||
return join(result[0:-2], ', ') . ' and ' . result[-1] | ||
endif | ||
endfunction |
This file contains 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
This file contains 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