ruby 自动对时器 Windows

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#! rubyw.exe
# encoding: gbk
require 'socket'
require 'time'
require 'cstruct/win32struct'
require 'cstruct'
require 'windows/time'
include Windows::Time

class Systemtime < Win32Struct
	int16:wYear
	WORD :wMonth
	WORD:wDayOfWeek
	WORD:wDay
	WORD:wHour
	WORD:wMinute
	WORD:wSecond
	WORD:wMilliseconds
end

def set_clock(time)
	local_time = Systemtime.new
	local_time.wYear          = time.year
	local_time.wMonth         = time.month
	local_time.wDay           = time.day
	local_time.wHour          = time.hour
	local_time.wMinute        = time.min
	local_time.wSecond        = time.sec
	local_time.wMilliseconds  = time.usec/1000
	SetLocalTime(local_time.data)
end

$geted = false
def g(s)
	exit if $geted
	t=s.recv(2000).match(/date:.*$/i)[0]
	sleep 0.5
	p t
	$geted = true
	puts 'get time'
	set_clock(Time.parse t[5..-1])
end

`title 自动对时`
20.times{|x|
	p 20-x
	sleep 1
}

#取http服务器时间
TCPSocket.open("www.g.cn", 80) {|s|
	s.send "GET / HTTP/1.0\r\n\r\n", 0
	20.times{ g(s) } 
}