5 Commits

4 changed files with 81 additions and 20 deletions

View File

@@ -1,6 +1,6 @@
services:
app:
image: chiko/77th_eventcalendarntfy:dev
image: chiko/77th_eventcalendarntfy:v0.1.2
build: .
volumes:
- ./data/db:/opt/app/data/db

View File

@@ -1,5 +1,5 @@
{
"version": "0.1.1",
"version": "0.1.2",
"name": "77th_eventcalendernotification",
"module": "./src/app.ts",
"type": "module",

View File

@@ -1,6 +1,6 @@
import { Database } from "bun:sqlite";
import { TEventType, type TEvent } from "./event.types";
import { getTsNow, pad_l2, transformArray } from "../../util";
import { getTsNow, pad_l2, transformArray, formatTimeDiff, isEuropeanDST, subtractHours } from "../../util";
const BASE_URL = "https://77th-jsoc.com/service.php?action=get_events";
@@ -26,6 +26,21 @@ export type TEventEntityNew = Omit<TEventEntity, "event_uid">
export class Event implements TEventEntity {
static table_name: "events"
event_uid: number;
uid: string;
title: string;
description: string;
date_at: string;
time_start: string;
time_end: string;
posted_by: string;
location: string;
event_type: TEventEntity["event_type"];
timezone: string;
link: string;
notification: TEventEntity["notification"];
deleteDate: TEventEntity["deleteDate"];
static createTable (db: Database): void {
const query = db.query(`CREATE TABLE IF NOT EXISTS "events" (
"event_uid" INTEGER NOT NULL,
@@ -104,21 +119,6 @@ export class Event implements TEventEntity {
return query.all();
}
event_uid: number;
uid: string;
title: string;
description: string;
date_at: string;
time_start: string;
time_end: string;
posted_by: string;
location: string;
event_type: TEventEntity["event_type"];
timezone: string;
link: string;
notification: TEventEntity["notification"];
deleteDate: TEventEntity["deleteDate"];
constructor(event_uid: number, uid: string, title: string, description: string, date_at: string, time_start: string, time_end: string, posted_by: string, location: string, event_type: TEventEntity["event_type"], timezone: string, link: string, notification: TEventEntity["notification"], deleteDate: TEventEntity["deleteDate"]) {
this.event_uid = event_uid;
this.uid = uid;
@@ -195,10 +195,13 @@ export class Event implements TEventEntity {
return `${title_prefix_arr.length >= 1 ? ( title_prefix_arr.join(" " ) + " - ") : "" }${this.title} (${ TEventType[ this.event_type ] })`;
}
get_body() {
const BaseTime = new Date(`${this.date_at} 21:00`);
const RelativeEventTime = new Date(`${this.date_at} ${this.get_time_start()}`);
const TimeDiff = formatTimeDiff( BaseTime, RelativeEventTime);
const body = [
`Title: ${this.title}`,
`Date: ${this.date_at}`,
`Time: ${this.time_start}`,
`Time: ${this.get_time_start()}${ TimeDiff ? ` (Optime ${TimeDiff})` : "" }`,
`Type: ${ TEventType[ this.event_type ] }`,
`Location: ${this.location}`,
`By: ${this.posted_by}`,
@@ -219,4 +222,15 @@ export class Event implements TEventEntity {
}
return false;
}
get_time_start () {
const date = new Date( `${this.date_at} ${this.time_start}` );
if ( ! isEuropeanDST( date ) ) {
const newDate = subtractHours( date, 1);
const hours = newDate.getHours();
const minutes = newDate.getMinutes();
return `${pad_l2(hours)}:${pad_l2(minutes)}`;
}
return this.time_start;
}
}

View File

@@ -42,3 +42,50 @@ export function getTsNow() {
export function unixToDate( unix_timestamp: number ) { return new Date(unix_timestamp * 1000) }
export function dateToUnix( date: Date ) { return Math.round( date.getTime()/1000 ) }
export function formatTimeDiff(dateA: Date, dateB: Date) {
// Difference in milliseconds
const diffMs = dateB.getTime() - dateA.getTime();
// Get sign (+ or -)
const sign = diffMs < 0 ? "-" : "";
// Convert to absolute minutes
const diffMinutes = Math.floor(Math.abs(diffMs) / 60000);
// Split into hours and minutes
const hours = Math.floor(diffMinutes / 60);
const minutes = diffMinutes % 60;
// Return formatted string
return `${sign}${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`;
}
export function subtractHours(date: Date, hours: number) {
// Create a new Date so we don't mutate the original
return new Date(date.getTime() - hours * 60 * 60 * 1000);
}
// Helper: get last Sunday of a given month
function lastSundayOfMonth(year: number, month: number ) {
const lastDay = new Date(Date.UTC(year, month + 1, 0)); // last day of month
const day = lastDay.getUTCDay(); // 0 = Sunday
const diff = day === 0 ? 0 : day; // how far back to go to reach Sunday
lastDay.setUTCDate(lastDay.getUTCDate() - diff);
return lastDay;
}
export function isEuropeanDST( date: Date ) {
const year = date.getFullYear();
// DST starts: last Sunday in March, 01:00 UTC
const start = lastSundayOfMonth(year, 2); // March (month = 2)
start.setUTCHours(1, 0, 0, 0);
// DST ends: last Sunday in October, 01:00 UTC
const end = lastSundayOfMonth(year, 9); // October (month = 9)
end.setUTCHours(1, 0, 0, 0);
// Return true if within DST period
return date >= start && date < end;
}