| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
package Unix::TimeStruct; |
| 5 |
|
| 6 |
use FFI::Platypus 2.00; |
| 7 |
use FFI::Platypus::Record; |
| 8 |
|
| 9 |
record_layout_1(qw( |
| 10 |
int tm_sec |
| 11 |
int tm_min |
| 12 |
int tm_hour |
| 13 |
int tm_mday |
| 14 |
int tm_mon |
| 15 |
int tm_year |
| 16 |
int tm_wday |
| 17 |
int tm_yday |
| 18 |
int tm_isdst |
| 19 |
long tm_gmtoff |
| 20 |
string tm_zone |
| 21 |
)); |
| 22 |
|
| 23 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 24 |
$ffi->lib(undef); |
| 25 |
# define a record class Unix::TimeStruct and alias it to "tm" |
| 26 |
$ffi->type("record(Unix::TimeStruct)*" => 'tm'); |
| 27 |
|
| 28 |
# attach the C localtime function as a constructor |
| 29 |
$ffi->attach( localtime => ['time_t*'] => 'tm', sub { |
| 30 |
my($inner, $class, $time) = @_; |
| 31 |
$time = time unless defined $time; |
| 32 |
$inner->(\$time); |
| 33 |
}); |
| 34 |
|
| 35 |
package main; |
| 36 |
|
| 37 |
# now we can actually use our Unix::TimeStruct class |
| 38 |
my $time = Unix::TimeStruct->localtime; |
| 39 |
printf "time is %d:%d:%d %s\n", |
| 40 |
$time->tm_hour, |
| 41 |
$time->tm_min, |
| 42 |
$time->tm_sec, |
| 43 |
$time->tm_zone; |