ruby 自动对时器 Windows

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

#! 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) } 
}

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...