| 1 | package Time::dt; | ||||||||||
| 2 | $VERSION = v0.0.1; | ||||||||||
| 3 | |||||||||||
| 4 | # Copyright (C) 2013 Eric L. Wilhelm | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | Time::dt - date and time succinctly | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | use Time::dt; | ||||||||||
| 17 | |||||||||||
| 18 | say "iso timestamp: ", dt; | ||||||||||
| 19 | say "stringification shmingification: ", dt->dt; | ||||||||||
| 20 | |||||||||||
| 21 | Time::dt::strptime($timestamp, "%Y-%m-%d %H:%M:%S"); # epoch | ||||||||||
| 22 | |||||||||||
| 23 | =cut | ||||||||||
| 24 | |||||||||||
| 25 | 1 | ▶ | BEGIN {require Exporter; *import = \&Exporter::import}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | our @EXPORT = qw(dt); | ||||||||||
| 27 | our @EXPORT_OK = qw(read_dt strptime); | ||||||||||
| 28 | |||||||||||
| 29 | { | ||||||||||
| 30 | package Time::Piece::Tidy; | ||||||||||
| 31 | 1 | ▶ | use base 'Time::Piece'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | ▶ | use overload '""' => \&dt; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 0 | ▶ | sub dt { | ||||||||
|
Subroutine
dt: not called CC=2 SCAR=17.9
| |||||||||||
| 34 | 0 | my $self = shift; | |||||||||
| 35 | 0 | my $fmt = '%Y-%m-%d %H:%M:%S'; | |||||||||
| 36 | 0 | ▶ | $fmt .= ' %Z' if ($self->[10]); # XXX this is bad or wrong | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | $self->strftime($fmt); | |||||||||
| 38 | } | ||||||||||
| 39 | *cdate = \&dt; | ||||||||||
| 40 | |||||||||||
| 41 | =head2 zdt | ||||||||||
| 42 | |||||||||||
| 43 | String format with timezone. | ||||||||||
| 44 | |||||||||||
| 45 | print $dt->zdt('US/Eastern'); | ||||||||||
| 46 | |||||||||||
| 47 | =cut | ||||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | sub zdt { | ||||||||
|
Subroutine
zdt: not called CC=1 SCAR=6.9
| |||||||||||
| 50 | 0 | my ($self, $tz) = @_; | |||||||||
| 51 | |||||||||||
| 52 | 0 | my $e = $self->epoch; | |||||||||
| 53 | 0 | local $ENV{TZ} = $tz; | |||||||||
| 54 | 0 | ref($self)->new($e)->dt; | |||||||||
| 55 | } # zdt ################################################################ | ||||||||||
| 56 | |||||||||||
| 57 | } # end package | ||||||||||
| 58 | ######################################################################## | ||||||||||
| 59 | |||||||||||
| 60 | =head1 Functions | ||||||||||
| 61 | |||||||||||
| 62 | =head2 dt | ||||||||||
| 63 | |||||||||||
| 64 | my $dt = dt(time); | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub dt { | ||||||||
|
Subroutine
dt: not called CC=1 SCAR=6.9
| |||||||||||
| 69 | 0 | my $t = shift; | |||||||||
| 70 | 0 | Time::Piece::Tidy->new($t); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | =head2 read_dt | ||||||||||
| 74 | |||||||||||
| 75 | my $dt = read_dt( | ||||||||||
| 76 | |||||||||||
| 77 | =cut | ||||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | sub read_dt { | ||||||||
|
Subroutine
read_dt: not called CC=1 SCAR=6.9
| |||||||||||
| 80 | 0 | my $ts = shift; | |||||||||
| 81 | 0 | dt(strptime($ts)); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | =head2 strptime | ||||||||||
| 85 | |||||||||||
| 86 | Returns the epoch seconds for a given parsed time. | ||||||||||
| 87 | |||||||||||
| 88 | my $t = strptime($string, $format); | ||||||||||
| 89 | |||||||||||
| 90 | =cut | ||||||||||
| 91 | |||||||||||
| 92 | { | ||||||||||
| 93 | my %zones = ( | ||||||||||
| 94 | PST => '-0800', | ||||||||||
| 95 | PDT => '-0700', | ||||||||||
| 96 | MST => '-0700', | ||||||||||
| 97 | MDT => '-0600', | ||||||||||
| 98 | CST => '-0600', | ||||||||||
| 99 | CDT => '-0500', | ||||||||||
| 100 | EST => '-0500', | ||||||||||
| 101 | EDT => '-0400', | ||||||||||
| 102 | AST => '-0400', | ||||||||||
| 103 | ALST => '-0900', | ||||||||||
| 104 | ALDT => '-0800', | ||||||||||
| 105 | HST => '-1000', | ||||||||||
| 106 | ); | ||||||||||
| 107 | my $zk = join('|', keys %zones); | ||||||||||
| 108 | sub strptime { | ||||||||||
| 109 | my ($string, $format) = @_; | ||||||||||
| 110 | $format ||= '%Y-%m-%d %H:%M:%S %Z'; | ||||||||||
| 111 | |||||||||||
| 112 | my $is_local; | ||||||||||
| 113 | if($format =~ s/%Z$/%z/) { | ||||||||||
| 114 | $string =~ s/ ($zk)$/ $zones{$1}/; # hack to parse named zones | ||||||||||
| 115 | } | ||||||||||
| 116 | elsif($format =~ m/%z$/) { | ||||||||||
| 117 | $string =~ s/:(\d\d)$/$1/; # fix broken tz | ||||||||||
| 118 | } | ||||||||||
| 119 | else { | ||||||||||
| 120 | $is_local = 1; # no zone means localtime | ||||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | my @vals = eval {Time::Piece::_strptime($string, $format)}; | ||||||||||
| 124 | die "$@ $string - $format" if $@; | ||||||||||
| 125 | |||||||||||
| 126 | |||||||||||
| 127 | return $is_local | ||||||||||
| 128 | ? Time::Local::timelocal(@vals[0..9]) | ||||||||||
| 129 | : Time::Local::timegm(@vals[0..9]); | ||||||||||
| 130 | }} | ||||||||||
| 131 | |||||||||||
| 132 | =head1 AUTHOR | ||||||||||
| 133 | |||||||||||
| 134 | Eric Wilhelm @ <ewilhelm at cpan dot org> | ||||||||||
| 135 | |||||||||||
| 136 | http://scratchcomputing.com/ | ||||||||||
| 137 | |||||||||||
| 138 | =head1 BUGS | ||||||||||
| 139 | |||||||||||
| 140 | If you found this module on CPAN, please report any bugs or feature | ||||||||||
| 141 | requests through the web interface at L<http://rt.cpan.org>. I will be | ||||||||||
| 142 | notified, and then you'll automatically be notified of progress on your | ||||||||||
| 143 | bug as I make changes. | ||||||||||
| 144 | |||||||||||
| 145 | If you pulled this development version from my /svn/, please contact me | ||||||||||
| 146 | directly. | ||||||||||
| 147 | |||||||||||
| 148 | =head1 COPYRIGHT | ||||||||||
| 149 | |||||||||||
| 150 | Copyright (C) 2013 Eric L. Wilhelm, All Rights Reserved. | ||||||||||
| 151 | |||||||||||
| 152 | =head1 NO WARRANTY | ||||||||||
| 153 | |||||||||||
| 154 | Absolutely, positively NO WARRANTY, neither express or implied, is | ||||||||||
| 155 | offered with this software. You use this software at your own risk. In | ||||||||||
| 156 | case of loss, no person or entity owes you anything whatsoever. You | ||||||||||
| 157 | have been warned. | ||||||||||
| 158 | |||||||||||
| 159 | =head1 LICENSE | ||||||||||
| 160 | |||||||||||
| 161 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 162 | under the same terms as Perl itself. | ||||||||||
| 163 | |||||||||||
| 164 | =cut | ||||||||||
| 165 | |||||||||||
| 166 | # vi:ts=2:sw=2:et:sta | ||||||||||
| 167 | 1; | ||||||||||