Sublime Text3时间戳查看转换插件的开发

平常配置表中,经常需要用到时间配置,比如活动开始结束。从可读性上,我们喜欢2017-04-27 17:00:00,从程序角度,我们喜欢用1493283600。前者是包含时区概念的,而后者市区无关,所以一般推荐直接使用数字时间戳格式来配置。

实际配置时,之前一直用MySQL的FROM_UNIXTIME()UNIX_TIMESTAMP函数,或者使用网页工具进行时间戳查看转换,还是十分繁琐的。突然想到为什么不直接写个插件,在编辑器里查看转换就好了。参考了网络上的一些示例并查阅了Sublime的相关API,过程如下。

1.依次执行Tools -> Developer -> New Plugin,新建一个插件脚本,命名为timestamp.py

2.脚本内容为以下代码代码,具体可以看注释:

    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.进行快捷键绑定,依次执行Project -> Key Bindings,添加代码{ "keys": ["ctrl+t"], "command": "timestamp"}

很简单,一个能自动进行时间戳转换的Sublime Text3插件就好了。选中文本,按快捷键CTRL+t,效果见下图:

代码放在了Github,更多欢迎访问个人网站Metazion

猜你喜欢

转载自www.cnblogs.com/kaleovon/p/8963520.html