mirror of
https://fastgit.cc/github.com/sourcegit-scm/sourcegit
synced 2026-04-30 13:51:53 +08:00
refactor: rewrite the way to format datetimes
Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user