Skip to content

Instantly share code, notes, and snippets.

@trauber
Created January 28, 2020 20:10
Show Gist options
  • Save trauber/1a658a6771011fdc107aa483a58c4c31 to your computer and use it in GitHub Desktop.
Save trauber/1a658a6771011fdc107aa483a58c4c31 to your computer and use it in GitHub Desktop.

Latest Awk for My Timetrap/Pandoc/LaTeX Invoicing System

Output is YAML for use as Pandoc metadata. The YAML contains LaTeX strings. \quad and \hphantom come in handy for spacing LaTeX item lists in the invoice template I'm using.

# Process tsv generated from timetrap.rb display output
# into yaml for use with a LaTeX invoice.
# Provide invoice_num on command line: `awk -v invoice_num="blah balh"

BEGIN {
  FS = "\t"
  rate = 25
  idx = 0
  timestamp=systime()
  datadate=strftime("%b %e, %Y",timestamp)
}


# String manipulations to turn timetrap's dates
# into gawk datespecs and thence into
# seconds since the epoch.

function sste(timestring) {
  gsub("-"," ",timestring)
  gsub(":"," ",timestring)
  timestring=mktime(timestring)
  return timestring / 3600

}

# Calculate duration and 
# round it to precision 1.

function dur(t1,t2) {
  t1=sste(t1)
  t2=sste(t2)
  # This printf expression rounds to precision 1.
  # Yay! Gotta love awk.
  duration=sprintf("%.*f", 1, t2 - t1)
}


# Gather the arrays here.
# I have a lot of stray global variables in
# this main loop.

{
  if (NR==1) { next }
  # init clear variables
  tstart=""
  tend=""
  note=""
  cost=""
  tstart = $1
  tend = $2
  note = $3
  dur(tstart,tend)
  cost = duration * rate
  ary_cost[idx] = cost
  ary_duration[idx] = duration
  ary_note[idx] = note
  total += cost
  idx += 1
}




# Iterate arrays and output YAML/LaTeX.

END {
  print "---"
  print "date: " datadate
  print "invoicenumber: " invoice_num

  for (i in ary_duration) {
    sum=""
    sum = ary_duration[i] * rate
    sum = sprintf("%.2f USD", sum)
    printf "    - {description: \\\\"
    printf "textbullet~"
    printf ary_note[i] " "
    printf "(" ary_duration[i] " hr), "
    printf "price: " rate " USD/hr, sum: " sum
    printf "}\n"
  }
  total = sprintf("%.2f USD", total)
  print "total: " total 
  print "---"
}


# TODO
# - figure out how to scope the vars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment