| 1 | 1 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | package Email::Date::Format 1.007; | ||||||||||
| 5 | # ABSTRACT: produce RFC 2822 date strings | ||||||||||
| 6 | |||||||||||
| 7 | 1 | our @EXPORT_OK = qw[email_date email_gmdate]; | |||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Exporter 5.57 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Time::Local (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | ▶ | my ($time) = @_; | ||||||||
|
Subroutine
_tz_diff: called CC=2 SCAR=6.9
| |||||||||||
| 56 | |||||||||||
| 57 | 7 | my @localtime = localtime $time; | |||||||||
| 58 | 7 | my @gmtime = gmtime $time; | |||||||||
| 59 | 7 | $localtime[5] += 1900; | |||||||||
| 60 | 7 | $gmtime[5] += 1900; | |||||||||
| 61 | 7 | my $diff = Time::Local::timegm(@localtime) | |||||||||
| 62 | - Time::Local::timegm(@gmtime); | ||||||||||
| 63 | |||||||||||
| 64 | 7 | ▶ | my $direc = $diff < 0 ? '-' : '+'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 7 | $diff = abs $diff; | |||||||||
| 66 | 7 | my $tz_hr = int( $diff / 3600 ); | |||||||||
| 67 | 7 | my $tz_mi = int( $diff / 60 - $tz_hr * 60 ); | |||||||||
| 68 | |||||||||||
| 69 | 7 | return ($direc, $tz_hr, $tz_mi); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | sub _format_date { | ||||||||||
| 73 | 2 | ▶ | my ($local) = @_; | ||||||||
|
Subroutine
_format_date: called CC=1 SCAR=0.0
| |||||||||||
| 74 | |||||||||||
| 75 | sub { | ||||||||||
| 76 | 3 | ▶ | my ($time) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 77 | 3 | ▶ | $time = time unless defined $time; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | 3 | ▶ | my ($sec, $min, $hour, $mday, $mon, $year, $wday) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | = $local ? (localtime $time) : (gmtime $time); | ||||||||||
| 81 | |||||||||||
| 82 | 3 | my $day = (qw[Sun Mon Tue Wed Thu Fri Sat])[$wday]; | |||||||||
| 83 | 3 | my $month = (qw[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec])[$mon]; | |||||||||
| 84 | 3 | $year += 1900; | |||||||||
| 85 | |||||||||||
| 86 | 3 | ▶ | my ($direc, $tz_hr, $tz_mi) = $local ? _tz_diff($time) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | : ('+', 0, 0); | ||||||||||
| 88 | |||||||||||
| 89 | 3 | 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 | } | |||||||||
| 93 | |||||||||||
| 94 | BEGIN { | ||||||||||
| 95 | 1 | ▶ | *email_date = _format_date(1); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 96 | 1 | *email_gmdate = _format_date(0); | |||||||||
| 97 | }; | ||||||||||
| 98 | |||||||||||
| 99 | 1 | 1; | |||||||||
| 100 | |||||||||||
| 101 | __END__ | ||||||||||