diff --git a/src/cards/productive-time-card.ts b/src/cards/productive-time-card.ts index 6c295d0d8..af09706b0 100644 --- a/src/cards/productive-time-card.ts +++ b/src/cards/productive-time-card.ts @@ -31,6 +31,24 @@ const getProductiveTimeSVG = function ( return svgString; }; +const adjustOffset = function (offset: number, RoundRobin: {offset: number}): number { + if (offset % 1 == 0) { + return offset; + // offset % 1 should be 0.3 or 0.7 but its js and it gives 0.29999 or -0.299999 thats why this frankenstein + } else if ((offset % 1 > 0.29 && offset % 1 < 0.31) || (offset % 1 < -0.29 && offset % 1 > -0.31)) { + // toggle up and down between hour + RoundRobin.offset = (RoundRobin.offset + 1) % 2; + return RoundRobin.offset === 0 ? Math.floor(offset) : Math.ceil(offset); + } else if ((offset % 1 > 0.44 && offset % 1 < 0.46) || (offset % 1 < -0.44 && offset % 1 > -0.45)) { + // distrubute 1 : 3 ratio for 0.45 utc time + RoundRobin.offset = (RoundRobin.offset + 1) % 4; + return RoundRobin.offset === 0 ? Math.floor(offset) : Math.ceil(offset); + } else { + // flood down , if utc is given right it will never be executed + return Math.floor(offset); + } +}; + const getProductiveTimeData = async function (username: string, utcOffset: number): Promise> { const until = new Date(); const since = new Date(); @@ -39,10 +57,13 @@ const getProductiveTimeData = async function (username: string, utcOffset: numbe // process productiveTime const chartData = new Array(24); chartData.fill(0); + const roundRobin = { + offset: 0 + }; for (const time of productiveTime.productiveDate) { const hour = new Date(time).getUTCHours(); // We use UTC+0 here - const afterOffset = Number(hour) + Number(utcOffset); // Add offset to hour - // covert afterOffset to 0-23 + const afterOffset = adjustOffset(Number(hour) + Number(utcOffset), roundRobin); // Add offset to hour + // convert afterOffset to 0-23 if (afterOffset < 0) { // if afterOffset is negative, we need to add 24 to get the correct hour chartData[24 + afterOffset] += 1; diff --git a/src/templates/productive-time-card.ts b/src/templates/productive-time-card.ts index 9b0eb042d..8f9ff218f 100644 --- a/src/templates/productive-time-card.ts +++ b/src/templates/productive-time-card.ts @@ -4,7 +4,7 @@ import * as d3 from 'd3'; import * as d3Axis from 'd3-axis'; export function createProductiveCard(chartData: number[], theme: Theme, utcOffset: number) { - const title = 'Commits ' + '(UTC ' + (utcOffset >= 0 ? '+' : '') + utcOffset + ')'; + const title = 'Commits ' + '(UTC ' + (utcOffset >= 0 ? '+' : '') + utcOffset.toFixed(2) + ')'; const card = new Card(title, 340, 200, theme); const svg = card.getSVG();