Skip to content

Commit

Permalink
Eliminate the need to globally override gmtime() and localtime()
Browse files Browse the repository at this point in the history
  • Loading branch information
schwern committed Jul 8, 2009
1 parent 2c0314a commit 6d174a9
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions lib/perl5i/DateTime.pm
Expand Up @@ -5,31 +5,50 @@ package perl5i::DateTime;
use 5.010;
use strict;
use warnings;
use Time::y2038;


## no critic (Subroutines::ProhibitSubroutinePrototypes)
sub dt_gmtime (;$) {
my $time = @_ ? shift : time;
return CORE::gmtime($time) if wantarray;
return gmtime($time) if wantarray;

my($sec, $min, $hour, $mday, $mon, $year) = gmtime($time);
$mon++;
$year += 1900;

require DateTime;
return DateTime::y2038->from_epoch(
epoch => $time + 0,
formatter => "DateTime::Format::CTime"
return DateTime::y2038->new(
year => $year,
month => $mon,
day => $mday,
hour => $hour,
minute => $min,
second => $sec,
formatter => "DateTime::Format::CTime"
);
}


## no critic (Subroutines::ProhibitSubroutinePrototypes)
sub dt_localtime (;$) {
my $time = @_ ? shift : time;
return CORE::localtime($time) if wantarray;
return localtime($time) if wantarray;

my($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
$mon++;
$year += 1900;

require DateTime;
return DateTime::y2038->from_epoch(
epoch => $time + 0,
time_zone => "local",
formatter => "DateTime::Format::CTime"
return DateTime::y2038->new(
year => $year,
month => $mon,
day => $mday,
hour => $hour,
minute => $min,
second => $sec,
time_zone => "local",
formatter => "DateTime::Format::CTime"
);
}

Expand All @@ -48,24 +67,11 @@ sub dt_time () {


{

package DateTime::y2038;

# Don't load DateTime until we need it.
our @ISA = qw(DateTime);

# Override gmtime and localtime with straight emulations
# so we can override it later.
{
*CORE::GLOBAL::gmtime = sub (;$) {
return @_ ? CORE::gmtime( $_[0] ) : CORE::gmtime();
};

*CORE::GLOBAL::localtime = sub (;$) {
return @_ ? CORE::localtime( $_[0] ) : CORE::localtime();
};
}

sub from_epoch {
my $class = shift;

Expand Down

0 comments on commit 6d174a9

Please sign in to comment.