Author: Udo Nesshoever
How to convert a RFC time string into TDateTime and vv.
Answer:
Conversion of date & time formats: RFC <-> TDateTime (local & UNC) RFC: e.g.
03.01.2001 05:45:00 -0500
TDateTime -> RFC use
DateTimeToRfcTime(date, diff, gmt)
RFC -> TDateTime use
1 RfcTimeToDateTime(time, gmt)
2
3 function DateTimeToRfcTime(
4 dt: TDateTime;
5 iDiff: integer;
6 blnGMT: boolean = false): string;
7 {*
8 Explanation:
9 iDiff is the local offset to GMT in minutes
10 if blnGMT then Result is UNC time else local time
11 e.g. local time zone: ET = GMT - 5hr = -300 minutes
12 dt is TDateTime of 3 Jan 2001 5:45am
13 blnGMT = true -> Result = 'Wed, 03 Jan 2001 05:45:00 GMT'
14 blnGMT = false -> Result = 'Wed, 03 Jan 2001 05:45:00 -0500'
15 *}
16 const
17 Weekday: array[1..7] of string =
18 ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
19 Month: array[1..12] of string = (
20 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
21 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
22 var
23 iDummy: Word;
24 iYear: Word;
25 iMonth: Word;
26 iDay: Word;
27 iHour: Word;
28 iMinute: Word;
29 iSecond: Word;
30 strZone: string;
31 begin
32 if blnGMT then
33 begin
34 dt := dt - iDiff / 1440;
35 strZone := 'GMT';
36 end
37 else
38 begin
39 iDiff := (iDiff div 60) * 100 + (iDiff mod 60);
40 if iDiff < 0 then
41 strZone := Format('-%.4d', [-iDiff])
42 else
43 strZone := Format('+%.4d', [iDiff]);
44 end;
45 DecodeDate(dt, iYear, iMonth, iDay);
46 DecodeTime(dt, iHour, iMinute, iSecond, iDummy);
47 Result := Format('%s, %.2d %s %4d %.2d:%.2d:%.2d %s', [
48 Weekday[DayOfWeek(dt)], iDay, Month[iMonth], iYear,
49 iHour, iMinute, iSecond, strZone]);
50 end;
51
52 function RfcTimeToDateTime(
53 strTime: string;
54 blnGMT: boolean = true): TDateTime;
55 {*
56 Explanation:
57 if blnGMT then Result is UNC time else local time
58 e.g. local time zone: ET = GMT - 5hr = -0500
59 strTime = 'Wed, 03 Jan 2001 05:45:00 -0500'
60 blnGMT = true -> FormatDateTime('...', Result) = '03.01.2001 10:45:00'
61 blnGMT = false -> FormatDateTime('...', Result) = '03.01.2001 05:45:00'
62 *}
63 const
64 wd = 'sun#mon#tue#wed#thu#fri#sat';
65 month = 'janfebmaraprmayjunjulaugsepoctnovdec';
66 var
67 s: string;
68 dd: Word;
69 mm: Word;
70 yy: Word;
71 hh: Word;
72 nn: Word;
73 ss: Word;
74 begin
75 s := LowerCase(Copy(strTime, 1, 3));
76 if Pos(s, wd) > 0 then
77 Delete(strTime, 1, Pos(' ', strTime));
78 s := Trim(Copy(strTime, 1, Pos(' ', strTime)));
79 Delete(strTime, 1, Length(s) + 1);
80 dd := StrToIntDef(s, 0);
81 s := LowerCase(Copy(strTime, 1, 3));
82 Delete(strTime, 1, 4);
83 mm := (Pos(s, month) div 3) + 1;
84 s := Copy(strTime, 1, 4);
85 Delete(strTime, 1, 5);
86 yy := StrToIntDef(s, 0);
87 Result := EncodeDate(yy, mm, dd);
88 s := strTime[1] + strTime[2];
89 hh := StrToIntDef(strTime[1] + strTime[2], 0);
90 nn := StrToIntDef(strTime[4] + strTime[5], 0);
91 ss := StrToIntDef(strTime[7] + strTime[8], 0);
92 Delete(strTime, 1, 9);
93 Result := Result + EncodeTime(hh, nn, ss, 0);
94 if (CompareText(strTime, 'gmt') <> 0) and blnGMT then
95 begin
96 hh := StrToIntDef(strTime[2] + strTime[3], 0);
97 nn := StrToIntDef(strTime[4] + strTime[5], 0);
98 if strTime[1] = '+' then
99 Result := Result - EncodeTime(hh, nn, 0, 0)
100 else
101 Result := Result + EncodeTime(hh, nn, 0, 0);
102 end;
103 end;
|