setting the hardware clock from ruby

  • Thread starter Joel VanderWerf
  • Start date
J

Joel VanderWerf

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...
 
J

Joel VanderWerf

Joel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top