Skip to content

Commit

Permalink
Bug 1935179 - Part 4: Make FormatUTCOffsetNanoseconds a file-static f…
Browse files Browse the repository at this point in the history
…unction. r=spidermonkey-reviewers,dminor

Differential Revision: https://phabricator.services.mozilla.com/D231121
  • Loading branch information
anba committed Dec 5, 2024
1 parent 8cf0786 commit 6435580
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 70 deletions.
65 changes: 0 additions & 65 deletions js/src/builtin/temporal/TimeZone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,71 +773,6 @@ bool js::temporal::GetOffsetNanosecondsFor(JSContext* cx,
return true;
}

/**
* FormatUTCOffsetNanoseconds ( offsetNanoseconds )
*/
JSString* js::temporal::FormatUTCOffsetNanoseconds(JSContext* cx,
int64_t offsetNanoseconds) {
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day));

// Step 1.
char sign = offsetNanoseconds >= 0 ? '+' : '-';

// Step 2.
int64_t absoluteNanoseconds = std::abs(offsetNanoseconds);

// Step 6. (Reordered)
int32_t subSecondNanoseconds = int32_t(absoluteNanoseconds % 1'000'000'000);

// Step 5. (Reordered)
int32_t quotient = int32_t(absoluteNanoseconds / 1'000'000'000);
int32_t second = quotient % 60;

// Step 4. (Reordered)
quotient /= 60;
int32_t minute = quotient % 60;

// Step 3.
int32_t hour = quotient / 60;
MOZ_ASSERT(hour < 24, "time zone offset mustn't exceed 24-hours");

// Format: "sign hour{2} : minute{2} : second{2} . fractional{9}"
constexpr size_t maxLength = 1 + 2 + 1 + 2 + 1 + 2 + 1 + 9;
char result[maxLength];

size_t n = 0;

// Steps 7-8. (Inlined FormatTimeString).
result[n++] = sign;
result[n++] = char('0' + (hour / 10));
result[n++] = char('0' + (hour % 10));
result[n++] = ':';
result[n++] = char('0' + (minute / 10));
result[n++] = char('0' + (minute % 10));

if (second != 0 || subSecondNanoseconds != 0) {
result[n++] = ':';
result[n++] = char('0' + (second / 10));
result[n++] = char('0' + (second % 10));

if (uint32_t fractional = subSecondNanoseconds) {
result[n++] = '.';

uint32_t k = 100'000'000;
do {
result[n++] = char('0' + (fractional / k));
fractional %= k;
k /= 10;
} while (fractional);
}
}

MOZ_ASSERT(n <= maxLength);

// Step 9.
return NewStringCopyN<CanGC>(cx, result, n);
}

/**
* TimeZoneEquals ( one, two )
*/
Expand Down
5 changes: 0 additions & 5 deletions js/src/builtin/temporal/TimeZone.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,6 @@ bool GetEpochNanosecondsFor(JSContext* cx, JS::Handle<TimeZoneValue> timeZone,
TemporalDisambiguation disambiguation,
EpochNanoseconds* result);

/**
* FormatUTCOffsetNanoseconds ( offsetNanoseconds )
*/
JSString* FormatUTCOffsetNanoseconds(JSContext* cx, int64_t offsetNanoseconds);

/**
* GetOffsetNanosecondsFor ( timeZone, epochNs )
*/
Expand Down
65 changes: 65 additions & 0 deletions js/src/builtin/temporal/ZonedDateTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,71 @@ static bool AddDurationToZonedDateTime(JSContext* cx,
return true;
}

/**
* FormatUTCOffsetNanoseconds ( offsetNanoseconds )
*/
static JSString* FormatUTCOffsetNanoseconds(JSContext* cx,
int64_t offsetNanoseconds) {
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day));

// Step 1.
char sign = offsetNanoseconds >= 0 ? '+' : '-';

// Step 2.
int64_t absoluteNanoseconds = std::abs(offsetNanoseconds);

// Step 6. (Reordered)
int32_t subSecondNanoseconds = int32_t(absoluteNanoseconds % 1'000'000'000);

// Step 5. (Reordered)
int32_t quotient = int32_t(absoluteNanoseconds / 1'000'000'000);
int32_t second = quotient % 60;

// Step 4. (Reordered)
quotient /= 60;
int32_t minute = quotient % 60;

// Step 3.
int32_t hour = quotient / 60;
MOZ_ASSERT(hour < 24, "time zone offset mustn't exceed 24-hours");

// Format: "sign hour{2} : minute{2} : second{2} . fractional{9}"
constexpr size_t maxLength = 1 + 2 + 1 + 2 + 1 + 2 + 1 + 9;
char result[maxLength];

size_t n = 0;

// Steps 7-8. (Inlined FormatTimeString).
result[n++] = sign;
result[n++] = char('0' + (hour / 10));
result[n++] = char('0' + (hour % 10));
result[n++] = ':';
result[n++] = char('0' + (minute / 10));
result[n++] = char('0' + (minute % 10));

if (second != 0 || subSecondNanoseconds != 0) {
result[n++] = ':';
result[n++] = char('0' + (second / 10));
result[n++] = char('0' + (second % 10));

if (uint32_t fractional = subSecondNanoseconds) {
result[n++] = '.';

uint32_t k = 100'000'000;
do {
result[n++] = char('0' + (fractional / k));
fractional %= k;
k /= 10;
} while (fractional);
}
}

MOZ_ASSERT(n <= maxLength);

// Step 9.
return NewStringCopyN<CanGC>(cx, result, n);
}

/**
* Temporal.ZonedDateTime ( epochNanoseconds, timeZone [ , calendar ] )
*/
Expand Down

0 comments on commit 6435580

Please sign in to comment.