Menu

[r4300]: / branches / parsnip / Src / Main / CS.Utils.Dates.pas  Maximize  Restore  History

Download this file

283 lines (240 with data), 8.3 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Date and time utilities.
}
unit CS.Utils.Dates;
interface
uses
SysUtils,
DateUtils {in interface for inlining},
Windows;
type
TUTCDateTime = record
strict private
var
fValue: TDateTime;
class function RoundDTToNearestSec(const DT: TDateTime): TDateTime; static;
class function TryConvertISO8601String(const Str: string;
out Y, M, D, H, N, S, MS: Word): Boolean; static;
public
constructor Create(const UTC: TDateTime; const RoundToSec: Boolean = False);
overload;
constructor Create(const Year, Month, Day: Word; const Hour: Word = 0;
const Minute: Word = 0; const Second: Word = 0; const MS: Word = 0);
overload;
class function CreateFromLocalDateTime(const DT: TDateTime;
const RoundToSec: Boolean = False): TUTCDateTime; static;
class function CreateNull: TUTCDateTime; static; inline;
class function CreateFromISO8601String(const Str: string): TUTCDateTime;
static;
class function Now(const RoundToSec: Boolean = False): TUTCDateTime; static;
class function IsValidISO8601String(const Str: string): Boolean; static;
function ToDateTime: TDateTime; inline;
function ToISO8601String(const RoundToSec: Boolean = False): string;
function ToString(const AFormat: string): string; overload;
function ToString(const AFormat: string;
const AFormatSettings: TFormatSettings): string; overload;
function IsNull: Boolean;
function RoundToNearestSecond: TUTCDateTime;
class operator Equal(const Left, Right: TUTCDateTime): Boolean;
class operator NotEqual(const Left, Right: TUTCDateTime): Boolean;
class operator GreaterThan(const Left, Right: TUTCDateTime): Boolean;
class operator GreaterThanOrEqual(const Left, Right: TUTCDateTime): Boolean;
class operator LessThan(const Left, Right: TUTCDateTime): Boolean;
class operator LessThanOrEqual(const Left, Right: TUTCDateTime): Boolean;
end;
EUTCDateTime = class(Exception);
// TODO: re-implement some of these routines in terms of TUTCDataTime.
/// <summary>Creates a date stamp for current date in RFC1123 format.</summary>
/// <returns>string. Required date and time as date stamp in UTC/GMT.</returns>
function RFC1123DateStamp: string; inline;
/// <summary>Returns the current date and time in GMT/UTC.</summary>
function NowGMT: TDateTime;
/// <summary>Converts a date-time value in SQL format into a TDateTime.
/// </summary>
/// <param name="SQLDate">string [in] SQL format date-time value to be
/// converted.</param>
/// <returns>TDateTime. Converted value.</returns>
/// <remarks>SQLDate must be in YYYY-MM-DD hh:mm:ss format.</remarks>
function ParseSQLDateTime(const SQLDate: string): TDateTime;
implementation
uses
Types,
UUtils;
function NowGMT: TDateTime;
var
ST: TSystemTime;
begin
// This Windows API function gets system time in UTC/GMT
// see http://msdn.microsoft.com/en-us/library/ms724390
GetSystemTime(ST);
Result := SystemTimeToDateTime(ST);
end;
function RFC1123DateStamp: string;
const
// Pattern to create RFC1123 date formats
cRFC1123Pattern = 'ddd, dd mmm yyyy HH'':''nn'':''ss ''GMT''';
begin
Result := FormatDateTime(cRFC1123Pattern, NowGMT);
end;
function ParseSQLDateTime(const SQLDate: string): TDateTime;
begin
Result := SysUtils.EncodeDate(
SysUtils.StrToInt(Copy(SQLDate, 1, 4)),
SysUtils.StrToInt(Copy(SQLDate, 6, 2)),
SysUtils.StrToInt(Copy(SQLDate, 9, 2))
)
+
SysUtils.EncodeTime(
SysUtils.StrToInt(Copy(SQLDate, 12, 2)),
SysUtils.StrToInt(Copy(SQLDate, 15, 2)),
SysUtils.StrToInt(Copy(SQLDate, 18, 2)),
0
);
end;
{ TUTCDateTime }
constructor TUTCDateTime.Create(const UTC: TDateTime;
const RoundToSec: Boolean);
begin
fValue := UTC;
if not RoundToSec then
fValue := UTC
else
fValue := RoundDTToNearestSec(UTC);
end;
constructor TUTCDateTime.Create(const Year, Month, Day, Hour, Minute, Second,
MS: Word);
begin
// Will raise EConvertError if any parameter is invalid
fValue := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, MS);
end;
class function TUTCDateTime.CreateFromISO8601String(
const Str: string): TUTCDateTime;
var
Y, M, D, H, N, S, MS: Word;
begin
if not TryConvertISO8601String(Str, Y, M, D, H, N, S, MS) then
raise EConvertError.CreateFmt('"%s" is not a valid date', [Str]);
Result := TUTCDateTime.Create(Y, M, D, H, N, S, MS);
end;
class function TUTCDateTime.CreateFromLocalDateTime(const DT: TDateTime;
const RoundToSec: Boolean): TUTCDateTime;
begin
Result := TUTCDateTime.Create(
TTimeZone.Local.ToUniversalTime(DT), RoundToSec
);
end;
class function TUTCDateTime.CreateNull: TUTCDateTime;
begin
Result := TUTCDateTime.Create(0.0);
end;
class operator TUTCDateTime.Equal(const Left, Right: TUTCDateTime): Boolean;
begin
Result := SameDateTime(Left.fValue, Right.fValue);
end;
class operator TUTCDateTime.GreaterThan(const Left,
Right: TUTCDateTime): Boolean;
begin
Result := CompareDateTime(Left.fValue, Right.fValue) = GreaterThanValue;
end;
class operator TUTCDateTime.GreaterThanOrEqual(const Left,
Right: TUTCDateTime): Boolean;
begin
Result := CompareDateTime(Left.fValue, Right.fValue) <> LessThanValue;
end;
function TUTCDateTime.IsNull: Boolean;
begin
Result := SameDateTime(fValue, 0.0);
end;
class function TUTCDateTime.IsValidISO8601String(const Str: string): Boolean;
var
Y, M, D, H, N, S, MS: Word;
begin
Result := TryConvertISO8601String(Str, Y, M, D, H, N, S, MS);
end;
class operator TUTCDateTime.LessThan(const Left, Right: TUTCDateTime): Boolean;
begin
Result := CompareDateTime(Left.fValue, Right.fValue) = LessThanValue;
end;
class operator TUTCDateTime.LessThanOrEqual(const Left,
Right: TUTCDateTime): Boolean;
begin
Result := CompareDateTime(Left.fValue, Right.fValue) <> GreaterThanValue;
end;
class operator TUTCDateTime.NotEqual(const Left, Right: TUTCDateTime): Boolean;
begin
Result := not SameDateTime(Left.fValue, Right.fValue);
end;
class function TUTCDateTime.Now(const RoundToSec: Boolean): TUTCDateTime;
begin
Result := TUTCDateTime.CreateFromLocalDateTime(SysUtils.Now, RoundToSec);
end;
class function TUTCDateTime.RoundDTToNearestSec(const DT: TDateTime): TDateTime;
begin
if MilliSecondOf(DT) >= 500 then
Result := IncSecond(DT)
else
Result := DT;
Result := RecodeMilliSecond(Result, 0);
end;
function TUTCDateTime.RoundToNearestSecond: TUTCDateTime;
begin
Result := TUTCDateTime.Create(RoundDTToNearestSec(fValue));
end;
function TUTCDateTime.ToDateTime: TDateTime;
begin
Result := fValue;
end;
function TUTCDateTime.ToISO8601String(const RoundToSec: Boolean): string;
begin
if RoundToSec then
Result := FormatDateTime(
'yyyy"-"mm"-"dd"T"hh":"nn":"ss"Z"', RoundDTToNearestSec(fValue)
)
else
Result := FormatDateTime(
'yyyy"-"mm"-"dd"T"hh":"nn":"ss"."zzz"Z"', fValue
);
end;
function TUTCDateTime.ToString(const AFormat: string): string;
begin
Result := FormatDateTime(AFormat, fValue);
end;
function TUTCDateTime.ToString(const AFormat: string;
const AFormatSettings: TFormatSettings): string;
begin
Result := FormatDateTime(AFormat, fValue, AFormatSettings);
end;
class function TUTCDateTime.TryConvertISO8601String(const Str: string; out Y, M,
D, H, N, S, MS: Word): Boolean;
begin
// We only support ISO 8601 string in form YYYY-MM-DD"T"HH:MM:SS"Z" and
// YYYY-MM-DD"T"HH:MM:SS.XXX"Z" or YYYY-MM-DD"T"HH:MM:SS,XXX"Z"
// 1234567890 1 234567890123 4
if Length(Str) < 20 then
Exit(False);
if not TryStrToWord(Copy(Str, 1, 4), Y) or not
TryStrToWord(Copy(Str, 6, 2), M) or not
TryStrToWord(Copy(Str, 9, 2), D) or not
TryStrToWord(Copy(Str, 12, 2), H) or not
TryStrToWord(Copy(Str, 15, 2), N) or not
TryStrToWord(Copy(Str, 18, 2), S) then
Exit(False);
if CharInSet(Str[20], ['.', ',']) then
begin
if (Length(Str) < 24) or not TryStrToWord(Copy(Str, 21, 3), MS) then
Exit(False);
end
else
MS := 0;
Result := True;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.