datetime - Converting date/time and generating offsets in bash script -
i need script date/time conversion. should take input particular time. should generates series of offsets, in both hour:minute form , number of milliseconds elapsed.
for 03:00, example, should give 04:02:07 (3727000ms*) 05:04:14 (7454000ms), 06:06:21 etc...
how go doing bash script? ideally work on both mac os x , linux (ubuntu or debian).
- (1hr * 60 minutes/hr *60 seconds/minute *1000ms/sec )+(2min*60 sec/min * 1000ms/sec)+(7sec*1000ms/sec) = (60*60*1000)+(2*60*1000)+(7*1000) = 3727000
time2ms () { local time=$1 hour minute second hour=${time%%:*} minute=${time#$hour:} minute=${minute%:*} second=${time#$hour:$minute} second=${second/:} echo "$(( ( (10#${hour} * 60 * 60) + (10#${minute} * 60) + 10#${second} ) * 1000 ))" } ms2time () { local ms=$1 hour minute second ((second = ms / 1000 % 60)) ((minute = ms / 1000 / 60 % 60)) ((hour = ms / 1000 / 60 / 60)) printf '%02d:%02d:%02d\n' "$hour" "${minute}" "${second}" } show_offsets () { local time=$1 interval=$2 time_ms interval_ms new_time time_ms=$(time2ms "$time") interval_ms=$(time2ms "$interval") new_time=$(ms2time $((time_ms + interval_ms)) ) echo "$new_time (${interval_ms}ms)" } demos:
$ show_offsets 03:00 1:02:07 04:02:07 (3727000ms) $ show_offsets 03:00 2:04:14 05:04:14 (7454000ms) $ show_offsets 03:00 3:06:21 06:06:21 (11181000ms)
Comments
Post a Comment