File Coverage

blib/lib/Email/Date/Format.pm
Criterion Covered Total %
statement 37 37 100.0
branch 8 8 100.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 54 54 100.0


line stmt bran cond sub pod time code
1 1     1   96687 use 5.006;
  1         12  
2 1     1   4 use strict;
  1         2  
  1         27  
3 1     1   4 use warnings;
  1         2  
  1         66  
4             package Email::Date::Format 1.007;
5             # ABSTRACT: produce RFC 2822 date strings
6              
7             our @EXPORT_OK = qw[email_date email_gmdate];
8              
9 1     1   6 use Exporter 5.57 'import';
  1         13  
  1         36  
10 1     1   429 use Time::Local ();
  1         2280  
  1         243  
11              
12             #pod =head1 SYNOPSIS
13             #pod
14             #pod use Email::Date::Format qw(email_date);
15             #pod
16             #pod my $header = email_date($date->epoch);
17             #pod
18             #pod Email::Simple->create(
19             #pod header => [
20             #pod Date => $header,
21             #pod ],
22             #pod body => '...',
23             #pod );
24             #pod
25             #pod =head1 DESCRIPTION
26             #pod
27             #pod This module provides a simple means for generating an RFC 2822 compliant
28             #pod datetime string. (In case you care, they're not RFC 822 dates, because they
29             #pod use a four digit year, which is not allowed in RFC 822.)
30             #pod
31             #pod =func email_date
32             #pod
33             #pod my $date = email_date; # now
34             #pod my $date = email_date( time - 60*60 ); # one hour ago
35             #pod
36             #pod C<email_date> accepts an epoch value, such as the one returned by C<time>.
37             #pod It returns a string representing the date and time of the input, as
38             #pod specified in RFC 2822. If no input value is provided, the current value
39             #pod of C<time> is used.
40             #pod
41             #pod C<email_date> is exported only if requested.
42             #pod
43             #pod =func email_gmdate
44             #pod
45             #pod my $date = email_gmdate;
46             #pod
47             #pod C<email_gmdate> is identical to C<email_date>, but it will return a string
48             #pod indicating the time in Greenwich Mean Time, rather than local time.
49             #pod
50             #pod C<email_gmdate> is exported only if requested.
51             #pod
52             #pod =cut
53              
54             sub _tz_diff {
55 7     7   884 my ($time) = @_;
56              
57 7         92 my @localtime = localtime $time;
58 7         23 my @gmtime = gmtime $time;
59 7         14 $localtime[5] += 1900;
60 7         8 $gmtime[5] += 1900;
61 7         22 my $diff = Time::Local::timegm(@localtime)
62             - Time::Local::timegm(@gmtime);
63              
64 7 100       420 my $direc = $diff < 0 ? '-' : '+';
65 7         9 $diff = abs $diff;
66 7         20 my $tz_hr = int( $diff / 3600 );
67 7         15 my $tz_mi = int( $diff / 60 - $tz_hr * 60 );
68              
69 7         49 return ($direc, $tz_hr, $tz_mi);
70             }
71              
72             sub _format_date {
73 2     2   4 my ($local) = @_;
74              
75             sub {
76 3     3   659 my ($time) = @_;
77 3 100       9 $time = time unless defined $time;
78              
79 3 100       42 my ($sec, $min, $hour, $mday, $mon, $year, $wday)
80             = $local ? (localtime $time) : (gmtime $time);
81              
82 3         7 my $day = (qw[Sun Mon Tue Wed Thu Fri Sat])[$wday];
83 3         8 my $month = (qw[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec])[$mon];
84 3         4 $year += 1900;
85              
86 3 100       10 my ($direc, $tz_hr, $tz_mi) = $local ? _tz_diff($time)
87             : ('+', 0, 0);
88              
89 3         38 sprintf "%s, %d %s %d %02d:%02d:%02d %s%02d%02d",
90             $day, $mday, $month, $year, $hour, $min, $sec, $direc, $tz_hr, $tz_mi;
91             }
92 2         36 }
93              
94             BEGIN {
95 1     1   4 *email_date = _format_date(1);
96 1         3 *email_gmdate = _format_date(0);
97             };
98              
99             1;
100              
101             __END__
102              
103             =pod
104              
105             =encoding UTF-8
106              
107             =head1 NAME
108              
109             Email::Date::Format - produce RFC 2822 date strings
110              
111             =head1 VERSION
112              
113             version 1.007
114              
115             =head1 SYNOPSIS
116              
117             use Email::Date::Format qw(email_date);
118              
119             my $header = email_date($date->epoch);
120              
121             Email::Simple->create(
122             header => [
123             Date => $header,
124             ],
125             body => '...',
126             );
127              
128             =head1 DESCRIPTION
129              
130             This module provides a simple means for generating an RFC 2822 compliant
131             datetime string. (In case you care, they're not RFC 822 dates, because they
132             use a four digit year, which is not allowed in RFC 822.)
133              
134             =head1 PERL VERSION
135              
136             This library should run on perls released even a long time ago. It should work
137             on any version of perl released in the last five years.
138              
139             Although it may work on older versions of perl, no guarantee is made that the
140             minimum required version will not be increased. The version may be increased
141             for any reason, and there is no promise that patches will be accepted to lower
142             the minimum required perl.
143              
144             =head1 FUNCTIONS
145              
146             =head2 email_date
147              
148             my $date = email_date; # now
149             my $date = email_date( time - 60*60 ); # one hour ago
150              
151             C<email_date> accepts an epoch value, such as the one returned by C<time>.
152             It returns a string representing the date and time of the input, as
153             specified in RFC 2822. If no input value is provided, the current value
154             of C<time> is used.
155              
156             C<email_date> is exported only if requested.
157              
158             =head2 email_gmdate
159              
160             my $date = email_gmdate;
161              
162             C<email_gmdate> is identical to C<email_date>, but it will return a string
163             indicating the time in Greenwich Mean Time, rather than local time.
164              
165             C<email_gmdate> is exported only if requested.
166              
167             =head1 AUTHORS
168              
169             =over 4
170              
171             =item *
172              
173             Casey West
174              
175             =item *
176              
177             Ricardo SIGNES <cpan@semiotic.systems>
178              
179             =back
180              
181             =head1 CONTRIBUTORS
182              
183             =for stopwords bitcardbmw@lsmod.de Eric Sproul Ricardo Signes
184              
185             =over 4
186              
187             =item *
188              
189             bitcardbmw@lsmod.de <bitcardbmw@lsmod.de>
190              
191             =item *
192              
193             Eric Sproul <esproul@omniti.com>
194              
195             =item *
196              
197             Ricardo Signes <rjbs@semiotic.systems>
198              
199             =back
200              
201             =head1 COPYRIGHT AND LICENSE
202              
203             This software is copyright (c) 2004 by Casey West.
204              
205             This is free software; you can redistribute it and/or modify it under
206             the same terms as the Perl 5 programming language system itself.
207              
208             =cut