From 90ad72ca30ca7e99e3e84c4cac4e507f91b47a4c Mon Sep 17 00:00:00 2001 From: 01Dri Date: Fri, 23 May 2025 16:33:47 -0300 Subject: [PATCH] Diff Time in Created At and LastModifiedAt --- .../Views/PreviewPanel.xaml.cs | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs index aaf1efdc1..1981a8b0e 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using System; +using System.ComponentModel; using System.Globalization; using System.IO; using System.Runtime.CompilerServices; @@ -65,22 +66,22 @@ public partial class PreviewPanel : UserControl, INotifyPropertyChanged if (Settings.ShowCreatedDateInPreviewPanel) { - CreatedAt = File - .GetCreationTime(filePath) - .ToString( - $"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}", - CultureInfo.CurrentCulture - ); + DateTime createdDate = File.GetCreationTime(filePath); + string formattedDate = createdDate.ToString( + $"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}", + CultureInfo.CurrentCulture + ); + CreatedAt = $"{GetDiffTimeString(createdDate)} - {formattedDate}"; } if (Settings.ShowModifiedDateInPreviewPanel) { - LastModifiedAt = File - .GetLastWriteTime(filePath) - .ToString( - $"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}", - CultureInfo.CurrentCulture - ); + DateTime lastModifiedDate = File.GetLastWriteTime(filePath); + string formattedDate = lastModifiedDate.ToString( + $"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}", + CultureInfo.CurrentCulture + ); + LastModifiedAt = $"{GetDiffTimeString(lastModifiedDate)} - {formattedDate}"; } _ = LoadImageAsync(); @@ -90,7 +91,27 @@ public partial class PreviewPanel : UserControl, INotifyPropertyChanged { PreviewImage = await Main.Context.API.LoadImageAsync(FilePath, true).ConfigureAwait(false); } + + private string GetDiffTimeString(DateTime fileDateTime) + { + DateTime now = DateTime.Now; + TimeSpan difference = now - fileDateTime; + if (difference.TotalDays < 1) + return "Today"; + if (difference.TotalDays < 30) + return $"{(int)difference.TotalDays} days ago"; + + int monthsDiff = (now.Year - fileDateTime.Year) * 12 + now.Month - fileDateTime.Month; + if (monthsDiff < 12) + return monthsDiff == 1 ? "1 month ago" : $"{monthsDiff} months ago"; + + int yearsDiff = now.Year - fileDateTime.Year; + if (now.Month < fileDateTime.Month || (now.Month == fileDateTime.Month && now.Day < fileDateTime.Day)) + yearsDiff--; + + return yearsDiff == 1 ? "1 year ago" : $"{yearsDiff} years ago"; + } public event PropertyChangedEventHandler? PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)