Hacker News new | past | comments | ask | show | jobs | submit login

I've settled on a couple of helper functions in bash to accomplish a similar thing (daily journaling):

  #######################################
  # Normalizes date arguments            
  # Globals:                             
  #   None                               
  # Arguments:                           
  #   date (defaults to today)           
  # Returns:                             
  #   string of format 'YYYY-MM-DD'      
  #######################################
  function getDate {
      local DATE=${1:-$(date -I)};
      if [[ "${DATE}" =~ [+]+ ]]; then
          DATE=$(date -I -d" ${DATE} days");
      elif [[ "${DATE}" =~ ^- ]]; then
          DATE=$(date -I -d" ${DATE} days");
      elif [[ "${DATE}" == "tomorrow" ]]; then
          DATE=$(date -I -d" +1 days");
      elif [[ "${DATE}" == "yesterday" ]]; then
          DATE=$(date -I -d" -1 days");
      fi
      echo "${DATE}";
  }
  
  #######################################
  # Load a journal file                  
  # Globals:                             
  #   None                               
  # Arguments:                           
  #   date (defaults to today)           
  # Returns:                             
  #   None                               
  #######################################
  function journal {
      local DATE=$(getDate "$1");
      emacs ~/notes/journal/${DATE}.md;
  }



Nitpick: here you can use a `case` instead of `if [[ ... ]] elif [[ ... ]] elif ...`:

    case "$DATE" in
      +*|-*)
        DATE=$(date -I -d" $DATE days") ;;
      tomorrow)
        DATE=$(date -I -d" +1 days") ;;
      yesterday)
        DATE=$(date -I -d" -1 days") ;;
    esac




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: