diff --git a/game/base-system/time/dateTime.js b/game/base-system/time/dateTime.js index 8415ea4708cabfc6f6e22b7ecc6aa852b6fac716..fc0bbd51883b657db73854399d4033822caea77f 100644 --- a/game/base-system/time/dateTime.js +++ b/game/base-system/time/dateTime.js @@ -1,12 +1,14 @@ -const secondsPerDay = 86400; -const secondsPerHour = 3600; -const secondsPerMinute = 60; -const standardYearMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; -const leapYearMonths = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; -const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; -const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; - class DateTime { + /* plain static variables are still too new of a feature for a considerable number of old mobiles, they are re-declared in Time object instead + static secondsPerDay = 86400; + static secondsPerHour = 3600; + static secondsPerMinute = 60; + static standardYearMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + static leapYearMonths = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + static monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; + static daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; + */ + constructor(year = 2020, month = 1, day = 1, hour = 0, minute = 0, second = 0) { if (arguments.length === 1) { // If the argument is a DateTime object, copy its properties @@ -42,7 +44,7 @@ class DateTime { } static getDaysOfMonthFromYear(year) { - return DateTime.isLeapYear(year) ? leapYearMonths : standardYearMonths; + return DateTime.isLeapYear(year) ? Time.leapYearMonths : Time.standardYearMonths; } static getDaysOfYear(year) { @@ -57,7 +59,7 @@ class DateTime { if (day < 1 || day > daysInMonth[month - 1]) throw new Error("Invalid date: Day must be between 1-" + daysInMonth[month - 1] + "."); const totalDays = DateTime.getTotalDaysSinceStart(year) + daysInMonth.slice(0, month - 1).reduce((a, b) => a + b, 0) + day - 1; - const totalSeconds = totalDays * secondsPerDay + hour * secondsPerHour + minute * secondsPerMinute + second; + const totalSeconds = totalDays * Time.secondsPerDay + hour * Time.secondsPerHour + minute * Time.secondsPerMinute + second; this.timeStamp = totalSeconds; this.year = year; @@ -73,9 +75,9 @@ class DateTime { // Initialize the year to 1 let year = 1; let month = 0; - let day = (timestamp / secondsPerDay) | 0; - const hour = (timestamp / secondsPerHour) | 0; - const minute = (timestamp / secondsPerMinute) | 0; + let day = (timestamp / Time.secondsPerDay) | 0; + const hour = (timestamp / Time.secondsPerHour) | 0; + const minute = (timestamp / Time.secondsPerMinute) | 0; const second = timestamp; // Maps the total number of days to the corresponding year and day. @@ -157,7 +159,7 @@ class DateTime { // Adding a negative value (e.g. -1) subtracts the days instead addDays(days) { if (days === 0) return this; - this.fromTimestamp(this.timeStamp + days * secondsPerDay); + this.fromTimestamp(this.timeStamp + days * Time.secondsPerDay); return this; } @@ -165,7 +167,7 @@ class DateTime { // Adding a negative value (e.g. -1) subtracts the hours instead addHours(hours) { if (hours === 0) return this; - this.timeStamp += hours * secondsPerHour; + this.timeStamp += hours * Time.secondsPerHour; this.fromTimestamp(this.timeStamp); return this; } @@ -174,7 +176,7 @@ class DateTime { // Adding a negative value (e.g. -1) subtracts the minutes instead addMinutes(minutes) { if (minutes === 0) return this; - this.timeStamp += minutes * secondsPerMinute; + this.timeStamp += minutes * Time.secondsPerMinute; this.fromTimestamp(this.timeStamp); return this; } @@ -191,7 +193,7 @@ class DateTime { // Returns the weekday (1-7 for Sun-Sat) of the current object's date. get weekDay() { const daysSinceStart = DateTime.getTotalDaysSinceStart(this.year + 1); - const daysInMonth = standardYearMonths.slice(0, this.month - 1).reduce((a, b) => a + b, 0); + const daysInMonth = Time.standardYearMonths.slice(0, this.month - 1).reduce((a, b) => a + b, 0); const isLeapYear = DateTime.isLeapYear(this.year) && this.month < 3; const weekDayOffset = V.weekDayOffset !== undefined ? V.weekDayOffset : 6; @@ -203,12 +205,12 @@ class DateTime { // Returns the name of the weekday (e.g. "Sunday") of the current object's date. get weekDayName() { - return daysOfWeek[this.weekDay - 1]; + return Time.daysOfWeek[this.weekDay - 1]; } // Returns the name of the month (e.g. "January") of the current object's date. get monthName() { - return monthNames[this.month - 1]; + return Time.monthNames[this.month - 1]; } // Returns a boolean indicating whether the current object's date falls on a weekend (Saturday or Sunday). diff --git a/game/base-system/time/time.js b/game/base-system/time/time.js index b5f8c12c9378cc1be15cc32996e84d39cfd0eeb4..4b0b6b1029d515c7bab9ae416d1ac228658017b3 100644 --- a/game/base-system/time/time.js +++ b/game/base-system/time/time.js @@ -1,7 +1,16 @@ /* eslint-disable jsdoc/require-description-complete-sentence */ /* eslint-disable no-undef */ const Time = (() => { + const secondsPerDay = 86400; + const secondsPerHour = 3600; + const secondsPerMinute = 60; + const standardYearMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + const leapYearMonths = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; + const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; + const holidayMonths = [4, 7, 8, 12]; + let currentDate = {}; function set(timeStamp = V.timeStamp) { @@ -127,7 +136,7 @@ const Time = (() => { return currentDate.year; }, get days() { - return Math.floor((currentDate.timeStamp - this.startDate.timeStamp) / DateTime.secondsPerDay); + return Math.floor((currentDate.timeStamp - this.startDate.timeStamp) / Time.secondsPerDay); }, get season() { return this.month > 11 || this.month < 3 ? "winter" : this.month > 8 ? "autumn" : this.month > 5 ? "summer" : "spring"; @@ -179,6 +188,15 @@ const Time = (() => { isSchoolTerm, isSchoolDay, isSchoolTime, + + secondsPerDay, + secondsPerHour, + secondsPerMinute, + standardYearMonths, + leapYearMonths, + monthNames, + daysOfWeek, + getNextSchoolTermStartDate: nextSchoolTermStartDate, getNextSchoolTermEndDate: nextSchoolTermEndDate, getNextWeekdayDate: weekDay => currentDate.getNextWeekdayDate(weekDay), diff --git a/game/base-system/time/timeMacros.js b/game/base-system/time/timeMacros.js index 1ade6991144c104b56d3413fe45d5dfe4bbe4a81..4bea5e14e211bca5ff4b7f5c336fd801470b71e9 100644 --- a/game/base-system/time/timeMacros.js +++ b/game/base-system/time/timeMacros.js @@ -1,7 +1,7 @@ /* eslint-disable no-undef */ function timeAfterXHours(hours) { const date = new DateTime(Time.date); - date.addSeconds(hours * DateTime.secondsPerHour); + date.addSeconds(hours * Time.secondsPerHour); return ampm(date.hour, date.minute); } DefineMacroS("timeAfterXHours", timeAfterXHours); @@ -49,16 +49,16 @@ Macro.add("passTimeUntil", { const secondsMapper = { sec: 1, seconds: 1, - min: DateTime.secondsPerMinute, - mins: DateTime.secondsPerMinute, - minute: DateTime.secondsPerMinute, - minutes: DateTime.secondsPerMinute, - hour: DateTime.secondsPerHour, - hours: DateTime.secondsPerHour, - day: DateTime.secondsPerDay, - days: DateTime.secondsPerDay, - week: DateTime.secondsPerDay * 7, - weeks: DateTime.secondsPerDay * 7, + min: Time.secondsPerMinute, + mins: Time.secondsPerMinute, + minute: Time.secondsPerMinute, + minutes: Time.secondsPerMinute, + hour: Time.secondsPerHour, + hours: Time.secondsPerHour, + day: Time.secondsPerDay, + days: Time.secondsPerDay, + week: Time.secondsPerDay * 7, + weeks: Time.secondsPerDay * 7, }; /**