-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget_death-clock.js
More file actions
79 lines (61 loc) · 1.84 KB
/
widget_death-clock.js
File metadata and controls
79 lines (61 loc) · 1.84 KB
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: gray; icon-glyph: smile-wink;
// 💁♂️ https://shortcutomation.com/death-clock
const CONFIG = {
DATE_OF_BIRTH: "1996-05-01",
LIFE_EXPECTANCY: 94,
FONT: {
NAME: "IowanOldStyle-BoldItalic",
SIZE: 24,
},
TEXT_OPACITY: 1.0,
TEXT_COLOR: Color.white(),
};
const ISO = new DateFormatter();
ISO.dateFormat = "yyyy-MM-dd";
const now = new Date();
const birth = ISO.date(CONFIG.DATE_OF_BIRTH);
const solarYearDays = 146097 / 400; // = 365.2425
const ageYears = daysBetween(birth, now) / solarYearDays;
const totalDays = CONFIG.LIFE_EXPECTANCY * solarYearDays;
const expiryDate = addDays(birth, totalDays);
const remainingDays = daysBetween(now, expiryDate);
const percentageLeft = (remainingDays / totalDays) * 100;
const widget = new ListWidget();
const stack = widget.addStack();
stack.layoutVertically();
const heart = "❥";
const line1 = addStyledText(
stack,
`${heart} I’m ${formatNumber(ageYears, 2)} years old`,
);
stack.addSpacer(6);
const line2 = addStyledText(
stack,
`${heart} My life is ${formatNumber(percentageLeft, 3)}% left`,
);
config.runsInWidget ? Script.setWidget(widget) : widget.presentMedium();
Script.complete();
// ================
// Helper functions
// ================
function daysBetween(a, b) {
const msPerDay = 24 * 60 * 60 * 1000;
return Math.floor((b - a) / msPerDay);
}
function addDays(date, days) {
const d = new Date(date);
d.setDate(d.getDate() + Math.round(days));
return d;
}
function formatNumber(num, decimals) {
return Number(num).toFixed(decimals);
}
function addStyledText(stack, str) {
const t = stack.addText(str);
t.leftAlignText();
t.font = new Font(CONFIG.FONT.NAME, CONFIG.FONT.SIZE);
t.textOpacity = CONFIG.TEXT_OPACITY;
t.textColor = CONFIG.TEXT_COLOR;
}