Skip to content

Commit c1b5c2b

Browse files
committed
[ts] fix date validation to allow zero based hours, minutes and seconds
1 parent 2c4e518 commit c1b5c2b

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ npm install @2bad/onvif
3939
```typescript
4040
import { Onvif } from '@2bad/onvif';
4141

42-
// Connect to a camera
43-
const cam = new Onvif({
42+
// Connect to a device
43+
const device = new Onvif({
4444
hostname: '192.168.1.123',
4545
username: 'admin',
4646
password: 'password',

source/onvif.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -259,29 +259,31 @@ export class Onvif extends EventEmitter {
259259

260260
const dateTime = systemDateAndTime.UTCDateTime || systemDateAndTime.localDateTime
261261

262-
let time: Date
263262
if (!dateTime) {
264263
// Fallback to current time if dateTime is undefined
265-
time = new Date()
266-
} else {
267-
const dt = linerase(dateTime) as DateTime
268-
if (
269-
!dt.date ||
270-
!dt.time ||
271-
!dt.date.year ||
272-
!dt.date.month ||
273-
!dt.date.day ||
274-
!dt.time.hour ||
275-
!dt.time.minute ||
276-
!dt.time.second
277-
) {
278-
throw new Error('Invalid date or time structure')
264+
const time = new Date()
265+
if (!this.timeShift) {
266+
this.timeShift = time.getTime() - process.uptime() * 1000
279267
}
280-
time = new Date(
281-
Date.UTC(dt.date.year, dt.date.month - 1, dt.date.day, dt.time.hour, dt.time.minute, dt.time.second)
282-
)
268+
return time
269+
}
270+
271+
const dt = linerase(dateTime) as DateTime
272+
if (!dt.date?.year || !dt.date?.month || !dt.date?.day) {
273+
throw new Error('Invalid date structure: year, month, and day are required')
283274
}
284275

276+
const time = new Date(
277+
Date.UTC(
278+
dt.date.year,
279+
dt.date.month - 1,
280+
dt.date.day,
281+
dt.time?.hour ?? 0,
282+
dt.time?.minute ?? 0,
283+
dt.time?.second ?? 0
284+
)
285+
)
286+
285287
if (!this.timeShift) {
286288
this.timeShift = time.getTime() - process.uptime() * 1000
287289
}

0 commit comments

Comments
 (0)