Added a Workarond for the DST (European Daylight Saving Time (DST))
This commit is contained in:
49
src/util.ts
49
src/util.ts
@@ -41,4 +41,51 @@ 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 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;
|
||||
}
|
||||
Reference in New Issue
Block a user