All files / src/core/extensions globalExtensions.ts

100% Statements 30/30
66.66% Branches 6/9
100% Functions 7/7
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                    9x 8x       8x     9x 4x   4x     9x 1065x 1x     1064x 1064x       1064x   1064x       1064x 1064x 1064x   1064x 1064x   1064x 1064x     1064x     1064x         9x 2128x 2128x 2128x     9x 1064x     9x 2128x    
export {};
 
declare global {
    interface Date {
        toQuery(this: Date): string
 
        toResponse(this: Date): string
 
        fromLocaltoUTC(this: Date): Date
    }
 
    interface String {
        padTime(this: string): string
 
        padUTCTimezone(this: string): string
    }
}
 
Date.prototype.toQuery = function (this: Date) {
    const query = this
        .toISOString()
        .replace(/T.*/, '');
 
    return query;
}
 
Date.prototype.toResponse = function (this: Date) {
    const response = this.toLocaleDateString('en-GB', { timeZone: 'UTC' });
 
    return response;
}
 
Date.prototype.fromLocaltoUTC = function (this: Date) {
    if (Number.isNaN(this.getTime())) {
        return this;
    }
 
    const tz = getBulgarianDSTOffset(this);
    const isoDateString = this
        .toISOString()
        .replace(/([+-]\d{2}:?\d{2}|Z)$/, tz);
 
    const utcDate = new Date(isoDateString);
 
    return utcDate;
}
 
function getBulgarianDSTOffset(date: Date) {
    const year = date.getFullYear();
    const month = date.getMonth();
    const day = date.getDate();
 
    const marchMonthIndex = 2;
    const octoberMonthIndex = 9;
 
    const lastSundayOfMarch = getLastSundayOfMonth(year, marchMonthIndex);
    const lastSundayOfOctober = getLastSundayOfMonth(year, octoberMonthIndex);
 
    const isInDST = 
        (month > marchMonthIndex || (month === marchMonthIndex && day >= lastSundayOfMarch)) &&
        (month < octoberMonthIndex || (month === octoberMonthIndex && day < lastSundayOfOctober));
        
    return isInDST
        ? '+03:00' // Bulgarian DST offset
        : '+02:00'; // Standard offset
}
 
const getLastSundayOfMonth = (year: number, month: number) => {
    const lastDayOfMonth = new Date(year, month + 1, 0).getDate(); // Get the last day of the month
    const lastSunday = lastDayOfMonth - new Date(year, month, lastDayOfMonth).getDay(); // Calculate the date of the last Sunday
    return lastSunday;
};
 
String.prototype.padTime = function (this: string) {
    return this.concat(' 00:00:00');
}
 
String.prototype.padUTCTimezone = function (this: string) {
    return this.concat('+00:00');
}