Ruby/DL and libc's localtime

E

Eric Hodel

--Apple-Mail-17--324655868
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

I'm trying to wrap localtime from libc, and am having no luck.

Here's what I've got:

require "dl"
require "dl/import"
require "dl/struct"

module LIBC

extend DL::Importable

begin
dlload "libc.so"
rescue
dlload "libc.dylib" # Mac
end

typealias "const time_t *clock", "long ref" # teach DL about const
time_t * clock

##
# I know this isn't necessary, but just so you can see it in
c_localtime below

StructTm = struct [
"int tm_sec", # seconds (0 - 60)
"int tm_min", # minutes (0 - 59)
"int tm_hour", # hours (0 - 23)
"int tm_mday", # day of month (1 - 31)
"int tm_mon", # month of year (0 - 11)
"int tm_year", # year - 1900
"int tm_wday", # day of week (Sunday = 0)
"int tm_yday", # day of year (0 - 365)
"int tm_isdst", # is summer time in effect?
"long tm_gmtoff", # offset from UTC in seconds
"char *tm_zone", # abbreviation of timezone name
]

extern "struct tm * localtime(const time_t *clock)"

def self.c_localtime(clock)
tm = LIBC.localtime(clock)
return tm.to_a('IIIIIIIIILc') # should match StructTm above
end

end

p LIBC.c_localtime(Time.now.to_i) # => []

If I add p tm before calling to_a, I see that its a pointer of size 0,
which makes me think I've got something wrong.

--
Eric Hodel - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-17--324655868
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFCOnICMypVHHlsnwQRAndeAKCjL+7uYdHmnZsdFsKSJ16c/hfA6gCgjHWA
dxiVylAiwqvTdsioo/1IGCQ=
=1lcc
-----END PGP SIGNATURE-----

--Apple-Mail-17--324655868--
 
T

Takaaki Tateishi

Eric said:
def self.c_localtime(clock)
tm = LIBC.localtime(clock)
return tm.to_a('IIIIIIIIILc') # should match StructTm above
end

Please use StructTm to create a proxy as follows.

return LIBC::StructTm.new(tm)

Now you can access each member as follows.

p [tm.tm_sec, tm.tm_min, tm.tm_hour,
tm.tm_mday, tm.tm_mon, tm.tm_year,
tm.tm_wday, tm.tm_yday, tm.tm_isdst,
tm.tm_gmtoff, tm.tm_zone]
 
E

Eric Hodel

--Apple-Mail-18--275850680
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

Eric said:
def self.c_localtime(clock)
tm = LIBC.localtime(clock)
return tm.to_a('IIIIIIIIILc') # should match StructTm above
end

Please use StructTm to create a proxy as follows.

return LIBC::StructTm.new(tm)

Now you can access each member as follows.

p [tm.tm_sec, tm.tm_min, tm.tm_hour,
tm.tm_mday, tm.tm_mon, tm.tm_year,
tm.tm_wday, tm.tm_yday, tm.tm_isdst,
tm.tm_gmtoff, tm.tm_zone]

Works perfect!

I changed DL::Importable::Internal::Memory like so to give more
sensible output:

class DL::Importable::Internal::Memory

def inspect
i = ""
i << "#<#{self.class}:0x#{self.object_id.to_s(16)} "
data = []
@names.each do |name|
data << "#{name}=#{self.send(name).inspect}"
end
i << data.join(', ')
i << ">"
return i
end

end

So now I get this:

p Time.at(1_111_111_111).to_a
p LIBC.c_localtime(1_111_111_111)

[31, 58, 17, 17, 3, 2005, 4, 76, false, "PST"]
#<DL::Importable::Internal::Memory:0x89a1c tm_sec=31, tm_min=58,
tm_hour=17, tm_mday=17, tm_mon=2, tm_year=105, tm_wday=4, tm_yday=75,
tm_isdst=0, tm_gmtoff=-28800, tm_zone=#<DL::ptrData:0x0x42f9f0
ptr=0x0x1808550 size=0 free=0x0x0>>

Note that Time#to_a returns some 1-based fields, while localtime uses
0-based fields

--
Eric Hodel - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-18--275850680
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFCOzCnMypVHHlsnwQRAhsXAKDS/9YehLdu5j97uIh3iUN8qBV9ZQCgj2Pt
4HkfCt3HriseCT05hMf1GVU=
=sKHN
-----END PGP SIGNATURE-----

--Apple-Mail-18--275850680--
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top