#! /bin/zsh # # this file: # https://dataswamp.org/~incal/conf/.zsh/time time-add () { local times=($@) local epoch='jan 1 1970' local sum=0 for t in $times; do sum="$(date -u -d "$epoch $t" +%s) + $sum" done s2hms $(echo $sum | bc) } alias time-sum=time-add time-from () { local beg=$1 local end=${2:-now} local time time=$(dateutils.ddiff $beg $end -f ' %Yy %mm %dd %Hh %Mmin %Ss') time=${time// 0(s|min|h|d|m|y)/} time="${time:1}" echo $time } alias time-since=time-from cond () { # 7y 5m 8d - no Rita # 7y 4m 24d - no nicotine # 7y 4m 19d - food coma # 7y 3m 11d - no Gluten # 7y 2m - acupressure # 7y 1m 7d - no salt, spit time-from 2016-09-09 } time-to () { local end=$1 local beg=${2:-now} time-from $beg $end } alias time-until=time-to secs () { local s=$(date -d "1970-01-01 UTC $(date +%T)" +%s) local t=$(( 24 * 60 * 60 )) local p=$(( 100.0 * ${s}/${t} )) printf '%.1f%%\n' $p } print-time-label () { local local_time=$(date +" %H:%M ") print-label 3 0 $local_time } s2hms () { local secs=$1 local time_string time_string=$(units -t $secs\s hms) local data=("${(@s/;/)time_string}") local h=$data[1] local m=$data[2] local s=$data[3] local out [[ $h > 0 ]] && out="${h}h " [[ $m > 0 ]] && out+="${m}m " [[ $s > 0 ]] && out+="${s}s" echo $out } alias hms=s2hms clock-time () { local beg=$1 local end=${2:-now} local if='%H:%M:%S' local of='%0H:%0M:%0S' dateutils.ddiff -i $if $beg $end -f $of } now-ns () { echo $(($(date +%s)*10**9 + $(date +%N))) } unix-time () { date +%s } today () { date +"%Y-%m-%d" } # https://dataswamp.org/~incal/pimgs/comp/clocks.png long-date () { # sound local quiet=$1 local sound=~/bo/up.mp3 # date local month=$(date +%m) [[ month[1] == '0' ]] && month=$month[1,-1] # era sputnik local es="ES$(( $(date +%Y) - 1956 ))" # day local jour=$(date +%j) # space age local space=" " local year_string=$(date +"%Y-%m-%d${space}${jour}${space}${es}") local time_string=$(print-time-label) local date_string="$(date +"%a${space}%b")" # internet ping-test -q local ping_ok=$? local internet_string if (( $ping_ok == 0 )); then internet_string="$(print-internet-up)" if [[ $quiet != "-q" ]]; then play-sound $sound > /dev/null 2>&1 & fi else internet_string="$(print-internet-down)" fi # merge set-color yellow echo -n "${space}${year_string}${space}${time_string}" set-color yellow echo "${space}${date_string}${space}${internet_string}" reset-color } alias d='long-date -q'