Skip to content

Commit 3363cb4

Browse files
committed
[commons] datetime helpers
1 parent 5b4b5c1 commit 3363cb4

File tree

1 file changed

+165
-4
lines changed

1 file changed

+165
-4
lines changed

Quick.Commons.pas

+165-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{ ***************************************************************************
22
3-
Copyright (c) 2016-2021 Kike Pérez
3+
Copyright (c) 2016-2021 Kike Pérez
44
55
Unit : Quick.Commons
66
Description : Common functions
7-
Author : Kike Pérez
7+
Author : Kike Pérez
88
Version : 2.0
99
Created : 14/07/2017
10-
Modified : 22/08/2021
10+
Modified : 29/08/2021
1111
1212
This file is part of QuickLib: https://github.com/exilon/QuickLib
1313
@@ -227,6 +227,37 @@ TPairEnumerator = class
227227
procedure Clear;
228228
end;
229229

230+
{$IFDEF DELPHIXE7_UP}
231+
TDateTimeHelper = record helper for TDateTime
232+
public
233+
function ToSQLString : string;
234+
procedure FromNow;
235+
procedure FromUTC(const aUTCTime : TDateTime);
236+
function IncDay(const aValue : Cardinal = 1) : TDateTime;
237+
function DecDay(const aValue : Cardinal = 1) : TDateTime;
238+
function IncMonth(const aValue : Cardinal = 1) : TDateTime;
239+
function DecMonth(const aValue : Cardinal = 1) : TDateTime;
240+
function IncYear(const aValue : Cardinal = 1) : TDateTime;
241+
function DecYear(const aValue : Cardinal = 1) : TDateTime;
242+
function IsEqualTo(const aDateTime : TDateTime) : Boolean;
243+
function IsAfter(const aDateTime : TDateTime) : Boolean;
244+
function IsBefore(const aDateTime : TDateTime) : Boolean;
245+
function IsSameDay(const aDateTime : TDateTime) : Boolean;
246+
function IsSameTime(const aTime : TTime) : Boolean;
247+
function DayOfTheWeek : Word;
248+
function ToJsonFormat : string;
249+
function ToGMTFormat: string;
250+
function ToTimeStamp : TTimeStamp;
251+
function ToUTC : TDateTime;
252+
function ToMilliseconds : Int64;
253+
function ToString : string;
254+
function Date : TDate;
255+
function Time : TTime;
256+
function IsAM : Boolean;
257+
function IsPM : Boolean;
258+
end;
259+
{$ENDIF}
260+
230261
EEnvironmentPath = class(Exception);
231262
EShellError = class(Exception);
232263

@@ -2034,7 +2065,7 @@ function RemoveLastChar(const aText : string) : string;
20342065

20352066
function DateTimeToSQL(aDateTime : TDateTime) : string;
20362067
begin
2037-
Result := FormatDateTime('YYYYMMDD hh:mm:ss',aDateTime);
2068+
Result := FormatDateTime('YYYY-MM-DD hh:mm:ss',aDateTime);
20382069
end;
20392070

20402071
function IsInteger(const aValue : string) : Boolean;
@@ -2147,6 +2178,135 @@ function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : TObject) : TObj
21472178
{$ENDIF}
21482179
{$ENDIF}
21492180

2181+
{ TDateTimeHelper }
2182+
2183+
{$IFDEF DELPHIXE7_UP}
2184+
function TDateTimeHelper.ToSQLString : string;
2185+
begin
2186+
Result := DateTimeToSQL(Self);
2187+
end;
2188+
2189+
procedure TDateTimeHelper.FromNow;
2190+
begin
2191+
Self := Now;
2192+
end;
2193+
2194+
procedure TDateTimeHelper.FromUTC(const aUTCTime: TDateTime);
2195+
begin
2196+
Self := UTCToLocalTime(aUTCTime);
2197+
end;
2198+
2199+
function TDateTimeHelper.IncDay(const aValue : Cardinal = 1) : TDateTime;
2200+
begin
2201+
Result := System.DateUtils.IncDay(Self,aValue);
2202+
end;
2203+
2204+
function TDateTimeHelper.DecDay(const aValue : Cardinal = 1) : TDateTime;
2205+
begin
2206+
Result := System.DateUtils.IncDay(Self,aValue * - 1);
2207+
end;
2208+
2209+
function TDateTimeHelper.IncMonth(const aValue : Cardinal = 1) : TDateTime;
2210+
begin
2211+
Result := System.DateUtils.IncDay(Self,aValue);
2212+
end;
2213+
2214+
function TDateTimeHelper.DecMonth(const aValue : Cardinal = 1) : TDateTime;
2215+
begin
2216+
Result := System.DateUtils.IncDay(Self,aValue * - 1);
2217+
end;
2218+
2219+
function TDateTimeHelper.IncYear(const aValue : Cardinal = 1) : TDateTime;
2220+
begin
2221+
Result := System.DateUtils.IncDay(Self,aValue);
2222+
end;
2223+
2224+
function TDateTimeHelper.DecYear(const aValue : Cardinal = 1) : TDateTime;
2225+
begin
2226+
Result := System.DateUtils.IncDay(Self,aValue * - 1);
2227+
end;
2228+
2229+
function TDateTimeHelper.IsEqualTo(const aDateTime : TDateTime) : Boolean;
2230+
begin
2231+
Result := Self = aDateTime;
2232+
end;
2233+
2234+
function TDateTimeHelper.IsAfter(const aDateTime : TDateTime) : Boolean;
2235+
begin
2236+
Result := Self > aDateTime;
2237+
end;
2238+
2239+
function TDateTimeHelper.IsBefore(const aDateTime : TDateTime) : Boolean;
2240+
begin
2241+
Result := Self < aDateTime;
2242+
end;
2243+
2244+
function TDateTimeHelper.IsSameDay(const aDateTime : TDateTime) : Boolean;
2245+
begin
2246+
Result := System.DateUtils.SameDate(Self,aDateTime);
2247+
end;
2248+
2249+
function TDateTimeHelper.IsSameTime(const aTime : TTime) : Boolean;
2250+
begin
2251+
Result := System.DateUtils.SameTime(Self,aTime);
2252+
end;
2253+
2254+
function TDateTimeHelper.DayOfTheWeek : Word;
2255+
begin
2256+
Result := System.DateUtils.NthDayOfWeek(Self);
2257+
end;
2258+
2259+
function TDateTimeHelper.ToJsonFormat : string;
2260+
begin
2261+
Result := DateTimeToJsonDate(Self);
2262+
end;
2263+
2264+
function TDateTimeHelper.ToGMTFormat : string;
2265+
begin
2266+
Result := DateTimeToGMT(Self);
2267+
end;
2268+
2269+
function TDateTimeHelper.ToTimeStamp : TTimeStamp;
2270+
begin
2271+
Result := DateTimeToTimeStamp(Self);
2272+
end;
2273+
2274+
function TDateTimeHelper.ToUTC : TDateTime;
2275+
begin
2276+
Result := LocalTimeToUTC(Self);
2277+
end;
2278+
2279+
function TDateTimeHelper.ToMilliseconds : Int64;
2280+
begin
2281+
Result := System.DateUtils.DateTimeToMilliseconds(Self);
2282+
end;
2283+
2284+
function TDateTimeHelper.ToString : string;
2285+
begin
2286+
Result := DateTimeToStr(Self);
2287+
end;
2288+
2289+
function TDateTimeHelper.Date : TDate;
2290+
begin
2291+
Result := System.DateUtils.DateOf(Self);
2292+
end;
2293+
2294+
function TDateTimeHelper.Time : TTime;
2295+
begin
2296+
Result := System.DateUtils.TimeOf(Self);
2297+
end;
2298+
2299+
function TDateTimeHelper.IsAM : Boolean;
2300+
begin
2301+
Result := System.DateUtils.IsAM(Self);
2302+
end;
2303+
2304+
function TDateTimeHelper.IsPM : Boolean;
2305+
begin
2306+
Result := System.DateUtils.IsPM(Self);
2307+
end;
2308+
{$ENDIF}
2309+
21502310
{$IFNDEF NEXTGEN}
21512311
initialization
21522312
try
@@ -2165,6 +2325,7 @@ initialization
21652325
end;
21662326
{$ENDIF}
21672327

2328+
21682329
end.
21692330

21702331

0 commit comments

Comments
 (0)