-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTools.cs
More file actions
182 lines (159 loc) · 4.67 KB
/
Copy pathTools.cs
File metadata and controls
182 lines (159 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Text.RegularExpressions;
using Avalonia.Data.Converters;
using Avalonia.Data;
using System.Globalization;
namespace SaemDesk;
/// <summary>
/// WinUI 3 + Native AOT 호환 유틸리티 클래스
/// </summary>
public partial class Tool
{
/// <summary>
/// 파일 크기 포맷
/// </summary>
public static string FormatSize(long bytes)
{
string[] suffixes = { "Bytes", "KB", "MB", "GB", "TB", "PB" };
int counter = 0;
decimal number = bytes;
while (number >= 1024 && counter < suffixes.Length - 1)
{
number /= 1024;
counter++;
}
return $"({number:n1}{suffixes[counter]})";
}
/// <summary>
/// NEIS 바이트 카운트 (영문 1byte, 한글 3byte)
/// </summary>
[GeneratedRegex("[a-zA-Z]")]
private static partial Regex Abc();
[GeneratedRegex("[가-\ud7af\u3130-\u318f]")]
private static partial Regex Ganada();
public static int CountNeisByte(string str)
{
int byteCount = 0;
foreach (char ch in str)
{
if (Abc().IsMatch(ch.ToString()))
{
byteCount += 1;
}
else if (Ganada().IsMatch(ch.ToString()))
{
byteCount += 3;
}
else
{
byteCount += 1;
}
}
return byteCount;
}
}
#region Value Converters (WinUI 3용 - string language 사용)
/// <summary>
/// 날짜를 문자열로 변환
/// </summary>
public partial class DateStringConverter : IValueConverter
{
public object Convert(object? sourceValue, Type targetType, object? parameter, System.Globalization.CultureInfo culture)
{
if (sourceValue is null) { return string.Empty; }
DateTime dt;
if (sourceValue is DateTime time1)
{
dt = time1;
}
else if (sourceValue is DateTimeOffset offset)
{
dt = offset.ToLocalTime().DateTime;
}
else
{
return string.Empty;
}
int day = (dt.Date - DateTime.Today).Days;
string date;
string weekday = dt.ToString("ddd");
switch (day)
{
case -2:
date = "그제";
break;
case -1:
date = "어제";
break;
case 0:
date = "오늘";
break;
case 1:
date = "내일";
break;
case 2:
date = "모레";
break;
default:
date = dt.ToString("M. d");
break;
}
string time = dt.Hour + dt.Minute == 0 ? string.Empty : dt.ToString("HH:mm");
return $"{date}({weekday}) {time}";
}
public object? ConvertBack(object? sourceValue, Type targetType, object? parameter, System.Globalization.CultureInfo culture)
{
string date = (string)(sourceValue ?? string.Empty);
if (date.Contains("오늘"))
{
date = date.Replace("오늘 ", "");
// 변수를 미리 선언
if (!int.TryParse(date.AsSpan(0, 2), null, out int hour))
{
return null;
}
if (!int.TryParse(date.AsSpan(3, 2), null, out int minute))
{
return null;
}
return DateTime.Today.AddHours(hour).AddMinutes(minute);
}
else
{
// 변수를 미리 선언
if (!int.TryParse(date.AsSpan(0, 2), null, out int month))
{
return null;
}
if (!int.TryParse(date.AsSpan(3, 2), null, out int day))
{
return null;
}
return new DateTime(DateTime.Today.Year, month, day);
}
}
}
/// <summary>
/// 날짜를 짧은 문자열로 변환
/// </summary>
public partial class DateToShortString : IValueConverter
{
public object Convert(object? sourceValue, Type targetType, object? parameter, System.Globalization.CultureInfo culture)
{
if (sourceValue is not null and DateTime)
{
DateTime dt = (DateTime)sourceValue;
return dt.ToString("M.d. ddd HH:mm");
}
else
{
return string.Empty;
}
}
public object ConvertBack(object? sourceValue, Type targetType, object? parameter, System.Globalization.CultureInfo culture)
{
return sourceValue is string @string ? DateTime.Parse(@string) : new DateTime();
}
}
// DateTimeToDateTimeOffsetConverter는 CommonConverters.cs에서 정의
#endregion