Repository: platers/obsidian-linter (ts)

Issue:
Bug: Invalid Date Format Makes Linter Always Update Date Modified Timestamp
- [x] I have verified that I am on the latest version of the Linter

## Describe the Bug

Linter always updates "date modified" in the front matter, even if no modifications were made by the linter or the user.

## How to Reproduce

Steps to reproduce the behavior:

1. Go to 'Linter Settings'
2. Got to 'YAML'
3. Scroll down to 'YAML Timestamp' > 'Format'
4. Enable "Keep track of the date file was last edited in the YAML Frontmatter..."
5. Enable "Date Modified"
6. Add "yyyy-MM-DDTHH:mm" Format
7. Create a new note with random name and random content
8. Execute Linter for the first time and front matter will be filled with the date modified
9. Repeatedly execute the linter after some time has passed (at least 1 min) without changing any content of the file.

[Linter-Debug.log](https://github.com/platers/obsidian-linter/files/12432676/Linter-Debug.log)


## Expected Behavior

I would expect that the 'date modified' is only updated if at least one change was made.

## Device

- [x] Desktop
- [ ] Mobile

## Additional Context

One place where this issue originates is the `handleDateCreatedValue` function in the yaml-timestamp.ts
This method will always return textModified true as far as I can see, thus resulting in an update of the dateModified, even if the `date created` is not changed.
However, apparently this is not the only place that needs to be changed to fix the behaviour. 

I haven't figured out if it is actually possible to know whether any changes were made in the `handleDateModifiedValue` function.
I'd be glad to create a pull request if there can be a fix for this.


Pull request:
Fix Timestamp Format Having Trailing Whitespace Making Format Invalid
Fixes #861 

Changes Made:
- Added a UT for the scenario in question
- Made sure to trim the end of the format when passing it into YAML Key Sort and in the logic for YAML Timestamp

Patch:
diff --git a/src/rules-runner.ts b/src/rules-runner.ts
index 4f4e0e5..70920da 100644
--- a/src/rules-runner.ts
+++ b/src/rules-runner.ts
@@ -136,7 +136,7 @@ export class RulesRunner {
 
     currentTime = runOptions.getCurrentTime();
     [newText] = YamlKeySort.applyIfEnabled(newText, runOptions.settings, this.disabledRules, {
-      currentTimeFormatted: currentTime.format(yamlTimestampOptions.format),
+      currentTimeFormatted: currentTime.format(yamlTimestampOptions.format.trimEnd()),
       yamlTimestampDateModifiedEnabled: isYamlTimestampEnabled && yamlTimestampOptions.dateModified,
       dateModifiedKey: yamlTimestampOptions.dateModifiedKey,
     });
diff --git a/src/rules/yaml-timestamp.ts b/src/rules/yaml-timestamp.ts
index 0ceccfb..2403a45 100644
--- a/src/rules/yaml-timestamp.ts
+++ b/src/rules/yaml-timestamp.ts
@@ -53,6 +53,7 @@ export default class YamlTimestamp extends RuleBuilder<YamlTimestampOptions> {
     let textModified = options.alreadyModified;
     const newText = initYAML(text);
     textModified = textModified || newText !== text;
+    options.format = options.format.trimEnd();
 
     return formatYAML(newText, (text) => {
       if (options.dateCreated) {


Test patch:
diff --git a/__tests__/yaml-timestamp.test.ts b/__tests__/yaml-timestamp.test.ts
index d24dfec..9e90ba2 100644
--- a/__tests__/yaml-timestamp.test.ts
+++ b/__tests__/yaml-timestamp.test.ts
@@ -41,6 +41,32 @@ ruleTest({
         format: '',
       },
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/861
+      testName: 'When format ends in whitespace, date created is not changed when time updates',
+      before: dedent`
+        ---
+        created: Wednesday, January 1st 2020, 12:00 am
+        modified: Thursday, January 2nd 2020, 12:00 am
+        ---
+      `,
+      after: dedent`
+        ---
+        created: Wednesday, January 1st 2020, 12:00 am
+        modified: Thursday, January 2nd 2020, 12:00 am
+        ---
+      `,
+      options: {
+        dateCreated: true,
+        dateCreatedKey: 'created',
+        dateModified: true,
+        dateModifiedKey: 'modified',
+        format: 'dddd, MMMM Do YYYY, h:mm a ',
+        currentTime: moment('Thursday, January 2nd 2020, 12:01 am', 'dddd, MMMM Do YYYY, h:mm a'),
+        alreadyModified: false,
+        forceRetentionOfCreatedValue: false,
+        fileModifiedTime: '2020-01-02T00:00:00-00',
+      },
+    },
     {
       testName: 'When the date format changes and `forceRetentionOfCreatedValue = true`, date created value is based on the one in the YAML frontmatter.',
       before: dedent`