1

I changed to a new macbook two days ago, and I don't know if it is because of the m1 chip, the system does not have its own php, which caused the timestamp conversion workflow I used before to fail. As a back-end engineer, the function of timestamp exchange is still very common, so I have toss and repair it. After manually installing php, it may still be unavailable because of the php version. That one is not very easy to use, so just write one yourself in python.

Let me talk about a few functions that my workflow implements:

  1. The current time can be obtained, and it supports obtaining second-level timestamps, millisecond-level timestamps, and date formats of yyyy-MM-dd and yyyy-MM-dd HH:mm:ss .
    在这里插入图片描述
  2. You can convert timestamps in seconds or milliseconds to yyyy-MM-dd and yyyy-MM-dd HH:mm:ss date formats. 在这里插入图片描述
  3. Of course, dates in yyyy-MM-dd and yyyy-MM-dd HH:mm:ss formats can also be converted to timestamps in seconds and milliseconds.
    在这里插入图片描述

The following will teach you how to implement the above functions in a very specific way. I believe that with your learning ability, you will be able to write other things soon. If you don't want to write, the download link is attached at the end of the article, you can use it directly.

Let's first look at the output data format required by Alfred workflow, either json or xml can be used. The title and subtitle fields are used for display. I only use the title field here. The arg field is used to pass parameters like the next-level workflow. If your workflow is purely for display, you don't need this. I often need to copy the results to the clipboard here, so I followed a Copy to clipboard module, so the arg parameter is 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"
    }
  ]
}

In fact, any way you generate the json string in the above format can be used to implement a new workflow, not limited to any language. So you can see that alfred's workflow can be written in various languages.

The workflow logic for timestamp conversion is very simple. It is to generate date data in various formats according to the input parameters, and then output the above json format. 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 = "秒: "+str(s), arg=s, valid = True)
    wf.add_item(uid = "ms", title = "毫秒: "+str(ms), arg=ms,  valid = True)
    wf.add_item(uid = "date", title = "日期: "+time.strftime("%Y-%m-%d", timeArray), arg=time.strftime("%Y-%m-%d", timeArray),  valid = True)
    wf.add_item(uid = "datetime", title = "时间: "+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)

The configuration in Alfred is as follows:
在这里插入图片描述
Finally, attach the download link of Workflow: https://pan.baidu.com/s/1PVS9XvKe-2fle2ZrreCWPA?pwd=ukd8 Extraction code: ukd8


xindoo
717 声望3k 粉丝