Skip to content

Bug: DateTimeUtils.UTCToString returns local time despite the "UTC" name (also: deprecated substr) #425

@kevinelliott

Description

@kevinelliott

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

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:

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. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions