Skip to content

Commit

Permalink
fix gamedate year on games scheduled after the new year #31
Browse files Browse the repository at this point in the history
Google will set string dates with non-existent years to the current year; all games scheduled for Jan+ being set to the previous year - while still in Nov, Dec, etc. This will check if today is within the months of Oct-Dec and set the gamedate year for any game scheduled Jan-April to the year ahead. NOTE: There may be edge cases were this could cause minor issues - such as game schedules that span more than 4 months in September or December.
  • Loading branch information
random-parts committed Jan 3, 2018
1 parent faf1a28 commit 192ced8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/services/ScheduleService.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function scheduleService () {
function updateSchedule () {
var s = schedule().raw() || "";
if (!s.length) { return } // Exit on empty web schedule

var today = new Date();
var c = schedule(ss).composite() || "";
var count = Math.max(s.length, c.length);
var composite_dates = schedule(ss).compositeDates();
Expand Down Expand Up @@ -123,7 +123,7 @@ function scheduleService () {

// Only set the date when status is a time or the date cell is empty
if (RegExp(/^[0-9]+/).test(s[i][4]) == true) {
this.date = utils(ss,tz).date.makeDateTime(s[i][0], s[i][4]);
this.date = utils(ss,tz).date.makeDateTime(s[i][0], s[i][4], today);
} else if (!this.date) {
try { this.date = s[i][0].replace(/-/, " ") }
catch (e) {}
Expand Down Expand Up @@ -258,7 +258,7 @@ function scheduleService () {
var n_date = (typeof yearday[i + 1] != "undefined")
? yearday[i + 1] : false;

if (current_date >= today && c_dates[i] != "") {
if (c_dates[i] != "" && current_date >= today || (today >= 330 && current_date <= 60)) {
game_dates.push(c_dates[i]);
game_columns.push(schedule(ss).gameColumn(c_dates[i]));
// Exit if the next day was found and there are no more days needed
Expand Down
37 changes: 32 additions & 5 deletions src/tools/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,40 @@ function utils (spreadsheet, tz) {
* @memberof! utils#
* @param {String} date - string month and day
* @param {String} time - string time
* @return {String} _"MMM dd h:mm a"_
* @param {Date} today - todays date
* @return {String} _"MMM dd h:mm a"_ || "M/d/y h:mm a"
*/
function rawDateTime (date, time) {
if (typeof date != "undefined") {
function rawDateTime (date, time, today) {
const months = ["jan", "feb", "mar", "apr", "may", "jun",
"jul","aug", "sep", "oct", "nov", "dec"];
try {
if (typeof date != "undefined") {
var today_month = today.getMonth();
var today_year = today.getFullYear();
var gamedate = date + " " + time;
// Split the date into its parts
var gd_parts = gamedate.slice(4).split(" ");
var game_month = months.indexOf(gd_parts[0].toLowerCase());

// If today is Oct-Dec - set new year for games scheduled Jan-Mar
if (today_month >= 9 && game_month <= 3) {
var game_year = today_year + 1;
} else {
throw {message: "utils().rawDateTime(): Dates not within range"}
}

var full_gamedate = (game_month +1 ) + "/" + gd_parts[1] + "/" + game_year
+ " " + gd_parts[2] + " " + gd_parts[3];
}
}
catch (e) {
// Run the old dateTime if the aboved erred
var clean_date = date.replace(/-/, " ");

return clean_date + " " + time;
var full_gamedate = clean_date + " " + time;
}

finally {
return full_gamedate;
}
}

Expand Down

0 comments on commit 192ced8

Please sign in to comment.