Skip to content

Instantly share code, notes, and snippets.

@adelzhang
Created September 22, 2013 01:16
Show Gist options
  • Save adelzhang/6655746 to your computer and use it in GitHub Desktop.
Save adelzhang/6655746 to your computer and use it in GitHub Desktop.
Sublime Time-stamp
#! python
"""
TextCommand:
insert_timetamp
EventListener:
on_pre_save: update_timestamp
"""
import datetime
import sublime
import sublime_plugin
TIMESTAMP_PATTERN = 'Last Modified:\\s*[\'"]201[0-9]-\\d+-\\d+\\s+\\d+:\\d+:\\d+(\\.\\d+)?[\'"]'
class InsertTimestampCommand (sublime_plugin.TextCommand):
"""
Replace selection with a current timestamp in ISO format.
"""
def run (self, edit, **args):
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.view.replace (edit, self.view.sel()[0], timestamp)
class UpdateTimestampListener (sublime_plugin.EventListener):
"""
Search for a timestamp to update before saving the file.
Maybe to simple.
"""
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 = 'Last Modified: "%s"' % timestamp
edit = view.begin_edit()
view.replace (edit, region, replacement)
view.end_edit (edit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment