@giveback007/util-lib 3.2.0 → 3.3.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/time.ts +42 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@giveback007/util-lib",
3
- "version": "3.2.0",
3
+ "version": "3.3.1",
4
4
  "description": "Utility library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/time.ts CHANGED
@@ -154,6 +154,27 @@ export function getTime(
154
154
  startOf: {
155
155
  day: () => getTime(zonedTemporal.startOfDay(), timeZone),
156
156
 
157
+ week: (
158
+ weekStartsOn: "sun" | "mon" = "sun"
159
+ ) => {
160
+ const zT = zonedTemporal;
161
+ // dayOfWeek: 1 = Monday, 7 = Sunday
162
+ const currentDay = zT.dayOfWeek;
163
+
164
+ // Calculate days to subtract to get to week start
165
+ let daysToSubtract: number;
166
+ if (weekStartsOn === "sun") {
167
+ // Week starts on Sunday
168
+ daysToSubtract = currentDay === 7 ? 0 : currentDay;
169
+ } else {
170
+ // Week starts on Monday
171
+ daysToSubtract = currentDay - 1;
172
+ }
173
+
174
+ const weekStart = zT.subtract({ days: daysToSubtract }).startOfDay();
175
+ return getTime(weekStart, timeZone);
176
+ },
177
+
157
178
  month: () => {
158
179
  const zT = zonedTemporal;
159
180
  return getTime([zT.year, zT.month, 1, 0, 0, 0, 0], timeZone)
@@ -170,6 +191,27 @@ export function getTime(
170
191
  return getTime([zT.year, zT.month, zT.day, 23, 59, 59, 999], timeZone)
171
192
  },
172
193
 
194
+ week: (
195
+ weekStartsOn: "sun" | "mon" = "sun"
196
+ ) => {
197
+ const zT = zonedTemporal;
198
+ // dayOfWeek: 1 = Monday, 7 = Sunday
199
+ const currentDay = zT.dayOfWeek;
200
+
201
+ // Calculate days to add to get to week end
202
+ let daysToAdd: number;
203
+ if (weekStartsOn === "sun") {
204
+ // Week ends on Saturday (day 6)
205
+ daysToAdd = currentDay === 7 ? 6 : 6 - currentDay;
206
+ } else {
207
+ // Week ends on Sunday (day 7)
208
+ daysToAdd = 7 - currentDay;
209
+ }
210
+
211
+ const weekEnd = zT.add({ days: daysToAdd });
212
+ return getTime([weekEnd.year, weekEnd.month, weekEnd.day, 23, 59, 59, 999], timeZone);
213
+ },
214
+
173
215
  month: () => {
174
216
  const zT = zonedTemporal;
175
217
  return getTime([zT.year, zT.month, zT.daysInMonth, 23, 59, 59, 999], timeZone)