setting the hardware clock from ruby

Discussion in 'Ruby' started by Joel VanderWerf, May 9, 2004.

  1. 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, May 9, 2004
    #1
    1. Advertising

  2. 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
     
    Joel VanderWerf, May 10, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Valentin Tihomirov

    Are clock and divided clock synchronous?

    Valentin Tihomirov, Oct 23, 2003, in forum: VHDL
    Replies:
    11
    Views:
    3,297
    louis lin
    Oct 28, 2003
  2. Replies:
    4
    Views:
    724
    Peter Alfke
    Apr 27, 2006
  3. Replies:
    5
    Views:
    2,171
    Ricardo
    Jun 23, 2006
  4. himassk
    Replies:
    1
    Views:
    1,235
    Paul Uiterlinden
    May 16, 2007
  5. pankaj.goel
    Replies:
    6
    Views:
    938
    pankaj.goel
    Nov 25, 2008
Loading...

Share This Page