diff --git a/src/Models/Blame.cs b/src/Models/Blame.cs index 6a75e750..a8fac34c 100644 --- a/src/Models/Blame.cs +++ b/src/Models/Blame.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace SourceGit.Models { @@ -10,7 +9,6 @@ namespace SourceGit.Models public string File { get; set; } = string.Empty; public string Author { get; set; } = string.Empty; public ulong Timestamp { get; set; } = 0; - public string Time => DateTime.UnixEpoch.AddSeconds(Timestamp).ToLocalTime().ToString(DateTimeFormat.Active.DateOnly); } public class BlameData diff --git a/src/Models/DateTimeFormat.cs b/src/Models/DateTimeFormat.cs index 1139bd6f..9010e523 100644 --- a/src/Models/DateTimeFormat.cs +++ b/src/Models/DateTimeFormat.cs @@ -5,38 +5,20 @@ namespace SourceGit.Models { public class DateTimeFormat { - public string DateOnly + public static readonly List Supported = new List { - get; - set; - } - - public string DateTime - { - get - { - if (_use24Hours != Use24Hours || string.IsNullOrEmpty(_dateTime)) - { - _use24Hours = Use24Hours; - _dateTime = _use24Hours ? $"{DateOnly} HH:mm:ss" : $"{DateOnly} hh:mm:ss tt"; - } - - return _dateTime; - } - } - - public string Example - { - get - { - return new DateTime(2025, 1, 31, 8, 0, 0, DateTimeKind.Local).ToString(DateOnly); - } - } - - public DateTimeFormat(string date) - { - DateOnly = date; - } + new("yyyy/MM/dd"), + new("yyyy.MM.dd"), + new("yyyy-MM-dd"), + new("MM/dd/yyyy"), + new("MM.dd.yyyy"), + new("MM-dd-yyyy"), + new("dd/MM/yyyy"), + new("dd.MM.yyyy"), + new("dd-MM-yyyy"), + new("MMM d yyyy"), + new("d MMM yyyy"), + }; public static int ActiveIndex { @@ -50,27 +32,35 @@ namespace SourceGit.Models set; } = true; - public static DateTimeFormat Active + public string DateFormat { - get => Supported[ActiveIndex]; + get; } - public static readonly List Supported = new List + public string Example { - new DateTimeFormat("yyyy/MM/dd"), - new DateTimeFormat("yyyy.MM.dd"), - new DateTimeFormat("yyyy-MM-dd"), - new DateTimeFormat("MM/dd/yyyy"), - new DateTimeFormat("MM.dd.yyyy"), - new DateTimeFormat("MM-dd-yyyy"), - new DateTimeFormat("dd/MM/yyyy"), - new DateTimeFormat("dd.MM.yyyy"), - new DateTimeFormat("dd-MM-yyyy"), - new DateTimeFormat("MMM d yyyy"), - new DateTimeFormat("d MMM yyyy"), - }; + get => DateTime.Now.ToString(DateFormat); + } - private bool _use24Hours = true; - private string _dateTime = null; + public DateTimeFormat(string date) + { + DateFormat = date; + } + + public static string Format(ulong timestamp, bool dateOnly = false) + { + var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime(); + return Format(localTime, dateOnly); + } + + public static string Format(DateTime localTime, bool dateOnly = false) + { + var actived = Supported[ActiveIndex]; + if (dateOnly) + return localTime.ToString(actived.DateFormat); + + var format = Use24Hours ? $"{actived.DateFormat} HH:mm:ss" : $"{actived.DateFormat} hh:mm:ss tt"; + return localTime.ToString(format); + } } } diff --git a/src/Models/SelfUpdate.cs b/src/Models/SelfUpdate.cs index 116b1b04..9cf95a14 100644 --- a/src/Models/SelfUpdate.cs +++ b/src/Models/SelfUpdate.cs @@ -28,7 +28,7 @@ namespace SourceGit.Models public bool IsNewVersion => CurrentVersion.CompareTo(new System.Version(TagName.Substring(1))) < 0; [JsonIgnore] - public string ReleaseDateStr => PublishedAt.ToString(DateTimeFormat.Active.DateOnly); + public string ReleaseDateStr => DateTimeFormat.Format(PublishedAt, true); public Version() { diff --git a/src/Views/Blame.axaml.cs b/src/Views/Blame.axaml.cs index a7f14afc..b0bb8b86 100644 --- a/src/Views/Blame.axaml.cs +++ b/src/Views/Blame.axaml.cs @@ -79,8 +79,9 @@ namespace SourceGit.Views var authorTop = y - author.Height * 0.5; context.DrawText(author, new Point(x, authorTop)); + var timeStr = Models.DateTimeFormat.Format(info.Timestamp); var time = new FormattedText( - info.Time, + timeStr, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, @@ -130,8 +131,9 @@ namespace SourceGit.Views _editor.Foreground); x += author.Width + 8; + var timeStr = Models.DateTimeFormat.Format(info.Timestamp); var time = new FormattedText( - info.Time, + timeStr, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, diff --git a/src/Views/CommitTimeTextBlock.cs b/src/Views/CommitTimeTextBlock.cs index 26f742e0..0e32e3aa 100644 --- a/src/Views/CommitTimeTextBlock.cs +++ b/src/Views/CommitTimeTextBlock.cs @@ -132,10 +132,7 @@ namespace SourceGit.Views var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime; if (ShowAsDateTime) - { - var format = Models.DateTimeFormat.Active.DateTime; - return DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime().ToString(format); - } + return Models.DateTimeFormat.Format(timestamp); var now = DateTime.Now; var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime(); diff --git a/src/Views/DateTimePresenter.cs b/src/Views/DateTimePresenter.cs index 39fa65b9..00478404 100644 --- a/src/Views/DateTimePresenter.cs +++ b/src/Views/DateTimePresenter.cs @@ -72,9 +72,7 @@ namespace SourceGit.Views change.Property == DateTimeFormatProperty || change.Property == TimestampProperty) { - var active = Models.DateTimeFormat.Active; - var format = ShowDateOnly ? active.DateOnly : active.DateTime; - var text = DateTime.UnixEpoch.AddSeconds(Timestamp).ToLocalTime().ToString(format); + var text = Models.DateTimeFormat.Format(Timestamp, ShowDateOnly); SetCurrentValue(TextProperty, text); } } diff --git a/src/Views/Preferences.axaml b/src/Views/Preferences.axaml index b839c057..91cfb02c 100644 --- a/src/Views/Preferences.axaml +++ b/src/Views/Preferences.axaml @@ -74,7 +74,7 @@ - +