Skip to content

Instantly share code, notes, and snippets.

@luis-fss
Forked from robcowie/timestamp.py
Created November 16, 2012 12:16
Show Gist options
  • Save luis-fss/4086846 to your computer and use it in GitHub Desktop.
Save luis-fss/4086846 to your computer and use it in GitHub Desktop.
Insert date and time stamps in Sublime Text 2
# -*- coding: utf-8 -*-
from datetime import datetime
import sublime_plugin
class TimestampCommand(sublime_plugin.EventListener):
"""Expand `isoD`, `now`, `datetime`, `utcnow`, `utcdatetime`,
`date` and `time`
"""
def on_query_completions(self, view, prefix, locations):
if prefix in ('isoD', 'now', 'datetime'):
val = datetime.now().strftime('%Y-%M-%dT%H:%M:%S')
elif prefix in ('utcnow', 'utcdatetime'):
val = datetime.utcnow().strftime('%Y-%M-%dT%H:%M:%S')
elif prefix == 'date':
val = datetime.now().strftime('%Y-%M-%d')
elif prefix == 'time':
val = datetime.now().strftime('%H:%M:%S')
else:
val = None
return [(prefix, prefix, val)] if val else []
# another version from: http://www.sublimetext.com/forum/viewtopic.php?p=28232&sid=7379fd02389130bbd305363e4f4e7444#p28232
import datetime
import sublime_plugin
class InsertTimestampCommand(sublime_plugin.TextCommand):
def run(self, edit):
#generate the timestamp
timestamp_str = datetime.datetime.now().strftime("%d.%m.%Y %H:%M")
#for region in the selection
#(i.e. if you have multiple regions selected,
# insert the timestamp in all of them)
for r in self.view.sel():
#put in the timestamp
#(if text is selected, it'll be
# replaced in an intuitive fashion)
if r.size() > 0:
self.view.replace(edit, r, timestamp_str)
else:
self.view.insert(edit, r.begin(), timestamp_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment