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

duration.js里头的位运算符 >> 存在问题 #313

Open
LianPaiPai opened this issue Jun 28, 2023 · 1 comment
Open

duration.js里头的位运算符 >> 存在问题 #313

LianPaiPai opened this issue Jun 28, 2023 · 1 comment

Comments

@LianPaiPai
Copy link

No description provided.

@LianPaiPai LianPaiPai changed the title duration.js duration.js里头的位运算符 >> 存在问题 Jun 28, 2023
@LianPaiPai
Copy link
Author

inDays: { get: () => (this._duration / Duration.microsecondsPerDay) >> 0, }, inHours: { get: () => (this._duration / Duration.microsecondsPerHour) >> 0, }, inMinutes: { get: () => (this._duration / Duration.microsecondsPerMinute) >> 0, }, inSeconds: { get: () => (this._duration / Duration.microsecondsPerSecond) >> 0, }, inMilliseconds: { get: () => (this._duration / Duration.microsecondsPerMillisecond) >> 0, },

这里 >> 0的目的看起来是想向下取整,但是 inMilliseconds 很容易就超出位运算上限。
我不是很懂JS,但是我看了一下说是JS的位运算都是32位的,所以这里很有可能出现问题。例如我在dart中写:
DateTime.now().subtract(Duration(days: 30));
时间跨度的计算,我看JS代码里头是以毫秒来计算的,30天得到的毫秒代码里是用(微秒/1000) >> 0得来的,除完之后再做位运算,推测已经到达位数上限,导致算出来的值有问题。

是不是用Math.floor比较好一点?

Uploading 截屏2023-06-28 14.58.08.png…

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

1 participant