Foxtable(狐表)用户栏目专家坐堂 → 如何快速获取北京时间


  共有2320人关注过本帖树形打印复制链接

主题:如何快速获取北京时间

帅哥哟,离线,有人找我吗?
lihe60
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:狐神 帖子:6885 积分:43595 威望:0 精华:0 注册:2009/3/2 14:07:00
如何快速获取北京时间  发帖心情 Post By:2018/2/24 15:25:00 [只看该作者]

如题

 回到顶部
帅哥哟,离线,有人找我吗?
lihe60
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:狐神 帖子:6885 积分:43595 威望:0 精华:0 注册:2009/3/2 14:07:00
  发帖心情 Post By:2018/2/24 15:27:00 [只看该作者]

下面代码耗时太长

Dim TimeServers As String(,) = New String(2, 1) {}
TimeServers(0, 0) = "time-b.timefreq.bldrdoc.gov"
TimeServers(0, 1) = "132.163.4.102"
TimeServers(1, 0) = "time-c.timefreq.bldrdoc.gov"
TimeServers(1, 1) = "132.163.4.103"
TimeServers(2, 0) = "utcnist.colorado.edu"
TimeServers(2, 1) = "128.138.140.44"
Dim portNum As Integer = 13
Dim hostName As String
Dim bytes As Byte() = New Byte(1023) {}
Dim bytesRead As Integer = 0
Dim client As New System.Net.Sockets.TcpClient()
Dim str1 As String
Dim count As Integer = 0
Do
    For i As Integer = 0 To 2
        hostName = TimeServers(i, 1)
        Try
            client.Connect(hostName, portNum)
            Dim ns As System.Net.Sockets.NetworkStream = client.GetStream()
            bytesRead = ns.Read(bytes, 0, bytes.Length)
            client.Close()
            
            Exit For
        Catch generatedExceptionName As System.Exception
        End Try
    Next
    str1 = System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRead)
    count += 1
Loop While count < 10 AndAlso str1 Is Nothing
Dim sp As Char() = New Char(0) {}
sp(0) = " "c
Dim dt As System.DateTime = New DateTime()
Dim s As String()
s = str1.Split(sp)
dt = System.DateTime.Parse(s(1) & " " & s(2))
'得到标准时间
dt = dt.AddHours(8)
'得到北京时间
Messagebox.show( dt)

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110629 积分:563047 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2018/2/24 15:59:00 [只看该作者]

参考:http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=109221&skin=0

 回到顶部