Skip to content

Commit

Permalink
fix: Fixed decimal UTC time bug (#123)
Browse files Browse the repository at this point in the history
* Fixed decimal UTC time bug

fixed #90 
floor down value because chart uses hours only

* added function to adjust offset

* updated label which shows utc time

* fixed eslint

* lint: re-enable eslint

Co-authored-by: Saurav Dharwadkar <[email protected]>
  • Loading branch information
vn7n24fzkq and SauravDharwadkar committed Jan 7, 2023
1 parent 159df73 commit 7fcb411
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/cards/productive-time-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<number>> {
const until = new Date();
const since = new Date();
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/templates/productive-time-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 7fcb411

Please sign in to comment.