@@ -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}
0 commit comments