ts-time-utils 1.1.0 → 2.0.0

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.
Files changed (74) hide show
  1. package/README.md +458 -12
  2. package/dist/calculate.d.ts +7 -2
  3. package/dist/calculate.d.ts.map +1 -1
  4. package/dist/calculate.js +13 -3
  5. package/dist/calendar.d.ts +103 -0
  6. package/dist/calendar.d.ts.map +1 -1
  7. package/dist/calendar.js +224 -0
  8. package/dist/compare.d.ts +217 -0
  9. package/dist/compare.d.ts.map +1 -0
  10. package/dist/compare.js +417 -0
  11. package/dist/cron.d.ts +82 -0
  12. package/dist/cron.d.ts.map +1 -0
  13. package/dist/cron.js +294 -0
  14. package/dist/esm/calculate.d.ts +7 -2
  15. package/dist/esm/calculate.d.ts.map +1 -1
  16. package/dist/esm/calculate.js +13 -3
  17. package/dist/esm/calendar.d.ts +103 -0
  18. package/dist/esm/calendar.d.ts.map +1 -1
  19. package/dist/esm/calendar.js +224 -0
  20. package/dist/esm/compare.d.ts +217 -0
  21. package/dist/esm/compare.d.ts.map +1 -0
  22. package/dist/esm/compare.js +417 -0
  23. package/dist/esm/cron.d.ts +82 -0
  24. package/dist/esm/cron.d.ts.map +1 -0
  25. package/dist/esm/cron.js +294 -0
  26. package/dist/esm/fiscal.d.ts +195 -0
  27. package/dist/esm/fiscal.d.ts.map +1 -0
  28. package/dist/esm/fiscal.js +295 -0
  29. package/dist/esm/format.d.ts +65 -0
  30. package/dist/esm/format.d.ts.map +1 -1
  31. package/dist/esm/format.js +202 -0
  32. package/dist/esm/index.d.ts +13 -6
  33. package/dist/esm/index.d.ts.map +1 -1
  34. package/dist/esm/index.js +14 -6
  35. package/dist/esm/iterate.d.ts +212 -0
  36. package/dist/esm/iterate.d.ts.map +1 -0
  37. package/dist/esm/iterate.js +409 -0
  38. package/dist/esm/parse.d.ts +45 -0
  39. package/dist/esm/parse.d.ts.map +1 -1
  40. package/dist/esm/parse.js +207 -0
  41. package/dist/esm/timezone.d.ts +52 -0
  42. package/dist/esm/timezone.d.ts.map +1 -1
  43. package/dist/esm/timezone.js +171 -0
  44. package/dist/esm/validate.d.ts +51 -0
  45. package/dist/esm/validate.d.ts.map +1 -1
  46. package/dist/esm/validate.js +92 -0
  47. package/dist/esm/workingHours.d.ts +70 -0
  48. package/dist/esm/workingHours.d.ts.map +1 -1
  49. package/dist/esm/workingHours.js +161 -0
  50. package/dist/fiscal.d.ts +195 -0
  51. package/dist/fiscal.d.ts.map +1 -0
  52. package/dist/fiscal.js +295 -0
  53. package/dist/format.d.ts +65 -0
  54. package/dist/format.d.ts.map +1 -1
  55. package/dist/format.js +202 -0
  56. package/dist/index.d.ts +13 -6
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js +14 -6
  59. package/dist/iterate.d.ts +212 -0
  60. package/dist/iterate.d.ts.map +1 -0
  61. package/dist/iterate.js +409 -0
  62. package/dist/parse.d.ts +45 -0
  63. package/dist/parse.d.ts.map +1 -1
  64. package/dist/parse.js +207 -0
  65. package/dist/timezone.d.ts +52 -0
  66. package/dist/timezone.d.ts.map +1 -1
  67. package/dist/timezone.js +171 -0
  68. package/dist/validate.d.ts +51 -0
  69. package/dist/validate.d.ts.map +1 -1
  70. package/dist/validate.js +92 -0
  71. package/dist/workingHours.d.ts +70 -0
  72. package/dist/workingHours.d.ts.map +1 -1
  73. package/dist/workingHours.js +161 -0
  74. package/package.json +30 -11
@@ -13,4 +13,74 @@ export declare function nextWorkingTime(date: Date, config?: WorkingHoursConfig)
13
13
  export declare function workingTimeBetween(start: Date, end: Date, config?: WorkingHoursConfig): number;
14
14
  /** Advance by working hours amount (simple iterative approach) */
15
15
  export declare function addWorkingHours(start: Date, hours: number, config?: WorkingHoursConfig): Date;
16
+ /**
17
+ * Add working days to a date
18
+ * @param start - start date
19
+ * @param days - number of working days to add
20
+ * @param config - working hours configuration
21
+ */
22
+ export declare function addWorkingDays(start: Date, days: number, config?: WorkingHoursConfig): Date;
23
+ /**
24
+ * Subtract working days from a date
25
+ * @param start - start date
26
+ * @param days - number of working days to subtract
27
+ * @param config - working hours configuration
28
+ */
29
+ export declare function subtractWorkingDays(start: Date, days: number, config?: WorkingHoursConfig): Date;
30
+ /**
31
+ * Get the next working day from a given date
32
+ * @param date - start date
33
+ * @param config - working hours configuration
34
+ */
35
+ export declare function getNextWorkingDay(date: Date, config?: WorkingHoursConfig): Date;
36
+ /**
37
+ * Get the previous working day from a given date
38
+ * @param date - start date
39
+ * @param config - working hours configuration
40
+ */
41
+ export declare function getPreviousWorkingDay(date: Date, config?: WorkingHoursConfig): Date;
42
+ /**
43
+ * Get the number of working days in a month
44
+ * @param year - year
45
+ * @param month - month (0-11)
46
+ * @param config - working hours configuration
47
+ */
48
+ export declare function getWorkingDaysInMonth(year: number, month: number, config?: WorkingHoursConfig): number;
49
+ /**
50
+ * Get all working days in a month as an array of dates
51
+ * @param year - year
52
+ * @param month - month (0-11)
53
+ * @param config - working hours configuration
54
+ */
55
+ export declare function getWorkingDaysInMonthArray(year: number, month: number, config?: WorkingHoursConfig): Date[];
56
+ /**
57
+ * Get working days between two dates (inclusive)
58
+ * @param start - start date
59
+ * @param end - end date
60
+ * @param config - working hours configuration
61
+ */
62
+ export declare function workingDaysBetween(start: Date, end: Date, config?: WorkingHoursConfig): number;
63
+ /**
64
+ * Check if a date is during work break
65
+ * @param date - date to check
66
+ * @param config - working hours configuration
67
+ */
68
+ export declare function isBreakTime(date: Date, config?: WorkingHoursConfig): boolean;
69
+ /**
70
+ * Get the start of the work day for a given date
71
+ * @param date - date to get work start for
72
+ * @param config - working hours configuration
73
+ */
74
+ export declare function getWorkDayStart(date: Date, config?: WorkingHoursConfig): Date;
75
+ /**
76
+ * Get the end of the work day for a given date
77
+ * @param date - date to get work end for
78
+ * @param config - working hours configuration
79
+ */
80
+ export declare function getWorkDayEnd(date: Date, config?: WorkingHoursConfig): Date;
81
+ /**
82
+ * Get the total working hours per day (excluding breaks)
83
+ * @param config - working hours configuration
84
+ */
85
+ export declare function getWorkingHoursPerDay(config?: WorkingHoursConfig): number;
16
86
  //# sourceMappingURL=workingHours.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"workingHours.d.ts","sourceRoot":"","sources":["../src/workingHours.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD,eAAO,MAAM,qBAAqB,EAAE,kBAInC,CAAC;AAEF,kDAAkD;AAClD,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,kBAA0C,GAAG,OAAO,CAEpG;AAOD,uDAAuD;AACvD,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,kBAA0C,GAAG,OAAO,CAUrG;AAED,+CAA+C;AAC/C,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,kBAA0C,GAAG,IAAI,CAMpG;AAYD,kDAAkD;AAClD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,GAAE,kBAA0C,GAAG,MAAM,CA+BrH;AAED,kEAAkE;AAClE,wBAAgB,eAAe,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,kBAA0C,GAAG,IAAI,CAcpH"}
1
+ {"version":3,"file":"workingHours.d.ts","sourceRoot":"","sources":["../src/workingHours.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD,eAAO,MAAM,qBAAqB,EAAE,kBAInC,CAAC;AAEF,kDAAkD;AAClD,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,kBAA0C,GAAG,OAAO,CAEpG;AAOD,uDAAuD;AACvD,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,kB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BAA0C,GAAG,MAAM,CAkBrH;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,kBAA0C,GAAG,OAAO,CASnG;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,kBAA0C,GAAG,IAAI,CAIpG;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,kBAA0C,GAAG,IAAI,CAIlG;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,GAAE,kBAA0C,GAAG,MAAM,CAUhG"}
@@ -107,3 +107,164 @@ export function addWorkingHours(start, hours, config = DEFAULT_WORKING_HOURS) {
107
107
  }
108
108
  return cursor;
109
109
  }
110
+ /**
111
+ * Add working days to a date
112
+ * @param start - start date
113
+ * @param days - number of working days to add
114
+ * @param config - working hours configuration
115
+ */
116
+ export function addWorkingDays(start, days, config = DEFAULT_WORKING_HOURS) {
117
+ if (days === 0)
118
+ return new Date(start);
119
+ const result = new Date(start);
120
+ const direction = days > 0 ? 1 : -1;
121
+ let remaining = Math.abs(days);
122
+ while (remaining > 0) {
123
+ result.setDate(result.getDate() + direction);
124
+ if (isWorkingDay(result, config)) {
125
+ remaining--;
126
+ }
127
+ }
128
+ return result;
129
+ }
130
+ /**
131
+ * Subtract working days from a date
132
+ * @param start - start date
133
+ * @param days - number of working days to subtract
134
+ * @param config - working hours configuration
135
+ */
136
+ export function subtractWorkingDays(start, days, config = DEFAULT_WORKING_HOURS) {
137
+ return addWorkingDays(start, -days, config);
138
+ }
139
+ /**
140
+ * Get the next working day from a given date
141
+ * @param date - start date
142
+ * @param config - working hours configuration
143
+ */
144
+ export function getNextWorkingDay(date, config = DEFAULT_WORKING_HOURS) {
145
+ const result = new Date(date);
146
+ result.setDate(result.getDate() + 1);
147
+ while (!isWorkingDay(result, config)) {
148
+ result.setDate(result.getDate() + 1);
149
+ }
150
+ return result;
151
+ }
152
+ /**
153
+ * Get the previous working day from a given date
154
+ * @param date - start date
155
+ * @param config - working hours configuration
156
+ */
157
+ export function getPreviousWorkingDay(date, config = DEFAULT_WORKING_HOURS) {
158
+ const result = new Date(date);
159
+ result.setDate(result.getDate() - 1);
160
+ while (!isWorkingDay(result, config)) {
161
+ result.setDate(result.getDate() - 1);
162
+ }
163
+ return result;
164
+ }
165
+ /**
166
+ * Get the number of working days in a month
167
+ * @param year - year
168
+ * @param month - month (0-11)
169
+ * @param config - working hours configuration
170
+ */
171
+ export function getWorkingDaysInMonth(year, month, config = DEFAULT_WORKING_HOURS) {
172
+ const daysInMonth = new Date(year, month + 1, 0).getDate();
173
+ let count = 0;
174
+ for (let day = 1; day <= daysInMonth; day++) {
175
+ const date = new Date(year, month, day);
176
+ if (isWorkingDay(date, config)) {
177
+ count++;
178
+ }
179
+ }
180
+ return count;
181
+ }
182
+ /**
183
+ * Get all working days in a month as an array of dates
184
+ * @param year - year
185
+ * @param month - month (0-11)
186
+ * @param config - working hours configuration
187
+ */
188
+ export function getWorkingDaysInMonthArray(year, month, config = DEFAULT_WORKING_HOURS) {
189
+ const daysInMonth = new Date(year, month + 1, 0).getDate();
190
+ const workingDays = [];
191
+ for (let day = 1; day <= daysInMonth; day++) {
192
+ const date = new Date(year, month, day);
193
+ if (isWorkingDay(date, config)) {
194
+ workingDays.push(date);
195
+ }
196
+ }
197
+ return workingDays;
198
+ }
199
+ /**
200
+ * Get working days between two dates (inclusive)
201
+ * @param start - start date
202
+ * @param end - end date
203
+ * @param config - working hours configuration
204
+ */
205
+ export function workingDaysBetween(start, end, config = DEFAULT_WORKING_HOURS) {
206
+ if (end < start)
207
+ return 0;
208
+ let count = 0;
209
+ const current = new Date(start);
210
+ current.setHours(0, 0, 0, 0);
211
+ const endDate = new Date(end);
212
+ endDate.setHours(0, 0, 0, 0);
213
+ while (current <= endDate) {
214
+ if (isWorkingDay(current, config)) {
215
+ count++;
216
+ }
217
+ current.setDate(current.getDate() + 1);
218
+ }
219
+ return count;
220
+ }
221
+ /**
222
+ * Check if a date is during work break
223
+ * @param date - date to check
224
+ * @param config - working hours configuration
225
+ */
226
+ export function isBreakTime(date, config = DEFAULT_WORKING_HOURS) {
227
+ if (!isWorkingDay(date, config))
228
+ return false;
229
+ if (!config.breaks || config.breaks.length === 0)
230
+ return false;
231
+ const h = toHourFraction(date);
232
+ for (const b of config.breaks) {
233
+ if (h >= b.start && h < b.end)
234
+ return true;
235
+ }
236
+ return false;
237
+ }
238
+ /**
239
+ * Get the start of the work day for a given date
240
+ * @param date - date to get work start for
241
+ * @param config - working hours configuration
242
+ */
243
+ export function getWorkDayStart(date, config = DEFAULT_WORKING_HOURS) {
244
+ const result = new Date(date);
245
+ result.setHours(config.hours.start, 0, 0, 0);
246
+ return result;
247
+ }
248
+ /**
249
+ * Get the end of the work day for a given date
250
+ * @param date - date to get work end for
251
+ * @param config - working hours configuration
252
+ */
253
+ export function getWorkDayEnd(date, config = DEFAULT_WORKING_HOURS) {
254
+ const result = new Date(date);
255
+ result.setHours(config.hours.end, 0, 0, 0);
256
+ return result;
257
+ }
258
+ /**
259
+ * Get the total working hours per day (excluding breaks)
260
+ * @param config - working hours configuration
261
+ */
262
+ export function getWorkingHoursPerDay(config = DEFAULT_WORKING_HOURS) {
263
+ let hours = config.hours.end - config.hours.start;
264
+ if (config.breaks) {
265
+ for (const b of config.breaks) {
266
+ hours -= (b.end - b.start);
267
+ }
268
+ }
269
+ return hours;
270
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-time-utils",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "A comprehensive TypeScript utility library for time, dates, durations, and calendar operations with full tree-shaking support",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -106,20 +106,31 @@
106
106
  "import": "./dist/esm/naturalLanguage.js",
107
107
  "require": "./dist/naturalLanguage.js",
108
108
  "types": "./dist/naturalLanguage.d.ts"
109
+ },
110
+ "./cron": {
111
+ "import": "./dist/esm/cron.js",
112
+ "require": "./dist/cron.js",
113
+ "types": "./dist/cron.d.ts"
114
+ },
115
+ "./fiscal": {
116
+ "import": "./dist/esm/fiscal.js",
117
+ "require": "./dist/fiscal.js",
118
+ "types": "./dist/fiscal.d.ts"
119
+ },
120
+ "./compare": {
121
+ "import": "./dist/esm/compare.js",
122
+ "require": "./dist/compare.js",
123
+ "types": "./dist/compare.d.ts"
124
+ },
125
+ "./iterate": {
126
+ "import": "./dist/esm/iterate.js",
127
+ "require": "./dist/iterate.js",
128
+ "types": "./dist/iterate.d.ts"
109
129
  }
110
130
  },
111
131
  "files": [
112
132
  "dist"
113
133
  ],
114
- "scripts": {
115
- "build": "npm run build:cjs && npm run build:esm",
116
- "build:cjs": "tsc -p tsconfig.json",
117
- "build:esm": "tsc -p tsconfig.esm.json",
118
- "test": "vitest run",
119
- "test:watch": "vitest",
120
- "lint": "eslint src --ext .ts",
121
- "prepublishOnly": "npm run build"
122
- },
123
134
  "keywords": [
124
135
  "time",
125
136
  "date",
@@ -162,5 +173,13 @@
162
173
  "homepage": "https://github.com/hatefrad/ts-time-utils#readme",
163
174
  "bugs": {
164
175
  "url": "https://github.com/hatefrad/ts-time-utils/issues"
176
+ },
177
+ "scripts": {
178
+ "build": "npm run build:cjs && npm run build:esm",
179
+ "build:cjs": "tsc -p tsconfig.json",
180
+ "build:esm": "tsc -p tsconfig.esm.json",
181
+ "test": "vitest run",
182
+ "test:watch": "vitest",
183
+ "lint": "eslint src --ext .ts"
165
184
  }
166
- }
185
+ }