Setting the hardware clock from ruby

Anybody happen to know if there is a platform independent way of setting
the hardware clock from ruby? I’m primarily interested in Windows at the
moment (for a port of some QNX code), so it can probably be done with
Win32API (or Ruby/DL, now). I didn’t find much under “clock” on ruby
talk or “ruby set clock” on google. Any suggestions appreciated…

Joel VanderWerf wrote:

Anybody happen to know if there is a platform independent way of setting
the hardware clock from ruby? I’m primarily interested in Windows at the
moment (for a port of some QNX code), so it can probably be done with
Win32API (or Ruby/DL, now). I didn’t find much under “clock” on ruby
talk or “ruby set clock” on google. Any suggestions appreciated…

Setting local time on windows turned out to be an easy exercise in
Ruby/DL. For posterity, here’s the code:

require “dl/import”
require “dl/struct”

module Kernel32
extend DL::Importable
dlload ‘kernel32’

SYSTEMTIME = struct [
“WORD wYear”,
“WORD wMonth”,
“WORD wDayOfWeek”,
“WORD wDay”,
“WORD wHour”,
“WORD wMinute”,
“WORD wSecond”,
“WORD wMilliseconds”,
]

These use GMT.

extern “void GetSystemTime(SYSTEMTIME*)”
extern “BOOL SetSystemTime(const SYSTEMTIME*)”

These use local time, like ruby’s Time.now.

extern “void GetLocalTime(SYSTEMTIME*)”
extern “BOOL SetLocalTime(const SYSTEMTIME*)”
end

+time+ is a Time

def set_clock(time)
local_time = Kernel32::SYSTEMTIME.malloc

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

unless Kernel32.setLocalTime(local_time)
raise “Could not set local time to #{time}.”
end
end