Skip to content

Commit 7181472

Browse files
committed
refactor: rewrite the way to format datetimes
Signed-off-by: leo <longshuang@msn.cn>
1 parent 94efd29 commit 7181472

File tree

7 files changed

+45
-60
lines changed

7 files changed

+45
-60
lines changed

src/Models/Blame.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32

43
namespace SourceGit.Models
54
{
@@ -10,7 +9,6 @@ public class BlameLineInfo
109
public string File { get; set; } = string.Empty;
1110
public string Author { get; set; } = string.Empty;
1211
public ulong Timestamp { get; set; } = 0;
13-
public string Time => DateTime.UnixEpoch.AddSeconds(Timestamp).ToLocalTime().ToString(DateTimeFormat.Active.DateOnly);
1412
}
1513

1614
public class BlameData

src/Models/DateTimeFormat.cs

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,62 @@ namespace SourceGit.Models
55
{
66
public class DateTimeFormat
77
{
8-
public string DateOnly
8+
public static readonly List<DateTimeFormat> Supported = new List<DateTimeFormat>
9+
{
10+
new("yyyy/MM/dd"),
11+
new("yyyy.MM.dd"),
12+
new("yyyy-MM-dd"),
13+
new("MM/dd/yyyy"),
14+
new("MM.dd.yyyy"),
15+
new("MM-dd-yyyy"),
16+
new("dd/MM/yyyy"),
17+
new("dd.MM.yyyy"),
18+
new("dd-MM-yyyy"),
19+
new("MMM d yyyy"),
20+
new("d MMM yyyy"),
21+
};
22+
23+
public static int ActiveIndex
924
{
1025
get;
1126
set;
12-
}
27+
} = 0;
1328

14-
public string DateTime
29+
public static bool Use24Hours
1530
{
16-
get
17-
{
18-
if (_use24Hours != Use24Hours || string.IsNullOrEmpty(_dateTime))
19-
{
20-
_use24Hours = Use24Hours;
21-
_dateTime = _use24Hours ? $"{DateOnly} HH:mm:ss" : $"{DateOnly} hh:mm:ss tt";
22-
}
31+
get;
32+
set;
33+
} = true;
2334

24-
return _dateTime;
25-
}
35+
public string DateFormat
36+
{
37+
get;
2638
}
2739

2840
public string Example
2941
{
30-
get
31-
{
32-
return new DateTime(2025, 1, 31, 8, 0, 0, DateTimeKind.Local).ToString(DateOnly);
33-
}
42+
get => DateTime.Now.ToString(DateFormat);
3443
}
3544

3645
public DateTimeFormat(string date)
3746
{
38-
DateOnly = date;
47+
DateFormat = date;
3948
}
4049

41-
public static int ActiveIndex
42-
{
43-
get;
44-
set;
45-
} = 0;
46-
47-
public static bool Use24Hours
48-
{
49-
get;
50-
set;
51-
} = true;
52-
53-
public static DateTimeFormat Active
50+
public static string Format(ulong timestamp, bool dateOnly = false)
5451
{
55-
get => Supported[ActiveIndex];
52+
var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
53+
return Format(localTime, dateOnly);
5654
}
5755

58-
public static readonly List<DateTimeFormat> Supported = new List<DateTimeFormat>
56+
public static string Format(DateTime localTime, bool dateOnly = false)
5957
{
60-
new DateTimeFormat("yyyy/MM/dd"),
61-
new DateTimeFormat("yyyy.MM.dd"),
62-
new DateTimeFormat("yyyy-MM-dd"),
63-
new DateTimeFormat("MM/dd/yyyy"),
64-
new DateTimeFormat("MM.dd.yyyy"),
65-
new DateTimeFormat("MM-dd-yyyy"),
66-
new DateTimeFormat("dd/MM/yyyy"),
67-
new DateTimeFormat("dd.MM.yyyy"),
68-
new DateTimeFormat("dd-MM-yyyy"),
69-
new DateTimeFormat("MMM d yyyy"),
70-
new DateTimeFormat("d MMM yyyy"),
71-
};
58+
var actived = Supported[ActiveIndex];
59+
if (dateOnly)
60+
return localTime.ToString(actived.DateFormat);
7261

73-
private bool _use24Hours = true;
74-
private string _dateTime = null;
62+
var format = Use24Hours ? $"{actived.DateFormat} HH:mm:ss" : $"{actived.DateFormat} hh:mm:ss tt";
63+
return localTime.ToString(format);
64+
}
7565
}
7666
}

src/Models/SelfUpdate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class Version
2828
public bool IsNewVersion => CurrentVersion.CompareTo(new System.Version(TagName.Substring(1))) < 0;
2929

3030
[JsonIgnore]
31-
public string ReleaseDateStr => PublishedAt.ToString(DateTimeFormat.Active.DateOnly);
31+
public string ReleaseDateStr => DateTimeFormat.Format(PublishedAt, true);
3232

3333
public Version()
3434
{

src/Views/Blame.axaml.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ public override void Render(DrawingContext context)
7979
var authorTop = y - author.Height * 0.5;
8080
context.DrawText(author, new Point(x, authorTop));
8181

82+
var timeStr = Models.DateTimeFormat.Format(info.Timestamp);
8283
var time = new FormattedText(
83-
info.Time,
84+
timeStr,
8485
CultureInfo.CurrentCulture,
8586
FlowDirection.LeftToRight,
8687
typeface,
@@ -130,8 +131,9 @@ protected override Size MeasureOverride(Size availableSize)
130131
_editor.Foreground);
131132
x += author.Width + 8;
132133

134+
var timeStr = Models.DateTimeFormat.Format(info.Timestamp);
133135
var time = new FormattedText(
134-
info.Time,
136+
timeStr,
135137
CultureInfo.CurrentCulture,
136138
FlowDirection.LeftToRight,
137139
typeface,

src/Views/CommitTimeTextBlock.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ private string GetDisplayText()
132132

133133
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
134134
if (ShowAsDateTime)
135-
{
136-
var format = Models.DateTimeFormat.Active.DateTime;
137-
return DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime().ToString(format);
138-
}
135+
return Models.DateTimeFormat.Format(timestamp);
139136

140137
var now = DateTime.Now;
141138
var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();

src/Views/DateTimePresenter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
7272
change.Property == DateTimeFormatProperty ||
7373
change.Property == TimestampProperty)
7474
{
75-
var active = Models.DateTimeFormat.Active;
76-
var format = ShowDateOnly ? active.DateOnly : active.DateTime;
77-
var text = DateTime.UnixEpoch.AddSeconds(Timestamp).ToLocalTime().ToString(format);
75+
var text = Models.DateTimeFormat.Format(Timestamp, ShowDateOnly);
7876
SetCurrentValue(TextProperty, text);
7977
}
8078
}

src/Views/Preferences.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<DataTemplate x:DataType="{x:Type m:DateTimeFormat}">
7575
<Grid ColumnDefinitions="*,8,*">
7676
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding Example}"/>
77-
<TextBlock Grid.Column="2" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DateOnly}" Foreground="{DynamicResource Brush.FG2}"/>
77+
<TextBlock Grid.Column="2" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DateFormat}" Foreground="{DynamicResource Brush.FG2}"/>
7878
</Grid>
7979
</DataTemplate>
8080
</ComboBox.ItemTemplate>

0 commit comments

Comments
 (0)