aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/util
diff options
context:
space:
mode:
authorKevin Hoerr <kjhoerr@protonmail.com>2022-11-21 18:19:16 -0500
committerGitHub <noreply@github.com>2022-11-21 18:19:16 -0500
commit651f019141b488a82fd42028bce5b0003abbfaf5 (patch)
tree5be06913d021752cb0a1d3f809f66b27359619c5 /src/util
parent6ec00afac5afc892dca5a184b66467d9408f14a5 (diff)
downloadsubmelon.dev-651f019141b488a82fd42028bce5b0003abbfaf5.tar.gz
submelon.dev-651f019141b488a82fd42028bce5b0003abbfaf5.tar.bz2
submelon.dev-651f019141b488a82fd42028bce5b0003abbfaf5.zip
Layout changes (#15)
* Layout refactor and standardization * Use split to condense short array * Fix verbage
Diffstat (limited to 'src/util')
-rw-r--r--src/util/timestamp.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/util/timestamp.ts b/src/util/timestamp.ts
new file mode 100644
index 0000000..f53f5ad
--- /dev/null
+++ b/src/util/timestamp.ts
@@ -0,0 +1,48 @@
+const SHORT_CHARS =
+ "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
+
+/**
+ * Transforms a number into a custom 62 char expression of that number,
+ * effectively making a "short" version of that number (0-9a-zA-Z). This does
+ * NOT convert to a base-62 number.
+ *
+ * There are vast gaps in the effective translation of numbers, which is why
+ * this function is mostly for concisely translating dates prior to the year
+ * 2063. Since all other values associated with dates (month, day, hours,
+ * minutes, seconds) effectively fall within the range of a single character,
+ * this is an accepted shortcoming.
+ *
+ * Examples:
+ *
+ * 0 = 0
+ *
+ * 48 = M
+ *
+ * 2022 = km (in base-62 this would be wC)
+ */
+export function toShort(valu: number): string {
+ return (
+ valu
+ .toString()
+ .match(/.{1,2}/g)
+ ?.map((s) => SHORT_CHARS[parseInt(s)])
+ .join("") ?? ""
+ );
+}
+
+/**
+ * Translates a Unix EPOCH timestamp to a 62-char expression of the date. See
+ * the `toShort()` method for more details on the meaning of the final output.
+ */
+export function getTimestamp(seconds: number): string {
+ const date = new Date(seconds * 1000);
+ const dateArr = [
+ date.getUTCFullYear(),
+ date.getUTCMonth(),
+ date.getUTCDate(),
+ date.getUTCHours(),
+ date.getUTCMinutes(),
+ ];
+
+ return dateArr.map(toShort).join(".") + "-0";
+}