Skip to content

Commit 90ad72c

Browse files
committed
Diff Time in Created At and LastModifiedAt
1 parent 2026bb7 commit 90ad72c

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel;
1+
using System;
2+
using System.ComponentModel;
23
using System.Globalization;
34
using System.IO;
45
using System.Runtime.CompilerServices;
@@ -65,22 +66,22 @@ public PreviewPanel(Settings settings, string filePath)
6566

6667
if (Settings.ShowCreatedDateInPreviewPanel)
6768
{
68-
CreatedAt = File
69-
.GetCreationTime(filePath)
70-
.ToString(
71-
$"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}",
72-
CultureInfo.CurrentCulture
73-
);
69+
DateTime createdDate = File.GetCreationTime(filePath);
70+
string formattedDate = createdDate.ToString(
71+
$"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}",
72+
CultureInfo.CurrentCulture
73+
);
74+
CreatedAt = $"{GetDiffTimeString(createdDate)} - {formattedDate}";
7475
}
7576

7677
if (Settings.ShowModifiedDateInPreviewPanel)
7778
{
78-
LastModifiedAt = File
79-
.GetLastWriteTime(filePath)
80-
.ToString(
81-
$"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}",
82-
CultureInfo.CurrentCulture
83-
);
79+
DateTime lastModifiedDate = File.GetLastWriteTime(filePath);
80+
string formattedDate = lastModifiedDate.ToString(
81+
$"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}",
82+
CultureInfo.CurrentCulture
83+
);
84+
LastModifiedAt = $"{GetDiffTimeString(lastModifiedDate)} - {formattedDate}";
8485
}
8586

8687
_ = LoadImageAsync();
@@ -90,7 +91,27 @@ private async Task LoadImageAsync()
9091
{
9192
PreviewImage = await Main.Context.API.LoadImageAsync(FilePath, true).ConfigureAwait(false);
9293
}
94+
95+
private string GetDiffTimeString(DateTime fileDateTime)
96+
{
97+
DateTime now = DateTime.Now;
98+
TimeSpan difference = now - fileDateTime;
99+
100+
if (difference.TotalDays < 1)
101+
return "Today";
102+
if (difference.TotalDays < 30)
103+
return $"{(int)difference.TotalDays} days ago";
104+
105+
int monthsDiff = (now.Year - fileDateTime.Year) * 12 + now.Month - fileDateTime.Month;
106+
if (monthsDiff < 12)
107+
return monthsDiff == 1 ? "1 month ago" : $"{monthsDiff} months ago";
93108

109+
int yearsDiff = now.Year - fileDateTime.Year;
110+
if (now.Month < fileDateTime.Month || (now.Month == fileDateTime.Month && now.Day < fileDateTime.Day))
111+
yearsDiff--;
112+
113+
return yearsDiff == 1 ? "1 year ago" : $"{yearsDiff} years ago";
114+
}
94115
public event PropertyChangedEventHandler? PropertyChanged;
95116

96117
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)

0 commit comments

Comments
 (0)