| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use FFI::Platypus 2.00; |
| 4 |
use FFI::C; |
| 5 |
|
| 6 |
my $ffi = FFI::Platypus->new( |
| 7 |
api => 2, |
| 8 |
lib => [undef], |
| 9 |
); |
| 10 |
FFI::C->ffi($ffi); |
| 11 |
|
| 12 |
package Unix::TimeStruct { |
| 13 |
|
| 14 |
FFI::C->struct(tm => [ |
| 15 |
tm_sec => 'int', |
| 16 |
tm_min => 'int', |
| 17 |
tm_hour => 'int', |
| 18 |
tm_mday => 'int', |
| 19 |
tm_mon => 'int', |
| 20 |
tm_year => 'int', |
| 21 |
tm_wday => 'int', |
| 22 |
tm_yday => 'int', |
| 23 |
tm_isdst => 'int', |
| 24 |
tm_gmtoff => 'long', |
| 25 |
_tm_zone => 'opaque', |
| 26 |
]); |
| 27 |
|
| 28 |
# For now 'string' is unsupported by FFI::C, but we |
| 29 |
# can cast the time zone from an opaque pointer to |
| 30 |
# string. |
| 31 |
sub tm_zone { |
| 32 |
my $self = shift; |
| 33 |
$ffi->cast('opaque', 'string', $self->_tm_zone); |
| 34 |
} |
| 35 |
|
| 36 |
# attach the C localtime function |
| 37 |
$ffi->attach( localtime => ['time_t*'] => 'tm', sub { |
| 38 |
my($inner, $class, $time) = @_; |
| 39 |
$time = time unless defined $time; |
| 40 |
$inner->(\$time); |
| 41 |
}); |
| 42 |
} |
| 43 |
|
| 44 |
# now we can actually use our Unix::TimeStruct class |
| 45 |
my $time = Unix::TimeStruct->localtime; |
| 46 |
printf "time is %d:%d:%d %s\n", |
| 47 |
$time->tm_hour, |
| 48 |
$time->tm_min, |
| 49 |
$time->tm_sec, |
| 50 |
$time->tm_zone; |