Updated Sublime Text timestamp command
After my last post I was looking through some of my own uses of Python’s datetime
module, and one of the things I uncovered was a Sublime Text command I created years ago and have blogged about before. I don’t know what I was thinking when I originally created the class. Is that Python 2 code? No idea.
Here’s my current version of that same module:
import sublime_plugin
import datetime
class Rfc3339DateCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(
edit,
self.view.sel()[0].begin(),
datetime.date.today().isoformat()
)
class Rfc3339DateTimeCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(
edit,
self.view.sel()[0].begin(),
datetime.datetime.now().astimezone().isoformat(timespec="seconds"),
)
class Rfc3339DateTimeUtcCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(
edit,
self.view.sel()[0].begin(),
datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds"),
)
(datetime.UTC
has been an alias for datetime.timezone.utc
since Python 3.11, but the current version of Sublime Text uses Python 3.8 at the time of writing this.)
When paired with appropriate entries in a .sublime-commands
file these three functions insert something like the following respectively:
2023-11-24
2023-11-24T14:03:58+11:00
2023-11-24T03:03:58+00:00