Skip to content

Instantly share code, notes, and snippets.

@twe4ked
Created May 28, 2018 00:04
Show Gist options
  • Save twe4ked/98f7b7aa19fd07396ff6081d7cba8b11 to your computer and use it in GitHub Desktop.
Save twe4ked/98f7b7aa19fd07396ff6081d7cba8b11 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Lazy sources:
#
# - https://stackoverflow.com/a/6018744
# - https://stackoverflow.com/a/4179491
require 'time'
def ranges_overlap?(a, b)
a.include?(b.begin) || b.include?(a.begin)
end
def merge_ranges(a, b)
[a.begin, b.begin].min..[a.end, b.end].max
end
def merge_overlapping_ranges(overlapping_ranges)
overlapping_ranges.sort_by(&:begin).inject([]) do |ranges, range|
if !ranges.empty? && ranges_overlap?(ranges.last, range)
ranges[0...-1] + [merge_ranges(ranges.last, range)]
else
ranges + [range]
end
end
end
# Find all timestamps in the git log
lines = `git log --format='format:%ai'`.split("\n")
# Pad them out to 30 minute ranges.
#
# This means that our calculations will assume you worked for 15 minutes before
# your first commit and 15 minutes after your last commit in a given session.
#
# A session is defined where the ranges overlap, so minimum of 30 minutes
# between a commit.
ranges_padded_to_30_mins = lines.map { |line| t = Time.parse(line); (t - 60 * 15)..(t + 60 * 15) }
# Merge into sessions and print out the duration of each session.
merge_overlapping_ranges(ranges_padded_to_30_mins).each_with_index do |range, i|
puts "Session #{i + 1}: #{(Time.mktime(0) + (range.end - range.begin)).strftime("%H:%M:%S")}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment