-
Notifications
You must be signed in to change notification settings - Fork 78
adding time in calculations of yesterday and today #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
35dbdae
1f7981b
59c8c4a
8cddb5e
a328c33
82f3473
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,35 +101,34 @@ function allIncluded() { | |
endingDate = getToday(); | ||
startingDate = getLastWeek(); | ||
} | ||
|
||
function getLastWeek() { | ||
var today = new Date(); | ||
var noDays_to_goback = gsoc == 0 ? 7 : 1; | ||
|
||
// Get user's timezone offset in mins | ||
const tzOffset = today.getTimezoneOffset(); | ||
|
||
// date for start of day in user's timezone | ||
var lastWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - noDays_to_goback); | ||
var lastWeekMonth = lastWeek.getMonth() + 1; | ||
var lastWeekDay = lastWeek.getDate(); | ||
var lastWeekYear = lastWeek.getFullYear(); | ||
var lastWeekDisplayPadded = | ||
('0000' + lastWeekYear.toString()).slice(-4) + | ||
'-' + | ||
('00' + lastWeekMonth.toString()).slice(-2) + | ||
'-' + | ||
('00' + lastWeekDay.toString()).slice(-2); | ||
return lastWeekDisplayPadded; | ||
lastWeek.setHours(0, 0, 0, 0); | ||
|
||
// Adjust timezone to make sure no activity is missed | ||
const utc = new Date(lastWeek.getTime() + (tzOffset * 60 * 1000)); | ||
|
||
return utc.toISOString().slice(0,19) + "Z"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider extracting the date transformation logic into a shared helper function to avoid repetition and improve code maintainability Extract a shared helper to handle the date transformation so you don’t repeat the setHours and offset logic. For example: function formatDateWithOffset(date, isStart) {
if (isStart) {
date.setHours(0, 0, 0, 0);
} else {
date.setHours(23, 59, 59, 999);
}
const offsetHours = 14;
const utc = new Date(date.getTime() + (offsetHours * 60 * 60 * 1000));
return utc.toISOString().slice(0, 19) + "Z";
} Then update your functions as follows: function getLastWeek() {
const today = new Date();
const daysBack = gsoc === 0 ? 7 : 1;
const lastWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - daysBack);
return formatDateWithOffset(lastWeek, true);
}
function getToday() {
const today = new Date();
return formatDateWithOffset(today, false);
} This refactoring reduces duplication while keeping the functionality identical. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is used in only two functions, also its a single line, I don't think a function is necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll respond to the comment by the developer: While I understand your perspective that the code is used in only two functions and appears to be a single line, creating a helper function offers several benefits:
The proposed refactoring is lightweight and doesn't add unnecessary complexity. It actually simplifies the existing code by:
I recommend considering the proposed refactoring, but if you feel strongly against it, that's okay too. The most important thing is maintaining clean, understandable code. Would you like to discuss this further or share your thoughts on the suggested approach? |
||
} | ||
function getToday() { | ||
var today = new Date(); | ||
var Week = new Date(today.getFullYear(), today.getMonth(), today.getDate()); | ||
var WeekMonth = Week.getMonth() + 1; | ||
var WeekDay = Week.getDate(); | ||
var WeekYear = Week.getFullYear(); | ||
var WeekDisplayPadded = | ||
('0000' + WeekYear.toString()).slice(-4) + | ||
'-' + | ||
('00' + WeekMonth.toString()).slice(-2) + | ||
'-' + | ||
('00' + WeekDay.toString()).slice(-2); | ||
return WeekDisplayPadded; | ||
} | ||
today.setHours(23, 59, 59, 999); | ||
|
||
// get timezone offset | ||
const tzOffset = today.getTimezoneOffset(); | ||
const utc = new Date(today.getTime() + (tzOffset * 60 * 1000)); | ||
|
||
return utc.toISOString().slice(0, 19) + "Z"; | ||
} | ||
|
||
// fetch github data | ||
function fetchGithubData() { | ||
var issueUrl = | ||
|
@@ -185,6 +184,30 @@ function allIncluded() { | |
githubUserData = data; | ||
}, | ||
}); | ||
filterAndStoreData(); | ||
} | ||
|
||
function filterDataByDate(data, startingDate, endingDate){ | ||
|
||
const localStart = new Date(startingDate + 'T00:00:00Z'); | ||
const localEnd = new Date(endingDate + 'T23:59:59Z'); | ||
|
||
return data.items.filter(item => { | ||
const updatedAt = new Date(item.updated_at); | ||
return updatedAt >= localStart && updatedAt <= localEnd; | ||
}) | ||
} | ||
|
||
function filterAndStoreData() { | ||
if(githubIssuesData) { | ||
githubIssuesData.items = filterDataByDate(githubIssuesData, startingDate, endingDate); | ||
} | ||
if(githubPrsReviewData) { | ||
githubPrsReviewData.items = filterDataByDate(githubPrsReviewData, startingDate, endingDate); | ||
} | ||
if(githubUserData) { | ||
githubUserData.items = filterDataByDate(githubUserData, startingDate, endingDate); | ||
} | ||
} | ||
|
||
function formatDate(dateString) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to add some comments to explain your algorithm, why adding 14 hours.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, yes earlier I was manually setting the timezoneOffset to -14 hours, which is incorrect, while working on this project, I learned a function getTimezoneOffset() and it serves our need.