Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Sublime Text 2 Plugin: Timestamp for creation date and last modified date, automatically added and updated.

View timestamp.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
"""
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 :
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
edit = view.begin_edit()
view.replace(edit, region, timestamp)
view.end_edit (edit)
region = view.find (TIMESTAMP_CODE, 1)
if region :
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
edit = view.begin_edit()
view.replace(edit, region, timestamp)
view.end_edit (edit)
 
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
edit = view.begin_edit()
view.replace (edit, region, replacement)
view.end_edit (edit)

Updated search string so this code will work past the year 2020 :)

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.