Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Updated timestamp.py for Sublime Text 3. Automatically add (if you have the correct code in your template) and update timestamps to mark the date a file was created and date it was last modified.

View timestamp2.py
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
"""
Automatically add and update time stamps in your files.
On file open, will replace [timeStamp] with the current date and time.
Currently looks for two instances of this (example below).
To use, place the text [timeStamp] in your template file where you want it.
ex.
// CREATED: timeStamp
// MODIFIED: timeStamp
When you open this file for the first time, both [timeStamp] will change to
the current date and time. Every save, the MODIFIED line will update.
NOTE: After installing, don't open this file in Sublime Text because it will change
[timeStamp] in the code to the current time. Oops =]
"""
import datetime
import sublime
import sublime_plugin
 
TIMESTAMP_CODE = 'timeStamp'
TIMESTAMP_PATTERN = 'MODIFIED:\\s*20[0-9][0-9]-\\d+-\\d+\\s+\\d+:\\d+:\\d+(\\.\\d+)?'
 
class InsertTimestampListener (sublime_plugin.EventListener):
"""
When opening file, search for TIMESTAMP_CODE and replace it with current time
"""
def on_load (self, view):
region = view.find (TIMESTAMP_CODE, 1)
if region :
replacement = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
with Edit(view) as edit:
edit.replace (region, replacement)
region = view.find (TIMESTAMP_CODE, 1)
if region :
replacement = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
with Edit(view) as edit:
edit.replace (region, replacement)
 
class UpdateTimestampListener (sublime_plugin.EventListener):
"""
On save, search for the MODIFIED line, and replace timestamp with current time
"""
def on_pre_save (self, view):
region = view.find (TIMESTAMP_PATTERN, 1)
if region :
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
replacement = 'MODIFIED: %s' % timestamp
with Edit(view) as edit:
edit.replace (region, replacement)
 
# edit.py
# buffer editing for both ST2 and ST3 that "just works"
# cortesy of "Edit.py" ST2/3 Edit Abstraction. See: http://www.sublimetext.com/forum/viewtopic.php?f=6&t=12551
 
import sublime
import sublime_plugin
from collections import defaultdict
 
try:
sublime.edit_storage
except AttributeError:
sublime.edit_storage = {}
 
class EditStep:
def __init__(self, cmd, *args):
self.cmd = cmd
self.args = args
 
def run(self, view, edit):
if self.cmd == 'callback':
return self.args[0](view, edit)
 
funcs = {
'insert': view.insert,
'erase': view.erase,
'replace': view.replace,
}
func = funcs.get(self.cmd)
if func:
func(edit, *self.args)
 
 
class Edit:
defer = defaultdict(dict)
 
def __init__(self, view):
self.view = view
self.steps = []
 
def step(self, cmd, *args):
step = EditStep(cmd, *args)
self.steps.append(step)
 
def insert(self, point, string):
self.step('insert', point, string)
 
def erase(self, region):
self.step('erase', region)
 
def replace(self, region, string):
self.step('replace', region, string)
 
def callback(self, func):
self.step('callback', func)
 
def run(self, view, edit):
for step in self.steps:
step.run(view, edit)
 
def __enter__(self):
return self
 
def __exit__(self, type, value, traceback):
view = self.view
if sublime.version().startswith('2'):
edit = view.begin_edit()
self.run(edit)
view.end_edit(edit)
else:
key = str(hash(tuple(self.steps)))
sublime.edit_storage[key] = self.run
view.run_command('apply_edit', {'key': key})
 
 
class apply_edit(sublime_plugin.TextCommand):
def run(self, edit, key):
sublime.edit_storage.pop(key)(self.view, edit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.