-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.DateUtils.pas
More file actions
175 lines (144 loc) · 4.03 KB
/
Copy pathMaxLogic.DateUtils.pas
File metadata and controls
175 lines (144 loc) · 4.03 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
Unit MaxLogic.DateUtils;
{
Version: 1.1
}
Interface
uses
SYSutils, classes, MaxLogic.sTRuTILS;
Function etime(aDate: TDateTime): String; Overload;
Function etime: String; Overload;
Function StrETimeToDateTime(Const etime: String): TDateTime;
Function SecToStrTime(sec: double): String;
Function msToPrettyStr(Const ms: int64): String;
Function eTimeWithTimeZone(aDate: TDateTime): String;
// a bit more advanced parsing
// returns 0 if the date could not be properly parsed
function ParseDateTime(const sDate: string): TDateTime;
Implementation
Uses
DateUtils, sTRuTILS, autoFree;
Function etime(aDate: TDateTime): String;
Const
cFormat = 'yyyy"-"mm"-"dd" "hh":"nn":"ss';
Begin
Result := '';
Try
If aDate <> System.MaxInt Then
Result := FormatDateTime(cFormat, aDate);
Except
Result := '';
End;
If Result = '' Then
Result := FormatDateTime(cFormat, 0);
end;
Function etime: String;
Begin
Result := etime(Now);
End;
Function StrETimeToDateTime(Const etime: String): TDateTime;
Var
yyyy, dd, mm, h, min, sec, ms: word;
Begin
Result := 0;
If Trim(etime) = '' Then
Exit;
Try
yyyy := StrToIntDef(copy(etime, 1, 4), 0);
mm := StrToIntDef(copy(etime, 6, 2), 99);
dd := StrToIntDef(copy(etime, 9, 2), 99);
h := StrToIntDef(copy(etime, 12, 2), 99);
min := StrToIntDef(copy(etime, 15, 2), 99);
sec := StrToIntDef(copy(etime, 18, 2), 99);
ms := StrToIntDef(copy(etime, 21, 3), 0);
If (yyyy > 0) And
(mm >= 1) And (mm <= 12) And
(dd >= 1) And (dd <= 31) And (dd <= DateUtils.DaysInAMonth(yyyy, mm)) And
(h <= 24) And
(min <= 59) And
(sec <= 59) And
(ms < 1000) Then
Result := DateUtils.EncodeDateTime(yyyy, mm, dd, h, min, sec, ms);
Except
Result := 0;
End;
End;
Function SecToStrTime(sec: double): String;
Var
h, m, s, ms: Integer;
Begin
h := Trunc(sec / 60 / 60);
sec := sec - h * 60 * 60;
m := Trunc(sec / 60);
sec := sec - m * 60;
s := Trunc(sec);
ms := Trunc(frac(sec) * 100);
Result := putBefore(IntToStr(m), '0', 2) +
':' + putBefore(IntToStr(s), '0', 2) +
'.' + putBefore(IntToStr(ms), '0', 3);
If h > 0 Then
Result := putBefore(IntToStr(h), '0', 2) + 'h ' + Result;
End;
Function msToPrettyStr(Const ms: int64): String;
Var
d, h, m, s: Integer;
Begin
If ms <= 1000 Then
Result := Format('%d ms', [ms])
// smaller then 1 minute
Else If ms <= 60000 Then
Result := Format('%5.3f s', [ms / 1000])
// smaller then 1h
Else If ms <= 60 * 60000 Then
Result := Format('%5.2f min', [ms / 60000])
Else If ms <= 60 * 60 * 60000 Then
Result := Format('%5.2f h', [ms / 60 * 60000])
End;
Function eTimeWithTimeZone(aDate: TDateTime): String;
Begin
Result := etime(aDate) + ' ' + DateUtils.TTimeZone.Local.
GetAbbreviation(aDate);
End;
function ParseDateTime(const sDate: string): TDateTime;
var
aFormatSettings: TFormatSettings;
sf: string;
sep: char;
formats: TArray<string>;
separators: TArray<char>;
HasTimePart: boolean;
function TryConvert(const sDate: string; var dt: TDateTime; const fs: TFormatSettings): boolean;
begin
if HasTimePart then
Result := TryStrToDateTime(sDate, dt, fs)
else
Result := TryStrToDate(sDate, dt, fs);
end;
begin
Result := 0;
if sDate = '' then
Exit;
HasTimePart := (pos(':', sDate) > 0)
or (pos('T', AnsiUppercase(sDate)) > 0);
aFormatSettings := TFormatSettings.Create;
if TryConvert(sDate, Result, aFormatSettings) then
Exit;
formats := ['de-de', 'pl-pl', 'en-uk', 'en-us'];
for sf in formats do
begin
aFormatSettings := TFormatSettings.Create(sf);
if TryConvert(sDate, Result, aFormatSettings) then
Exit;
end;
formats := ['dd/mm/yyyy', 'yyyy/mm/dd', 'd/m/yyyy', 'd/m/yy'];
separators := ['-', '.'];
aFormatSettings := TFormatSettings.Create();
for sep in separators do
for sf in formats do
begin
aFormatSettings.ShortDateFormat := sf;
aFormatSettings.DateSeparator := sep;
if TryConvert(sDate, Result, aFormatSettings) then
Exit;
end;
end;
end.