| 1 | package Time::FFI; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use Carp 'croak'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use FFI::Platypus; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use FFI::Platypus::Buffer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use FFI::Platypus::Memory; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use Time::FFI::tm; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 3 | our $VERSION = '2.002'; | |||||||||
| 13 | |||||||||||
| 14 | 3 | our @EXPORT_OK = qw(asctime ctime gmtime localtime mktime strftime strptime timegm timelocal); | |||||||||
| 15 | 3 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||
| 16 | |||||||||||
| 17 | 3 | my $ffi = FFI::Platypus->new(api => 1, lib => [undef], ignore_not_found => 1); | |||||||||
| 18 | 3 | $ffi->type('record(Time::FFI::tm)' => 'tm'); | |||||||||
| 19 | 3 | my $char_size = $ffi->sizeof('char'); | |||||||||
| 20 | |||||||||||
| 21 | 3 | ▶ | if (defined $ffi->find_symbol('asctime_r')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 2 | ▶ | $ffi->attach([asctime_r => 'asctime'] => ['tm*', 'opaque'] => 'string' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 23 | 2 | my ($xsub, $tm) = @_; | |||||||||
| 24 | 2 | my $rc = $xsub->($tm, my $buf = calloc(26, $char_size)); | |||||||||
| 25 | 2 | free $buf; | |||||||||
| 26 | 2 | ▶ | croak "asctime: $!" unless defined $rc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 2 | return $rc; | |||||||||
| 28 | 3 | }); | |||||||||
| 29 | } else { | ||||||||||
| 30 | 0 | ▶ | $ffi->attach(asctime => ['tm*'] => 'string' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 31 | 0 | my ($xsub, $tm) = @_; | |||||||||
| 32 | 0 | my $rc = $xsub->($tm); | |||||||||
| 33 | 0 | ▶ | croak "asctime: $!" unless defined $rc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 0 | return $rc; | |||||||||
| 35 | 0 | }); | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 3 | ▶ | if (defined $ffi->find_symbol('ctime_r')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 1 | ▶ | $ffi->attach([ctime_r => 'ctime'] => ['time_t*', 'opaque'] => 'string' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||
| 40 | 1 | my ($xsub, $time) = @_; | |||||||||
| 41 | 1 | ▶ | $time = time unless defined $time; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 1 | my $rc = $xsub->(\$time, my $buf = calloc(26, $char_size)); | |||||||||
| 43 | 1 | free $buf; | |||||||||
| 44 | 1 | ▶ | croak "ctime: $!" unless defined $rc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 1 | return $rc; | |||||||||
| 46 | 3 | }); | |||||||||
| 47 | } else { | ||||||||||
| 48 | 0 | ▶ | $ffi->attach(ctime => ['time_t*'] => 'string' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 49 | 0 | my ($xsub, $time) = @_; | |||||||||
| 50 | 0 | ▶ | $time = time unless defined $time; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | my $rc = $xsub->(\$time); | |||||||||
| 52 | 0 | ▶ | croak "ctime: $!" unless defined $rc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | return $rc; | |||||||||
| 54 | 0 | }); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 3 | ▶ | if (defined $ffi->find_symbol('gmtime_r')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 2 | ▶ | $ffi->attach([gmtime_r => 'gmtime'] => ['time_t*', 'tm*'] => 'opaque' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||
| 59 | 2 | my ($xsub, $time) = @_; | |||||||||
| 60 | 2 | ▶ | $time = time unless defined $time; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 2 | my $rc = $xsub->(\$time, my $tm = Time::FFI::tm->new); | |||||||||
| 62 | 2 | ▶ | croak "gmtime: $!" unless defined $rc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 2 | return $tm; | |||||||||
| 64 | 3 | }); | |||||||||
| 65 | } else { | ||||||||||
| 66 | 0 | ▶ | $ffi->attach(gmtime => ['time_t*'] => 'tm*' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 67 | 0 | my ($xsub, $time) = @_; | |||||||||
| 68 | 0 | ▶ | $time = time unless defined $time; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | my $rc = $xsub->(\$time); | |||||||||
| 70 | 0 | ▶ | croak "gmtime: $!" unless defined $rc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | return $rc; | |||||||||
| 72 | 0 | }); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 3 | ▶ | if (defined $ffi->find_symbol('localtime_r')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 1 | ▶ | $ffi->attach([localtime_r => 'localtime'] => ['time_t*', 'tm*'] => 'opaque' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||
| 77 | 1 | my ($xsub, $time) = @_; | |||||||||
| 78 | 1 | ▶ | $time = time unless defined $time; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 1 | my $rc = $xsub->(\$time, my $tm = Time::FFI::tm->new); | |||||||||
| 80 | 1 | ▶ | croak "localtime: $!" unless defined $rc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 1 | return $tm; | |||||||||
| 82 | 3 | }); | |||||||||
| 83 | } else { | ||||||||||
| 84 | 0 | ▶ | $ffi->attach(localtime => ['time_t*'] => 'tm*' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 85 | 0 | my ($xsub, $time) = @_; | |||||||||
| 86 | 0 | ▶ | $time = time unless defined $time; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | my $rc = $xsub->(\$time); | |||||||||
| 88 | 0 | ▶ | croak "localtime: $!" unless defined $rc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | return $rc; | |||||||||
| 90 | 0 | }); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 9 | ▶ | $ffi->attach(mktime => ['tm*'] => 'time_t' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 94 | 9 | my ($xsub, $tm) = @_; | |||||||||
| 95 | 9 | my $rc = $xsub->($tm); | |||||||||
| 96 | 9 | ▶ | croak "mktime: $!" if $rc == -1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 9 | return $rc; | |||||||||
| 98 | 3 | }); | |||||||||
| 99 | |||||||||||
| 100 | 2 | ▶ | $ffi->attach(strftime => ['opaque', 'size_t', 'string', 'tm*'] => 'size_t' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||
| 101 | 2 | my ($xsub, $format, $tm) = @_; | |||||||||
| 102 | 2 | my $max_size = length($format) * 20; | |||||||||
| 103 | 2 | my $buf_size = 200; | |||||||||
| 104 | 2 | my $rc = 0; | |||||||||
| 105 | 2 | my $buf; | |||||||||
| 106 | 2 | ▶ | until ($rc != 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 2 | $rc = $xsub->($buf = realloc($buf, $buf_size * $char_size), $buf_size, $format, $tm); | |||||||||
| 108 | 2 | ▶ | last if $buf_size > $max_size; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | } continue { | ||||||||||
| 110 | 0 | $buf_size *= 2; | |||||||||
| 111 | } | ||||||||||
| 112 | 2 | my $str = buffer_to_scalar $buf, $rc * $char_size; | |||||||||
| 113 | 2 | free $buf; | |||||||||
| 114 | 2 | return $str; | |||||||||
| 115 | 3 | }); | |||||||||
| 116 | |||||||||||
| 117 | 3 | ▶ | $ffi->attach(strptime => ['string', 'string', 'tm*'] => 'string' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 118 | 3 | my ($xsub, $str, $format, $tm, $remaining) = @_; | |||||||||
| 119 | 3 | ▶ | $tm = Time::FFI::tm->new unless defined $tm; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 3 | my $rc = $xsub->($str, $format, $tm); | |||||||||
| 121 | 3 | ▶ | croak "strptime: Failed to match input to format string" unless defined $rc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 3 | $tm->isdst(-1); | |||||||||
| 123 | 3 | ▶ | $$remaining = $rc if defined $remaining; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 3 | return $tm; | |||||||||
| 125 | 3 | }); | |||||||||
| 126 | |||||||||||
| 127 | 1 | ▶ | $ffi->attach(timegm => ['tm*'] => 'time_t' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 128 | 1 | my ($xsub, $tm) = @_; | |||||||||
| 129 | 1 | my $rc = $xsub->($tm); | |||||||||
| 130 | 1 | ▶ | croak "timegm: $!" if $rc == -1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | 1 | return $rc; | |||||||||
| 132 | 3 | }); | |||||||||
| 133 | |||||||||||
| 134 | 1 | ▶ | $ffi->attach(timelocal => ['tm*'] => 'time_t' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 135 | 1 | my ($xsub, $tm) = @_; | |||||||||
| 136 | 1 | my $rc = $xsub->($tm); | |||||||||
| 137 | 1 | ▶ | croak "timelocal: $!" if $rc == -1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 1 | return $rc; | |||||||||
| 139 | 3 | }); | |||||||||
| 140 | |||||||||||
| 141 | 3 | 1; | |||||||||
| 142 | |||||||||||
| 143 | =head1 NAME | ||||||||||
| 144 | |||||||||||
| 145 | Time::FFI - libffi interface to POSIX date and time functions | ||||||||||
| 146 | |||||||||||
| 147 | =head1 SYNOPSIS | ||||||||||
| 148 | |||||||||||
| 149 | use Time::FFI qw(localtime mktime strptime strftime); | ||||||||||
| 150 | |||||||||||
| 151 | my $tm = strptime '1995-01-02 13:15:39', '%Y-%m-%d %H:%M:%S'; | ||||||||||
| 152 | my $epoch = mktime $tm; | ||||||||||
| 153 | print "$epoch: ", strftime('%I:%M:%S %p on %B %e, %Y', $tm); | ||||||||||
| 154 | |||||||||||
| 155 | my $tm = localtime time; | ||||||||||
| 156 | my $datetime = $tm->to_object_as_local('DateTime'); | ||||||||||
| 157 | |||||||||||
| 158 | my $tm = gmtime time; | ||||||||||
| 159 | my $moment = $tm->to_object_as_utc('Time::Moment'); | ||||||||||
| 160 | |||||||||||
| 161 | use Time::FFI::tm; | ||||||||||
| 162 | my $tm = Time::FFI::tm->from_object(DateTime->now); | ||||||||||
| 163 | my $epoch = $tm->epoch_as_local; | ||||||||||
| 164 | my $piece = $tm->to_object_as_local('Time::Piece'); | ||||||||||
| 165 | |||||||||||
| 166 | =head1 DESCRIPTION | ||||||||||
| 167 | |||||||||||
| 168 | B<Time::FFI> provides a L<libffi|FFI::Platypus> interface to POSIX date and | ||||||||||
| 169 | time functions found in F<time.h>. | ||||||||||
| 170 | |||||||||||
| 171 | The L</gmtime> and L</localtime> functions behave very differently from the | ||||||||||
| 172 | core functions of the same name, as well as those exported by L<Time::Piece>, | ||||||||||
| 173 | so you may wish to call them as e.g. C<Time::FFI::gmtime> rather than importing | ||||||||||
| 174 | them. | ||||||||||
| 175 | |||||||||||
| 176 | All functions will throw an exception in the event of an error. For functions | ||||||||||
| 177 | other than L</strftime> and L</strptime>, this exception will contain the | ||||||||||
| 178 | syscall error message, and L<perlvar/$!> will also have been set by the | ||||||||||
| 179 | syscall, so you could check it after trapping the exception for finer exception | ||||||||||
| 180 | handling. | ||||||||||
| 181 | |||||||||||
| 182 | =head1 FUNCTIONS | ||||||||||
| 183 | |||||||||||
| 184 | All functions are exported individually, or with the C<:all> export tag. | ||||||||||
| 185 | |||||||||||
| 186 | =head2 asctime | ||||||||||
| 187 | |||||||||||
| 188 | my $str = asctime $tm; | ||||||||||
| 189 | |||||||||||
| 190 | Returns a string in the format C<Wed Jun 30 21:49:08 1993\n> representing the | ||||||||||
| 191 | passed L<Time::FFI::tm> record. The thread-safe L<asctime_r(3)> function is | ||||||||||
| 192 | used if available. | ||||||||||
| 193 | |||||||||||
| 194 | =head2 ctime | ||||||||||
| 195 | |||||||||||
| 196 | my $str = ctime $epoch; | ||||||||||
| 197 | my $str = ctime; | ||||||||||
| 198 | |||||||||||
| 199 | Returns a string in the format C<Wed Jun 30 21:49:08 1993\n> representing the | ||||||||||
| 200 | passed epoch timestamp (defaulting to the current time) in the local time zone. | ||||||||||
| 201 | This is equivalent to L<POSIX/ctime> but uses the thread-safe L<ctime_r(3)> | ||||||||||
| 202 | function if available. | ||||||||||
| 203 | |||||||||||
| 204 | =head2 gmtime | ||||||||||
| 205 | |||||||||||
| 206 | my $tm = gmtime $epoch; | ||||||||||
| 207 | my $tm = gmtime; | ||||||||||
| 208 | |||||||||||
| 209 | Returns a L<Time::FFI::tm> record representing the passed epoch timestamp | ||||||||||
| 210 | (defaulting to the current time) in UTC. The thread-safe L<gmtime_r(3)> | ||||||||||
| 211 | function is used if available. | ||||||||||
| 212 | |||||||||||
| 213 | =head2 localtime | ||||||||||
| 214 | |||||||||||
| 215 | my $tm = localtime $epoch; | ||||||||||
| 216 | my $tm = localtime; | ||||||||||
| 217 | |||||||||||
| 218 | Returns a L<Time::FFI::tm> record representing the passed epoch timestamp | ||||||||||
| 219 | (defaulting to the current time) in the local time zone. The thread-safe | ||||||||||
| 220 | L<localtime_r(3)> function is used if available. | ||||||||||
| 221 | |||||||||||
| 222 | =head2 mktime | ||||||||||
| 223 | |||||||||||
| 224 | my $epoch = mktime $tm; | ||||||||||
| 225 | |||||||||||
| 226 | Returns the epoch timestamp representing the passed L<Time::FFI::tm> record | ||||||||||
| 227 | interpreted in the local time zone. The time is interpreted from the C<sec>, | ||||||||||
| 228 | C<min>, C<hour>, C<mday>, C<mon>, C<year>, and C<isdst> members of the record, | ||||||||||
| 229 | ignoring the rest. DST status will be automatically determined if C<isdst> is a | ||||||||||
| 230 | negative value. The record will also be updated to normalize any out-of-range | ||||||||||
| 231 | values and populate the C<isdst>, C<wday>, and C<yday> values, as well as | ||||||||||
| 232 | C<gmtoff> and C<zone> if supported. | ||||||||||
| 233 | |||||||||||
| 234 | =head2 strftime | ||||||||||
| 235 | |||||||||||
| 236 | my $str = strftime $format, $tm; | ||||||||||
| 237 | |||||||||||
| 238 | Returns a string formatted according to the passed format string, representing | ||||||||||
| 239 | the passed L<Time::FFI::tm> record. Consult your system's L<strftime(3)> manual | ||||||||||
| 240 | for available format descriptors. | ||||||||||
| 241 | |||||||||||
| 242 | =head2 strptime | ||||||||||
| 243 | |||||||||||
| 244 | my $tm = strptime $str, $format; | ||||||||||
| 245 | $tm = strptime $str, $format, $tm; | ||||||||||
| 246 | my $tm = strptime $str, $format, undef, \my $remaining; | ||||||||||
| 247 | $tm = strptime $str, $format, $tm, \my $remaining; | ||||||||||
| 248 | |||||||||||
| 249 | Returns a L<Time::FFI::tm> record representing the passed string, parsed | ||||||||||
| 250 | according to the passed format. Consult your system's L<strptime(3)> manual for | ||||||||||
| 251 | available format descriptors. The C<isdst> value will be set to -1; all other | ||||||||||
| 252 | unspecified values will default to 0. Note that the default C<mday> value of 0 | ||||||||||
| 253 | is outside of the standard range [1,31] and may cause an error or be | ||||||||||
| 254 | interpreted as the last day of the previous month. | ||||||||||
| 255 | |||||||||||
| 256 | A L<Time::FFI::tm> record may be passed as the third argument, in which case it | ||||||||||
| 257 | will be modified in place to (on most systems) update only the date/time | ||||||||||
| 258 | elements which were parsed from the string. Additionally, an optional scalar | ||||||||||
| 259 | reference may be passed as the fourth argument, in which case it will be set to | ||||||||||
| 260 | the remaining unprocessed characters of the input string if any. | ||||||||||
| 261 | |||||||||||
| 262 | This function is usually not available on Windows. | ||||||||||
| 263 | |||||||||||
| 264 | =head2 timegm | ||||||||||
| 265 | |||||||||||
| 266 | my $epoch = timegm $tm; | ||||||||||
| 267 | |||||||||||
| 268 | I<Since version 1.002> | ||||||||||
| 269 | |||||||||||
| 270 | Like L</mktime>, but interprets the passed L<Time::FFI::tm> record as UTC. This | ||||||||||
| 271 | function is not always available. | ||||||||||
| 272 | |||||||||||
| 273 | =head2 timelocal | ||||||||||
| 274 | |||||||||||
| 275 | my $epoch = timelocal $tm; | ||||||||||
| 276 | |||||||||||
| 277 | I<Since version 1.002> | ||||||||||
| 278 | |||||||||||
| 279 | The same as L</mktime>, but not always available. | ||||||||||
| 280 | |||||||||||
| 281 | =head1 BUGS | ||||||||||
| 282 | |||||||||||
| 283 | Report any issues on the public bugtracker. | ||||||||||
| 284 | |||||||||||
| 285 | =head1 AUTHOR | ||||||||||
| 286 | |||||||||||
| 287 | Dan Book <dbook@cpan.org> | ||||||||||
| 288 | |||||||||||
| 289 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 290 | |||||||||||
| 291 | This software is Copyright (c) 2019 by Dan Book. | ||||||||||
| 292 | |||||||||||
| 293 | This is free software, licensed under: | ||||||||||
| 294 | |||||||||||
| 295 | The Artistic License 2.0 (GPL Compatible) | ||||||||||
| 296 | |||||||||||
| 297 | =head1 SEE ALSO | ||||||||||
| 298 | |||||||||||
| 299 | L<Time::Piece>, L<Time::Moment>, L<DateTime>, L<POSIX>, L<POSIX::strptime> | ||||||||||