[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby/DL and libc's localtime

Eric Hodel

3/18/2005 6:16:00 AM

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 - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
3 Answers

Takaaki Tateishi

3/18/2005 9:50:00 AM

0

Eric Hodel wrote:
> 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]
--
Takaaki Tateishi <ttate@ttsky.net>



Eric Hodel

3/18/2005 7:49:00 PM

0

On 18 Mar 2005, at 01:50, Takaaki Tateishi wrote:

> Eric Hodel wrote:
>> 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 - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

anything

6/13/2009 12:07:00 AM

0

On Fri, 12 Jun 2009 12:16:24 -0700, walkinay@nv.net (hank alrich)
wrote:

>Deb Cowan <dcowan@DebraSPAMMENOT.com> wrote:
>
>> guitarbench wrote:
>> > As astounding as it sounds, Tom Doughty is looking for gigs on the
>> > west coast on his way to Healdsburg where he'll be demonstrating.
>> >
>> > Tom says:" so far, I have Folk Music Centre in Claremont on sat 8th
>> > and Healdsburg following Friday 15th.
>> > looking for things in between. I've tried, McCabe's, Freight &
>> > Salvage, Palms in Davis, no replies from any. "
>> >
>> > If anyone has any suggestions or contacts, please email tom at the
>> > address below
>> > tomATtomdoughtyDOTcom
>>
>> Hi Terence. It's a very political situation with the clubs you
>> mention and you are not likely to hear from them. I'd suggest
>> you try to find a house concert for Tom. It's also the kind of
>> situation where those places you mention book at least a year in
>> advance.
>
>Right on. Getting into those often depends on tetting local airplay, and
>finding a personal link, someone you know who knows the person who
>handles bookings.
>
>> Also, I do hope you realise that California is as big as the
>> entire UK, though the travle situation is a bit better.
>
>No kidding.

It's prolly non PC to mention at this point that Tom is wheelchair
bound so he does need some support to get into and out of places.

His playing speaks for itself though - when you're listening it's just
a guy sitting down playing slide - very_ well_ indeed.

Tom was well pissed off at missing UK 12 because he was committed to a
3 day gig in Poland (all those blonde Polish girls....heartbreaking -
was his take on it..:-)

You'se guys - find somewhere to make his trip worthwhile.

Pete