Sublime Text3 Timestamp View Conversion Plugin Development

In the usual configuration table, time configuration is often used, such as the start and end of an activity. From readability, we like 2017-04-27 17:00:00 , and from a program perspective, we like to use 1493283600 . The former contains the concept of time zone, while the latter has nothing to do with the urban area, so it is generally recommended to directly use the digital timestamp format for configuration.

In actual configuration, it is still very cumbersome to use MySQL's FROM_UNIXTIME() and UNIX_TIMESTAMP functions, or to use web tools to view and convert timestamps. Suddenly I thought of why not just write a plug-in and check the conversion in the editor. Referring to some examples on the Internet and consulting Sublime's related API, the process is as follows.

1. Execute Tools -> Developer -> New Plugin in turn, create a new plug-in script and name it timestamp.py

2. The content of the script is the following code code, you can see the comments for details:

    from datetime import datetime
    import re
    import time
    import sublime
    import sublime_plugin

    def getParseResult(text):
        #patten1 匹配10位整数时间戳(1493283600)
        pattern1 = re.compile('^\d{10}')
        match1 = pattern1.match(text)

        #pattern2 匹配可读时间格式(2017-04-27 17:00:00)
        pattern2 = re.compile('^(\d{4})-(\d{1,2})-(\d{1,2})\s(\d{1,2}):(\d{1,2}):(\d{1,2})')
        match2 = pattern2.match(text)

        if text in ('now'):
            result = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        elif text in ('ts', 'timestamp'):
            result = str(time.time()).split('.')[0]
        elif match1:
            timestamp = int(match1.group(0))
            timeArray = time.localtime(timestamp)
            result = time.strftime('%Y-%m-%d %H:%M:%S', timeArray)
        elif match2:
            timeArray = time.strptime(text, "%Y-%m-%d %H:%M:%S")
            result = str(time.mktime(timeArray)).split('.')[0]
        return result

    class TimestampCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            for s in self.view.sel():
                if s.empty() or s.size() <= 1:
                    break

                # 只处理第一个Region
                text = self.view.substr(s)
                print(text)

                # 得到转换结果
                result = getParseResult(text)

                # 进行文本替换并弹窗显示
                self.view.replace(edit, s, result)
                self.view.show_popup(result, sublime.HIDE_ON_MOUSE_MOVE_AWAY, -1, 600, 600)
                break

3. Perform shortcut key bindings, execute Project -> Key Bindings in turn, and add code{ "keys": ["ctrl+t"], "command": "timestamp"}

Very simple, a Sublime Text3 plugin that can automatically convert timestamps is fine. Select the text and press the shortcut key CTRL+t, the effect is shown in the following figure:

The code is placed on Github , more welcome to visit the personal website Metazion

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325016588&siteId=291194637