Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getOffsetStr() evaluates incorrectly for negative values #60

Closed
dondevi opened this issue Oct 31, 2023 · 1 comment
Closed

getOffsetStr() evaluates incorrectly for negative values #60

dondevi opened this issue Oct 31, 2023 · 1 comment

Comments

@dondevi
Copy link

dondevi commented Oct 31, 2023

The problem

The utcOffset of Pacific/Marquesas is -570, but utcOffsetStr returns -10:30.

What is expected

The utcOffsetStr returns -09:30.

What is happening

The code in src/build-timezone.js

function getOffsetStr(offset) {
  const hours = Math.floor(offset / 60);  // There may be a calculation error here
  const min = offset % 60;
  const sign = offset < 0 ? '-' : '+';

  return `${sign}${getNumStr(hours)}:${getNumStr(min)}`;
}

This evaluates incorrectly for negative values

Math.floor(570 / 60); // 9
Math.floor(-570 / 60); // -10

Suggest to fix

const hours = Math.floor(Math.abs(offset) / 60);
// or
const hours = ~~(offset / 60);
@manuelmhtr
Copy link
Owner

Hi @dondevi thanks a lot for reporting this issue and proposing a solution. It has been fixed in v3.5.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants