I have a log file which contains time stamps and logs.
For example
01:03:45  abc
01:03:47  bcd

I want to change time stamp by some time (for example 1 hour, 2
minutes and 3 seconds), so I want to increment each time stamp by
01:02:03.
________________________________________________________

function! IncTime(t, increment)
  let l:i_h = matchstr(a:increment, '^\d\d')
  let l:i_m = matchstr(a:increment, ':\zs\d\d\ze:')
  let l:i_s = matchstr(a:increment, ':\zs\d\d$')
  let l:h = matchstr(a:t, '^\d\d') + l:i_h
  let l:m = matchstr(a:t, ':\zs\d\d\ze:') + l:i_m
  let l:s = matchstr(a:t, ':\zs\d\d$') + l:i_s
  if l:s > 60
    let l:m += (l:s/60)
    let l:s = l:s % 60
  endif
  if l:m > 60
    let l:h += (l:m/60)
    let l:m = l:m % 60
  endif
return matchstr('0'.l:h, '..$').':'.matchstr('0'.l:m, '..$').':'.matchstr('0'.l:s, '..$')
  echo l:h . '/'.l:m.'/'.l:s
endfunc
" a test harness
$?^data:$?,$s/^\d\d:\d\d:\d\d/\=IncTime(submatch(0), '01:02:03')
finish

data:
01:03:45  abc
01:03:47  bcd
01:03:59  bcd
01:59:59  bcd
________________________________________________________

The above function takes a time as a string, and an increment as a string, and then returns the resulting time. The line under "a test harness" performs a demo, converting the lines at the bottom (from "data:" to the end), incrementing them by your mentioned "01:02:03".

Enjoy...

-tim



Reply via email to