Skip to content

Instantly share code, notes, and snippets.

@chihane
Created August 18, 2022 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chihane/065cf94d24267f7bd2fb9a2d0df9554b to your computer and use it in GitHub Desktop.
Save chihane/065cf94d24267f7bd2fb9a2d0df9554b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title TimeConverter
# @raycast.mode compact
# Optional parameters:
# @raycast.icon 🤖
# @raycast.argument1 { "type": "text", "placeholder": "timestamp / datetime", "optional": true }
# @raycast.packageName chihane.ts2dt
# Documentation:
# @raycast.description Convert between timestamp & datetime & date
# @raycast.author Chihane
import sys, time, subprocess, datetime
arg = sys.argv[1] if len(sys.argv) > 1 else None
if not arg:
arg = str(datetime.datetime.now())
result = ''
try:
if arg.isdigit():
dt = datetime.datetime.fromtimestamp(int(arg))
result = dt.strftime('%Y-%m-%d %H:%M:%S')
else:
try:
dt = datetime.datetime.strptime(arg, '%Y-%m-%d %H:%M:%S')
except ValueError:
try:
dt = datetime.datetime.strptime(arg, '%Y-%m-%d')
except ValueError:
dt = datetime.datetime.strptime(arg, '%Y-%m-%d %H:%M:%S.%f')
timestamp = int(dt.timestamp())
result = str(timestamp)
except Exception as e:
print('时间解析失败')
exit(1)
try:
subprocess.run('pbcopy', universal_newlines=True, input=result)
print(result)
except Exception as e:
print('结果拷贝失败')
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment