Sublime Forum

Problem inserting formatted date

#1

I placed the python script below in my …/User directory, then mapped the command to the f5 key, and it works. What I cannot accomplish is inserting a formatted date like 02/08/2017. I’ve tried with the commented lines in the script below, but when I press f5, nothing.

I’m a sublime AND python noob!

{ “keys”: [“f5”], “command”: “insert_datetime”}

import sublime, sublime_plugin, time
class InsertDatetimeCommand(sublime_plugin.TextCommand):
def run(self, edit):
sel = self.view.sel();
for s in sel:
#now = datetime.datetime.now()
#self.view.replace(edit, s, now.strftime("%Y%m%dT%H%M%S"))
#self.view.replace(edit, s, str(datetime.date.today())
self.view.replace(edit, s, time.ctime())

0 Likes

#2

As you’ve outlined it in your message, this should work for you. Here I’ve redacted the commented out lines just to make the image as small as possible:

The code as you presented it above is not properly formatted python code (python is sensitive to indentation), so if that’s what the code you have currently looks like, that is the reason why it doesn’t work for you. In that case, you want to format it more the way you see here.

You may also want to verify that you’ve stored the code in the correct directory (i.e. User sounds more correct than ..\User, although it’s hard to tell from the way you’ve described it). Your User package is available by selecting Preferences > Browse Packages... from the menu and then going inside of the User folder that you see there.

When in doubt, if you select Tools > Developer > New Plugin... from the menu, Sublime will create an empty plugin for you, and will automatically save the file into the correct location for you when you save it.

As mentioned, the code that you have posted (except for the formatting) should work properly. There are some problems with the commented out lines of code, though:

now = datetime.datetime.now()
self.view.replace(edit, s, now.strftime("%Y%m%dT%H%M%S"))
self.view.replace(edit, s, str(datetime.date.today())

These lines won’t work as written because datetime isn’t known; for these to work, you need to add datetime to the list of items being imported at the top of the plugin, so that it knows what a datetime is.

Also, the last line has more open parentheses than it does close parentheses, which makes the compiler mad. To fix that you need to add another one to the end of the line:

import sublime, sublime_plugin, time, datetime

self.view.replace(edit, s, str(datetime.date.today()))

When working with creating your own plugin code, it’s a good idea to check out the Sublime console when things don’t seem to be working the way you expect them to. If anything is wrong with the code, it will show up there. You can view the console by pressing Ctrl+` or by selecting View > Show Console from the menu.

Sublime will reload the file as soon as you save it (this is also mentioned in the console when it happens), so you can tell right away if there is anything structurally wrong with the code (if you don’t see it saying it is reloading the plugin, you have it saved in the wrong location).

If that doesn’t trigger an error, you may still see one when you actually press the key, because code that is syntactically correct is not necessarily logically correct.

0 Likes

#3

You may also use https://packagecontrol.io/packages/InsertDate for your date and time insertion needs.

1 Like

#4

Thank you for your in-depth answer. I added datetime to the import as you demonstrated and it worked. I have a couple of questions regarding this:

  1. This script is called using “insert_datetime”. There is no reference to “insert_datetime” in the script whatsoever. How does this magical interpretation occur?

{ “keys”: [“f5”], “command”: “insert_datetime”}

  1. I tried importing strftime, but it was not found. I then tried to find/install it via package control but could not. Is it possible to import strftime?

Thanks again.

0 Likes

#5

The name of the command comes from the name of the class that implements it. There’s information on how that works here in the unofficial documentation, but in short, since the class is called InsertDatetimeCommand, Sublime converts it into insert_datetime when it loads the plugin.

strftime is a Python thing and isn’t specific to Sublime in any way, so there’s no package in Package Control that you need to add. There are a few versions of it available. It’s a method in things like datetime.date, datetime.time and datetime.datetime, where it formats the time represented by some specific instance of the class (in your case, now being an instance of datetime.datetime).

It’s also available as a function as time.strftime, in which case you need to explicitly tell it what date to format, and it assumes the current date and time if you don’t. So you can do something like this:

from time import strftime

print(strftime("%Y/%m/%d"))

The version of Python that Sublime uses internally is currently 3.3.6. I would highly recommend keeping a browser tab open to the Python Documentation for that version of Python, which is an invaluable and handy reference for all of things that you can do right in python itself.

It also contains a tutorial section that teaches you the basics to get you up to speed on the language.

0 Likes

#6

So if I installed Python 3.3.6 on my Windows 10 PC, would strftime then be available in Sublime?

import sublime, sublime_plugin, time, datetime, strftime
ImportError: No module named ‘strftime’

-Thanks again

0 Likes

#7

Sublime has its own built in version of Python that’s completely distinct from any version you might have installed on your computer, so installing another one doesn’t affect it at all.

The reason it doesn’t work is because it’s not a module that you can import, it’s a function in a module.

# does not work; there is no module named strftime
import strftime 

# tells python to import strftime from the time module
from time import strftime 

# An alternative to the above is to just import the time module. In
# that case, you have to call it as time.strftime
import time 

That’s straying outside of the bounds of Sublime and into a programming tutorial, however.

0 Likes

#8

Thanks again. I’ve been using Vim for decades, and used Emacs for about 5 years. I’m an old dog looking at learning some new tricks. Although I do have Vintage mode enabled! :slightly_smiling:

1 Like