File Coverage

blib/lib/Email/Date/Format.pm
Criterion Covered Total %
statement 34 34 100.0
branch 6 6 100.0
condition 2 3 66.6
subroutine 8 8 100.0
pod n/a
total 50 51 98.0


line stmt bran cond sub pod time code
1 1     1   69765 use v5.12.0;
  1         18  
2 1     1   6 use warnings;
  1         1  
  1         73  
3             package Email::Date::Format 1.008;
4             # ABSTRACT: produce RFC 2822 date strings
5              
6             our @EXPORT_OK = qw[email_date email_gmdate];
7              
8 1     1   7 use Exporter 5.57 'import';
  1         19  
  1         59  
9 1     1   550 use Time::Local 1.27 ();
  1         2630  
  1         322  
10              
11             #pod =head1 SYNOPSIS
12             #pod
13             #pod use Email::Date::Format qw(email_date);
14             #pod
15             #pod my $header = email_date($date->epoch);
16             #pod
17             #pod Email::Simple->create(
18             #pod header => [
19             #pod Date => $header,
20             #pod ],
21             #pod body => '...',
22             #pod );
23             #pod
24             #pod =head1 DESCRIPTION
25             #pod
26             #pod This module provides a simple means for generating an RFC 2822 compliant
27             #pod datetime string. (In case you care, they're not RFC 822 dates, because they
28             #pod use a four digit year, which is not allowed in RFC 822.)
29             #pod
30             #pod =func email_date
31             #pod
32             #pod my $date = email_date; # now
33             #pod my $date = email_date( time - 60*60 ); # one hour ago
34             #pod
35             #pod C accepts an epoch value, such as the one returned by C
36             #pod It returns a string representing the date and time of the input, as
37             #pod specified in RFC 2822. If no input value is provided, the current value
38             #pod of C
39             #pod
40             #pod C is exported only if requested.
41             #pod
42             #pod =func email_gmdate
43             #pod
44             #pod my $date = email_gmdate;
45             #pod
46             #pod C is identical to C, but it will return a string
47             #pod indicating the time in Greenwich Mean Time, rather than local time.
48             #pod
49             #pod C is exported only if requested.
50             #pod
51             #pod =cut
52              
53             sub _tz_diff {
54 7     7   602 my ($time) = @_;
55              
56 7         119 my @localtime = localtime $time;
57 7         28 my @gmtime = gmtime $time;
58 7         16 $localtime[5] += 1900;
59 7         11 $gmtime[5] += 1900;
60 7         26 my $diff = Time::Local::timegm_modern(@localtime)
61             - Time::Local::timegm_modern(@gmtime);
62              
63 7 100       455 my $direc = $diff < 0 ? '-' : '+';
64 7         11 $diff = abs $diff;
65 7         11 my $tz_hr = int( $diff / 3600 );
66 7         15 my $tz_mi = int( $diff / 60 - $tz_hr * 60 );
67              
68 7         45 return ($direc, $tz_hr, $tz_mi);
69             }
70              
71             sub _format_date {
72 2     2   5 my ($local) = @_;
73              
74             sub {
75 3     3   879 my ($time) = @_;
76 3   66     14 $time //= time;
77              
78 3 100       46 my ($sec, $min, $hour, $mday, $mon, $year, $wday)
79             = $local ? (localtime $time) : (gmtime $time);
80              
81 3         11 my $day = (qw[Sun Mon Tue Wed Thu Fri Sat])[$wday];
82 3         6 my $month = (qw[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec])[$mon];
83 3         7 $year += 1900;
84              
85 3 100       11 my ($direc, $tz_hr, $tz_mi) = $local ? _tz_diff($time)
86             : ('+', 0, 0);
87              
88 3         56 sprintf "%s, %d %s %d %02d:%02d:%02d %s%02d%02d",
89             $day, $mday, $month, $year, $hour, $min, $sec, $direc, $tz_hr, $tz_mi;
90             }
91 2         60 }
92              
93             BEGIN {
94 1     1   5 *email_date = _format_date(1);
95 1         3 *email_gmdate = _format_date(0);
96             };
97              
98             1;
99              
100             __END__