refactor: rewrite the way to format datetimes

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2026-03-12 18:02:04 +08:00
parent 94efd29509
commit 7181472c6f
7 changed files with 47 additions and 62 deletions

View File

@@ -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

View File

@@ -5,38 +5,20 @@ namespace SourceGit.Models
{
public class DateTimeFormat
{
public string DateOnly
public static readonly List<DateTimeFormat> Supported = new List<DateTimeFormat>
{
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<DateTimeFormat> Supported = new List<DateTimeFormat>
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);
}
}
}

View File

@@ -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()
{

View File

@@ -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,

View File

@@ -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();

View File

@@ -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);
}
}

View File

@@ -74,7 +74,7 @@
<DataTemplate x:DataType="{x:Type m:DateTimeFormat}">
<Grid ColumnDefinitions="*,8,*">
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding Example}"/>
<TextBlock Grid.Column="2" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DateOnly}" Foreground="{DynamicResource Brush.FG2}"/>
<TextBlock Grid.Column="2" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DateFormat}" Foreground="{DynamicResource Brush.FG2}"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>