Skip to content

Instantly share code, notes, and snippets.

@xtman
Created August 21, 2012 01:33
Show Gist options
  • Save xtman/3410404 to your computer and use it in GitHub Desktop.
Save xtman/3410404 to your computer and use it in GitHub Desktop.
Calculate the duration of the specified mr study directory.
#!/bin/bash
function adjust() {
local cwd=$(pwd)
local sd=$1
local total=$2
local mtime1=0
local mtime2=0
local gap=0
cd ${sd}
for ssd in $(ls -t -r -d */)
do
if [[ ! ${ssd} =~ ^[0-9]+ ]]; then
continue
fi
if [[ $mtime1 -eq 0 ]]; then
mtime1=$(stat -c "%Y" ${ssd})
fi
mtime2=$(stat -c "%Y" ${ssd})
gap=$((mtime2-mtime1))
if [[ ${gap} -gt 86400 ]]; then
total=$((total-gap))
fi
mtime1=${mtime2}
done
cd ${cwd}
echo ${total}
}
CWD=$(pwd)
if [[ -z $1 || ! -d $1 ]]; then
echo "Usage:"
echo " $(basename $0) <study-directory>"
exit 1
fi
SDIR=$1
cd ${SDIR}
SDIR=$(pwd)
if [[ "$(basename $(dirname ${SDIR}))" != "nmr" ]]; then
echo "Error: ${SDIR} is not a valid directory." 1>&2
exit 1
fi
ADJUSTED=false
MIN_MTIME=$(for f in $(ls); do stat -c "%Y" $f; done | sort -n | head -n 1)
MAX_MTIME=$(for f in $(ls); do stat -c "%Y" $f; done | sort -n | tail -n 1)
if [[ ${MIN_MTIME} -lt ${START} ]]; then
continue
fi
SECONDS=$((MAX_MTIME-MIN_MTIME))
if [[ $SECONDS -gt 86400 ]]; then
SECONDS=$(adjust $SDIR $SECONDS)
ADJUSTED=true
fi
MINUTES=$(printf "%5.2f" $(echo "${SECONDS}/60" | bc -l))
HOURS=$(printf "%3.2f" $(echo "${SECONDS}/3600" | bc -l))
echo "\"$(basename ${SDIR})\", ${SECONDS}, ${MINUTES}, ${HOURS}, ${ADJUSTED}"
cd ${CWD}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment