@dvsa/appdev-api-common 0.5.0 → 0.6.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dvsa/appdev-api-common",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "keywords": ["dvsa", "nodejs", "typescript"],
5
5
  "author": "DVSA",
6
6
  "description": "Utils library for common API functionality",
@@ -1,26 +1,164 @@
1
1
  import dayjs from "dayjs";
2
2
  type AcceptableDate = DateTime | string | Date | null;
3
+ /**
4
+ * DateTime utility class for handling dates with UK timezone support
5
+ */
3
6
  export declare class DateTime {
4
7
  private instance;
5
8
  private static readonly UKLocalDateTimeFormat;
6
9
  private static readonly UKLocalDateFormat;
10
+ private static readonly UK_TIMEZONE;
11
+ /**
12
+ * Creates a new DateTime instance
13
+ * @param sourceDateTime - Initial date/time (string, Date, or another DateTime)
14
+ * @param format - Optional format string for parsing string dates
15
+ */
7
16
  constructor(sourceDateTime?: AcceptableDate, format?: string | undefined);
17
+ /**
18
+ * Converts to a JavaScript Date object
19
+ * @returns Date object
20
+ */
21
+ toDate(): Date;
22
+ /**
23
+ * Creates a new DateTime instance from a date source
24
+ * @param sourceDateTime - Source date/time
25
+ * @param format - Optional format string for parsing
26
+ * @returns New DateTime instance or null if source is null
27
+ */
8
28
  static at(sourceDateTime: AcceptableDate, format?: string | undefined): DateTime | null;
29
+ /**
30
+ * Formats a date in UK local date time format (DD/MM/YYYY HH:mm:ss)
31
+ * @param sourceDateTime - Source date/time
32
+ * @returns Formatted string or null if source is null
33
+ */
9
34
  static StandardUkLocalDateTimeAdapter(sourceDateTime: AcceptableDate): string | null;
35
+ /**
36
+ * Formats a date in UK local date format (DD/MM/YYYY)
37
+ * @param sourceDateTime - Source date/time
38
+ * @returns Formatted string or null if source is null
39
+ */
10
40
  static StandardUkLocalDateAdapter(sourceDateTime: AcceptableDate): string | null;
41
+ /**
42
+ * Adds time to this DateTime instance (mutable operation)
43
+ * @param amount - Amount to add
44
+ * @param unit - Unit of time (day, month, year, etc.)
45
+ * @returns This instance for chaining
46
+ */
11
47
  add(amount: number, unit: dayjs.ManipulateType): DateTime;
48
+ /**
49
+ * Subtracts time from this DateTime instance (mutable operation)
50
+ * @param amount - Amount to subtract
51
+ * @param unit - Unit of time (day, month, year, etc.)
52
+ * @returns This instance for chaining
53
+ */
12
54
  subtract(amount: number, unit: dayjs.ManipulateType): DateTime;
55
+ /**
56
+ * Formats the date with a custom format string
57
+ * @param formatString - Format pattern
58
+ * @returns Formatted date string
59
+ */
13
60
  format(formatString: string): string;
61
+ /**
62
+ * Gets the day of week (0-6, Sunday is 0)
63
+ * @returns Day of week
64
+ */
14
65
  day(): number;
66
+ /**
67
+ * Sets the date to the start of a specified unit (mutable operation)
68
+ * @param unit - Unit of time (day, month, year, etc.)
69
+ * @returns This instance for chaining
70
+ */
71
+ startOf(unit: dayjs.OpUnitType): DateTime;
72
+ /**
73
+ * Converts to string in UK date time format
74
+ * @returns Formatted date string
75
+ */
15
76
  toString(): string;
77
+ /**
78
+ * Gets ISO string representation
79
+ * @returns ISO format string
80
+ */
16
81
  toISOString(): string;
82
+ /**
83
+ * Calculates the difference between dates
84
+ * @param targetDate - Target date to compare with
85
+ * @param unit - Unit for the difference calculation
86
+ * @param precise - Whether to return decimal result
87
+ * @returns Difference in specified units
88
+ */
17
89
  diff(targetDate: AcceptableDate, unit: dayjs.QUnitType, precise?: boolean): number;
90
+ /**
91
+ * Calculates the difference in days
92
+ * @param targetDate - Target date to compare with
93
+ * @returns Difference in days
94
+ */
18
95
  daysDiff(targetDate: AcceptableDate): number;
96
+ /**
97
+ * Calculates duration from this date to target
98
+ * @param targetDate - Target date
99
+ * @param unit - Unit for duration calculation
100
+ * @returns Duration in specified units
101
+ */
19
102
  compareDuration(targetDate: AcceptableDate, unit: dayjs.QUnitType): number;
103
+ /**
104
+ * Checks if the date is valid
105
+ * @returns True if valid date
106
+ */
20
107
  isValid(): boolean;
108
+ /**
109
+ * Checks if this date is before the target date
110
+ * @param targetDate - Target date to compare with
111
+ * @returns True if this date is before target
112
+ */
21
113
  isBefore(targetDate: AcceptableDate): boolean;
114
+ /**
115
+ * Checks if this date is after the target date
116
+ * @param targetDate - Target date to compare with
117
+ * @returns True if this date is after target
118
+ */
22
119
  isAfter(targetDate: AcceptableDate): boolean;
120
+ /**
121
+ * Checks if this date is between start and end dates
122
+ * @param startDate - Start date of range
123
+ * @param endDate - End date of range
124
+ * @returns True if date is between start and end
125
+ */
23
126
  isBetween(startDate: AcceptableDate, endDate: AcceptableDate): boolean;
24
- static today(): Date;
127
+ /**
128
+ * Sets the timezone (mutable operation)
129
+ * @param tz - Timezone identifier
130
+ * @returns This instance for chaining
131
+ */
132
+ setTimezone(tz: string): DateTime;
133
+ /**
134
+ * Converts to UK timezone (mutable operation)
135
+ * @returns This instance for chaining
136
+ */
137
+ toUKTime(): DateTime;
138
+ /**
139
+ * Gets the hour component
140
+ * @returns Hour (0-23)
141
+ */
142
+ getHour(): number;
143
+ /**
144
+ * Gets the minute component
145
+ * @returns Minute (0-59)
146
+ */
147
+ getMinute(): number;
148
+ /**
149
+ * Gets the second component
150
+ * @returns Second (0-59)
151
+ */
152
+ getSecond(): number;
153
+ /**
154
+ * Creates a clone of this DateTime
155
+ * @returns New DateTime instance with the same date/time
156
+ */
157
+ clone(): DateTime;
158
+ /**
159
+ * Provides debug information about this DateTime
160
+ * @returns Object with diagnostic information
161
+ */
162
+ debug(): object;
25
163
  }
26
164
  export {};
@@ -6,89 +6,268 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.DateTime = void 0;
7
7
  const dayjs_1 = __importDefault(require("dayjs"));
8
8
  const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat"));
9
+ const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
10
+ const utc_1 = __importDefault(require("dayjs/plugin/utc"));
11
+ /**
12
+ * DateTime utility class for handling dates with UK timezone support
13
+ */
9
14
  class DateTime {
10
15
  instance;
11
16
  static UKLocalDateTimeFormat = "DD/MM/YYYY HH:mm:ss";
12
17
  static UKLocalDateFormat = "DD/MM/YYYY";
18
+ static UK_TIMEZONE = "Europe/London";
19
+ /**
20
+ * Creates a new DateTime instance
21
+ * @param sourceDateTime - Initial date/time (string, Date, or another DateTime)
22
+ * @param format - Optional format string for parsing string dates
23
+ */
13
24
  constructor(sourceDateTime, format = undefined) {
25
+ // Set up dayjs plugins
14
26
  dayjs_1.default.extend(customParseFormat_1.default);
27
+ dayjs_1.default.extend(timezone_1.default);
28
+ dayjs_1.default.extend(utc_1.default);
15
29
  if (sourceDateTime === undefined || sourceDateTime === null) {
16
- this.instance = (0, dayjs_1.default)();
30
+ // For current time, directly get the UK time but preserve the timezone
31
+ this.instance = (0, dayjs_1.default)().tz(DateTime.UK_TIMEZONE);
17
32
  }
18
33
  else if (typeof sourceDateTime === "string" ||
19
34
  sourceDateTime instanceof Date) {
20
- this.instance = (0, dayjs_1.default)(sourceDateTime, format);
35
+ // For string inputs, PRESERVE THE ORIGINAL TIME without timezone conversion
36
+ // This is essential for test consistency
37
+ if (format) {
38
+ this.instance = (0, dayjs_1.default)(sourceDateTime, format);
39
+ }
40
+ else {
41
+ this.instance = (0, dayjs_1.default)(sourceDateTime);
42
+ }
43
+ }
44
+ else if (sourceDateTime instanceof DateTime) {
45
+ // Clone a DateTime instance
46
+ this.instance = sourceDateTime.instance.clone();
21
47
  }
22
48
  else {
23
- this.instance = (0, dayjs_1.default)(sourceDateTime.instance, format);
49
+ throw new Error("Invalid date input");
24
50
  }
25
51
  }
52
+ /**
53
+ * Converts to a JavaScript Date object
54
+ * @returns Date object
55
+ */
56
+ toDate() {
57
+ return this.instance.toDate();
58
+ }
59
+ /**
60
+ * Creates a new DateTime instance from a date source
61
+ * @param sourceDateTime - Source date/time
62
+ * @param format - Optional format string for parsing
63
+ * @returns New DateTime instance or null if source is null
64
+ */
26
65
  static at(sourceDateTime, format = undefined) {
27
66
  if (!sourceDateTime) {
28
67
  return null;
29
68
  }
30
69
  return new DateTime(sourceDateTime, format);
31
70
  }
71
+ /**
72
+ * Formats a date in UK local date time format (DD/MM/YYYY HH:mm:ss)
73
+ * @param sourceDateTime - Source date/time
74
+ * @returns Formatted string or null if source is null
75
+ */
32
76
  static StandardUkLocalDateTimeAdapter(sourceDateTime) {
33
77
  return (DateTime.at(sourceDateTime)?.format(DateTime.UKLocalDateTimeFormat) ||
34
78
  null);
35
79
  }
80
+ /**
81
+ * Formats a date in UK local date format (DD/MM/YYYY)
82
+ * @param sourceDateTime - Source date/time
83
+ * @returns Formatted string or null if source is null
84
+ */
36
85
  static StandardUkLocalDateAdapter(sourceDateTime) {
37
86
  return (DateTime.at(sourceDateTime)?.format(DateTime.UKLocalDateFormat) || null);
38
87
  }
88
+ /**
89
+ * Adds time to this DateTime instance (mutable operation)
90
+ * @param amount - Amount to add
91
+ * @param unit - Unit of time (day, month, year, etc.)
92
+ * @returns This instance for chaining
93
+ */
39
94
  add(amount, unit) {
40
95
  this.instance = this.instance.add(amount, unit);
41
96
  return this;
42
97
  }
98
+ /**
99
+ * Subtracts time from this DateTime instance (mutable operation)
100
+ * @param amount - Amount to subtract
101
+ * @param unit - Unit of time (day, month, year, etc.)
102
+ * @returns This instance for chaining
103
+ */
43
104
  subtract(amount, unit) {
44
105
  this.instance = this.instance.subtract(amount, unit);
45
106
  return this;
46
107
  }
108
+ /**
109
+ * Formats the date with a custom format string
110
+ * @param formatString - Format pattern
111
+ * @returns Formatted date string
112
+ */
47
113
  format(formatString) {
48
114
  return this.instance.format(formatString);
49
115
  }
116
+ /**
117
+ * Gets the day of week (0-6, Sunday is 0)
118
+ * @returns Day of week
119
+ */
50
120
  day() {
51
121
  return this.instance.day();
52
122
  }
123
+ /**
124
+ * Sets the date to the start of a specified unit (mutable operation)
125
+ * @param unit - Unit of time (day, month, year, etc.)
126
+ * @returns This instance for chaining
127
+ */
128
+ startOf(unit) {
129
+ this.instance = this.instance.startOf(unit);
130
+ return this;
131
+ }
132
+ /**
133
+ * Converts to string in UK date time format
134
+ * @returns Formatted date string
135
+ */
53
136
  toString() {
54
- return this.instance.toString();
137
+ return this.format(DateTime.UKLocalDateTimeFormat);
55
138
  }
139
+ /**
140
+ * Gets ISO string representation
141
+ * @returns ISO format string
142
+ */
56
143
  toISOString() {
57
144
  return this.instance.toISOString();
58
145
  }
146
+ /**
147
+ * Calculates the difference between dates
148
+ * @param targetDate - Target date to compare with
149
+ * @param unit - Unit for the difference calculation
150
+ * @param precise - Whether to return decimal result
151
+ * @returns Difference in specified units
152
+ */
59
153
  diff(targetDate, unit, precise) {
60
154
  const date = new DateTime(targetDate);
61
155
  return this.instance.diff(date.instance, unit, precise);
62
156
  }
157
+ /**
158
+ * Calculates the difference in days
159
+ * @param targetDate - Target date to compare with
160
+ * @returns Difference in days
161
+ */
63
162
  daysDiff(targetDate) {
64
163
  const date = new DateTime(targetDate);
65
164
  return this.instance
66
165
  .startOf("day")
67
166
  .diff(date.instance.startOf("day"), "day");
68
167
  }
168
+ /**
169
+ * Calculates duration from this date to target
170
+ * @param targetDate - Target date
171
+ * @param unit - Unit for duration calculation
172
+ * @returns Duration in specified units
173
+ */
69
174
  compareDuration(targetDate, unit) {
70
175
  const date = new DateTime(targetDate);
71
176
  return date.instance.diff(this.instance, unit);
72
177
  }
178
+ /**
179
+ * Checks if the date is valid
180
+ * @returns True if valid date
181
+ */
73
182
  isValid() {
74
183
  return this.instance.isValid();
75
184
  }
185
+ /**
186
+ * Checks if this date is before the target date
187
+ * @param targetDate - Target date to compare with
188
+ * @returns True if this date is before target
189
+ */
76
190
  isBefore(targetDate) {
77
191
  const date = new DateTime(targetDate);
78
192
  return this.instance.isBefore(date.instance);
79
193
  }
194
+ /**
195
+ * Checks if this date is after the target date
196
+ * @param targetDate - Target date to compare with
197
+ * @returns True if this date is after target
198
+ */
80
199
  isAfter(targetDate) {
81
200
  const date = new DateTime(targetDate);
82
201
  return this.instance.isAfter(date.instance);
83
202
  }
203
+ /**
204
+ * Checks if this date is between start and end dates
205
+ * @param startDate - Start date of range
206
+ * @param endDate - End date of range
207
+ * @returns True if date is between start and end
208
+ */
84
209
  isBetween(startDate, endDate) {
85
210
  const start = new DateTime(startDate);
86
211
  const end = new DateTime(endDate);
87
212
  return (this.instance.isAfter(start.instance) &&
88
213
  this.instance.isBefore(end.instance));
89
214
  }
90
- static today() {
91
- return (0, dayjs_1.default)().toDate();
215
+ /**
216
+ * Sets the timezone (mutable operation)
217
+ * @param tz - Timezone identifier
218
+ * @returns This instance for chaining
219
+ */
220
+ setTimezone(tz) {
221
+ this.instance = this.instance.tz(tz);
222
+ return this;
223
+ }
224
+ /**
225
+ * Converts to UK timezone (mutable operation)
226
+ * @returns This instance for chaining
227
+ */
228
+ toUKTime() {
229
+ this.instance = this.instance.tz(DateTime.UK_TIMEZONE);
230
+ return this;
231
+ }
232
+ /**
233
+ * Gets the hour component
234
+ * @returns Hour (0-23)
235
+ */
236
+ getHour() {
237
+ return this.instance.hour();
238
+ }
239
+ /**
240
+ * Gets the minute component
241
+ * @returns Minute (0-59)
242
+ */
243
+ getMinute() {
244
+ return this.instance.minute();
245
+ }
246
+ /**
247
+ * Gets the second component
248
+ * @returns Second (0-59)
249
+ */
250
+ getSecond() {
251
+ return this.instance.second();
252
+ }
253
+ /**
254
+ * Creates a clone of this DateTime
255
+ * @returns New DateTime instance with the same date/time
256
+ */
257
+ clone() {
258
+ return new DateTime(this);
259
+ }
260
+ /**
261
+ * Provides debug information about this DateTime
262
+ * @returns Object with diagnostic information
263
+ */
264
+ debug() {
265
+ return {
266
+ formatted: this.format("DD/MM/YYYY HH:mm:ss"),
267
+ isoString: this.toISOString(),
268
+ hour: this.getHour(),
269
+ utcOffset: this.instance.utcOffset(),
270
+ };
92
271
  }
93
272
  }
94
273
  exports.DateTime = DateTime;