程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Using Python to implement an Alfred workflow with date and time stamp conversion

編輯:Python

A new one was changed two days ago macbook, I don't know if it's because m1 The reason for the chip , The system doesn't come with php, Resulting in the timestamp conversion I used before workflow It doesn't work . As a back-end Engineer , The function of time stamp mutual conversion is still very common , So I tossed and repaired , Manual installation php Maybe it's because php The reason for the version , Still unavailable , I thought I'd better forget it No more tossing , It turns out that it's not very easy to use , Just use it yourself python Write a .

Let's start with my workflow Several functions realized :

  1. You can get the current time , Support to obtain second time stamp , Millisecond timestamps , as well as yyyy-MM-dd and yyyy-MM-dd HH:mm:ss Date format for .
  2. You can convert the second or millisecond timestamp to yyyy-MM-dd and yyyy-MM-dd HH:mm:ss Date format for .
  3. Of course, you can also yyyy-MM-dd and yyyy-MM-dd HH:mm:ss The date in the format is converted to a timestamp of seconds and milliseconds .

The following will specifically teach you how to realize the above functions , I believe that with everyone's learning ability , Soon I can write other . If you don't want to write , A download link is attached at the end of the article , You can use it directly .

Let's look at it first Alfred workflow Required output data format , Use json perhaps xml Fine , among title and subtitle Fields are used for presentation , I only use title Field .arg Fields are used to look like the next level workflow Passing parameters , If your workflow Just to show , You don't need this . I often need to copy the results to the pasteboard , So there's one in the back Copy to clipboard modular , therefore arg Parameters are necessary .

{
"items": [
{
"arg": 1645346653,
"valid": true,
"subtitle": "",
"uid": "s",
"title": "\u79d2: 1645346653"
},
{
"arg": "1645346653000",
"valid": true,
"subtitle": "",
"uid": "ms",
"title": "\u6beb\u79d2: 1645346653000"
},
{
"arg": "2022-02-20",
"valid": true,
"subtitle": "",
"uid": "date",
"title": "\u65e5\u671f: 2022-02-20"
},
{
"arg": "2022-02-20 16:44:13",
"valid": true,
"subtitle": "",
"uid": "datetime",
"title": "\u65f6\u95f4: 2022-02-20 16:44:13"
}
]
}

actually , You can generate the above format in any way json strand , Can be used to implement a new workflow, Not limited to any language . So you can see alfred Of workflow You can write in various languages .

On timestamp conversion workflow The logic is simple , It is to generate date data in various formats according to input parameters , Then it will start with the above json Format output , The complete code is as follows :

# -*- coding: utf-8 -*-
import sys
import time
import datetime
import re
from workflow import Workflow3
def getTime(ts):
wf = Workflow3()
s = ts
timeArray = time.localtime(ts)
# otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", ts)
ms = str(ts*1000)
wf.add_item(uid = "s", title = " second : "+str(s), arg=s, valid = True)
wf.add_item(uid = "ms", title = " millisecond : "+str(ms), arg=ms, valid = True)
wf.add_item(uid = "date", title = " date : "+time.strftime("%Y-%m-%d", timeArray), arg=time.strftime("%Y-%m-%d", timeArray), valid = True)
wf.add_item(uid = "datetime", title = " Time : "+time.strftime("%Y-%m-%d %H:%M:%S", timeArray), arg=time.strftime("%Y-%m-%d %H:%M:%S", timeArray), valid = True)
wf.send_feedback()
if __name__ == '__main__':
if len(sys.argv) == 1:
ts = time.time()
getTime(int(ts))
exit(0)
query = sys.argv[1]
# print(query)
if query == 'now':
ts = time.time()
getTime(int(ts))
elif re.match(r"\d+-\d+-\d+ \d+:\d+:\d+", query):
ts = time.mktime(time.strptime(query, '%Y-%m-%d %H:%M:%S'))
getTime(int(ts))
elif re.match(r"\d+-\d+-\d+", query):
ts = time.mktime(time.strptime(query, '%Y-%m-%d'))
getTime(int(ts))
elif re.match(r"\d+", query):
ts = int(query)
if ts > 253402185600:
ts = ts/1000
getTime(ts)

stay Alfred The configuration is as follows :

The last attached Workflow Download link for : https://pan.baidu.com/s/1PVS9XvKe-2fle2ZrreCWPA?pwd=ukd8 Extraction code : ukd8


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved