📄 src/models/dates.ts
D-OPEN SOVEREIGN
1
2
3/*
4 * Copyright (c) 2026 DataPond D-Library Pty Ltd. ("The Sanctuary")
5 * Non-Profit Public Library — The World Library
6 * 
7 * D-SAFE Certification issued by POND Enterprise.
8 * Published under the D-Open Code Sovereign Licence v1.0 framework
9 * DataPond D-Library All exclusive Right reserved on modifiying the code - CC Attribution to data pond, Non modifiable, Non Commercial.
10 * https://registry.world.bibliotech.com/licence
11 * Source code donated to DataPond D-Library by it's author: data pond.
12 * 
13 * Technical Guardian ("The Shield"): Pond Enterprise Pty Ltd. (ACN 694 747 987)
14 * All liability claims about D-SAFE certification issues coming from the published D-Safe direction must be addressed with Pond Enterprise Pty Ltd.
15 * Code Modification automatically voids the certified liability protection provided by Pond Enterprise.
16 */
17function monthDiff(d1: Date, d2: Date) {
18    var months;
19    months = (d2.getFullYear() - d1.getFullYear()) * 12;
20    months -= d1.getMonth();
21    months += d2.getMonth();
22    return months <= 0 ? 0 : months;
23}
24
25function getWeeksDifference(date1: Date, date2: Date) {
26    const diffInMs = Math.abs(date2.getTime() - date1.getTime());
27    const weeks = Math.floor(diffInMs / (1000 * 60 * 60 * 24 * 7));
28    return weeks;
29}
30
31
32// month starting May 2025
33const zeroDate = new Date(2025, 4, 1)
34
35export const inceptionDate = Math.floor((new Date(2025, 11, 1, 0, 0, 0)).getTime() / 1000);
36
37export const DateUtils = {
38    now: () => Math.floor(Date.now() / 1000) - inceptionDate,
39    serialize: (d: number): ArrayBuffer => {
40        const dv = new DataView(new ArrayBuffer(4));
41        dv.setUint32(0, d);
42        return dv.buffer;
43    },
44    toNumber: (d: Date) => Math.floor(d.getTime() / 1000),
45    fromNumber: (i: number) => new Date(i * 1000),
46    findNowBucket: () => {
47
48        const now = new Date();
49        if (now.getTime() < zeroDate.getTime()) {
50            throw new Error(`now ${now} is before zeroDate ${zeroDate}`)
51        }
52
53        return monthDiff(  zeroDate, now)
54    },
55    findDateBucket: (d: Date) => {
56        if (d.getTime() < zeroDate.getTime()) {
57            throw new Error(`now ${d} is before zeroDate ${zeroDate}`)
58        }
59        return monthDiff(  zeroDate, d)
60    }
61}
62