DateTime-Lite

 view release on metacpan or  search on metacpan

lib/DateTime/Lite/TimeZone.pm  view on Meta::CPAN

    return(0) if( $self->{is_floating} || $self->{is_utc} );
    return( $self->{fixed_offset} ) if( exists( $self->{fixed_offset} ) );
    if( !defined( $dt ) )
    {
        return( $self->error( "No DateTime::Lite or DateTime object was provided." ) );
    }
    elsif( !Scalar::Util::blessed( $dt ) )
    {
        return( $self->error( "The object provided (", overload::StrVal( $dt ), ") is not an object." ) );
    }
    elsif( !$dt->can( 'local_rd_as_seconds' ) )
    {
        return( $self->error( "The object provided (", overload::StrVal( $dt ), ") does not support the method 'local_rd_as_seconds'." ) );
    }
    my $span = $self->_lookup_span_local( $dt->local_rd_as_seconds ) || return(0);
    return( $span->{offset} + 0 );
}

sub pass_error
{
    my $self = shift( @_ );
    my $pack = ref( $self ) || $self;
    my $opts = {};
    my( $err, $class, $code );
    no strict 'refs';
    if( scalar( @_ ) )
    {
        # Either an hash defining a new error and this will be passed along to error(); or
        # an hash with a single property: { class => 'Some::ExceptionClass' }
        if( scalar( @_ ) == 1 && ref( $_[0] ) eq 'HASH' )
        {
            $opts = $_[0];
        }
        else
        {
            if( scalar( @_ ) > 1 && ref( $_[-1] ) eq 'HASH' )
            {
                $opts = pop( @_ );
            }
            $err = $_[0];
        }
    }
    $err = $opts->{error} if( !defined( $err ) && CORE::exists( $opts->{error} ) && defined( $opts->{error} ) && CORE::length( $opts->{error} ) );
    # We set $class only if the hash provided is a one-element hash and not an error-defining hash
    $class = $opts->{class} if( CORE::exists( $opts->{class} ) && defined( $opts->{class} ) && CORE::length( $opts->{class} ) );
    $code  = $opts->{code} if( CORE::exists( $opts->{code} ) && defined( $opts->{code} ) && CORE::length( $opts->{code} ) );

    # called with no argument, most likely from the same class to pass on an error 
    # set up earlier by another method; or
    # with an hash containing just one argument class => 'Some::ExceptionClass'
    if( !defined( $err ) && ( !scalar( @_ ) || defined( $class ) ) )
    {
        # $error is a previous erro robject
        my $error = ref( $self ) ? $self->{error} : length( ${ $pack . '::ERROR' } ) ? ${ $pack . '::ERROR' } : undef;
        if( !defined( $error ) )
        {
            warn( "No error object provided and no previous error set either! It seems the previous method call returned a simple undef" );
        }
        else
        {
            $err = ( defined( $class ) ? bless( $error => $class ) : $error );
            $err->code( $code ) if( defined( $code ) );
        }
    }
    elsif( defined( $err ) && 
           Scalar::Util::blessed( $err ) && 
           ( scalar( @_ ) == 1 || 
             ( scalar( @_ ) == 2 && defined( $class ) ) 
           ) )
    {
        $self->{error} = ${ $pack . '::ERROR' } = ( defined( $class ) ? bless( $err => $class ) : $err );
        $self->{error}->code( $code ) if( defined( $code ) && $self->{error}->can( 'code' ) );

        if( $self->{fatal} || ( defined( ${"${pack}\::FATAL_EXCEPTIONS"} ) && ${"${pack}\::FATAL_EXCEPTIONS"} ) )
        {
            die( $self->{error} );
        }
    }
    # If the error provided is not an object, we call error to create one
    else
    {
        return( $self->error( @_ ) );
    }

    if( want( 'OBJECT' ) )
    {
        rreturn( DateTime::Lite::NullObject->new );
    }
    return;
}

sub resolve_abbreviation
{
    my $self = shift( @_ );
    my $abbr = shift( @_ ) ||
        return( $self->error( "No timezone abbreviation was provided." ) );
    my %opts = @_;

    local $@;
    my $sth;
    my $has_offset = (
        defined( $opts{utc_offset} ) &&
        Scalar::Util::looks_like_number( $opts{utc_offset} )
    ) ? 1 : 0;
    my $extended = $opts{extended} ? 1 : 0;

    # Build the dynamic WHERE skeleton for the IANA types query.
    # Based on Locale::Unicode::Data::_fetch_all(): each filter value may be prefixed
    # with a comparison operator (<, <=, >, >=, =, !=).
    # When period is an array ref, each element adds one AND condition on
    # MAX(tr.trans_time). When period is the special string 'current', the query
    # is restricted to the zone's last transition (MAX = most recent) and requires
    # that last transition to be in the past, effectively returning only zones that
    # still use the abbreviation today.
    my $op_map = { '=' => 'IS', '!=' => 'IS NOT' };

    # Conditions on MAX(tr.trans_time) built from the 'period' option.
    # Each element of @period_skels is a HAVING clause fragment.
    # @period_key_parts accumulates tokens for the cache key; they must distinguish
    # ISO date strings from raw epoch integers since each uses a different SQL
    # placeholder form ('e' = epoch, 'd' = ISO date).
    my @period_skels     = ();
    my @period_values    = ();
    my @period_key_parts = ();
    my $current_special  = 0;

    if( defined( $opts{period} ) )
    {
        my @period_items = ref( $opts{period} ) eq 'ARRAY'
            ? @{$opts{period}}
            : ( $opts{period} );



( run in 0.693 second using v1.01-cache-2.11-cpan-364913b4093 )