实时获取网络时间 并转换为北京时间的函数

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,Winapi.msxml, System.DateUtils,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
//实时获取网络时间的函数, 得到的是格林威治时间; 默认从 sohu 服务器获取, 因为它最快, 平均只需 15 毫秒
function GetNetTime(aUrl: WideString = ‘http://www.sohu.com‘): string;
begin
  with CoXMLHTTP.Create do
  begin
    open(‘Post‘, aUrl, False, EmptyParam, EmptyParam);
    send(EmptyParam);
    Result := getResponseHeader(‘Date‘);
  end;
end;

//格林威治时间(字符串)转换到北京时间  1
function GMT2BjDateTime(const GMT: string): TDateTime;
var
  A: TArray<string>;
begin
  A := GMT.Split([‘,‘, ‘ ‘], ExcludeEmpty); //XE4 支持

  with TStringList.Create do begin
    CommaText := ‘Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12‘;
    A[2] := Values[A[2]];
    Free;
  end;

  Result := StrToDateTime(Format(‘%s/%s/%s %s‘, [A[3], A[2], A[1], A[4]]), FormatSettings.Create(2052));
  Result := Result + 8/24; //换算成北京时间
end;
//格林威治时间(字符串)转换到北京时间  2
function GMT2BjDateTime2(const GMT: string): TDateTime;
var
  A: TArray<string>;
begin
  A := GMT.Split([‘,‘, ‘ ‘], ExcludeEmpty); //XE4 支持

  with TStringList.Create do begin
    CommaText := ‘Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12‘;
    A[2] := Values[A[2]];
    Free;
  end;

  Result := StrToDateTime(Format(‘%s/%s/%s %s‘, [A[3], A[2], A[1], A[4]]), FormatSettings.Create(2052));
  Result := Result + 8/24; //换算成北京时间
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  strGMT: string;
  bjDateTime: TDateTime;
  bjDateTime2: TDateTime;
begin
  strGMT := GetNetTime();
  bjDateTime := GMT2BjDateTime(strGMT);
  bjDateTime2 := GMT2BjDateTime2(strGMT);

  Label1.Caption:=strGMT + #10 + DateTimeToStr(bjDateTime);
  Label2.Caption:=strGMT + #10 + DateTimeToStr(bjDateTime2);
end;

end.

时间: 2024-08-22 14:27:36

实时获取网络时间 并转换为北京时间的函数的相关文章

如何将一个格林威治字符串时间格式化转换为本地时间

python 时间函数学习了一大堆,那到底怎么将一个格林威治的时间字符串转换为本地时间呢? 这里将以一个格林威治时间字符串转换为北京时间为例进行说明. 格林威治时间字符串:2015-08-31T11:20:48 首先将这个字符串转换为表示时间的tuple格式: >>> t=time.strptime('2015-08-31T11:20:48','%Y-%m-%dT%H:%M:%S') >>> t time.struct_time(tm_year=2015, tm_mon

调整php和mysql的时间 utc转为北京时间

date_default_timezone_set('PRC') or die('时区设置失败,请联系管理员!'); //设置php的时区,例如使用time函数 mysql_query("SET time_zone = '+8:00'") or die('时区设置失败,请联系管理员!'); //设置mysql的时区,例如使用now函数 调整php和mysql的时间 utc转为北京时间

Centos 用ntpdate将时间修改为北京时间

用ntpdate从时间服务器更新时间 1 [[email protected] ~]# ntpdate time.nist.gov 2 7 Nov 13:49:41 ntpdate[3032]: no servers can be used, exiting 3 [[email protected] ~]# ntpdate time.nist.gov 4 7 Nov 14:16:45 ntpdate[3033]: step time server 24.56.178.140 offset 159

服务器时间无法同步北京时间

1.[[email protected] ~]# rm -rf /etc/localtime   2.[[email protected] ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

在国外主机上将Linux系统时间修改为北京时间的方法

[[email protected] ~]#date Sun Aug 14 23:18:41 EDT 2011 [[email protected] ~]# rm -rf /etc/localtime [[email protected] ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime [[email protected] ~]# date Mon Aug 15 11:20:00 CST 2011

Nodejs 获取北京时间

服务器部署在美国,时区不同.需要把系统时间转换成北京时间. 用 china-time 即可 npm i --save china-time 使用 const chinaTime = require('china-time'); console.log(chinaTime()); // 2018-02-07T04:38:00.000Z console.log(chinaTime().getTime()); // 1517978280000 console.log(chinaTime('YYYY-M

javascript如何将时间日期转换为Date对象

javascript如何将时间日期转换为Date对象:有时候需要讲一个字符串型的时间日期转换为Date时间对象,下面就通过一个简单的实例提供一种解决方案,当然也是一种思路,可以进行一定的变通,以达到举一反三的效果.例如这里有一个时间日期字符串: 2013-9-15 8:25:30 下面就将它们转换为时间对象.代码如下: var timeStr="2013-9-15 8:25:30"; var strArray=timeStr.split(" "); var strD

获取网络上的北京时间,如果大于设定的过期时间就...

设置一个过期时间 ,定义时间变量dd,dd获取网络上的北京时间,如果获取失败,就获取计算机当前时间,再比对dd与过期时间,如果dd大于过期时间,就执行... // 比对固定时间,看是否超时. private void CompareTime() { try { dd = GetBeijingTime(); i = DateTime.Compare(dd, Convert.ToDateTime("2013-1-1 00:00:00")); if (i < 0) { dd = Dat

.net 获取网络时间(北京时间)24小时制

/// <summary> /// 更新系统时间 /// </summary> public class UpdateTime { //设置系统时间的API函数 [DllImport("kernel32.dll")] private static extern bool SetLocalTime(ref SYSTEMTIME time); [StructLayout(LayoutKind.Sequential)] private struct SYSTEMTIM