Repository: obsidian-tasks-group/obsidian-tasks (ts)

Issue:
Urgency Score for due date changes at midday
### Please check that this issue hasn't been reported before.

- [X] I searched previous [Bug Reports](https://github.com/obsidian-tasks-group/obsidian-tasks/issues?q=is%3Aopen+is%3Aissue+label%3A%22type%3A+bug%22+sort%3Acomments-desc) didn't find any similar reports.

### Expected Behavior

When I have a task whose due date is today, the due date component of the urgency should be 8.8.

In the Unit tests, when 
- **Given** today is `2023-06-26`
- **When** calculating the urgency for this task: `- [ ] 🔽 📅 2023-06-26`
- **Then** the score should be 8.8 for `due` (today) and `0.0` for `Low` priority

This afternoon at 17:45 in the UK, the urgency score is `9.26` which in the tests is the component for `due 1 day ago`.


### Current behaviour

See above

### Steps to reproduce

1. Create a file called `Sample Tests`
1. Paster in the task:

```
- [ ] #task 🔽 📅 2023-06-26
```

2. Set its `due` date to the current day.
3. Paste in this query:

~~~
```tasks
filename includes Sample Tests
group by urgency
show urgency
```
~~~


5. **Note the tasks urgency - it should be 8.8**
6. **Repeat in the afternoon - it should be 8.8**

### Which Operating Systems are you using?

- [ ] Android
- [ ] iPhone/iPad
- [ ] Linux
- [X] macOS
- [ ] Windows

### Obsidian Version

1.3.5

### Tasks Plugin Version

4.0.0

### Checks

- [ ] I have tried it with all other plugins disabled and the error still occurs

### Possible solution

_No response_

Pull request:
fix: Prevent date components of Urgency changing and midday
# Description

Adjust the Urgency calculation code so that it no longer depends on the time of day.

Previously at mid-day a task due on today's date changed its score to be the value for `a task due yesterday`. See #2068.

The fix was identified by @schemar - thank you!

## Motivation and Context

This fixes #2068.

## How has this been tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, tests ran to see how -->
<!--- your change affects other areas of the code, etc. -->

- With a failing test that then passed after the bug fix
- Manually in the Demo vault.

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

Changes visible to users:

- [x] **Bug fix** (prefix: `fix` - non-breaking change which fixes an issue)

Internal changes:

- [x] **Tests** (prefix: `test` - additions and improvements to unit tests and the smoke tests)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [x] My code follows the code style of this project and passes `yarn run lint`.
- [ ] My change requires a change to the documentation.
- [ ] I have [updated the documentation](https://publish.obsidian.md/tasks-contributing/Documentation/About+Documentation) accordingly.
- [x] My change has adequate [Unit Test coverage](https://publish.obsidian.md/tasks-contributing/Testing/About+Testing).

## Terms

<!--
By submitting this pull request, you must agree to follow our
[contributing guide](https://publish.obsidian.md/tasks-contributing) and
[Code of Conduct](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CODE_OF_CONDUCT.md).
Put an x in the boxes to confirm you agree.
-->

- [x] My contribution follow this project's [contributing guide](https://publish.obsidian.md/tasks-contributing)
- [x] I agree to follow this project's [Code of Conduct](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CODE_OF_CONDUCT.md)


Patch:
diff --git a/src/Urgency.ts b/src/Urgency.ts
index 1b771fe4..a26535aa 100644
--- a/src/Urgency.ts
+++ b/src/Urgency.ts
@@ -13,7 +13,8 @@ export class Urgency {
 
         if (task.dueDate !== null) {
             // Map a range of 21 days to the value 0.2 - 1.0
-            const daysOverdue = Math.round(window.moment().diff(task.dueDate) / Urgency.milliSecondsPerDay);
+            const startOfToday = window.moment().startOf('day');
+            const daysOverdue = Math.round(startOfToday.diff(task.dueDate) / Urgency.milliSecondsPerDay);
 
             let dueMultiplier: number;
             if (daysOverdue >= 7.0) {


Test patch:
diff --git a/tests/Query/Urgency.test.ts b/tests/Query/Urgency.test.ts
index 6b836a60..ff508aaf 100644
--- a/tests/Query/Urgency.test.ts
+++ b/tests/Query/Urgency.test.ts
@@ -7,6 +7,7 @@ import { TaskBuilder } from '../TestingTools/TaskBuilder';
 import { Urgency } from '../../src/Urgency';
 import { Priority } from '../../src/Task';
 import { calculateRelativeDate } from '../TestingTools/DateTestHelpers';
+import { fromLine } from '../TestHelpers';
 
 window.moment = moment;
 
@@ -40,6 +41,38 @@ function lowPriorityBuilder() {
     return new TaskBuilder().priority(Priority.Low);
 }
 
+// -----------------------------------------------------------------
+// Time of day does not affect result
+
+describe('urgency - test time-of-day impact on due-date score', () => {
+    // Test to reproduce https://github.com/obsidian-tasks-group/obsidian-tasks/issues/2068
+    beforeEach(() => {
+        jest.useFakeTimers();
+    });
+
+    afterEach(() => {
+        jest.useRealTimers();
+    });
+
+    const task = fromLine({ line: '- [ ] #task 🔽 📅 2023-06-26', path: 'a/b/c.md', precedingHeader: null });
+
+    it.each([
+        // Force new line for each time
+        ['00:00'],
+        ['00:01'],
+        ['06:00'],
+        ['09:00'],
+        ['11:59'],
+        ['12:00'],
+        ['12:01'],
+        ['19:00'],
+        ['23:59'],
+    ])('with time  "%s"', (time: string) => {
+        jest.setSystemTime(new Date('2023-06-26 ' + time));
+        expect(Urgency.calculate(task)).toEqual(8.8);
+    });
+});
+
 // -----------------------------------------------------------------
 // Priority tests