 
         # Execute the callback with the result
         callback found_entry
+
+  # Takes a Date object and figures out which day in its
+  # year it represents and returns that one. Leap years
+  # are honoured.
+  day_of_year: (date) ->
+    start = new Date(date.getFullYear(), 0, 0)
+    return Math.ceil((date - start) / 86400000)

#     stops the first active timer it can find. The project and
#     task arguments may be abbreviated as with start.
#
#   hubot daily harvest [of <user>]
#     Hubot responds with your/a specific user's entries
#     for today.
#
#   hubot list harvest tasks [of <user>]
#     Gives you a list of all project/task combinations available

    msg.reply "Okay, I erased your credentials from my memory."

  # Retrieve your or a specific user's timesheet for today.
  robot.respond /daily harvest( of (\w+))?( at (\d{4})-(\d{2})-(\d{2}))?/i, (msg) ->
    unless user = check_user(robot, msg, msg.match[2])
      return
    harvest = new HarvestService(user.harvest_account)

    if msg.match[3]
      target_date = new Date(parseInt(msg.match[4]), parseInt(msg.match[5] - 1), parseInt(msg.match[6])) # Month starts at 0

    try
      if target_date
        harvest.daily_at msg, target_date, (status, body) ->
          if 200 <= status <= 299
            if body.day_entries.length == 0
              msg.reply "#{user.name} has no entries on #{target_date}."
            else
              msg.reply "#{user.name}'s entries on #{target_date}:"
              
            for entry in body.day_entries
              if entry.ended_at == ""
                msg.reply "• #{entry.project} (#{entry.client}) → #{entry.task} <#{entry.notes}> [running since #{entry.started_at} (#{entry.hours}h)]"
              else
                msg.reply "• #{entry.project} (#{entry.client}) → #{entry.task} <#{entry.notes}> [#{entry.started_at} – #{entry.ended_at} (#{entry.hours}h)]"
          else
            msg.reply "Request failed with status #{status}."
      else
        harvest.daily msg, (status, body) ->
          if 200 <= status <= 299
            msg.reply "Your entries for today, #{user.name}:"
            for entry in body.day_entries
              if entry.ended_at == ""
                msg.reply "• #{entry.project} (#{entry.client}) → #{entry.task} <#{entry.notes}> [running since #{entry.started_at} (#{entry.hours}h)]"
              else
                msg.reply "• #{entry.project} (#{entry.client}) → #{entry.task} <#{entry.notes}> [#{entry.started_at} – #{entry.ended_at} (#{entry.hours}h)]"
          else
            msg.reply "Request failed with status #{status}."
    catch error
      msg.reply("Fatal error: #{error}")

