From 1d90a17e69bc2b18642c4b97700ca04a5b76405b Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 26 Feb 2024 15:21:24 +0100 Subject: [PATCH] added timestamp display for durations closes #20 --- src/settings-tab.ts | 11 ++++++ src/settings.ts | 4 +- src/tracker.ts | 38 +++++++++++-------- .../plugins/simple-time-tracker/data.json | 5 ++- test-vault/duration_accumulation_test.md | 2 +- 5 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/settings-tab.ts b/src/settings-tab.ts index b3aa7fa..ff7fb0b 100644 --- a/src/settings-tab.ts +++ b/src/settings-tab.ts @@ -52,6 +52,17 @@ export class SimpleTimeTrackerSettingsTab extends PluginSettingTab { }); }); + new Setting(this.containerEl) + .setName("Timestamp Durations") + .setDesc("Whether durations should be displayed in a timestamp format (12:15:01) rather than the default duration format (12h 15m 1s).") + .addToggle(t => { + t.setValue(this.plugin.settings.timestampDurations); + t.onChange(async v => { + this.plugin.settings.timestampDurations = v; + await this.plugin.saveSettings(); + }); + }); + new Setting(this.containerEl) .setName("Display Segments in Reverse Order") .setDesc("Whether older tracker segments should be displayed towards the bottom of the tracker, rather than the top.") diff --git a/src/settings.ts b/src/settings.ts index cb63680..a70e817 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -3,7 +3,8 @@ export const defaultSettings: SimpleTimeTrackerSettings = { editableTimestampFormat: "YYYY-MM-DD HH:mm:ss", csvDelimiter: ",", fineGrainedDurations: true, - reverseSegmentOrder: false + reverseSegmentOrder: false, + timestampDurations: false }; export interface SimpleTimeTrackerSettings { @@ -13,5 +14,6 @@ export interface SimpleTimeTrackerSettings { csvDelimiter: string; fineGrainedDurations: boolean; reverseSegmentOrder: boolean; + timestampDurations: boolean; } diff --git a/src/tracker.ts b/src/tracker.ts index 6dd0260..4d93614 100644 --- a/src/tracker.ts +++ b/src/tracker.ts @@ -215,23 +215,31 @@ function unformatEditableTimestamp(formatted: string, settings: SimpleTimeTracke function formatDuration(totalTime: number, settings: SimpleTimeTrackerSettings): string { let ret = ""; let duration = moment.duration(totalTime); - let hours: number; - if (settings.fineGrainedDurations) { - if (duration.years() > 0) - ret += duration.years() + "y "; - if (duration.months() > 0) - ret += duration.months() + "M "; - if (duration.days() > 0) - ret += duration.days() + "d "; - hours = duration.hours(); + let hours = settings.fineGrainedDurations ? duration.hours() : Math.floor(duration.asHours()); + + if (settings.timestampDurations) { + if (settings.fineGrainedDurations) { + let days = Math.floor(duration.asDays()); + if (days > 0) + ret += days + "."; + } + ret += `${hours.toString().padStart(2, "0")}:${duration.minutes().toString().padStart(2, "0")}:${duration.seconds().toString().padStart(2, "0")}`; } else { - hours = Math.floor(duration.asHours()); + if (settings.fineGrainedDurations) { + let years = Math.floor(duration.asYears()); + if (years > 0) + ret += years + "y "; + if (duration.months() > 0) + ret += duration.months() + "M "; + if (duration.days() > 0) + ret += duration.days() + "d "; + } + if (hours > 0) + ret += hours + "h "; + if (duration.minutes() > 0) + ret += duration.minutes() + "m "; + ret += duration.seconds() + "s"; } - if (hours > 0) - ret += hours + "h "; - if (duration.minutes() > 0) - ret += duration.minutes() + "m "; - ret += duration.seconds() + "s"; return ret; } diff --git a/test-vault/.obsidian/plugins/simple-time-tracker/data.json b/test-vault/.obsidian/plugins/simple-time-tracker/data.json index 11e1cee..de2b77c 100644 --- a/test-vault/.obsidian/plugins/simple-time-tracker/data.json +++ b/test-vault/.obsidian/plugins/simple-time-tracker/data.json @@ -2,6 +2,7 @@ "timestampFormat": "YY-MM-DD hh:mm:ss", "editableTimestampFormat": "YYYY-MM-DD HH:mm:ss", "csvDelimiter": ",", - "fineGrainedDurations": false, - "reverseSegmentOrder": true + "fineGrainedDurations": true, + "reverseSegmentOrder": false, + "timestampDurations": true } \ No newline at end of file diff --git a/test-vault/duration_accumulation_test.md b/test-vault/duration_accumulation_test.md index c5773f2..a289167 100644 --- a/test-vault/duration_accumulation_test.md +++ b/test-vault/duration_accumulation_test.md @@ -6,5 +6,5 @@ More notes for my cool project! This note shows that we can correctly display ac ``` ```simple-time-tracker -{"entries":[{"name":"Segment 1","startTime":null,"endTime":null,"subEntries":[{"name":"Part 1","startTime":"2024-02-26T13:37:59.292Z","endTime":"2024-02-26T13:38:01.437Z","subEntries":null},{"name":"Part 2","startTime":"2024-02-26T13:38:16.235Z","endTime":"2024-02-26T13:38:18.895Z","subEntries":null}]}]} +{"entries":[{"name":"Segment 1","startTime":null,"endTime":null,"subEntries":[{"name":"Part 1","startTime":null,"endTime":null,"subEntries":[{"name":"Part 1","startTime":"2024-02-26T13:37:59.292Z","endTime":"2024-02-26T13:38:01.437Z","subEntries":null},{"name":"Part 2","startTime":"2024-02-26T14:04:14.156Z","endTime":"2024-02-26T14:04:30.576Z","subEntries":null}]},{"name":"Part 2","startTime":"2024-02-26T13:38:16.235Z","endTime":"2024-02-26T13:38:18.895Z","subEntries":null}]}]} ```