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   68133 use 5.006;
  1         14  
2 1     1   5 use strict;
  1         2  
  1         20  
3 1     1   5 use warnings;
  1         1  
  1         85  
4             package Email::Date::Format 1.006;
5             # ABSTRACT: produce RFC 2822 date strings
6              
7             our @EXPORT_OK = qw[email_date email_gmdate];
8              
9 1     1   14 use Exporter 5.57 'import';
  1         17  
  1         30  
10 1     1   552 use Time::Local ();
  1         2272  
  1         310  
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 accepts an epoch value, such as the one returned by C
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
40             #pod
41             #pod C 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 is identical to C, but it will return a string
48             #pod indicating the time in Greenwich Mean Time, rather than local time.
49             #pod
50             #pod C is exported only if requested.
51             #pod
52             #pod =cut
53              
54             sub _tz_diff {
55 7     7   821 my ($time) = @_;
56              
57 7         117 my @localtime = localtime $time;
58 7         58 my @gmtime = gmtime $time;
59 7         16 $localtime[5] += 1900;
60 7         10 $gmtime[5] += 1900;
61 7         22 my $diff = Time::Local::timegm(@localtime)
62             - Time::Local::timegm(@gmtime);
63              
64 7 100       357 my $direc = $diff < 0 ? '-' : '+';
65 7         9 $diff = abs $diff;
66 7         24 my $tz_hr = int( $diff / 3600 );
67 7         19 my $tz_mi = int( $diff / 60 - $tz_hr * 60 );
68              
69 7         42 return ($direc, $tz_hr, $tz_mi);
70             }
71              
72             sub _format_date {
73 2     2   5 my ($local) = @_;
74              
75             sub {
76 3     3   628 my ($time) = @_;
77 3 100       12 $time = time unless defined $time;
78              
79 3 100       52 my ($sec, $min, $hour, $mday, $mon, $year, $wday)
80             = $local ? (localtime $time) : (gmtime $time);
81              
82 3         11 my $day = (qw[Sun Mon Tue Wed Thu Fri Sat])[$wday];
83 3         7 my $month = (qw[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec])[$mon];
84 3         7 $year += 1900;
85              
86 3 100       10 my ($direc, $tz_hr, $tz_mi) = $local ? _tz_diff($time)
87             : ('+', 0, 0);
88              
89 3         42 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         39 }
93              
94             BEGIN {
95 1     1   6 *email_date = _format_date(1);
96 1         3 *email_gmdate = _format_date(0);
97             };
98              
99             1;
100              
101             __END__