DateTimeX-Lite
view release on metacpan or search on metacpan
lib/DateTimeX/Lite.pm view on Meta::CPAN
{
my $mod = DateTimeX::Lite::LeapSecond::day_length( $utc_rd_days - 1 ) - SECONDS_PER_DAY;
unless ( $mod == 0 )
{
$self->{utc_rd_secs} -= $mod;
DateTimeX::Lite::Util::normalize_seconds($self);
}
}
}
}
sub _calc_local_rd
{
my $self = shift;
delete $self->{local_c};
# We must short circuit for UTC times or else we could end up with
# loops between DateTime.pm and DateTimeX::Lite::TimeZone
if ( $self->{tz}->is_utc || $self->{tz}->is_floating )
{
$self->{local_rd_days} = $self->{utc_rd_days};
$self->{local_rd_secs} = $self->{utc_rd_secs};
}
else
{ my $offset = $self->offset;
$self->{local_rd_days} = $self->{utc_rd_days};
$self->{local_rd_secs} = $self->{utc_rd_secs} + $offset;
# intentionally ignore leap seconds here
DateTimeX::Lite::Util::normalize_tai_seconds( $self->{local_rd_days}, $self->{local_rd_secs} );
$self->{local_rd_secs} += $self->{offset_modifier};
}
$self->_calc_local_components;
}
sub _calc_local_components
{
my $self = shift;
@{ $self->{local_c} }{ qw( year month day day_of_week
day_of_year quarter day_of_quarter) } =
DateTimeX::Lite::Util::rd2ymd( $self->{local_rd_days}, 1 );
@{ $self->{local_c} }{ qw( hour minute second ) } =
DateTimeX::Lite::Util::seconds_as_components
( $self->{local_rd_secs}, $self->{utc_rd_secs}, $self->{offset_modifier} );
}
sub from_object {
my ($class, %p) = @_;
my $object = delete $p{object};
my ( $rd_days, $rd_secs, $rd_nanosecs ) = $object->utc_rd_values;
# A kludge because until all calendars are updated to return all
# three values, $rd_nanosecs could be undef
$rd_nanosecs ||= 0;
# This is a big hack to let _seconds_as_components operate naively
# on the given value. If the object _is_ on a leap second, we'll
# add that to the generated seconds value later.
my $leap_seconds = 0;
if ( $object->can('time_zone') && ! $object->time_zone->is_floating
&& $rd_secs > 86399 && $rd_secs <= DateTimeX::Lite::LeapSecond::day_length($rd_days) )
{
$leap_seconds = $rd_secs - 86399;
$rd_secs -= $leap_seconds;
}
my %args;
@args{ qw( year month day ) } = DateTimeX::Lite::Util::rd2ymd($rd_days);
@args{ qw( hour minute second ) } =
DateTimeX::Lite::Util::seconds_as_components($rd_secs);
$args{nanosecond} = $rd_nanosecs;
$args{second} += $leap_seconds;
my $new = $class->new( %p, %args, time_zone => 'UTC' );
if ( $object->can('time_zone') )
{
$new->set_time_zone( $object->time_zone );
}
else
{
$new->set_time_zone( 'floating' );
}
return $new;
}
sub last_day_of_month {
my ($class, %p) = @_;
if ($p{month} > 12 || $p{month} < 1) {
Carp::croak(qq|The 'month' parameter ("$p{month}") to DateTimeX::Lite::last_day_of_month did not pass the 'is between 1 and 12' callback|);
}
return $class->new(%p, day => DateTimeX::Lite::Util::month_length($p{year}, $p{month}));
}
sub offset { $_[0]->{tz}->offset_for_datetime( $_[0] ) }
sub _offset_for_local_datetime { $_[0]->{tz}->offset_for_local_datetime( $_[0] ) }
sub nanosecond { $_[0]->{rd_nanosecs} }
sub fractional_second { $_[0]->second + $_[0]->nanosecond / MAX_NANOSECONDS }
sub millisecond { _round( $_[0]->{rd_nanosecs} / 1000000 ) }
sub microsecond { _round( $_[0]->{rd_nanosecs} / 1000 ) }
sub _round
{
my $val = shift;
lib/DateTimeX/Lite.pm view on Meta::CPAN
1;
__END__
=head1 NAME
DateTimeX::Lite - A Low Calorie DateTime
=head1 SYNOPSIS
use DateTimeX::Lite;
my $dt = DateTimeX::Lite->new(year => 2008, month => 12, day => 1);
$dt->year;
$dt->month;
$dt->day;
$dt->hour;
$dt->minuute;
$dt->second;
# Arithmetic doesn't come with DateTimeX::Lite by default
use DateTimeX::Lite qw(Arithmetic);
$dt->add( DateTimeX::Lite::Duration->new(days => 5) );
# Strftime doesn't come with DateTimeX::Lite by default
use DateTimeX::Lite qw(Strftime);
$dt->strftime('%Y %m %d');
# ZeroBase accessors doesn't come with DateTimeX::Lite by default
use DateTimeX::Lite qw(ZeroBase);
$dt->month_0;
# Overloading is disabled by default
use DateTimeX::Lite qw(Overload);
print "the date is $dt\n";
if ($dt1 < $dt2) {
print "dt1 is less than dt2\n";
}
=head1 DESCRIPTION
This is a lightweight version of DateTime.pm, which requires no XS, and aims to be light(er) than the original, for a given B<subset> of the problems that the original DateTime.pm can solve.
The idea is to encourage light users to use DateTime compatible API, while adapting to realistic environments (such as people without access to C compilers, people on rental servers who can't install modules, people who needs to convince pointy-haire...
Please make no mistake: B<THIS IS NOT A REPLACEMENT FOR Datetime.pm>. I will try to keep up with DateTime.pm, but DateTime.pm is the referece implementation. This is just stripped down version.
Please also note that internally, this module is a complete rip-off of the original DateTime.pm module. The author simply copied and pasted about 90% of the code, tweaked it and repackaged it. All credits go to the original DateTime.pm's authors.
=head1 RATIONALE
The aim of this module is as follows:
=over 4
=item (1) Target those who do not need the full feature of DateTime.pm.
In particular, I'm thinking of people who wants to simply grab a date, maybe do some date arithmetic on it, and print the year/month/date or store those values somewhere. These people do not use advanced date logic, sets, or calendars.
=item (2) Target the newbies who are afraid of XS code.
Let's face it, /we/ the developers know how to deal with XS. But we can't expect that out of everybody. DateTime.pm doesn't require XS, but to get decent performance it's sort of a requirement. We do our best to get there without XS.
=item (3) Get better performance.
In particular,
* Reduce the amount of memory consumed, and
* Reduce the time it takes to load the module
Again, /we/ know why it's worth it to use DateTime. Some people don't, and will judge DateTime (and worse yet, maybe perl itself) unusable simply because it takes more memory to load DateTime. We want to avoid that.
=item (4) Make it easy to install on rental servers.
This also ties into (2). No XS code, becuse compilers may not be available, or people simply wouldn't know how to use compilers.
If we can simply copy the DateTimeX::Lite files over via FTP instead of 'make install', that's even better.
=item (5) Bundle everything in one distribution, including timezones and locales
This goes with (4). We like time zones and locales. However, we would like to limit the number of dependencies. It would be even better if we can choose which locales and timezones to install.
=item (6) Be compatible enough with DateTime.pm
While given all of the above, we would like to leave a way for users to easily (relatively speaking) switch back to DateTime.pm, when they so choose to. Hence, the API needs to remain mostly compatible.
=back
=head1 COMPATIBILITY WITH DateTime.pm
As stated elsewhere, DateTimeX::Lite does not intend to be a drop-in replacement for DateTime.pm.
You should not expect other DateTime::* modules (such as Format and Calendar) to work with it. It might, but we won't guarantee it.
We feel that if you use the extended features of the DateTime family, you should be using the original DateTime.pm
=head2 NOTABLE DIFFERENCES
DateTimeX::Lite tries to be as compatible as possible with DateTime.pm, but there are a few places it deliberately changed from DateTime.pm. Some notable differences from DateTime.pm are as follows
=over 4
=item Non-essential methods are loaded on demand
For example, A lot of times you don't even need to do date time arithmetic. These methods are separated out onto a different file, so you need to load it on demand. To load, include "Arithmetic" in the use line.
use DateTimeX::Lite qw(Arithmetic);
Similarly, strftime() imposes a lot of code on DateTime. So if ymd(), iso8601() or the like is sufficient, it would be best not to load it. To load, include "Strftime" in the use line.
use DateTimeX::Lite qw(Strftime);
A lot of methods in original DateTime have aliases. They are not loaded unless
you ask for them:
use DateTimeX::Lite qw(Aliases);
Zero-based accessors are also taken out of the core DateTimeX::Lite code.
( run in 0.982 second using v1.01-cache-2.11-cpan-39bf76dae61 )