Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Valexr committed May 5, 2024
1 parent e3f2c56 commit f32b484
Show file tree
Hide file tree
Showing 3 changed files with 375 additions and 68 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"devDependencies": {
"@types/luxon": "^3.4.2",
"@types/node": "^20.12.8",
"dayjs": "^1.11.11",
"esbuild": "^0.20.2",
"esbuild-svelte": "^0.8.0",
"luxon": "^3.4.4",
Expand Down
74 changes: 7 additions & 67 deletions src/lib/counters.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@
import { Writable, derived } from 'svelte/store';
import { dates } from './dates';
import { quotes } from './quotes';
import { DateTime, Interval, Duration } from "luxon";
import { DateTime, Interval } from "luxon";

export const counters = derived<[Writable<StartDate[]>, Writable<Quote[]>], Counter[]>(([dates, quotes]),
([$dates, $quotes], set) => {

set($dates.map(({ start, title, quote }, id) => count(id, start, title, quote)))

function count(id: number, start: string, title: string, quote: Quote) {
const elapsed = elapse(start)

// console.log(title, convert(elapsed.ms))
const { years, months, weeks, days, interval } = elapse(start)
return {
id, title, quote: quote || quotes.random(id), start: start,
years: elapsed.years,
months: elapsed.months,
days: elapsed.days,
weeks: elapsed.weeks,
years, months, days, weeks,
full: {
months: Math.trunc(elapsed.interval.length('months')),
weeks: Math.trunc(elapsed.interval.length('weeks')),
days: Math.trunc(elapsed.interval.length('days')),
hours: Math.trunc(elapsed.interval.length('hours')),
// months: Math.trunc(elapsed.duration.as('months')),
// weeks: Math.trunc(elapsed.duration.as('weeks')),
// days: Math.trunc(elapsed.duration.as('days')),
// hours: Math.trunc(elapsed.duration.as('hours')),
// months: elapsed.years * 12 + elapsed.months,
// weeks: Math.trunc(elapsed.ms / (3600000 * 24 * 7)),
// days: Math.trunc(elapsed.ms / (3600000 * 24)),
// hours: Math.trunc(elapsed.ms / 3600000)
months: Math.trunc(interval.length('months')),
weeks: Math.trunc(interval.length('weeks')),
days: Math.trunc(interval.length('days')),
hours: Math.trunc(interval.length('hours')),
}
}
}
}, [])

function elapse(start: string) {
// const FROM = new Date(start)
// const TO = new Date()

// const DATE = new Date(TO.getTime() - FROM.getTime());

// DATE.setDate(FROM.getUTCDate())
// console.log(FROM, TO, DATE)

// const leap = new Date(year, 1, 29).getDate() === 29;

// const years = DATE.getUTCFullYear() - 1970
// const months = DATE.getUTCMonth()
// const days = DATE.getUTCDate() - 1

const TO = DateTime.now();
const FROM = DateTime.fromISO(start);

Expand All @@ -64,40 +37,7 @@ function elapse(start: string) {
months: DIFF.months || 0,
days: DIFF.days || 0,
weeks: DIFF.weeks || 0,
// ms: DIFF.milliseconds || 0,
duration: Duration.fromObject(DIFF),
interval: Interval.fromDateTimes(FROM, TO)
// days: days % 7,
// weeks: Math.floor(days / 7),
// hours: DATE.getUTCHours(),
// minutes: DATE.getUTCMinutes(),
// seconds: DATE.getUTCSeconds(),
// ms: DATE.getTime()
}
};


function convert(ms: number) {
let Y, M, W, D, h, m, s;
s = Math.floor(ms / 1000);
m = Math.floor(s / 60);
s = s % 60;
h = Math.floor(m / 60);
m = m % 60;

D = Math.floor(h / 24);
h = h % 24;

W = Math.floor(D / 7);
D = W % 7;

M = Math.floor(W / 4);
W = M % 4;

Y = Math.floor(M / 12);
M = M % 12

// Y = Y;
return { Y, M, W, D };
}

Loading

0 comments on commit f32b484

Please sign in to comment.