This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvilWorks.System.DateUtils.pas
119 lines (95 loc) · 2.76 KB
/
EvilWorks.System.DateUtils.pas
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
unit EvilWorks.System.DateUtils;
interface
uses
WinApi.Windows,
System.SysUtils,
System.DateUtils;
function DateNowUTC: TDateTime;
function DateTimeFromFileTime(aFileTime: TFileTime): TDateTime;
function DateHTTPTimestamp: string;
function DateTimeFromTwitterTimestamp(const aTimeStamp: string; var aDateTime: TDateTime): boolean;
var
EnUsFormatSettings: TFormatSettings;
implementation
uses
EvilWorks.System.StrUtils;
{ Now() equivalent returns time in UTC/GMT timezone. }
function DateNowUTC: TDateTime;
var
sysTime: TSystemTime;
begin
GetSystemTime(sysTime);
Result := SystemTimeToDateTime(sysTime);
end;
{ Converts TFileTime to TDateTime. }
function DateTimeFromFileTime(aFileTime: TFileTime): TDateTime;
var
temp : TDateTime;
localFileTime : TFileTime;
localSystemTime: TSystemTime;
begin
FileTimeToLocalFileTime(aFileTime, localFileTime);
FileTimeToSystemTime(localFileTime, localSystemTime);
TryEncodeDate(localSystemTime.wYear, localSystemTime.wMonth, localSystemTime.wDay, temp);
Result := temp;
TryEncodeTime(localSystemTime.wHour, localSystemTime.wMinute, localSystemTime.wSecond, localSystemTime.wMilliseconds, temp);
Result := Result + temp;
end;
{ Creates a HTTP timestamp. }
function DateHTTPTimestamp: string;
begin
Result := FormatDateTime('ddd, dd mmm yyyy hh:nn:ss', DateNowUTC, EnUsFormatSettings) + ' GMT';
end;
{ Converts Twitter's timestamp string to TDateTime. Don't look at the code when you look at the code. }
function DateTimeFromTwitterTimestamp(const aTimeStamp: string; var aDateTime: TDateTime): boolean;
var
tokens: TTokens;
eYear, eMonth, eDay, eHour, eMinute, eSecond: word;
eOffset, i: integer;
begin
Result := False;
if (aTimeStamp = '') then
Exit;
tokens := TextTokenize(aTimeStamp);
if (tokens.Count <> 6) then
Exit;
eMonth := $FFFF;
for i := 1 to high(EnUsFormatSettings.ShortMonthNames) do
begin
if (SameText(tokens[1], EnUsFormatSettings.ShortMonthNames[i])) then
begin
eMonth := i;
Break;
end;
end;
if (eMonth = $FFFF) then
Exit;
eDay := TextToInt(tokens[2], $FFFF);
if (eday = $FFFF) then
Exit;
eOffset := TextToInt(tokens[4], MaxInt);
if (eOffset = maxInt) then
Exit;
eYear := TextToInt(tokens[5], $FFFF);
if (eYear = $FFFF) then
Exit;
tokens := TextTokenize(tokens[3], ':');
if (tokens.Count <> 3) then
Exit;
eHour := TextToInt(tokens[0], $FFFF);
if (eHour = $FFFF) then
Exit;
eMinute := TextToInt(tokens[1], $FFFF);
if (eMinute = $FFFF) then
Exit;
eSecond := TextToInt(tokens[2], $FFFF);
if (eSecond = $FFFF) then
Exit;
if (TryEncodeDateTime(eYear, eMonth, eDay, eHour, eMinute, eSecond, 0, aDateTime) = False) then
Exit;
aDateTime := IncHour(aDateTime, eOffset);
Result := True;
end;
initialization
EnUsFormatSettings := TFormatSettings.Create('en-us');
end.