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   1172 use 5.008005;
  2         7  
3 2     2   10 use strict;
  2         3  
  2         36  
4 2     2   13 use warnings;
  2         4  
  2         89  
5              
6             our $VERSION = "1.01";
7              
8 2     2   757 use parent qw/Exporter/;
  2         499  
  2         9  
9             our @EXPORT_OK = qw/strptime/;
10              
11 2     2   113 use Carp ();
  2         4  
  2         25  
12 2     2   668 use Time::Strptime::Format;
  2         6  
  2         172  
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     17 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<dt=DateTime, ts=Time::Strptime, tp=Time::Piece>
51              
52             Benchmark: running dt, dt(cached), pt, tm, tp, tp(cached), ts, ts(cached) for at least 10 CPU seconds...
53             dt: 10 wallclock secs (10.62 usr + 0.03 sys = 10.65 CPU) @ 2189.58/s (n=23319)
54             dt(cached): 11 wallclock secs (10.76 usr + 0.03 sys = 10.79 CPU) @ 7451.53/s (n=80402)
55             tp: 11 wallclock secs (10.31 usr + 0.02 sys = 10.33 CPU) @ 120097.97/s (n=1240612)
56             tp(cached): 10 wallclock secs (10.75 usr + 0.03 sys = 10.78 CPU) @ 271138.50/s (n=2922873)
57             ts: 10 wallclock secs (10.30 usr + 0.02 sys = 10.32 CPU) @ 3626.74/s (n=37428)
58             ts(cached): 11 wallclock secs (10.38 usr + 0.02 sys = 10.40 CPU) @ 168626.35/s (n=1753714)
59             Rate dt ts dt(cached) tp ts(cached) tp(cached)
60             dt 2190/s -- -40% -71% -98% -99% -99%
61             ts 3627/s 66% -- -51% -97% -98% -99%
62             dt(cached) 7452/s 240% 105% -- -94% -96% -97%
63             tp 120098/s 5385% 3211% 1512% -- -29% -56%
64             ts(cached) 168626/s 7601% 4550% 2163% 40% -- -38%
65             tp(cached) 271138/s 12283% 7376% 3539% 126% 61% --
66              
67             benchmark:Asia/Tokyo(-0900) C<dt=DateTime, ts=Time::Strptime, tp=Time::Piece>
68              
69             Benchmark: running dt, dt(cached), pt, tm, tp, tp(cached), ts, ts(cached) for at least 10 CPU seconds...
70             dt: 11 wallclock secs (10.87 usr + 0.02 sys = 10.89 CPU) @ 2075.76/s (n=22605)
71             dt(cached): 11 wallclock secs (10.59 usr + 0.03 sys = 10.62 CPU) @ 6561.11/s (n=69679)
72             tp: 12 wallclock secs (10.99 usr + 0.03 sys = 11.02 CPU) @ 120083.39/s (n=1323319)
73             tp(cached): 10 wallclock secs (10.69 usr + 0.03 sys = 10.72 CPU) @ 270033.49/s (n=2894759)
74             ts: 10 wallclock secs (10.73 usr + 0.03 sys = 10.76 CPU) @ 3179.37/s (n=34210)
75             ts(cached): 12 wallclock secs (10.95 usr + 0.04 sys = 10.99 CPU) @ 79787.26/s (n=876862)
76             Rate dt ts dt(cached) ts(cached) tp tp(cached)
77             dt 2076/s -- -35% -68% -97% -98% -99%
78             ts 3179/s 53% -- -52% -96% -97% -99%
79             dt(cached) 6561/s 216% 106% -- -92% -95% -98%
80             ts(cached) 79787/s 3744% 2410% 1116% -- -34% -70%
81             tp 120083/s 5685% 3677% 1730% 51% -- -56%
82             tp(cached) 270033/s 12909% 8393% 4016% 238% 125% --
83              
84             =head1 FAQ
85              
86             =head2 What's the difference between this module and other modules?
87              
88             This module is fast and not require XS. but, support epoch C<strptime> only.
89             L<DateTime> is very useful and stable! but, It is slow.
90             L<Time::Piece> is fast and useful! but, treatment of time zone is confusing. and, require XS.
91             L<Time::Moment> is very fast and useful! but, not support C<strptime>. and, require XS.
92              
93             =head2 How to specify a time zone?
94              
95             Set time zone name or L<DateTime::TimeZone> object to C<time_zone> option.
96              
97             use Time::Strptime::Format;
98              
99             my $format = Time::Strptime::Format->new('%Y-%m-%d %H:%M:%S', { time_zone => 'Asia/Tokyo' });
100             my ($epoch, $offset) = $format->parse('2014-01-01 00:00:00');
101              
102             =head2 How to specify a locale?
103              
104             Set locale name object to C<locale> option.
105              
106             use Time::Strptime::Format;
107              
108             my $format = Time::Strptime::Format->new('%Y-%m-%d %H:%M:%S', { locale => 'ja_JP' });
109             my ($epoch, $offset) = $format->parse('2014-01-01 00:00:00');
110              
111             =head1 LICENSE
112              
113             Copyright (C) karupanerura.
114              
115             This library is free software; you can redistribute it and/or modify
116             it under the same terms as Perl itself.
117              
118             =head1 AUTHOR
119              
120             karupanerura E<lt>karupa@cpan.orgE<gt>
121              
122             =cut