@shisyamo4131/air-guard-v2-schemas 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shisyamo4131/air-guard-v2-schemas",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Schemas for AirGuard V2",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -170,9 +170,11 @@ export default class WorkingResult extends FireModel {
170
170
  date: {
171
171
  get: () => {
172
172
  if (!this.dateAt) return "";
173
- const year = this.dateAt.getFullYear();
174
- const month = String(this.dateAt.getMonth() + 1).padStart(2, "0"); // 月は0始まり
175
- const day = String(this.dateAt.getDate()).padStart(2, "0");
173
+ // UTC時刻に9時間(JST)を加算してJST日付を取得
174
+ const jstDate = new Date(this.dateAt.getTime() + 9 * 60 * 60 * 1000);
175
+ const year = jstDate.getUTCFullYear();
176
+ const month = String(jstDate.getUTCMonth() + 1).padStart(2, "0");
177
+ const day = String(jstDate.getUTCDate()).padStart(2, "0");
176
178
  return `${year}-${month}-${day}`;
177
179
  },
178
180
  set: (v) => {},
@@ -55,29 +55,34 @@ export default class CutoffDate {
55
55
  */
56
56
  static calculateActualCutoffDay(year, month, cutoffDateValue) {
57
57
  if (cutoffDateValue === CutoffDate.VALUES.END_OF_MONTH) {
58
- // Get last day of the month
59
- return new Date(year, month + 1, 0).getDate();
58
+ // Get last day of the month using UTC
59
+ return new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
60
60
  }
61
61
  return cutoffDateValue;
62
62
  }
63
63
 
64
64
  /**
65
65
  * Calculate billing period for given date and cutoff setting
66
- * @param {Date} targetDate - Target date
66
+ * @param {Date} targetDate - Target date in UTC (representing a JST date)
67
67
  * @param {number} cutoffDateValue - Cutoff date value from VALUES
68
68
  * @returns {Object} Billing period object
69
- * @property {Date} periodStart - Period start date
70
- * @property {Date} periodEnd - Period end date
69
+ * @property {Date} periodStart - Period start date in UTC (representing JST date)
70
+ * @property {Date} periodEnd - Period end date in UTC (representing JST date)
71
71
  * @property {string} periodLabel - Period label (YYYY-MM format)
72
72
  * @example
73
- * // Calculate billing period for March 15, 2024 with 10th cutoff
74
- * const period = CutoffDate.calculateBillingPeriod(new Date(2024, 2, 15), CutoffDate.VALUES.DAY_10);
75
- * // Returns period from March 11 to April 10
73
+ * // For JST 2025-01-20 with 15th cutoff
74
+ * // Input: UTC date representing JST 2025-01-20
75
+ * const period = CutoffDate.calculateBillingPeriod(new Date('2025-01-19T15:00:00Z'), 15);
76
+ * // Returns:
77
+ * // periodStart: UTC date representing JST 2025-01-16
78
+ * // periodEnd: UTC date representing JST 2025-02-15
76
79
  */
77
80
  static calculateBillingPeriod(targetDate, cutoffDateValue) {
78
- const year = targetDate.getFullYear();
79
- const month = targetDate.getMonth();
80
- const day = targetDate.getDate();
81
+ // Convert UTC to JST by adding 9 hours to get the JST date
82
+ const jstDate = new Date(targetDate.getTime() + 9 * 60 * 60 * 1000);
83
+ const year = jstDate.getUTCFullYear();
84
+ const month = jstDate.getUTCMonth();
85
+ const day = jstDate.getUTCDate();
81
86
 
82
87
  // Calculate cutoff day for current month
83
88
  const currentCutoffDay = CutoffDate.calculateActualCutoffDay(
@@ -90,7 +95,6 @@ export default class CutoffDate {
90
95
 
91
96
  if (day <= currentCutoffDay) {
92
97
  // Target date is within current month's billing period
93
- // Period: Previous month's cutoff + 1 to current month's cutoff
94
98
  const prevMonth = month - 1;
95
99
  const prevYear = prevMonth < 0 ? year - 1 : year;
96
100
  const normalizedPrevMonth = prevMonth < 0 ? 11 : prevMonth;
@@ -101,12 +105,19 @@ export default class CutoffDate {
101
105
  cutoffDateValue
102
106
  );
103
107
 
104
- periodStart = new Date(prevYear, normalizedPrevMonth, prevCutoffDay + 1);
105
- periodEnd = new Date(year, month, currentCutoffDay);
108
+ // Create dates in UTC representing JST dates (subtract 9 hours)
109
+ const startJst = Date.UTC(
110
+ prevYear,
111
+ normalizedPrevMonth,
112
+ prevCutoffDay + 1
113
+ );
114
+ const endJst = Date.UTC(year, month, currentCutoffDay);
115
+
116
+ periodStart = new Date(startJst - 9 * 60 * 60 * 1000);
117
+ periodEnd = new Date(endJst - 9 * 60 * 60 * 1000);
106
118
  periodLabel = `${year}-${String(month + 1).padStart(2, "0")}`;
107
119
  } else {
108
120
  // Target date is within next month's billing period
109
- // Period: Current month's cutoff + 1 to next month's cutoff
110
121
  const nextMonth = month + 1;
111
122
  const nextYear = nextMonth > 11 ? year + 1 : year;
112
123
  const normalizedNextMonth = nextMonth > 11 ? 0 : nextMonth;
@@ -117,8 +128,12 @@ export default class CutoffDate {
117
128
  cutoffDateValue
118
129
  );
119
130
 
120
- periodStart = new Date(year, month, currentCutoffDay + 1);
121
- periodEnd = new Date(nextYear, normalizedNextMonth, nextCutoffDay);
131
+ // Create dates in UTC representing JST dates (subtract 9 hours)
132
+ const startJst = Date.UTC(year, month, currentCutoffDay + 1);
133
+ const endJst = Date.UTC(nextYear, normalizedNextMonth, nextCutoffDay);
134
+
135
+ periodStart = new Date(startJst - 9 * 60 * 60 * 1000);
136
+ periodEnd = new Date(endJst - 9 * 60 * 60 * 1000);
122
137
  periodLabel = `${nextYear}-${String(normalizedNextMonth + 1).padStart(
123
138
  2,
124
139
  "0"
@@ -158,22 +173,25 @@ export default class CutoffDate {
158
173
 
159
174
  /**
160
175
  * Calculate the cutoff date as Date object for given sales date and cutoff setting
161
- * @param {Date} salesDate - Sales date
176
+ * @param {Date} salesDate - Sales date in UTC (representing a JST date)
162
177
  * @param {number} cutoffDateValue - Cutoff date value from VALUES
163
- * @returns {Date} Cutoff date as Date object
178
+ * @returns {Date} Cutoff date in UTC (representing a JST date)
164
179
  * @example
165
- * // Sales on March 15, 2024 with 10th cutoff
166
- * const cutoffDate = CutoffDate.calculateCutoffDate(new Date(2024, 2, 15), CutoffDate.VALUES.DAY_10);
167
- * // Returns Date object for 2024-04-10
180
+ * // For JST 2024-03-15 with 10th cutoff
181
+ * // Input: UTC date representing JST 2024-03-15
182
+ * const cutoffDate = CutoffDate.calculateCutoffDate(new Date('2024-03-14T15:00:00Z'), CutoffDate.VALUES.DAY_10);
183
+ * // Returns: UTC date representing JST 2024-04-10
168
184
  *
169
- * // Sales on March 5, 2024 with 10th cutoff
170
- * const cutoffDate = CutoffDate.calculateCutoffDate(new Date(2024, 2, 5), CutoffDate.VALUES.DAY_10);
171
- * // Returns Date object for 2024-03-10
185
+ * // For JST 2024-03-05 with 10th cutoff
186
+ * const cutoffDate = CutoffDate.calculateCutoffDate(new Date('2024-03-04T15:00:00Z'), CutoffDate.VALUES.DAY_10);
187
+ * // Returns: UTC date representing JST 2024-03-10
172
188
  */
173
189
  static calculateCutoffDate(salesDate, cutoffDateValue) {
174
- const year = salesDate.getFullYear();
175
- const month = salesDate.getMonth();
176
- const day = salesDate.getDate();
190
+ // Convert UTC to JST by adding 9 hours to get the JST date
191
+ const jstDate = new Date(salesDate.getTime() + 9 * 60 * 60 * 1000);
192
+ const year = jstDate.getUTCFullYear();
193
+ const month = jstDate.getUTCMonth();
194
+ const day = jstDate.getUTCDate();
177
195
 
178
196
  // Calculate cutoff day for current month
179
197
  const currentCutoffDay = CutoffDate.calculateActualCutoffDay(
@@ -185,7 +203,8 @@ export default class CutoffDate {
185
203
  if (day <= currentCutoffDay) {
186
204
  // Sales date is within current month's billing period
187
205
  // Cutoff date is current month's cutoff day
188
- return new Date(year, month, currentCutoffDay);
206
+ const cutoffJst = Date.UTC(year, month, currentCutoffDay);
207
+ return new Date(cutoffJst - 9 * 60 * 60 * 1000);
189
208
  } else {
190
209
  // Sales date is within next month's billing period
191
210
  // Cutoff date is next month's cutoff day
@@ -199,18 +218,19 @@ export default class CutoffDate {
199
218
  cutoffDateValue
200
219
  );
201
220
 
202
- return new Date(nextYear, normalizedNextMonth, nextCutoffDay);
221
+ const cutoffJst = Date.UTC(nextYear, normalizedNextMonth, nextCutoffDay);
222
+ return new Date(cutoffJst - 9 * 60 * 60 * 1000);
203
223
  }
204
224
  }
205
225
 
206
226
  /**
207
227
  * Calculate the cutoff date string (YYYY-MM-DD) for given sales date and cutoff setting
208
- * @param {Date} salesDate - Sales date
228
+ * @param {Date} salesDate - Sales date in UTC (representing a JST date)
209
229
  * @param {number} cutoffDateValue - Cutoff date value from VALUES
210
- * @returns {string} Cutoff date in YYYY-MM-DD format
230
+ * @returns {string} Cutoff date in YYYY-MM-DD format (JST)
211
231
  * @example
212
- * // Sales on March 15, 2024 with 10th cutoff
213
- * const cutoffDateString = CutoffDate.calculateCutoffDateString(new Date(2024, 2, 15), CutoffDate.VALUES.DAY_10);
232
+ * // For JST 2024-03-15 with 10th cutoff
233
+ * const cutoffDateString = CutoffDate.calculateCutoffDateString(new Date('2024-03-14T15:00:00Z'), CutoffDate.VALUES.DAY_10);
214
234
  * // Returns "2024-04-10"
215
235
  */
216
236
  static calculateCutoffDateString(salesDate, cutoffDateValue) {
@@ -218,6 +238,11 @@ export default class CutoffDate {
218
238
  salesDate,
219
239
  cutoffDateValue
220
240
  );
221
- return cutoffDate.toISOString().split("T")[0];
241
+ // Convert UTC back to JST for string representation
242
+ const jstDate = new Date(cutoffDate.getTime() + 9 * 60 * 60 * 1000);
243
+ const year = jstDate.getUTCFullYear();
244
+ const month = String(jstDate.getUTCMonth() + 1).padStart(2, "0");
245
+ const day = String(jstDate.getUTCDate()).padStart(2, "0");
246
+ return `${year}-${month}-${day}`;
222
247
  }
223
248
  }
@@ -1,13 +1,13 @@
1
1
  /**
2
- * Utility function to get a Date object at a specific time.
3
- * This function takes a date and a time string in HH:MM format,
4
- * and returns a Date object with the specified time set.
2
+ * Utility function to get a Date object at a specific JST time.
3
+ * This function takes a date and a JST time string in HH:MM format,
4
+ * and returns a Date object with the specified JST time set.
5
5
  * @param {string|Date} date The date to set the time on. Can be a string or a Date object.
6
6
  * If not provided, the current date is used.
7
- * @param {string} time The time to set in HH:MM format.
8
- * If not provided, defaults to 00:00 (midnight).
7
+ * @param {string} time The JST time to set in HH:MM format.
8
+ * If not provided, defaults to 00:00 (midnight JST).
9
9
  * @param {number} [dateOffset=0] Optional offset in days to apply to the date.
10
- * @returns {Date} A Date object with the specified date and time.
10
+ * @returns {Date} A Date object with the specified date and JST time.
11
11
  * @throws {Error} If the date is not a string or Date, or if the time is not a string in HH:MM format.
12
12
  * @throws {Error} If the time format is invalid.
13
13
  */
@@ -38,10 +38,10 @@ export function getDateAt(date, time, dateOffset = 0) {
38
38
  // If date is not provided, use the current date
39
39
  const result = new Date(date || Date.now());
40
40
 
41
- // Set the hours and minutes
42
- result.setHours(hour, minute, 0, 0);
41
+ // JSTはUTC+9なので、UTC時刻として設定してから9時間引く
42
+ result.setUTCHours(hour - 9, minute, 0, 0);
43
+ result.setUTCDate(result.getUTCDate() + dateOffset);
43
44
 
44
- result.setDate(result.getDate() + dateOffset);
45
45
  return result;
46
46
  }
47
47