{"id":2573,"date":"2015-06-09T21:23:24","date_gmt":"2015-06-09T13:23:24","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=2573"},"modified":"2015-06-09T21:23:24","modified_gmt":"2015-06-09T13:23:24","slug":"%e3%80%90delphi%e3%80%91gmt%e6%97%b6%e9%97%b4%e4%b8%8etdatetime%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=2573","title":{"rendered":"\u3010Delphi\u3011GMT\u65f6\u95f4\u4e0eTDateTime\u8f6c\u6362"},"content":{"rendered":"<p>\u8bf4\u5230GMT\u65f6\u95f4\uff0c\u4eba\u4eec\u5929\u5929\u90fd\u5728\u7528\uff0c\u4f46\u4e00\u822c\u4eba\u4e0d\u4f1a\u88ab\u6ce8\u610f\u5230\u3002<\/p>\n<p>\u5728Http\u7684\u4e16\u754c\u91cc\u9762\uff0cGMT\u65e0\u5904\u4e0d\u5728\uff0c\u4e0d\u4fe1\u4f60\u6293\u5305\u770b\u770b\u5404\u79cdHeader\u91cc\u9762\u3002<\/p>\n<p>GMT\u662f\u4e16\u754c\u65f6\u95f4\uff0c\u5728\u5904\u7406\u7684\u65f6\u5019\u9700\u8981\u5904\u7406\u5f53\u524d\u7684\u65f6\u533a\u3002<\/p>\n<p>\u7b97\u4e86\uff0c\u6211\u61d2\u5f97\u591a\u8bf4\uff0c\u76f4\u63a5\u8d34\u4ee3\u7801\u5427\u3002\u5439\u5439\u725b\uff0c\u6211\u8fd9\u4e2a\u53ef\u4ee5\u9ad8\u6027\u80fd\u7684\u54e6~~~~<\/p>\n<pre class=\"lang:delphi decode:true \">const\r\n  Convert: array[0..255] of Integer =\r\n    (\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n      0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,\r\n     -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\r\n     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1\r\n     );\r\n\r\nfunction PCharToIntDef(const S: PAnsichar; Len: Integer; def: Integer = 0): Integer;\r\nvar\r\n  I: Integer;\r\n  v: Integer;\r\nbegin\r\n  Result := 0;\r\n  for I := 0 to len-1 do begin\r\n    V := Convert[ord(s[i])];\r\n    if V&lt;0 then begin\r\n      Result := def;\r\n      Exit;\r\n    end;\r\n    result := (result * 10) + V;\r\n  end;\r\nend;\r\n\r\nfunction LocalTimeZoneBias: Integer;\r\n{$IFDEF LINUX}\r\nvar\r\n  TV: TTimeval;\r\n  TZ: TTimezone;\r\nbegin\r\n  gettimeofday(TV, TZ);\r\n  Result := TZ.tz_minuteswest;\r\nend;\r\n{$ELSE}\r\nvar\r\n  TimeZoneInformation: TTimeZoneInformation;\r\n  Bias: Longint;\r\nbegin\r\n  case GetTimeZoneInformation(TimeZoneInformation) of\r\n    TIME_ZONE_ID_STANDARD: Bias := TimeZoneInformation.Bias + TimeZoneInformation.StandardBias;\r\n    TIME_ZONE_ID_DAYLIGHT: Bias := TimeZoneInformation.Bias + ((TimeZoneInformation.DaylightBias div 60) * -100);\r\n  else\r\n    Bias := TimeZoneInformation.Bias;\r\n  end;\r\n  Result := Bias;\r\nend;\r\n{$ENDIF}\r\n\r\nvar\r\n  DLocalTimeZoneBias: Double = 0;\r\n\r\nfunction DateTimeToGMT(const DT: TDateTime): TDateTime; inline;\r\nbegin\r\n  Result := DT + DLocalTimeZoneBias;\r\nend;\r\n\r\nfunction GMTToDateTime(const DT: TDateTime): TDateTime; inline;\r\nbegin\r\n  Result := DT - DLocalTimeZoneBias;\r\nend;\r\n\r\nfunction DateTimeToGMTRFC822(const DateTime: TDateTime): string;\r\nconst\r\n  WEEK: array[1..7] of string = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');\r\n  STR_ENGLISH_M: array[1..12] of string = ('Jan', 'Feb', 'Mar', 'Apr', 'May',\r\n    'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');\r\nvar\r\n  wWeek, wYear, wMonth, wDay, wHour, wMin, wSec, wMilliSec: Word;\r\nbegin\r\n  DecodeDateTime(DateTimeToGMT(DateTime), wYear, wMonth, wDay, wHour, wMin, wSec, wMilliSec);\r\n  wWeek := DayOfWeek(DateTimeToGMT(DateTime));\r\n  Result := Format('%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT',\r\n    [WEEK[wWeek], wDay, STR_ENGLISH_M[wMonth], wYear, wHour, wMin, wSec]);\r\nend;\r\n\r\nfunction GMTRFC822ToDateTime(const pSour: AnsiString): TDateTime;\r\n\r\n  function GetMonthDig(const Value: PAnsiChar): Integer;\r\n  const\r\n    STR_ENGLISH_M: array[1..12] of PAnsiChar = ('Jan', 'Feb', 'Mar', 'Apr', 'May',\r\n      'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');\r\n  begin\r\n    for Result := Low(STR_ENGLISH_M) to High(STR_ENGLISH_M) do begin\r\n      if StrLIComp(Value, STR_ENGLISH_M[Result], 3) = 0 then\r\n        Exit;\r\n    end;\r\n    Result := 0;\r\n  end;\r\nvar\r\n  P1, P2, PMax: PAnsiChar;\r\n  wDay, wMonth, wYear, wHour, wMinute, wSec: SmallInt;\r\nbegin\r\n  Result := 0;\r\n  if Length(pSour) &lt; 25 then Exit;\r\n  P1 := Pointer(pSour);\r\n  P2 := P1;\r\n  PMax := P1 + Length(pSour);\r\n  while (P1 &lt; PMax) and (P1^ &lt;&gt; ',') do Inc(P1); Inc(P1);\r\n  if (P1^ &lt;&gt; #32) and (P1 - P2 &lt; 4) then Exit;\r\n  Inc(P1); P2 := P1;\r\n  while (P1 &lt; PMax) and (P1^ &lt;&gt; #32) do Inc(P1);\r\n  if (P1^ &lt;&gt; #32) then Exit;\r\n  wDay := PCharToIntDef(P2, P1 - P2);\r\n  if wDay = 0 then Exit;  \r\n  Inc(P1); P2 := P1;\r\n\r\n  while (P1 &lt; PMax) and (P1^ &lt;&gt; #32) do Inc(P1);\r\n  if (P1^ &lt;&gt; #32) and (P1 - P2 &lt; 3) then Exit;\r\n  wMonth := GetMonthDig(P2);\r\n  Inc(P1); P2 := P1;\r\n\r\n  while (P1 &lt; PMax) and (P1^ &lt;&gt; #32) do Inc(P1);\r\n  if (P1^ &lt;&gt; #32) then Exit;\r\n  wYear := PCharToIntDef(P2, P1 - P2);\r\n  if wYear = 0 then Exit;\r\n  Inc(P1); P2 := P1;\r\n\r\n  while (P1 &lt; PMax) and (P1^ &lt;&gt; ':') do Inc(P1);\r\n  if (P1^ &lt;&gt; ':') then Exit;\r\n  wHour := PCharToIntDef(P2, P1 - P2);\r\n  if wHour = 0 then Exit;\r\n  Inc(P1); P2 := P1;\r\n\r\n  while (P1 &lt; PMax) and (P1^ &lt;&gt; ':') do Inc(P1);\r\n  if (P1^ &lt;&gt; ':') then Exit;\r\n  wMinute := PCharToIntDef(P2, P1 - P2);\r\n  if wMinute = 0 then Exit;\r\n  Inc(P1); P2 := P1;\r\n\r\n  while (P1 &lt; PMax) and (P1^ &lt;&gt; #32) do Inc(P1);\r\n  if (P1^ &lt;&gt; #32) then Exit;\r\n  wSec := PCharToIntDef(P2, P1 - P2);\r\n  if wSec = 0 then Exit;\r\n\r\n  Result := GMTToDateTime(EnCodeDateTime(wYear, wMonth, wDay, wHour, wMinute, wSec, 0));\r\nend;\r\n\r\ninitialization\r\n  DLocalTimeZoneBias := LocalTimeZoneBias \/ 1440;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf4\u5230GMT\u65f6\u95f4\uff0c\u4eba\u4eec\u5929\u5929\u90fd\u5728\u7528\uff0c\u4f46\u4e00\u822c\u4eba [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[450,422],"class_list":["post-2573","post","type-post","status-publish","format-standard","hentry","category-delphi","tag-delphi","tag-gmt"],"views":10106,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2573"}],"version-history":[{"count":1,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2573\/revisions"}],"predecessor-version":[{"id":2574,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2573\/revisions\/2574"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}