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

fix: timepicker on 23 and 25-hour days #4244

Merged
merged 6 commits into from
Sep 15, 2023

Conversation

lemming
Copy link
Contributor

@lemming lemming commented Sep 11, 2023

This is a fix for #4243.

Notes

Tests

I would gladly add tests but time zones are involved which makes things complicated. In Node.js timezone could be changed only with environment variable which requires significant changes in test running process if we want to test several of them.

timeClassName prop

Signature of timeClassName changed as currH and currM don't make sense anymore. This is a potentially breaking change and also add inconvenience for the user willing to highlight selected time.

Time range

Time range is also prone to DST bugs (if someone accidentally would set a date on the DST boundary) and the interface looks somewhat strange to me. I think it should either accept an array of ranges or start and end times in HH:mm format.

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ This pull request was sent to the PullRequest network.


@lemming you can click here to see the review status or cancel the code review job.

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PullRequest Breakdown

Reviewable lines of change

+ 97
- 39

74% JavaScript
26% JavaScript (tests)

Type of change

Fix - These changes are likely to be fixing a bug or issue.

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the changes work, I've made a suggestion about how you can simplify your isSameMinute function to make it more readable. No functional issues are seen with these changes.

Note: While testing things with timezones is hard, there are some good articles that talk about how to build jest mocking to help

Image of David K David K


Reviewed with ❤️ by PullRequest


return (
_date1.getTime() - seconds1 * 1000 - milliseconds1 ===
_date2.getTime() - seconds2 * 1000 - milliseconds2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally you would have a function that takes a date and returns the time without seconds and milliseconds, which you could re-use on two different dates. That said, the overall math of this feels complicated since based on my understanding you should be able to do the following function, which is simpler and involves less code.

function truncToMinute(d: Date) {
    return Math.trunc(d.getTime() / 60_000);
}

🔹 Simplify Code (Nice to have)

Image of David K David K

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, it won't work for strange historical time zones with subminute offsets.

Example

Prerequisite

Set timezone to 'Europe/Paris'

const toDate = d => new Date(d)

const d1 = new Date(1900, 0, 1, 0, 0);
console.log(d1.toISOString());
//=> '1899-12-31T23:50:39.000Z'           ¯\_(ツ)_/¯
const d2 new Date(1900, 0, 1, 0, 0, 40);
console.log(d2);
//=> '1899-12-31T23:51:19.000Z'

function truncToMinute(d) {
  return Math.trunc(d.getTime() / 60_000);
}

function isSameMinute(d1, d2) {
  const _date1 = toDate(d1);
  const _date2 = toDate(d2);

  const seconds1 = _date1.getSeconds();
  const seconds2 = _date2.getSeconds();
  const milliseconds1 = _date1.getMilliseconds();
  const milliseconds2 = _date2.getMilliseconds();

  return (
    _date1.getTime() - seconds1 * 1000 - milliseconds1 ===
    _date2.getTime() - seconds2 * 1000 - milliseconds2
    );
}

console.log(truncToMinute(d1) === truncToMinute(d2));
//=> false <- WRONG

console.log(isSameMinute(d1, d2));
//=> true <- RIGHT

But I can introduce startOfMinute function to make code more concise. date-fns's version can't be used here as it suffers from the same bug with DST.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding in a comment in code to note down the edge case permanently, I can see this being accidentally refactored into a regression in the future, consider the relatively niche timezone-related knowledge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! Added comments and a few tests (not yet related to time zones and daylight-saving time quirks, unfortunately)

src/time.jsx Outdated Show resolved Hide resolved
Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the feedback left by David, and would recommend applying those changes.

Image of Graham C Graham C


Reviewed with ❤️ by PullRequest

src/time.jsx Outdated Show resolved Hide resolved
Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PullRequest reviewed the updates made to #4244 up until the latest commit (bfb1a6b). No further issues were found.

Reviewed by:

Image of Graham C Graham C

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid improvement, a few more suggestions. My goal as a reviewer is to help create code that is easy for future readers to understand quickly.

Image of David K David K


Reviewed with ❤️ by PullRequest

src/date_utils.js Show resolved Hide resolved
src/date_utils.js Outdated Show resolved Hide resolved
Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PullRequest reviewed the updates made to #4244 up until the latest commit (5d8d4ac). No further issues were found.

Reviewed by:

Image of Graham C Graham C

@martijnrusschen
Copy link
Member

@lemming can you take a look at the test failures?

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PullRequest reviewed the updates made to #4244 since our last review was posted. This includes comments that have been posted by non-PullRequest reviewers. No further issues were found.

Reviewed by:

Image of Graham C Graham C

@lemming
Copy link
Contributor Author

lemming commented Sep 12, 2023

@lemming can you take a look at the test failures?

Sure

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PullRequest reviewed the updates made to #4244 since our last review was posted. This includes comments that have been posted by non-PullRequest reviewers. No further issues were found.

Reviewed by:

Image of Graham C Graham C

@codecov
Copy link

codecov bot commented Sep 12, 2023

Codecov Report

Merging #4244 (d6adb99) into main (21bfd83) will increase coverage by 0.00%.
Report is 16 commits behind head on main.
The diff coverage is 100.00%.

❗ Current head d6adb99 differs from pull request most recent head 0ea56aa. Consider uploading reports for the commit 0ea56aa to get more accurate results

@@           Coverage Diff           @@
##             main    #4244   +/-   ##
=======================================
  Coverage   96.33%   96.34%           
=======================================
  Files          25       25           
  Lines        2346     2351    +5     
  Branches      959      957    -2     
=======================================
+ Hits         2260     2265    +5     
  Misses         86       86           
Files Changed Coverage Δ
src/date_utils.js 98.07% <100.00%> (+0.04%) ⬆️
src/index.jsx 93.76% <100.00%> (+0.02%) ⬆️
src/time.jsx 89.71% <100.00%> (-0.64%) ⬇️

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PullRequest reviewed the updates made to #4244 up until the latest commit (3bdf133). No further issues were found.

Reviewed by:

Image of David K David K

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PullRequest reviewed the updates made to #4244 since our last review was posted. This includes comments that have been posted by non-PullRequest reviewers. No further issues were found.

Reviewed by:

Image of David K David K

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PullRequest reviewed the updates made to #4244 up until the latest commit (0ea56aa). No further issues were found.

Reviewed by:

Image of Graham C Graham C

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

Successfully merging this pull request may close these issues.

3 participants