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 :
yyyy-MM-dd
and yyyy-MM-dd HH:mm:ss
Date format for . yyyy-MM-dd
and yyyy-MM-dd HH:mm:ss
Date format for .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