Monday, August 15, 2005

because Windows keeps jacking my clock

Of course the real solution is to eliminate the Windows partition altogether--and if I was done with school, that'd be acceptable.

Instead, here's a Ruby time synchronization hack/work-around I'll place in my startup script.

#!/bin/ruby
require 'net/http'
require 'date'

proxy_addr=ENV['http_proxy'].split(/:/)[1].split('/')[2]
proxy_port=80
#handle the case when the proxy_addr is null
date=nil


while(date == nil)
#fetch date
begin
Net::HTTP::Proxy(proxy_addr,proxy_port).start('www.google.com') {|http|
http.read_timeout = 1
http.open_timeout = 1
p = http.request_head('/')
puts p['content-type']
puts p['date']
date = p['date']
}
rescue
puts "is net connected?"
end

#set date
if(date != nil)
puts date
datetime = DateTime.parse(date)
puts datetime
mst = (datetime.new_offset(-0.25) )
#will need to determine if this is during daylight savings or not and adjust fraction accordingly
puts mst

#parse and format for posix
dates = mst.to_s.split('-')
times = dates.to_s.split(":")
finalDate = dates[1]
finalDate += dates[2][0,2]
finalDate += times[0][9,11]
finalDate += times[1]
finalDate += dates[0]
puts finalDate
`date #{finalDate}`
end
sleep(1)
end

No comments: