App-MtAws
view release on metacpan or search on metacpan
lib/App/MtAws/DateTime.pm view on Meta::CPAN
# mt-aws-glacier - Amazon Glacier sync client
# Copyright (C) 2012-2014 Victor Efimov
# http://mt-aws.com (also http://vs-dev.com) vs@vs-dev.com
# License: GPLv3
#
# This file is part of "mt-aws-glacier"
#
# mt-aws-glacier is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# mt-aws-glacier is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
package App::MtAws::DateTime;
our $VERSION = '1.120';
use strict;
use warnings;
use utf8;
use POSIX;
use Time::Local;
use App::MtAws::Utils;
use Exporter 'import';
our @EXPORT = qw/epoch_to_iso8601 iso8601_to_epoch/;
#
# Implementing this as I don't want to have non-core dependencies
#
use constant SEC_PER_DAY => 86400;
use constant YEARS_PER_CENTURY => 100;
use constant DAYS_PER_YEAR => 365;
sub is_leap
{
($_[0] % 400 ==0) || ( ($_[0] % 100 != 0) && ($_[0] % 4 == 0) )
}
our %_leap_cache;
sub number_of_leap_years
{
my ($y1, $y2, $m) = @_;
$_leap_cache{$y1,$y2, ($m < 3 ? '0' : '1') } ||= do {
my $cnt = 0;
for ($y1+1..$y2-1) {
$cnt++ if is_leap($_);
}
$cnt++ if ($m < 3 ) && is_leap($y1);
$cnt++ if ($m >= 3) && is_leap($y2);
$cnt;
}
}
# allowed range Y1000 - Y9999
# should work with Y2038 dates if underlying OS supports 64bit time (otherwise we don't need such conversion in
# mt-aws-glacier)
sub epoch_to_iso8601
{
my ($time) = @_;
return if $time < -30610224000 || $time > 253402300799;
strftime("%Y%m%dT%H%M%SZ", gmtime($time));
}
our %_year_month_shift;
# allowed range Y1000 - Y9999
# should work with Y2038 dates always
sub iso8601_to_epoch
{
( run in 0.966 second using v1.01-cache-2.11-cpan-0d23b851a93 )