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

很简单,一个方便查看转换时间戳的插件就写好了。选中文本,按快捷键CTRL+t,效果图:

代码放在了Github,更多欢迎访问个人网站Metazion
Sublime Text3时间戳查看转换插件的开发的更多相关文章
- Sublime Text3下的markdown插件的安装及配置
Sublime Text3下的markdown插件的安装及配置 安装准备--安装Package Control 安装MarkdownEditing 安装Markdown Preview或OmniMar ...
- Sublime Text3快捷键以及常用插件
工若善其事,必先利其器.做为一个web前端开发人员,必须有一个得心应手的编码工具,本人推荐Sublime Text3和WebStorm,但WebStorm太过智能和耗性能了对初学者来说弊大于利,所以我 ...
- sublime text3好用的插件
1.安装package control sublime text3 的安装方法,ctrl+`,调出控制台输入 import urllib.request,os; pf = 'Package Contr ...
- Sublime Text3使用及常用插件
1.安装packages组件: 参考一: https://sublime.wbond.net/installation 参考二: http://blog.csdn.net/superskk6/arti ...
- sublime text3 破解及常用插件
sublime text3 下载 破解 submelime Text > About sublime Text //看是否注册并 查看当前的版本 然后百度或google搜索 'sublime t ...
- sublime text3 安装package control 插件,解决访问被墙的问题
1.在github上下载Package Control的安装包 https://github.com/wbond/sublime_package_control 2.打开sublime存放插件的目录: ...
- Sublime Text3的快捷键和插件
今天重装了一下Sublime Text3,发现了一个不错的网站,关于Sublime Text3的插件安装介绍的很详细,还有右键增强菜单和浏览器打开快捷键的创建.奉上链接 http://www.cnbl ...
- Sublime Text3安装及常用插件安装
为了使用强大好用的代码编辑器来进行selenium3+Python3的自动化测试. 使用Sublime Text 3非常适合. 1.下载安装 首先到http://www.sublimetext.com ...
- mac电脑sublime text3安装pretty json插件
因http://packagecontrol.io/地址被墙无法实现自动安装,导致sublime Text3安装插件非常麻烦,总是出现There Are No Packages Available F ...
随机推荐
- 新概念英语(1-23)Which glasses?
Which glasses does the man want? A:Give me some glasses please, Jane? B:Which glasses? These glasses ...
- FatMouse's Speed ~(基础DP)打印路径的上升子序列
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...
- 微信小程序授权获取用户详细信息openid
小程序获取用户的头像昵称openid之类 第一种使用wx.getUserInfo直接获取微信头像,昵称 wx.getUserInfo({ success: function (res) { that. ...
- Linux-centos-7.2-64bit 安装配置mysql
2018-04-12 安装在/usr/local/下,配置文件在/etc/my.ini 1.下载mysql安装包到 /usr/local/software cd /usr/local/software ...
- 1.1 WEB API 在帮助文档页面进行测试
这篇文章http://www.cnblogs.com/landeanfen/p/5210356.html写得比较详细, 我就挑简单的来说. 首先用这功能要在WEB API创建的帮助文档下面,如果你使用 ...
- Lua版组合算法
高效率的排列组合算法--<编程珠矶>--Lua实现 原文链接 原文是python实现的,这里给出lua版本的实现 组合算法 本程序的思路是开一个数组,其下标表示1到m个数,数组元素 ...
- [LeetCode] Add Bold Tag in String 字符串中增添加粗标签
Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and ...
- 2018年Java后端面试经历
楼主16年毕业,16年三月份进入上一家公司到今年3月底,所以这是一份两年工作经验面经分享. 都说金三银四,往些年都是听着过没啥特别的感觉.今年自己倒是确确实实体验了一把银四,从3月26裸辞到4月17号 ...
- WebApi的安全性及其解决方案
一.前言 WebApi的小白想要了解一些关于WebApi安全性相关的问题,本篇文章是整理一些关于WebApi安全.权限认证的文章. 二.内容正文 2.1 不进行验证 客户端调用:http:// ...
- [SCOI2009]windy数
题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个windy数? 输入输出格式 输 ...