aboutsummaryrefslogtreecommitdiffstats
path: root/timelog.sh
blob: 5b55ecfa3c6830a9f03afad87141246a037fe14e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/bash

# ripping off https://github.com/colindean/hejmo/blob/master/scripts/t

timelog="/home/lemon/Budget/ledger/timetracking.ledger"

# clock in
_t_in() {
  printf "i %s %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$*" >>  "$timelog"
}

# clock out
_t_out() {
  printf "o %s %s" "$(date '+%Y-%m-%d %H:%M:%S')" "$*" >>  "$timelog"
}

_t_cur() {
  sed -e '/^i/!d;$!d' "${timelog}"
}

_t_bal() {
	hledger -f "${timelog}" bal
}

_t_reg() {
	hledger -f "${timelog}" reg
}

# this doesn't work in OpenBSD because of the date issue
# look for another way to calcuate relative dates
_t_start() {
  if [[ ! -z "$(_t_cur)" ]]; then
    echo "Cannot add an entry when one is checked out. Run '$(basename $0) out' to check out first."
    exit 1
  fi
  local start_date="$(date --date "${1}" '+%Y-%m-%d %H:%M:%S')"
  if [[ -z "${start_date}" ]]; then
    exit 1 #rely on date to have shown the error
  fi
  echo i "${start_date}" "${2}" >> "${timelog}"
}

# this doesn't work in OpenBSD because of the date issue
# look for another way to calcuate relative dates
_t_end() {
  local end_date
  if [[ -z "${1}" ]]; then
    _t_out "${@}"
    return 0
  fi
  end_date="$(date --date "${1}" '+%Y-%m-%d %H:%M:%S')"
  if [[ -z "${end_date}" ]]; then
    exit 1 #rely on date to have shown the error
  fi
  echo o "${end_date}" >> "${timelog}"
}

# Explicitly log a start and end
_t_log() {
  _t_start "${1}" "${3}"
  _t_end "${2}"
}

action=$1; shift

case "$action" in
  in) _t_in "$@";;
  out) _t_out "$@";;
	cur) _t_cur "$@";;
	bal) _t_bal "$@";;
  reg) _t_reg "$@";;
#	log) _t_log "$@";;
esac

exit 0