Here's the code for my Sublime Text time recorder script.
This is actually just a Python 3 port of my Notepad++ Python time recorder script
import sublime, sublime_plugin
import time
import datetime
class mbtimerec(sublime_plugin.TextCommand):
def run(self, edit):
time = datetime.datetime.now()
date = time.strftime( '%d/%m/%Y' )
''' Round to nearest 5 minutes '''
time = time - datetime.timedelta(minutes=time.minute % 2.5,
seconds=time.second,
microseconds=time.microsecond)
discard = datetime.timedelta(minutes=time.minute % 5,
seconds=time.second,
microseconds=time.microsecond)
time -= discard
if discard >= datetime.timedelta(minutes=2.5):
time += datetime.timedelta(minutes=5)
time = time.strftime('%I:%M%p')
dt = date + " " + time
for region in self.view.sel():
selection = self.view.substr(self.view.line(self.view.sel()[0]))
if region.empty():
line = self.view.line(region)
if line.empty():
lineRegion = sublime.Region(line.begin(), line.end())
self.view.replace(edit, line, dt + ' *In progress*')
elif '*In progress*' in selection:
startTime = selection[:18] #TODO: make this more robust
startTime = datetime.datetime.strptime(startTime, '%d/%m/%Y %I:%M%p')
endTime = date + " " + time
endTime = datetime.datetime.strptime(endTime, '%d/%m/%Y %I:%M%p')
timeDiff = endTime - startTime
timeDiff = "(" + str(timeDiff)[:-3] + ")"
selection = selection.replace("*In progress*", "- " + time + " " + timeDiff + " ");
self.view.replace(edit, line, selection)
else: #Region not empty
totalHours = 0
totalMinutes = 0
lines = selection.split('\n')
for line in lines:
#Get duration i.e. (10:20)
duration = (line[28:36]) #This is not strictly necessary..
#Get hours
hours = duration[duration.find("(")+1:duration.find(":")]
minutes = duration[duration.find(":")+1:duration.find(")")]
if hours.isdigit():
totalHours += int(hours)
if minutes.isdigit():
totalMinutes += int(minutes)
totalHours += int(totalMinutes / 60)
totalMinutes = int(totalMinutes % 60)
if totalMinutes < 10:
totalMinutes = '0'+str(totalMinutes)
self.view.insert(edit, self.view.sel()[0].end(), "Total: (" + str(totalHours) + ":" + str(totalMinutes) + ")")
How to use:
Include this script as a plugin and assign a key to run it (I like to use F5).
The 'timer'
To start the 'timer', open a blank text file (or any file really) and press F5. You should see something similar to the following:
10/05/2015 08:15PM In progressTo stop the 'timer', ensure the same line is selected, then press F5 again. You should see something like this:
10/05/2015 08:15PM - 08:35PM (0:20)
Calculating total time spent
Highlight the lines created by the script and press F5 again. The total should be printed at the cursor position.
For example:
Highlight the following three lines:
07/05/2015 08:15PM - 08:35PM (0:20) 09/05/2015 08:40PM - 08:45PM (0:05) 10/05/2015 08:40AM - 08:45PM (12:05)And press F5:
07/05/2015 08:15PM - 08:35PM (0:20) 09/05/2015 08:40PM - 08:45PM (0:05) 10/05/2015 08:40AM - 08:45PM (12:05) Total: (12:30)
Note:
You can record time periods longer than 23:59, resulting in something like the following:
08/05/2015 08:45PM - 08:50PM (2 days, 0:05)
but the total calculator won't work. If you want to calculate periods > 24 hours at a time, you'll need to add this functionality yourself.
Github repo here - likely to contain the most up-to-date code