Bug: DateTimeUtils.UTCToString returns local time despite the "UTC" name (also: deprecated substr)
bug
## Summary
`DateTimeUtils.UTCToString` is named and documented as returning a UTC time string, but it returns the local-timezone string via `Date.prototype.toTimeString()`. Anyone consuming this output has to know to ignore the function name, and tests/CI on machines in different timezones can produce different results.
## Affected code
`lib/DateTimeUtils.ts:3-7`
```ts
public static UTCToString(UTCString: string) {
let utcDate = new Date();
utcDate.setUTCHours(+UTCString.substr(0, 2), +UTCString.substr(2, 2), 0);
return utcDate.toTimeString(); // <-- returns LOCAL time string
}
```
`toTimeString()` produces e.g. `"08:30:00 GMT-0700 (Pacific Daylight Time)"` for an input that was conceptually 15:30 UTC.
## Suggested fix
Either return a UTC representation:
```ts
return utcDate.toUTCString();
// or, just the time portion:
return utcDate.toISOString().slice(11, 19);
```
…or rename the method if the local-time behavior is intentional.
## Related smaller cleanup
The whole class uses `String.prototype.substr()`, which is [a legacy/deprecated API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr). It still works but should be replaced with `substring()` or `slice()` for consistency with the rest of the codebase (`convertHHMMSSToTod`, `convertDayTimeToTod`, etc., already use `substring`). Affected: `UTCToString`, `UTCDateTimeToString`.
0 条评论