forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-calculator.js
executable file
·94 lines (77 loc) · 2.56 KB
/
time-calculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
// Dependency: This script requires Nodejs.
// Install Node: https://nodejs.org/en/download/
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Time Calculator
// @raycast.mode compact
// Optional parameters:
// @raycast.icon 🕒
// @raycast.argument1 { "type": "text", "placeholder": "Starting Date (Default: now)", "optional":true }
// @raycast.argument2 { "type": "text", "placeholder": "Time to Add/Subtract (eg: -44, 44, +44)" }
// @raycast.argument3 { "type": "text", "placeholder": "s, m, h, d, w, y (Default: h)", "optional":true}
// @raycast.description Add or Subtract specified amount of time from given date.
// @raycast.packageName Developer Utilities
// Documentation:
// @raycast.author Federico Miraglia
// @raycast.authorURL https://github.com/Mitra98t
const dateReg = new RegExp(/^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$/)
var value = process.argv.slice(2)[1]
var unit = process.argv.slice(2)[2] == "" ? "h" : process.argv.slice(2)[2]
var operator = ""
if (unit != "s" && unit != "m" && unit != "h" && unit != "d" && unit != "w" && unit != "y") {
console.log("Use s, m, h, d, w, y as units of measure")
return null
}
if (!/^[-\+]?\d+$/.test(value)) {
console.log("Only numbers eg: -44, 44, +44")
return null
}
else{
operator = value.charAt(0)
if(/^[-\+]$/.test(operator))
value = value.substring(1)
}
function conversion(n, unit) {
switch (unit) {
case "s":
return n * 1000
break;
case "m":
return n * 60000
break;
case "h":
return n * 3600000
break;
case "d":
return (n * 24) * 3600000
break;
case "w":
return ((n * 7) * 24) * 3600000
break;
case "y":
return n * 31556952000
break;
default:
return { err: true, message: "Wrong time format" }
break;
}
}
var nowDate = process.argv.slice(2)[0]
if (nowDate != "") {
if(!dateReg.test(nowDate)){
console.log("Use format yyyy-mm-dd or yyyy-m-d")
return null
}
else{
nowDate = new Date(nowDate)
}
}
else{
nowDate = new Date()
}
var nowMS = nowDate.getTime()
var resMS = operator !== "-" ? nowMS + conversion(value, unit) : nowMS - conversion(value, unit)
var resDate = new Date(resMS)
var resString = `${resDate.getFullYear()}-${resDate.getMonth() + 1}-${resDate.getDate()} | ${resDate.getHours()}:${resDate.getMinutes()}:${resDate.getSeconds()} | Unix ${resMS}`
console.log(resString)