File Coverage

blib/lib/Time/Strptime.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition 1 3 33.3
subroutine 7 7 100.0
pod 0 1 0.0
total 29 32 90.6


line stmt bran cond sub pod time code
1             package Time::Strptime;
2 2     2   1081 use 5.008005;
  2         12  
3 2     2   11 use strict;
  2         4  
  2         40  
4 2     2   8 use warnings;
  2         4  
  2         79  
5              
6             our $VERSION = "1.04";
7              
8 2     2   869 use parent qw/Exporter/;
  2         637  
  2         11  
9             our @EXPORT_OK = qw/strptime/;
10              
11 2     2   128 use Carp ();
  2         5  
  2         33  
12 2     2   804 use Time::Strptime::Format;
  2         6  
  2         239  
13              
14             my %instance_cache;
15             sub strptime {
16 1     1 0 130 my ($format_text, $date_text) = @_;
17              
18 1         4 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
19 1   33     14 my $format = $instance_cache{$format_text} ||= Time::Strptime::Format->new($format_text);
20 1         5 return $format->parse($date_text);
21             }
22              
23             1;
24             __END__
25              
26             =encoding utf-8
27              
28             =head1 NAME
29              
30             Time::Strptime - parse date and time string.
31              
32             =head1 SYNOPSIS
33              
34             use Time::Strptime qw/strptime/;
35              
36             # function
37             my ($epoch_f, $offset_f) = strptime('%Y-%m-%d %H:%M:%S', '2014-01-01 00:00:00');
38              
39             # OO style
40             my $fmt = Time::Strptime::Format->new('%Y-%m-%d %H:%M:%S');
41             my ($epoch_o, $offset_o) = $fmt->parse('2014-01-01 00:00:00');
42              
43             =head1 DESCRIPTION
44              
45             Time::Strptime is pure perl date and time string parser.
46             In other words, This is pure perl implementation a L<strptime(3)>.
47              
48             This module allows you to perform better by pre-compile the format by string.
49              
50             benchmark:GMT(-0000) C<tp=Time::Piece, ts=Time::Strptime, pt=POSIX::strptime(+Time::Local), tm=Time::Moment>
51              
52             Benchmark: running pt, tm, tp, tp(cached), ts(cached) for at least 10 CPU seconds...
53             pt: 11 wallclock secs (10.41 usr + 0.01 sys = 10.42 CPU) @ 297345.59/s (n=3098341)
54             tm: 10 wallclock secs (10.17 usr + 0.01 sys = 10.18 CPU) @ 2481673.28/s (n=25263434)
55             tp: 10 wallclock secs (10.52 usr + 0.01 sys = 10.53 CPU) @ 56390.98/s (n=593797)
56             tp(cached): 11 wallclock secs (10.53 usr + 0.01 sys = 10.54 CPU) @ 80838.24/s (n=852035)
57             ts(cached): 11 wallclock secs (10.60 usr + 0.01 sys = 10.61 CPU) @ 267686.15/s (n=2840150)
58             Rate tp tp(cached) ts(cached) pt tm
59             tp 56391/s -- -30% -79% -81% -98%
60             tp(cached) 80838/s 43% -- -70% -73% -97%
61             ts(cached) 267686/s 375% 231% -- -10% -89%
62             pt 297346/s 427% 268% 11% -- -88%
63             tm 2481673/s 4301% 2970% 827% 735% --
64              
65             benchmark:Asia/Tokyo(-0900) C<tp=Time::Piece, ts=Time::Strptime, pt=POSIX::strptime(+Time::Local), tm=Time::Moment>
66              
67             Benchmark: running pt, tm, tp, tp(cached), ts(cached) for at least 10 CPU seconds...
68             pt: 10 wallclock secs (10.29 usr + 0.05 sys = 10.34 CPU) @ 147048.07/s (n=1520477)
69             tm: 10 wallclock secs (10.00 usr + 0.03 sys = 10.03 CPU) @ 2344311.67/s (n=23513446)
70             tp: 10 wallclock secs (10.15 usr + 0.02 sys = 10.17 CPU) @ 44565.39/s (n=453230)
71             tp(cached): 11 wallclock secs (10.41 usr + 0.06 sys = 10.47 CPU) @ 50136.29/s (n=524927)
72             ts(cached): 10 wallclock secs (10.73 usr + 0.07 sys = 10.80 CPU) @ 114871.48/s (n=1240612)
73             Rate tp tp(cached) ts(cached) pt tm
74             tp 44565/s -- -11% -61% -70% -98%
75             tp(cached) 50136/s 13% -- -56% -66% -98%
76             ts(cached) 114871/s 158% 129% -- -22% -95%
77             pt 147048/s 230% 193% 28% -- -94%
78             tm 2344312/s 5160% 4576% 1941% 1494% --
79              
80             =head1 FAQ
81              
82             =head2 What's the difference between this module and other modules?
83              
84             This module is fast and not require XS. but, support epoch C<strptime> only.
85             L<DateTime> is very useful and stable! but, It is slow.
86             L<Time::Piece> is fast and useful! but, treatment of time zone is confusing. and, require XS.
87             L<Time::Moment> is very fast and useful! but, does not support C<strptime>. and, require XS.
88              
89             =head2 How to specify a time zone?
90              
91             Set time zone name or L<DateTime::TimeZone> object to C<time_zone> option.
92              
93             use Time::Strptime::Format;
94              
95             my $format = Time::Strptime::Format->new('%Y-%m-%d %H:%M:%S', { time_zone => 'Asia/Tokyo' });
96             my ($epoch, $offset) = $format->parse('2014-01-01 00:00:00');
97              
98             =head2 How to specify a locale?
99              
100             Set locale name object to C<locale> option.
101              
102             use Time::Strptime::Format;
103              
104             my $format = Time::Strptime::Format->new('%Y-%m-%d %H:%M:%S', { locale => 'ja_JP' });
105             my ($epoch, $offset) = $format->parse('2014-01-01 00:00:00');
106              
107             =head1 LICENSE
108              
109             Copyright (C) karupanerura.
110              
111             This library is free software; you can redistribute it and/or modify
112             it under the same terms as Perl itself.
113              
114             =head1 AUTHOR
115              
116             karupanerura E<lt>karupa@cpan.orgE<gt>
117              
118             =cut