DateTime-Locale-FromCLDR

 view release on metacpan or  search on metacpan

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

##----------------------------------------------------------------------------
## Unicode Locale Identifier - ~/lib/m
## Version v0.8.2
## Copyright(c) 2026 DEGUEST Pte. Ltd.
## Author: Jacques Deguest <jack@deguest.jp>
## Created 2024/07/07
## Modified 2026/04/16
## All rights reserved
## 
## 
## This program is free software; you can redistribute  it  and/or  modify  it
## under the same terms as Perl itself.
##----------------------------------------------------------------------------
package DateTime::Locale::FromCLDR;
BEGIN
{
    use strict;
    use warnings;
    use warnings::register;
    use vars qw(
        $ERROR $VERSION $DEBUG $EMPTY_SET $FATAL_EXCEPTIONS
        $TZ_DST_CACHE
    );
    use overload (
        '""'    => 'as_string',
        bool    => sub{1},
        fallback => 1,
    );
    use utf8;
    use Locale::Unicode;
    use Locale::Unicode::Data;
    use Scalar::Util ();
    use Wanted;
    # "If a given short metazone form is known NOT to be understood in a given locale and the parent locale has this value such that it would normally be inherited, the inheritance of this value can be explicitly disabled by use of the 'no inheritanc...
    # <https://unicode.org/reports/tr35/tr35-dates.html#Metazone_Names>
    our $EMPTY_SET = "∅∅∅";
    our $VERSION = 'v0.8.2';
};

use strict;
use warnings;

sub new
{
    my $this = shift( @_ );
    my $self = bless( { calendar => 'gregorian' } => ( ref( $this ) || $this ) );
    my $locale = shift( @_ ) ||
        return( $self->error( "No locale was provided." ) );
    $locale = $self->_locale_object( $locale ) ||
        return( $self->pass_error );
    my $core = $locale->core;
    unless( $core eq $locale )
    {
        $locale = Locale::Unicode->new( $core ) ||
            return( $self->pass_error( Locale::Unicode->error ) );
    }
    $self->{default_date_format_length} = 'medium';
    $self->{default_time_format_length} = 'medium';
    $self->{fatal} = ( $FATAL_EXCEPTIONS // 0 );
    my @args = @_;
    if( scalar( @args ) == 1 &&
        defined( $args[0] ) &&
        ref( $args[0] ) eq 'HASH' )
    {
        my $opts = shift( @args );
        @args = %$opts;
    }
    elsif( ( scalar( @args ) % 2 ) )
    {
        return( $self->error( sprintf( "Uneven number of parameters provided (%d). Should receive key => value pairs. Parameters provided are: %s", scalar( @args ), join( ', ', @args ) ) ) );
    }

    for( my $i = 0; $i < scalar( @args ); $i += 2 )
    {
        if( $args[$i] eq 'fatal' )
        {
            $self->{fatal} = $args[$i + 1];
            last;
        }
    }

    # Then, if the user provided with an hash or hash reference of options, we apply them
    for( my $i = 0; $i < scalar( @args ); $i++ )
    {
        my $name = $args[ $i ];
        my $val  = $args[ ++$i ];
        my $meth = $self->can( $name );
        if( !defined( $meth ) )
        {
            return( $self->error( "Unknown method \"${meth}\" provided for locale \"${locale}\"." ) );
        }
        elsif( !defined( $meth->( $self, $val ) ) )
        {
            if( defined( $val ) && $self->error )
            {
                return( $self->pass_error );
            }
        }
    }
    $self->{locale} = $locale;
    $self->{calendar} //= 'gregorian';
    $self->{_cldr} = Locale::Unicode::Data->new ||
        return( $self->pass_error( Locale::Unicode::Data->error ) );
    return( $self );
}

sub am_pm_abbreviated { return( shift->am_pm_format_abbreviated( @_ ) ); }

sub am_pm_format_abbreviated { return( shift->_am_pm(
    context => [qw( format stand-alone )],
    width   => [qw( abbreviated wide )],
) ); }

sub am_pm_format_narrow { return( shift->_am_pm(
    context => [qw( format stand-alone )],
    width   => [qw( narrow abbreviated wide )],
) ); }

sub am_pm_format_wide { return( shift->_am_pm(
    context => [qw( format stand-alone )],
    width   => [qw( wide abbreviated )],
) ); }

sub am_pm_standalone_abbreviated { return( shift->_am_pm(
    context => [qw( stand-alone format )],
    width   => [qw( abbreviated wide )],
) ); }

sub am_pm_standalone_narrow { return( shift->_am_pm(
    context => [qw( stand-alone format )],
    width   => [qw( narrow abbreviated wide )],
) ); }

sub am_pm_standalone_wide { return( shift->_am_pm(
    context => [qw( stand-alone format )],
    width   => [qw( wide abbreviated )],
) ); }

sub as_string
{
    my $self = shift( @_ );
    my $str;
    unless( defined( $str = $self->{as_string} ) )
    {
        my $locale = $self->{locale} ||
            die( "No locale is set!" );
        $str = $self->{as_string} = $locale->as_string;
    }
    return( $str );
}

sub available_formats
{
    my $self = shift( @_ );
    my $ref;
    unless( defined( $ref = $self->{available_formats} ) )
    {
        my $locale = $self->{locale} || die( "Locale value is gone!" );
        my $cldr = $self->{_cldr} || die( "The Locale::Unicode::Data object is gone!" );
        my $tree = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        my $calendar = $self->{calendar} || 'gregorian';
        foreach my $loc ( @$tree )
        {
            my $all = $cldr->calendar_available_formats(
                locale      => $loc,
                calendar    => $calendar,
                alt         => undef,
                # count might contain some value
            );
            return( $self->pass_error ) if( !defined( $all ) && $cldr->error );
            if( $all && scalar( @$all ) )
            {
                # Make sure we have unique keys
                my $uniq = sub
                {
                    my %seen;
                    grep( !$seen{ $_ }++, @_ );
                };

                # $ref = [map( $_->{format_id}, @$all )];
                $ref = [$uniq->( map( $_->{format_id}, @$all ) )];
                last;
            }
        }
        $self->{available_formats} = $ref;
    }
    return( $ref );
}

sub available_format_patterns
{
    my $self = shift( @_ );
    my $ref;
    unless( defined( $ref = $self->{available_format_patterns} ) )
    {
        my $locale = $self->{locale} || die( "Locale value is gone!" );
        my $cldr = $self->{_cldr} || die( "The Locale::Unicode::Data object is gone!" );
        my $tree = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        my $calendar = $self->{calendar} || 'gregorian';
        foreach my $loc ( @$tree )
        {
            my $all = $cldr->calendar_available_formats(
                locale      => $loc,
                calendar    => $calendar,
                alt         => undef,
                # count might contain some value
            );
            return( $self->pass_error ) if( !defined( $all ) && $cldr->error );
            if( $all && scalar( @$all ) )
            {
                $ref = +{ map{ $_->{format_id} => $_->{format_pattern} } @$all };
                last;
            }
        }
        $self->{available_format_patterns} = $ref;
    }
    return( $ref );
}

sub calendar
{
    my $self = shift( @_ );
    if( @_ )
    {
        my $cal_id = shift( @_ );
        if( defined( $cal_id ) )
        {
            if( $cal_id !~ /^[a-zA-Z][a-zA-Z0-9]+(\-[a-zA-Z][a-zA-Z0-9]+)*$/ )
            {
                return( $self->error( "Calendar ID provided (", ( $cal_id // 'undef' ), ") is invalid." ) );
            }
            $cal_id = lc( $cal_id );
        }
        $self->{calendar} = $cal_id;
    }
    return( $self->{calendar} );
}

sub code { return( shift->{locale} ); }

sub date_at_time_format_full { return( shift->_datetime_format(
    type        => 'atTime',
    width       => 'full',
) ); }

sub date_at_time_format_long { return( shift->_datetime_format(
    type        => 'atTime',
    width       => 'long',
) ); }

sub date_at_time_format_medium { return( shift->_datetime_format(
    type        => 'atTime',
    width       => 'medium',
) ); }

sub date_at_time_format_short { return( shift->_datetime_format(
    type        => 'atTime',
    width       => 'short',
) ); }

sub date_format_default { return( shift->date_format_medium ); }

sub date_format_full { return( shift->_date_time_format(
    type        => 'date',
    width       => 'full',
) ); }

sub date_format_long { return( shift->_date_time_format(
    type        => 'date',
    width       => 'long',
) ); }

sub date_format_medium { return( shift->_date_time_format(
    type        => 'date',
    width       => 'medium',
) ); }

sub date_format_short { return( shift->_date_time_format(
    type        => 'date',
    width       => 'short',
) ); }

sub date_formats
{
    my $self = shift( @_ );
    my $formats = {};
    foreach my $t ( qw( full long medium short ) )
    {
        my $code;
        unless( $code = $self->can( "date_format_${t}" ) )
        {
            die( "The method date_format_${t} is not defined in class ", ( ref( $self ) || $self ) );
        }
        $formats->{ $t } = $code->( $self );
    }
    return( $formats );
}

sub datetime_format { return( shift->datetime_format_medium ); }

sub datetime_format_default { return( shift->datetime_format_medium ); }

sub datetime_format_full { return( shift->_datetime_format(
    type        => 'standard',
    width       => 'full',
) ); }

sub datetime_format_long { return( shift->_datetime_format(
    type        => 'standard',
    width       => 'long',
) ); }

sub datetime_format_medium { return( shift->_datetime_format(
    type        => 'standard',
    width       => 'medium',
) ); }

sub datetime_format_short { return( shift->_datetime_format(
    type        => 'standard',
    width       => 'short',
) ); }

sub day_format_abbreviated { return( shift->_calendar_terms(
    id      => 'day_format_abbreviated',
    type    => 'day',
    context => [qw( format stand-alone )],
    width   => [qw( abbreviated wide )],
) ); }

sub day_format_narrow { return( shift->_calendar_terms(
    id      => 'day_format_narrow',
    type    => 'day',
    context => [qw( format stand-alone )],
    width   => [qw( narrow short abbreviated wide )],
) ); }

# NOTE: day short exists in CLDR, but is left out in DateTime::Locale::FromData
sub day_format_short { return( shift->_calendar_terms(
    id      => 'day_format_short',
    type    => 'day',
    context => [qw( format stand-alone )],
    width   => [qw( short narrow abbreviated )],
) ); }

sub day_format_wide { return( shift->_calendar_terms(
    id      => 'day_format_wide',
    type    => 'day',
    context => [qw( format stand-alone )],
    width   => 'wide',
) ); }

sub day_period_format_abbreviated { return( shift->_day_period({
    context => 'format',
    width => 'abbreviated',
}, @_ ) ); }

sub day_period_format_narrow { return( shift->_day_period({
    context => 'format',
    width => [qw( narrow abbreviated )],
}, @_ ) ); }

sub day_period_format_wide { return( shift->_day_period({
    context => 'format',
    width => 'wide',
}, @_ ) ); }

sub day_period_stand_alone_abbreviated { return( shift->_day_period({
    context => 'stand-alone',
    width => 'abbreviated',
}, @_ ) ); }

sub day_period_stand_alone_narrow { return( shift->_day_period({
    context => 'stand-alone',
    width => [qw( narrow abbreviated )],
}, @_ ) ); }

sub day_period_stand_alone_wide { return( shift->_day_period({
    context => 'stand-alone',
    width => 'wide',
}, @_ ) ); }

sub day_periods
{
    my $self = shift( @_ );
    my $periods;
    unless( defined( $periods = $self->{day_periods} ) )
    {
        my $locale = $self->{locale} ||
            return( $self->error( "No locale is set!" ) );
        $locale = $self->_locale_object( $locale ) ||
            return( $self->pass_error );
        my $cldr = $self->{_cldr} ||
            return( $self->error( "Unable to get the Locale::Unicode::Data object!" ) );
        my $tree = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        $periods = {};
        foreach my $loc ( @$tree )
        {
            my $all = $cldr->day_periods( locale => $loc );
            if( scalar( @$all ) )
            {
                foreach my $ref ( @$all )
                {
                    $periods->{ $ref->{day_period} } = [@$ref{qw( start until )}];
                }
                last;
            }
        }
    }
    return( $periods );
}

sub day_stand_alone_abbreviated { return( shift->_calendar_terms(
    id      => 'day_stand_alone_abbreviated',
    type    => 'day',
    context => [qw( stand-alone format )],
    width   => [qw( abbreviated wide )],
) ); }

sub day_stand_alone_narrow { return( shift->_calendar_terms(
    id      => 'day_stand_alone_narrow',
    type    => 'day',
    context => [qw( stand-alone format )],
    width   => [qw( narrow wide )],
) ); }

# NOTE: day short exists in CLDR, but is left out in DateTime::Locale::FromData
sub day_stand_alone_short { return( shift->_calendar_terms(
    id      => 'day_stand_alone_short',
    type    => 'day',
    context => [qw( stand-alone format )],
    width   => [qw( short abbreviated )],
) ); }

sub day_stand_alone_wide { return( shift->_calendar_terms(
    id      => 'day_stand_alone_wide',
    type    => 'day',
    context => [qw( stand-alone format )],
    width   => 'wide',
) ); }

sub default_date_format_length { return( shift->{default_date_format_length} ); }

sub default_time_format_length { return( shift->{default_time_format_length} ); }

sub era_abbreviated { return( shift->_calendar_eras(
    id      => 'era_abbreviated',
    width   => [qw( abbreviated wide )],
    alt     => undef,
) ); }

sub era_narrow { return( shift->_calendar_eras(
    id      => 'era_narrow',
    width   => [qw( narrow abbreviated wide )],
    alt     => undef,
) ); }

sub era_wide { return( shift->_calendar_eras(
    id      => 'era_wide',
    width   => [qw( wide abbreviated )],
    alt     => undef,
) ); }

sub error
{
    my $self = shift( @_ );
    if( @_ )
    {
        my $msg = join( '', map( ( ref( $_ ) eq 'CODE' ) ? $_->() : $_, @_ ) );
        $self->{error} = $ERROR = DateTime::Locale::FromCLDR::Exception->new({
            skip_frames => 1,
            message => $msg,
        });
        if( $self->fatal )
        {
            die( $self->{error} );
        }
        else
        {
            warn( $msg ) if( warnings::enabled() );
            if( want( 'ARRAY' ) )
            {
                rreturn( [] );
            }
            elsif( want( 'OBJECT' ) )
            {
                rreturn( DateTime::Locale::FromCLDR::NullObject->new );
            }
            return;
        }
    }
    return( ref( $self ) ? $self->{error} : $ERROR );
}

sub fatal { return( shift->_set_get_prop( 'fatal', @_ ) ); }

sub first_day_of_week
{
    my $self = shift( @_ );
    my $dow;
    unless( defined( $dow = $self->{first_day_of_week} ) )
    {
        my $locale = $self->{locale} ||
            return( $self->error( "No locale is set!" ) );
        my $cldr = $self->{_cldr} ||
            return( $self->error( "Unable to get the Locale::Unicode::Data object!" ) );
        $locale = Locale::Unicode->new( $locale ) unless( Scalar::Util::blessed( $locale ) && $locale->isa( 'Locale::Unicode' ) );
        my $info = $self->_territory_info( locale => $locale ) ||
            return( $self->pass_error );
        if( !defined( $info->{first_day} ) ||
            !length( $info->{first_day} // '' ) )
        {
            $info = $self->_territory_info( territory => '001' ) ||
                return( $self->error( "Unable to get territory information for the World!" ) );
            if( !defined( $info->{first_day} ) ||
                !length( $info->{first_day} // '' ) )
            {
                return( $self->error( "First day of the week property (first_day) for territory '$info->{territory}' is missing in Locale::Unicode::Data" ) );

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

    eval
    {
        require DateTime;
    };
    if( $@ )
    {
        return( $self->error( "Unable to load the DateTime object: $@" ) );
    }
    my $dt = eval
    {
        DateTime->now( time_zone => $timezone );
    };
    if( $@ )
    {
        return( $self->error( "Unable to instantiate a DateTime object with time zone '${timezone}': $@" ) );
    }
    my $year = $dt->year;
    my $jan = eval
    {
        DateTime->new( year => $year, month => 1, day => 1, time_zone => $dt->time_zone )->offset;
    };
    return( $self->error( "Unable to get the time zone offset for '${timezone}' at ${year}/1/1: $@" ) ) if( $@ );
    my $jul = eval
    {
        DateTime->new( year => $year, month => 7, day => 1, time_zone => $dt->time_zone )->offset;
    };
    return( $self->error( "Unable to get the time zone offset for '${timezone}' at ${year}/7/1: $@" ) ) if( $@ );
    my $bool = ( $jan != $jul ? 1 : 0 );
    $TZ_DST_CACHE->{ lc( $timezone ) } = $bool;
    return( $bool );
}

sub interval_format
{
    my $self = shift( @_ );
    my $id = shift( @_ ) || return( $self->error( "No interval format ID was provided." ) );
    my $greatest_diff = shift( @_ ) ||
        return( $self->error( "No greatest difference token was provided." ) );
    my $locale = $self->{locale} ||
        return( $self->error( "No locale is set!" ) );
    if( ref( $id ) && !overload::Method( $id => '""' ) )
    {
        return( $self->error( "Interval format ID provided (", overload::StrVal( $id ), ") is a reference that does not stringify!" ) );
    }
    elsif( ref( $greatest_diff ) && !overload::Method( $greatest_diff => '""' ) )
    {
        return( $self->error( "Greatest difference provided (", overload::StrVal( $greatest_diff ), ") is a reference that does not stringify!" ) );
    }
    elsif( $id !~ /^[a-zA-Z]+$/ )
    {
        return( $self->error( "Invalid interval format ID provided." ) );
    }
    elsif( $greatest_diff !~ /^[a-zA-Z]$/ )
    {
        return( $self->error( "Invalid greatest difference value provided (", overload::StrVal( $greatest_diff ), ")" ) );
    }
    $locale = $self->_locale_object( $locale ) ||
        return( $self->pass_error );
    my $cldr = $self->{_cldr} ||
        return( $self->error( "Unable to get the Locale::Unicode::Data object!" ) );
    my $calendar = $self->{calendar} || 'gregorian';
    my $tree = $cldr->make_inheritance_tree( $locale ) ||
        return( $self->pass_error( $cldr->error ) );
    my $ref;
    foreach my $loc ( @$tree )
    {
        $ref = $cldr->calendar_interval_format(
            locale => $loc,
            calendar => $calendar,
            format_id => $id,
            greatest_diff_id => ( lc( $greatest_diff ) eq 'h' ? [ uc( $greatest_diff ), lc( $greatest_diff )] : $greatest_diff ),
        );
        if( !defined( $ref ) && $cldr->error )
        {
            return( $self->pass_error( $cldr->error ) );
        }
        elsif( $ref )
        {
            last;
        }
    }
    return( [] ) if( !$ref );
    return( [@$ref{qw( part1 separator part2 format_pattern )}] );
}

sub interval_formats
{
    my $self = shift( @_ );
    my $formats;
    unless( defined( $formats = $self->{interval_formats} ) )
    {
        my $locale = $self->{locale} ||
            return( $self->error( "No locale is set!" ) );
        $locale = $self->_locale_object( $locale ) ||
            return( $self->pass_error );
        my $cldr = $self->{_cldr} ||
            return( $self->error( "Unable to get the Locale::Unicode::Data object!" ) );
        my $calendar = $self->{calendar} || 'gregorian';
        my $tree = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        foreach my $loc ( @$tree )
        {
            $formats = $cldr->interval_formats(
                locale => $loc,
                calendar => $calendar,
            ) || return( $self->pass_error( $cldr->error ) );
            if( !defined( $formats ) && $cldr->error )
            {
                return( $self->pass_error( $cldr->error ) );
            }
            last if( $formats && scalar( keys( %$formats ) ) );
        }
        $formats //= {};
        $self->{interval_formats} = $formats;
    }
    return( $formats );
}

# <https://unicode.org/reports/tr35/tr35-dates.html#intervalFormats>
# a: am, pm period
# B: flexible day periods
#    00:00 (midnight)
#    06:00 - 12:00 (morning1)
#    12:00 - 12:00 (noon)
#    12:00 - 18:00 (afternoon1)
#    18:00 - 21:00 (evening1)
#    21:00 - 06:00 (night1)
# d: day
# G: era
# h: hour
# H: hour
# M: month
# m: minute
# y: year
sub interval_greatest_diff
{
    my $self = shift( @_ );
    my $dt1 = shift( @_ ) ||
        return( $self->error( "No DateTime object was provided. \$locale->interval_greatest_diff( \$dt1, \$dt2 )" ) );
    my $dt2 = shift( @_ ) ||
        return( $self->error( "Missing DateTime object. I was expecting 2 DateTime object, but only 1 was provided. \$locale->interval_greatest_diff( \$dt1, \$dt2 )" ) );
    my $opts = $self->_get_args_as_hash( @_ );
    foreach my $dt ( $dt1, $dt2 )
    {
        if( !defined( $dt ) ||
            !Scalar::Util::blessed( $dt ) ||
            !$dt->isa( 'DateTime' ) )
        {
            return( $self->error( "Invalid DateTime object provided (", overload::StrVal( $dt // 'undef' ), ")." ) );
        }
    }
    my $locale = $self->{locale} ||
        die( "The locale ID is gone!" );
    if( $opts->{day_period_first} && ref( $opts->{day_period_first} ) && !overload::Method( $opts->{day_period_first} => '""' ) )
    {
        return( $self->error( "Day period first option provided (", overload::StrVal( $opts->{day_period_first} ), ") is a reference that does not stringify!" ) );
    }

    local $@;
    # try-catch
    eval { require DateTime; };
    if( $@ )
    {
        return( $self->error( "Unable to load the DateTime object: $@" ) );
    }

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

            # <https://unicode.org/reports/tr35/tr35-numbers.html#otherNumberingSystems>
            elsif( $ref->{native} )
            {
                $str = 'latn';
                last;
            }
        }
        if( defined( $str ) )
        {
            my $this = $cldr->number_system( number_system => $str );
            return( $self->pass_error( $cldr->error ) ) if( !defined( $this ) && $cldr->error );
            if( $this )
            {
                $ref = [ $str, $this->{digits} ];
            }
            else
            {
                die( "No digits data found for numbering system '${str}' for locale '${locale}' !" );
            }
        }
        $ref //= [];
        $self->{locale_number_system} = $ref;
    }
    return( $ref );
}

sub metazone_daylight_long { return( shift->_metazone_name({
    type        => 'daylight',
    width       => 'long',
}, @_ ) ); }

sub metazone_daylight_short { return( shift->_metazone_name({
    type        => 'daylight',
    width       => 'short',
}, @_ ) ); }

sub metazone_generic_long { return( shift->_metazone_name({
    type        => 'generic',
    width       => 'long',
    location    => 1,
}, @_ ) ); }

sub metazone_generic_short { return( shift->_metazone_name({
    type        => 'generic',
    width       => 'short',
    location    => 1,
}, @_ ) ); }

sub metazone_standard_long { return( shift->_metazone_name({
    type        => 'standard',
    width       => 'long',
}, @_ ) ); }

sub metazone_standard_short { return( shift->_metazone_name({
    type        => 'standard',
    width       => 'short',
}, @_ ) ); }

# NOTE: "if the abbreviated format data for Gregorian does not exist in a language X (in the chain up to root), then it inherits from the wide format data in that same language X."
# <https://unicode.org/reports/tr35/tr35-dates.html#months_days_quarters_eras>
sub month_format_abbreviated { return( shift->_calendar_terms(
    id      => 'month_format_abbreviated',
    type    => 'month',
    context => 'format',
    width   => [qw( abbreviated wide )],
) ); }

sub month_format_narrow { return( shift->_calendar_terms(
    id      => 'month_format_narrow',
    type    => 'month',
    context => 'format',
    width   => [qw( narrow wide )],
) ); }

# NOTE: There is no 'short' format for month, but there is for 'day'

sub month_format_wide { return( shift->_calendar_terms(
    id      => 'month_format_wide',
    type    => 'month',
    context => 'format',
    width   => 'wide',
) ); }

sub month_stand_alone_abbreviated { return( shift->_calendar_terms(
    id      => 'month_stand_alone_abbreviated',
    type    => 'month',
    context => 'stand-alone',
    width   => [qw( abbreviated wide )],
) ); }

sub month_stand_alone_narrow { return( shift->_calendar_terms(
    id      => 'month_stand_alone_narrow',
    type    => 'month',
    context => 'stand-alone',
    width   => [qw( narrow wide )],
) ); }

# NOTE: There is no 'short' stand-alone for month, but there is for 'day'

sub month_stand_alone_wide { return( shift->_calendar_terms(
    id      => 'month_stand_alone_narrow',
    type    => 'month',
    context => 'stand-alone',
    width   => 'wide',
) ); }

sub name
{
    my $self = shift( @_ );
    my $name;
    unless( defined( $name = $self->{name} ) )
    {
        my $locale = $self->{locale} || die( "Locale is not set!" );
        $locale = $self->_locale_object( $locale ) ||
            return( $self->pass_error );
        my $cldr = $self->{_cldr} || die( "The Locale::Unicode::Data object is gone!" );
        # my $lang = $locale->language || $locale->language3;
        # Building an inheritance tree just for locale 'en' is not necessary, but in the future
        # This API should change to allow for localisation locales other than 'en'
        my $tree = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        # We remove the last entry, which is always 'und'
        # because otherwise, we would get 'Unknown language' when we really want an empty string
        pop( @$tree );
        foreach my $loc ( @$tree )
        {
            my $ref = $cldr->locale_l10n(
                locale_id   => $loc,
                locale      => 'en',
                alt         => undef,
            );
            return( $self->pass_error( $cldr->error ) ) if( !defined( $ref ) && $cldr->error );
            if( $ref && $ref->{locale_name} )
            {
                $name = $ref->{locale_name};
                last;
            }
        }
        $name //= '';
        $self->{name} = $name;
    }
    return( $name );
}

sub native_language
{
    my $self = shift( @_ );
    my $name;
    unless( defined( $name = $self->{native_language} ) )
    {
        my $locale = $self->{locale} || die( "Locale is not set!" );
        $locale = $self->_locale_object( $locale ) ||
            return( $self->pass_error );
        my $cldr = $self->{_cldr} || die( "The Locale::Unicode::Data object is gone!" );
        my $lang = ( $locale->language || $locale->language3 );
        my $tree = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        # We remove the last entry, which is always 'und'
        # because otherwise, we would get 'Unknown language' when we really want an empty string
        pop( @$tree );

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

            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( ${"${class}\::FATAL_EXCEPTIONS"} ) && ${"${class}\::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::Locale::FromCLDR::NullObject->new );
    }
    return;
}

# sub prefers_24_hour_time
# {
#     my $self = shift( @_ );
#     my $bool;
#     unless( defined( $bool = $self->{prefers_24_hour_time} ) )
#     {
#         my $pat = $self->time_format_short;
#         my @parts = split( /(?:'(?:(?:[^']|'')*)')/, $pat );
#         $bool = $self->{prefers_24_hour_time} = scalar( grep( /h|K/, @parts ) ) ? 0 : 1;
#     }
#     return( $bool );
# }

sub prefers_24_hour_time
{
    my $self = shift( @_ );
    my $pref = $self->time_format_preferred;
    return( $self->pass_error ) if( !defined( $pref ) );
    # 'H': 0-23
    # 'k': 1-24
    # 'h': 1-12
    # 'K': 0-11
    return( ( $pref eq 'H' || $pref eq 'k' ) ? 1 : 0 );
}

sub quarter_format_abbreviated { return( shift->_calendar_terms(
    id      => 'quarter_format_abbreviated',
    type    => 'quarter',
    context => [qw( format stand-alone )],
    width   => [qw( abbreviated wide )],
) ); }

sub quarter_format_narrow { return( shift->_calendar_terms(
    id      => 'quarter_format_narrow',
    type    => 'quarter',
    context => [qw( format stand-alone )],
    width   => [qw( narrow wide )],
) ); }

# NOTE: There is no 'short' format for quarter, but there is for 'day'

sub quarter_format_wide { return( shift->_calendar_terms(
    id      => 'quarter_format_wide',
    type    => 'quarter',
    context => [qw( format stand-alone )],
    width   => 'wide',
) ); }

sub quarter_stand_alone_abbreviated { return( shift->_calendar_terms(
    id      => 'quarter_stand_alone_abbreviated',
    type    => 'quarter',
    context => [qw( stand-alone format )],
    width   => [qw( abbreviated wide )],
) ); }

sub quarter_stand_alone_narrow { return( shift->_calendar_terms(
    id      => 'quarter_stand_alone_narrow',
    type    => 'quarter',
    context => [qw( stand-alone format )],
    width   => [qw( narrow abbreviated wide )],
) ); }

# NOTE: There is no 'short' stand-alone for quarter, but there is for 'day'

sub quarter_stand_alone_wide { return( shift->_calendar_terms(
    id      => 'quarter_stand_alone_narrow',
    type    => 'quarter',
    context => [qw( stand-alone format )],
    width   => 'wide',
) ); }

sub script
{
    my $self = shift( @_ );
    my $name;
    unless( defined( $name = $self->{script} ) )
    {
        my $locale = $self->{locale} || die( "Locale is not set!" );
        $locale = $self->_locale_object( $locale ) ||
            return( $self->pass_error );
        if( my $script = $locale->script )
        {
            my $cldr = $self->{_cldr} || die( "The Locale::Unicode::Data object is gone!" );
            # Building an inheritance tree just for locale 'en' is not necessary, but in the future
            # This API should change to allow for localisation locales other than 'en'
            my $tree = $cldr->make_inheritance_tree( 'en' ) ||
                return( $self->pass_error( $cldr->error ) );
            foreach my $loc ( @$tree )
            {
                my $ref = $cldr->script_l10n(
                    script  => $script,
                    locale  => $loc,
                    alt     => undef,
                );
                return( $self->pass_error( $cldr->error ) ) if( !defined( $ref ) && $cldr->error );
                if( $ref && $ref->{locale_name} )
                {
                    $name = $ref->{locale_name};
                    last;
                }
            }
            $name //= '';
            $self->{script} = $name;
        }
    }
    return( $name );
}

sub script_code
{
    my $self = shift( @_ );
    my $str;
    unless( defined( $str = $self->{script_code} ) )
    {
        my $locale = $self->{locale} ||
            die( "No locale is set!" );
        $str = $self->{script_code} = $locale->script;
    }
    return( $str );
}

sub split_interval
{
    my $self = shift( @_ );
    my $cldr = $self->{_cldr} || die( "The Locale::Unicode::Data object is gone!" );

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

        if( my $territory = $locale->territory )
        {
            my $cldr = $self->{_cldr} || die( "The Locale::Unicode::Data object is gone!" );
            # Building an inheritance tree just for locale 'en' is not necessary, but in the future
            # This API should change to allow for localisation locales other than 'en'
            my $tree = $cldr->make_inheritance_tree( 'en' ) ||
                return( $self->pass_error( $cldr->error ) );
            foreach my $loc ( @$tree )
            {
                my $ref = $cldr->territory_l10n(
                    territory   => $territory,
                    locale      => $loc,
                    alt         => undef,
                );
                return( $self->pass_error( $cldr->error ) ) if( !defined( $ref ) && $cldr->error );
                if( $ref && $ref->{locale_name} )
                {
                    $name = $ref->{locale_name};
                    last;
                }
            }
            $name //= '';
            $self->{territory} = $name;
        }
    }
    return( $name );
}

sub territory_code
{
    my $self = shift( @_ );
    my $str;
    unless( defined( $str = $self->{territory_code} ) )
    {
        my $locale = $self->{locale} ||
            die( "No locale is set!" );
        $str = $self->{territory_code} = $locale->territory;
    }
    return( $str );
}

sub territory_info
{
    my $self = shift( @_ );
    my $locale = $self->{locale} || die( "Locale is not set!" );
    my $info;
    unless( defined( $info = $self->{territory_info} ) )
    {
        $info = $self->_territory_info( locale => $locale );
        return( $self->pass_error ) if( !defined( $info ) );
        $self->{territory_info} = $info;
    }
    return( $info );
}

sub time_format_allowed { return( shift->_time_formats( 'allowed', @_ ) ); }

sub time_format_default { return( shift->time_format_medium ); }

sub time_format_full { return( shift->_date_time_format(
    calendar    => 'gregorian',
    type        => 'time',
    width       => 'full',
) ); }

sub time_format_long { return( shift->_date_time_format(
    calendar    => 'gregorian',
    type        => 'time',
    width       => 'long',
) ); }

sub time_format_medium { return( shift->_date_time_format(
    calendar    => 'gregorian',
    type        => 'time',
    width       => 'medium',
) ); }

sub time_format_preferred { return( shift->_time_formats( 'preferred', @_ ) ); }

sub time_format_short { return( shift->_date_time_format(
    calendar    => 'gregorian',
    type        => 'time',
    width       => 'short',
) ); }

sub time_formats
{
    my $self = shift( @_ );
    my $formats = {};
    foreach my $t ( qw( full long medium short ) )
    {
        my $code;
        unless( $code = $self->can( "time_format_${t}" ) )
        {
            die( "The method time_format_${t} is not defined in class ", ( ref( $self ) || $self ) );
        }
        $formats->{ $t } = $code->( $self );
    }
    return( $formats );
}

sub timezone_canonical
{
    my $self = shift( @_ );
    my $tz = shift( @_ ) ||
        return( $self->error( "No timezone was provided." ) );
    my $cldr = $self->{_cldr} || die( "Locale::Unicode::Data object is gone!" );
    my $str = $cldr->timezone_canonical( $tz );
    return( $self->pass_error( $cldr->error ) ) if( !defined( $str ) && $cldr->error );
    return( $str );
}

sub timezone_city
{
    my $self = shift( @_ );
    my $opts = $self->_get_args_as_hash( @_ );
    my $timezone = $opts->{timezone} ||
        return( $self->error( "No timezone was provided to get the examplar city." ) );
    return( $self->error( "Time zone provided (", overload::StrVal( $timezone ), ") is a reference that does not stringify!" ) ) if( ref( $timezone ) && !overload::Method( $timezone => '""' ) );
    my $meth_id = 'timezone_city_for_tz_' . $timezone;
    my $name;
    unless( defined( $name = $self->{ $meth_id } ) )
    {
        my $locale = $self->{locale} || die( "Locale value is gone!" );
        my $cldr = $self->{_cldr} || die( "Locale::Unicode::Data object is gone!" );
        my $locales = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        my $ref;
        LOCALE: foreach my $loc ( @$locales )
        {
            $ref = $cldr->timezone_city(
                timezone    => $timezone,
                locale      => $loc,
            );
            return( $self->pass_error ) if( !defined( $ref ) && $cldr->error );
            if( $ref )
            {
                $name = $ref->{city};
                last LOCALE;
            }
        }

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

}

sub variant_code
{
    my $self = shift( @_ );
    my $str;
    unless( defined( $str = $self->{variant_code} ) )
    {
        my $locale = $self->{locale} ||
            die( "No locale is set!" );
        $str = $self->{variant_code} = $locale->variant;
    }
    return( $str );
}

sub variants
{
    my $self = shift( @_ );
    my $ref;
    unless( defined( $ref = $self->{variants} ) )
    {
        my $locale = $self->{locale} ||
            die( "No locale is set!" );
        $ref = $self->{variants} = $locale->variants;
    }
    return( $ref );
}

sub version
{
    my $self = shift( @_ );
    my $vers;
    unless( defined( $vers = $self->{version} ) )
    {
        my $cldr = $self->{_cldr} || die( "Locale::Unicode::Data object is gone!" );
        $vers = $cldr->cldr_version;
    }
    return( $vers );
}

sub _am_pm
{
    my $self = shift( @_ );
    my $opts = $self->_get_args_as_hash( @_ );
    if( !$opts->{context} )
    {
        die( "No context was provided to retrieve AM/PN localised terms." );
    }
    elsif( !$opts->{width} )
    {
        die( "No width was provided to retrieve AM/PN localised terms." );
    }
    elsif( ref( $opts->{context} ) && ref( $opts->{context} ) ne 'ARRAY' && !overload::Method( $opts->{context} => '""' ) )
    {
        return( $self->error( "Context provided (", overload::StrVal( $opts->{context} ), ") is a reference that does not stringify!" ) );
    }
    elsif( ref( $opts->{width} ) && ref( $opts->{width} ) ne 'ARRAY' && !overload::Method( $opts->{width} => '""' ) )
    {
        return( $self->error( "Width provided (", overload::StrVal( $opts->{width} ), ") is a reference that does not stringify!" ) );
    }
    elsif( $opts->{calendar} && ref( $opts->{calendar} ) && !overload::Method( $opts->{calendar} => '""' ) )
    {
        return( $self->error( "Calendar provided (", overload::StrVal( $opts->{calendar} ), ") is a reference that does not stringify!" ) );
    }
    my $calendar = $opts->{calendar} || $self->{calendar} || 'gregorian';
    my $meth_id = 'am_pm_' . $calendar . '_' . $opts->{width} . '_' . $opts->{context};
    my $ampm;
    unless( defined( $ampm = $self->{ $meth_id } ) )
    {
        my $locale = $self->{locale} || die( "Locale value is gone!" );
        my $cldr = $self->{_cldr} || die( "The Locale::Unicode::Data object is gone!" );
        my $tree = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        # We do not want to fallback to the 'und' locale on this.
        pop( @$tree );
        my $widths = ref( $opts->{width} ) eq 'ARRAY' ? $opts->{width} : [$opts->{width}];
        my $contexts = ref( $opts->{context} ) eq 'ARRAY' ? $opts->{context} : [$opts->{context}];
        $ampm = [];
        LOCALES: foreach my $loc ( @$tree )
        {
            foreach my $context ( @$contexts )
            {
                foreach my $width ( @$widths )
                {
                    my $all = $cldr->calendar_term(
                        locale          => $loc,
                        calendar        => $calendar,
                        term_context    => $context,
                        term_width      => $width,
                        term_name       => [qw( am pm )],
                    );
                    return( $self->pass_error ) if( !defined( $all ) );
                    if( scalar( @$all ) )
                    {
                        if( scalar( @$all ) != 2 )
                        {
                            return( $self->error( "Data seems to be corrupted for locale ${loc} in Locale::Unicode::Data. I received ", scalar( @$all ), " sets of data when I expected 2." ) );
                        }
                        @$ampm = map( $_->{term_value}, @$all );
                        last LOCALES;
                    }
                }
            }
        }
        return( $self->{ $meth_id } = $ampm );
    }
    return( $ampm );
}

sub _available_formats
{
    my $self = shift( @_ );
    my $opts = $self->_get_args_as_hash( @_ );
    if( !$opts->{id} )
    {
        die( "No format ID specified." );
    }
    elsif( !length( $opts->{id} // '' ) )
    {
        return( $self->error( "No format ID was provided" ) );
    }
    elsif( ref( $opts->{id} ) && !overload::Method( $opts->{id} => '""' ) )
    {
        return( $self->error( "Format ID provided (", overload::StrVal( $opts->{id} ), ") is a reference that does not stringify!" ) );
    }
    elsif( $opts->{calendar} && ref( $opts->{calendar} ) && !overload::Method( $opts->{calendar} => '""' ) )
    {
        return( $self->error( "Calendar provided (", overload::StrVal( $opts->{calendar} ), ") is a reference that does not stringify!" ) );
    }
    my $calendar = $opts->{calendar} || $self->{calendar} || 'gregorian';
    my $meth_id = 'available_formats_' . $calendar . '_' . $opts->{id};
    my $pattern;
    unless( defined( $pattern = $self->{ $meth_id } ) )
    {
        my $locale = $self->{locale} || die( "Locale value is gone!" );
        my $cldr = $self->{_cldr} || die( "Locale::Unicode::Data object is gone!" );
        my $tree = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        my $ref;
        LOCALE: foreach my $loc ( @$tree )
        {
            $ref = $cldr->calendar_available_format(
                locale      => $loc,
                calendar    => $calendar,
                format_id   => $opts->{id},
                count       => [undef, qw( few many one other two zero)],
            );
            return( $self->pass_error ) if( !defined( $ref ) && $cldr->error );
            if( $ref && $ref->{format_pattern} )
            {
                $pattern = $ref->{format_pattern};
                last LOCALE;
            }
        }
        $self->{ $meth_id } = $pattern;
    }
    return( $pattern );
}

sub _calendar_eras
{
    my $self = shift( @_ );
    my $opts = $self->_get_args_as_hash( @_ );
    my $id   = $opts->{id} || die( "Missing ID" );
    die( "Missing width" ) if( !$opts->{width} );
    if( ref( $opts->{id} ) && !overload::Method( $opts->{id} => '""' ) )
    {
        return( $self->error( "Format ID provided (", overload::StrVal( $opts->{id} ), ") is a reference that does not stringify!" ) );
    }
    elsif( ref( $opts->{width} ) && ref( $opts->{width} ) ne 'ARRAY' && !overload::Method( $opts->{width} => '""' ) )
    {
        return( $self->error( "Width provided (", overload::StrVal( $opts->{width} ), ") is a reference that does not stringify!" ) );
    }
    elsif( $opts->{calendar} && ref( $opts->{calendar} ) && !overload::Method( $opts->{calendar} => '""' ) )
    {
        return( $self->error( "Calendar provided (", overload::StrVal( $opts->{calendar} ), ") is a reference that does not stringify!" ) );
    }
    my $calendar = $opts->{calendar} || $self->{calendar} || 'gregorian';
    $opts->{width} = [$opts->{width}] unless( ref( $opts->{width} ) eq 'ARRAY' );
    my $eras;
    unless( defined( $eras = $self->{ "${id}_${calendar}" } ) )
    {
        my $locale = $self->{locale} || die( "Locale value is gone!" );
        my $cldr = $self->{_cldr} || die( "Locale::Unicode::Data object is gone!" );
        my $tree = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        $eras = [];
        LOCALE: foreach my $loc ( @$tree )
        {
            foreach my $width ( @{$opts->{width}} )
            {
                my $all = $cldr->calendar_eras_l10n(
                    locale          => $loc,
                    calendar        => $calendar,
                    era_width       => $width,
                    ( exists( $opts->{alt} ) ? ( alt => $opts->{alt} ) : () ),
                    order => [era_id => 'integer'],
                );
                return( $self->pass_error( $cldr->error ) ) if( !defined( $all ) && $cldr->error );
                if( $all && scalar( @$all ) )
                {
                    @$eras = map( $_->{locale_name}, @$all );
                    last LOCALE;
                }
            }
        }
        $self->{ $id } = $eras;
    }
    return( $eras );
}

sub _calendar_terms
{
    my $self = shift( @_ );
    my $opts = $self->_get_args_as_hash( @_ );
    my $id   = $opts->{id} || die( "Missing ID" );
    die( "Missing type" ) if( !$opts->{type} );
    die( "Missing context" ) if( !$opts->{context} );
    die( "Missing width" ) if( !$opts->{width} );
    if( ref( $id ) && !overload::Method( $id => '""' ) )
    {
        return( $self->error( "Format ID provided (", overload::StrVal( $id ), ") is a reference that does not stringify!" ) );
    }
    elsif( ref( $opts->{type} ) && !overload::Method( $opts->{type} => '""' ) )
    {
        return( $self->error( "Type provided (", overload::StrVal( $opts->{type} ), ") is a reference that does not stringify!" ) );
    }
    elsif( ref( $opts->{context} ) && ref( $opts->{context} ) ne 'ARRAY' && !overload::Method( $opts->{context} => '""' ) )
    {
        return( $self->error( "Context provided (", overload::StrVal( $opts->{context} ), ") is a reference that does not stringify!" ) );
    }
    elsif( ref( $opts->{width} ) && ref( $opts->{width} ) ne 'ARRAY' && !overload::Method( $opts->{width} => '""' ) )
    {
        return( $self->error( "Width provided (", overload::StrVal( $opts->{width} ), ") is a reference that does not stringify!" ) );
    }
    elsif( $opts->{calendar} && ref( $opts->{calendar} ) && !overload::Method( $opts->{calendar} => '""' ) )
    {
        return( $self->error( "Calendar provided (", overload::StrVal( $opts->{calendar} ), ") is a reference that does not stringify!" ) );
    }
    my $calendar = $opts->{calendar} || $self->{calendar} || 'gregorian';
    $opts->{width} = [$opts->{width}] unless( ref( $opts->{width} ) eq 'ARRAY' );
    # If some type (e.g. short, narrow, etc) are missing in 'format', we can try to look for it in 'stand-alone'
    $opts->{context} = [$opts->{context}] unless( ref( $opts->{context} ) eq 'ARRAY' );
    my $meth_id = "${id}_${calendar}_type=$opts->{type}_context=" . join( ';', @{$opts->{context}} ) . '_width=' . join( ';', @{$opts->{width}} );
    my $terms;
    unless( defined( $terms = $self->{ $meth_id } ) )
    {
        my $locale = $self->{locale} || die( "Locale value is gone!" );
        my $cldr = $self->{_cldr} || die( "Locale::Unicode::Data object is gone!" );
        my $locales = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        my $expects =
        {
            day => 7,
            month => 12,
            quarter => 4,
        };
        LOCALE: foreach my $loc ( @$locales )
        {
            foreach my $ctx ( @{$opts->{context}} )
            {
                foreach my $width ( @{$opts->{width}} )
                {
                    my $all = $cldr->calendar_terms(
                        locale          => $loc,
                        calendar        => $calendar,
                        term_type       => $opts->{type},
                        term_context    => $ctx,
                        term_width      => $width,
                        ( $opts->{type} eq 'day' ? ( order_by_value => [term_name => [qw( mon tue wed thu fri sat sun )]] ) : () ),
                        ( ( $opts->{type} eq 'month' || $opts->{type} eq 'quarter' ) ? ( order => [term_name => 'integer'] ) : () ),
                    );
                    return( $self->pass_error ) if( !defined( $all ) && $cldr->error );
                    if( $all && scalar( @$all ) >= $expects->{ $opts->{type} } )
                    {
                        $terms = [];
                        for( @$all )
                        {
                            push( @$terms, $_->{term_value} );
                        }
                        last LOCALE;
                    }
                }
            }
        }
        # We make it NOT undef, so we do not go through this again, in the unlikely event nothing was found.
        $terms = [] if( !defined( $terms ) );
        $self->{ $meth_id } = $terms;
    }
    return( $terms );
}

# This is the date or time format with CLDR patterns
sub _date_time_format
{
    my $self = shift( @_ );
    my $opts = $self->_get_args_as_hash( @_ );
    if( !$opts->{width} )
    {
        die( "No date format width specified." );
    }
    elsif( !$opts->{type} )
    {
        die( "No type provided. Please specify either 'date' or 'time'" );
    }
    elsif( ref( $opts->{type} ) && ref( $opts->{type} ) ne 'ARRAY' && !overload::Method( $opts->{type} => '""' ) )
    {
        return( $self->error( "Type provided (", overload::StrVal( $opts->{type} ), ") is a reference that does not stringify!" ) );
    }
    elsif( $opts->{type} ne 'date' &&
           $opts->{type} ne 'time' )
    {
        die( "Invalid type provided. Please specify either 'date' or 'time'" );
    }
    elsif( ref( $opts->{width} ) && ref( $opts->{width} ) ne 'ARRAY' && !overload::Method( $opts->{width} => '""' ) )
    {
        return( $self->error( "Width provided (", overload::StrVal( $opts->{width} ), ") is a reference that does not stringify!" ) );
    }
    elsif( $opts->{calendar} && ref( $opts->{calendar} ) && !overload::Method( $opts->{calendar} => '""' ) )
    {
        return( $self->error( "Calendar provided (", overload::StrVal( $opts->{calendar} ), ") is a reference that does not stringify!" ) );
    }
    my $widths = ref( $opts->{width} ) eq 'ARRAY' ? $opts->{width} : [$opts->{width}];
    my $calendar = $opts->{calendar} || $self->{calendar} || 'gregorian';
    my $meth_id = '_date_time_format_' . $opts->{type} . '_format_' . $calendar . '_' . $widths->[0];
    my $pattern;
    unless( defined( $pattern = $self->{ $meth_id } ) )
    {
        my $locale = $self->{locale} || die( "Locale value is gone!" );
        my $cldr = $self->{_cldr} || die( "Locale::Unicode::Data object is gone!" );
        my $locales = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        my $ref;
        LOCALE: foreach my $loc ( @$locales )
        {
            foreach my $width ( @$widths )
            {
                $ref = $cldr->calendar_format_l10n(
                    locale          => $loc,
                    calendar        => $calendar,
                    format_type     => $opts->{type},
                    format_length   => $width,
                );
                return( $self->pass_error ) if( !defined( $ref ) && $cldr->error );
                if( $ref && $ref->{format_pattern} )
                {
                    $pattern = $ref->{format_pattern};
                    last LOCALE;
                }
            }
        }
        # We default to empty string, so we do not recompute it if this locale has no data (should not happen though);
        $pattern //= '';
        $self->{ $meth_id } = $pattern;
    }
    return( $pattern );
}

# This is the datetime, i.e. date and time formatting, such as {1}, {0}
sub _datetime_format
{
    my $self = shift( @_ );
    my $opts = $self->_get_args_as_hash( @_ );
    if( !$opts->{width} )
    {
        die( "No date format width specified." );
    }
    elsif( ref( $opts->{width} ) && !overload::Method( $opts->{width}, '""' ) )
    {
        die( "The 'width' parameter provided is a reference (", ref( $opts->{width} ), ", but it is not stringifyable." );
    }
    elsif( !$opts->{type} )
    {
        die( "No type provided. Please specify either 'atTime' or 'standard'" );
    }
    elsif( ref( $opts->{type} ) && !overload::Method( $opts->{type} => '""' ) )
    {
        return( $self->error( "Type provided (", overload::StrVal( $opts->{type} ), ") is a reference that does not stringify!" ) );
    }
    elsif( $opts->{type} ne 'atTime' &&
           $opts->{type} ne 'standard' )
    {
        die( "Invalid type provided. Please specify either 'atTime' or 'standard'" );
    }
    elsif( $opts->{calendar} && ref( $opts->{calendar} ) && !overload::Method( $opts->{calendar} => '""' ) )
    {
        return( $self->error( "Calendar provided (", overload::StrVal( $opts->{calendar} ), ") is a reference that does not stringify!" ) );
    }
    my $calendar = $opts->{calendar} || $self->{calendar} || 'gregorian';
    my $meth_id = "_datetime_format_" . $calendar . '_' . $opts->{width} . '_' . $opts->{type};
    my $pattern;
    unless( defined( $pattern = $self->{ $meth_id } ) )
    {
        my $locale = $self->{locale} || die( "Locale value is gone!" );
        my $cldr = $self->{_cldr} || die( "Locale::Unicode::Data object is gone!" );
        my $locales = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        my $ref;
        LOCALE: foreach my $loc ( @$locales )
        {
            $ref = $cldr->calendar_datetime_format(
                locale          => $loc,
                calendar        => $calendar,
                format_type     => $opts->{type},
                format_length   => $opts->{width},
            );
            return( $self->pass_error ) if( !defined( $ref ) && $cldr->error );
            if( $ref && $ref->{format_pattern} )
            {
                $pattern = $ref->{format_pattern};
                last LOCALE;
            }
        }
        # We default to empty string, so we do not recompute it if this locale has no data (should not happen though);
        $pattern //= '';
        if( length( $pattern ) )
        {
            my $pats = {};
            foreach my $t ( qw( date time ) )
            {
                my $meth = $t . '_format_' . $opts->{width};
                my $code;
                unless( $code = $self->can( $meth ) )
                {
                    die( "Something is wrong. Unable to find the method ${meth} in our object class ", ( ref( $self ) || $self ) );
                }
                $pats->{ $t } = $code->( $self );
                return( $self->pass_error ) if( !defined( $pats->{ $t } ) && $self->error );
            }
            $pattern =~ s/\{0\}/$pats->{time}/g;
            $pattern =~ s/\{1\}/$pats->{date}/g;
        }
        $self->{ $meth_id } = $pattern;
    }
    return( $pattern );
}

sub _day_period
{
    my $self = shift( @_ );
    my $def = shift( @_ );
    my $dt = shift( @_ );
    my $opts = $self->_get_args_as_hash( @_ );
    my $period = $self->_find_day_period( $dt ) ||
        return( $self->pass_error );
    my $locale = $self->{locale} ||
        return( $self->error( "The local value is gone!" ) );
    my $cldr = $self->{_cldr} ||
        return( $self->error( "The Locale::Unicode::Data object is gone!" ) );
    if( $opts->{calendar} && ref( $opts->{calendar} ) && !overload::Method( $opts->{calendar} => '""' ) )
    {
        return( $self->error( "Calendar provided (", overload::StrVal( $opts->{calendar} ), ") is a reference that does not stringify!" ) );
    }
    my $calendar = $opts->{calendar} || $self->{calendar} || 'gregorian';
    die( "No 'context' argument was provided." ) if( !exists( $def->{context} ) );
    die( "No 'width' argument was provided." ) if( !exists( $def->{width} ) );
    if( ref( $def->{context} ) && ref( $def->{context} ) ne 'ARRAY' && !overload::Method( $def->{context} => '""' ) )
    {
        return( $self->error( "Context provided (", overload::StrVal( $def->{context} ), ") is a reference that does not stringify!" ) );
    }
    elsif( ref( $def->{width} ) && ref( $def->{width} ) ne 'ARRAY' && !overload::Method( $def->{width} => '""' ) )
    {
        return( $self->error( "Width provided (", overload::StrVal( $def->{width} ), ") is a reference that does not stringify!" ) );
    }

    my $width = ref( $def->{width} ) eq 'ARRAY' ? $def->{width} : [$def->{width}];
    my $tree = $cldr->make_inheritance_tree( $locale ) ||
        return( $self->pass_error( $cldr->error ) );
    my $name;
    LOCALE: foreach my $loc ( @$tree )
    {
        foreach my $width ( @$width )
        {
            my $ref = $cldr->calendar_term(
                locale => $loc,
                calendar => $calendar,
                term_context => $def->{context},
                term_width => $width,
                term_name => $period,
            );
            if( !defined( $ref ) && $cldr->error )
            {
                return( $self->pass_error( $cldr->error ) );
            }
            if( $ref )
            {
                $name = $ref->{term_value};
                last LOCALE;
            }
        }
    }
    # LDML: "If the locale doesn't have the notion of a unique "noon" = 12:00, then the PM form may be substituted. Similarly for "midnight" = 00:00 and the AM form"
    if( !defined( $name ) &&
        ( $period eq 'noon' || $period eq 'midnight' ) )
    {
        my $ampm = $self->am_pm_format_abbreviated;
        if( defined( $ampm ) && 
            ref( $ampm ) eq 'ARRAY' && 
            scalar( @$ampm ) )
        {
            if( $period eq 'midnight' )
            {
                $name = $ampm->[0];
            }
            elsif( $period eq 'noon' )
            {
                $name = $ampm->[1];
            }
        }
    }
    $name //= '';
    return( $name );
}

sub _find_day_period
{
    my $self = shift( @_ );
    my $dt = shift( @_ ) ||
        return( $self->error( "No DateTime object was provided." ) );
    unless( Scalar::Util::blessed( $dt ) &&
            $dt->isa( 'DateTime' ) )
    {
        return( $self->error( "The DateTime object provided (", overload::StrVal( $dt ), ") is actually not a DateTime object." ) );
    }
    my $opts = $self->_get_args_as_hash( @_ );

    my $locale = $self->{locale} ||
        die( "The locale ID is gone!" );
    my $cldr = $self->{_cldr} ||
        die( "The Locale::Unicode::Data object is gone!" );
    my $ref;
    # So we can provide a cached data and avoid making useless queries
    unless( $ref = $opts->{day_periods} && 
            ref( $ref // '' ) eq 'HASH' )
    {
        $ref = $self->day_periods || return( $self->pass_error );

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

    elsif( $def->{type} !~ /^(?:generic|standard|daylight)$/ )
    {
        die( "Bad type provided. It must be one of: generic, generic or daylight" );
    }
    elsif( $def->{width} !~ /^(?:short|long)$/ )
    {
        die( "Bad width provided. It must be one of: short or long" );
    }
    elsif( !$opts->{timezone} )
    {
        return( $self->error( "No timezone was provided." ) );
    }
    elsif( ref( $opts->{timezone} ) && !overload::Method( $opts->{timezone} => '""' ) )
    {
        return( $self->error( "Time zone provided (", overload::StrVal( $opts->{timezone} ), ") is a reference that does not stringify!" ) );
    }
    my $meth_id = "timezone_name_" . $def->{type} . '_' . $def->{width} . '_tz=' . $opts->{timezone};
    my $name;
    unless( defined( $name = $self->{ $meth_id } ) )
    {
        my $locale = $self->{locale} || die( "Locale value is gone!" );
        my $cldr = $self->{_cldr} || die( "Locale::Unicode::Data object is gone!" );
        my $locales = $cldr->make_inheritance_tree( $locale ) ||
            return( $self->pass_error( $cldr->error ) );
        my $timezone = $opts->{timezone};
        my $type     = $def->{type};
        my $width    = $def->{width};
        my $location = $def->{location};
        my $ref;
        my $tz_info = $cldr->timezone(
            timezone => $timezone,
        );
        return( $self->pass_error ) if( !defined( $tz_info ) && $cldr->error );
        return( $self->error( "No time zone ${timezone} found." ) ) if( !$tz_info );
        LOCALE: foreach my $loc ( @$locales )
        {
            $ref = $cldr->timezone_names(
                timezone    => $timezone,
                locale      => $loc,
                width       => $width,
            );
            return( $self->pass_error ) if( !defined( $ref ) && $cldr->error );
            if( $ref && $ref->{ $type } )
            {
                $name = $ref->{ $type };
                last LOCALE;
            }
        }
        # Failed to find a suitable match
        $name //= '';
        $self->{ $meth_id } = $name;
    }
    return( $name );
}

sub FREEZE
{
    my $self = CORE::shift( @_ );
    my $serialiser = CORE::shift( @_ ) // '';
    my $class = CORE::ref( $self );
    my @keys = qw( locale calendar default_date_format_length default_time_format_length fatal );
    my %hash = ();
    @hash{ @keys } = @$self{ @keys };
    # Return an array reference rather than a list so this works with Sereal and CBOR
    # On or before Sereal version 4.023, Sereal did not support multiple values returned
    CORE::return( [$class, %hash] ) if( $serialiser eq 'Sereal' && Sereal::Encoder->VERSION <= version->parse( '4.023' ) );
    # But Storable want a list with the first element being the serialised element
    CORE::return( $class, \%hash );
}

sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }

sub STORABLE_thaw { return( shift->THAW( @_ ) ); }

# NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments.
# NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
sub THAW
{
    my( $self, undef, @args ) = @_;
    my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
    my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
    my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
    my $locale = Locale::Unicode->new( CORE::delete( $hash->{locale} ) // 'en' );
    my $new;
    # Storable pattern requires to modify the object it created rather than returning a new one
    if( CORE::ref( $self ) )
    {
        foreach( CORE::keys( %$hash ) )
        {
            $self->{ $_ } = CORE::delete( $hash->{ $_ } );
        }
        $new = $self;
    }
    else
    {
        $new = CORE::bless( $hash => $class );
    }
    $new->{locale} = $locale;
    $new->{_cldr} = Locale::Unicode::Data->new;
    CORE::return( $new );
}

sub TO_JSON { return( shift->as_string ); }

# NOTE: DateTime::Locale::FromCLDR::Exception class
package DateTime::Locale::FromCLDR::Exception;
BEGIN
{
    use strict;
    use warnings;
    use vars qw( $VERSION );
    use overload (
        '""'    => 'as_string',
        bool    => sub{ $_[0] },
        fallback => 1,
    );
    our $VERSION = 'v0.1.0';
};
use strict;
use warnings;

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

    {
        $new = CORE::bless( $hash => $class );
    }
    CORE::return( $new );
}

sub TO_JSON { return( shift->as_string ); }

{
    # NOTE: DateTime::Locale::FromCLDR::NullObject class
    package
        DateTime::Locale::FromCLDR::NullObject;
    BEGIN
    {
        use strict;
        use warnings;
        use overload (
            '""'    => sub{ '' },
            fallback => 1,
        );
        use Wanted;
    };
    use strict;
    use warnings;

    sub new
    {
        my $this = shift( @_ );
        my $ref = @_ ? { @_ } : {};
        return( bless( $ref => ( ref( $this ) || $this ) ) );
    }

    sub AUTOLOAD
    {
        my( $method ) = our $AUTOLOAD =~ /([^:]+)$/;
        my $self = shift( @_ );
        if( want( 'OBJECT' ) )
        {
            rreturn( $self );
        }
        # Otherwise, we return undef; Empty return returns undef in scalar context and empty list in list context
        return;
    };
}

1;
# NOTE: POD
__END__

=encoding utf-8

=head1 NAME

DateTime::Locale::FromCLDR - DateTime Localised Data from Unicode CLDR

=head1 SYNOPSIS

    use DateTime::Locale::FromCLDR;
    my $locale = DateTime::Locale::FromCLDR->new( 'ja-Kana-JP' ) ||
        die( DateTime::Locale::FromCLDR->error );
    my $locale = DateTime::Locale::FromCLDR->new( 'ja-Kana-JP', calendar => 'japanese' ) ||
        die( DateTime::Locale::FromCLDR->error );
    my $array = $locale->am_pm_abbreviated;
    my $array = $locale->available_formats;
    $locale->calendar( 'hebrew' );
    my $str = $locale->calendar;
    # a Locale::Unicode object that stringifies to the initial locale value (ja-Kana-JP)
    my $obj = $locale->code;
    my $str = $locale->date_at_time_format_full;
    my $str = $locale->date_at_time_format_long;
    my $str = $locale->date_at_time_format_medium;
    my $str = $locale->date_at_time_format_short;
    my $str = $locale->date_format_default;
    my $str = $locale->date_format_full;
    my $str = $locale->date_format_long;
    my $str = $locale->date_format_medium;
    my $str = $locale->date_format_short;
    my $str = $locale->date_formats;
    my $str = $locale->datetime_format;
    my $str = $locale->datetime_format_default;
    my $str = $locale->datetime_format_full;
    my $str = $locale->datetime_format_long;
    my $str = $locale->datetime_format_medium;
    my $str = $locale->datetime_format_short;
    my $str = $locale->day_format_abbreviated;
    my $str = $locale->day_format_narrow;
    my $str = $locale->day_format_short;
    my $str = $locale->day_format_wide;
    my $str = $locale->day_period_format_abbreviated( $datetime_object );
    my $str = $locale->day_period_format_narrow( $datetime_object );
    my $str = $locale->day_period_format_wide( $datetime_object );
    my $str = $locale->day_period_stand_alone_abbreviated( $datetime_object );
    my $str = $locale->day_period_stand_alone_narrow( $datetime_object );
    my $str = $locale->day_period_stand_alone_wide( $datetime_object );
    my $hashref = $locale->day_periods;
    my $str = $locale->day_stand_alone_abbreviated;
    my $str = $locale->day_stand_alone_narrow;
    my $str = $locale->day_stand_alone_short;
    my $str = $locale->day_stand_alone_wide;
    my $str = $locale->default_date_format_length;
    my $str = $locale->default_time_format_length;
    my $str = $locale->era_abbreviated;
    my $str = $locale->era_narrow;
    my $str = $locale->era_wide;
    my $str = $locale->first_day_of_week;
    my $str = $locale->format_for( 'yMEd' );
    my $str = $locale->gmt_format(0);
    my $str = $locale->gmt_format(3600);
    my $str = $locale->gmt_format(-3600);
    my $str = $locale->gmt_format(-3600, width => 'short');
    my $str = $locale->gmt_format(-3600, { width => 'short' });
    # Alias for method 'code'
    my $obj = $locale->id;
    my $array = $locale->interval_format( GyMEd => 'd' );
    my $hashref = $locale->interval_formats;
    my $greatest_diff = $locale->interval_greatest_diff( $datetime_object_1, $datetime_object_2 );
    my $str = $locale->language;
    my $str = $locale->language_code;
    # Alias for method 'language_code'
    my $str = $locale->language_id;
    # Locale::Unicode object
    my $obj = $locale->locale;
    # Equivalent to $locale->locale->as_string
    my $str = $locale->locale_as_string;
    # As per standard, it falls back to 'wide' format if it is not available
    my $str = $locale->metazone_daylight_long( metazone => 'Taipei' );

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

    {
        say "Oops: ", $e->message;
    }

Or, you could set the global variable C<$FATAL_EXCEPTIONS> instead:

    use v5.34;
    use experimental 'try';
    no warnings 'experimental';
    $DateTime::Locale::FromCLDR::FATAL_EXCEPTIONS = 1;
    try
    {
        my $locale = DateTime::Locale::FromCLDR->new( 'en' );
        # Missing the 'offset' argument
        my $str = $locale->format_gmt;
        # More code
    }
    catch( $e )
    {
        say "Oops: ", $e->message;
    }

=head1 VERSION

    v0.8.2

=head1 DESCRIPTION

This is a powerful replacement for L<DateTime::Locale> and L<DateTime::Locale::FromData> that use static data from over 1,000 pre-generated modules, whereas L<DateTime::Locale::FromCLDR> builds a C<locale> object to access its Unicode L<CLDR|https://...

It provides the same API as L<DateTime::Locale>, but in a dynamic way. This is important since in the Unicode L<LDML specifications|https://unicode.org/reports/tr35/>, a C<locale> inherits from its parent's data.

Once a data is retrieved by a method, it is cached to avoid waste of time.

It also adds a few methods to access the C<locale> L<at time patterns|https://unicode.org/reports/tr35/tr35-dates.html#dateTimeFormats>, such as L<date_at_time_format_full|/date_at_time_format_full>, and L<native_variants|/native_variants>

It also provides key support for L<day period|https://unicode.org/reports/tr35/tr35-dates.html#Day_Period_Rule_Sets>

It also provides support for interval datetime, and L<a method to find the greatest datetime difference element between 2 datetimes|/interval_greatest_diff>, as well as a method to get all the L<available format patterns for intervals|/interval_forma...

It adds the C<short> format for day missing in L<DateTime::Locale::FromData>

Note that in C<CLDR> parlance, there are standard pattern formats. For example C<full>, C<long>, C<medium>, C<short> or also C<abbreviated>, C<short>, C<wide>, C<narrow> providing various level of conciseness.

=head1 CONSTRUCTOR

=head2 new

    # Japanese as spoken in Japan
    my $locale = DateTime::Locale::FromCLDR->new( 'ja-JP' ) ||
        die( DateTime::Locale::FromCLDR->error );
    # Okinawan as spoken in Japan Southern islands
    my $locale = DateTime::Locale::FromCLDR->new( 'ryu-Kana-JP-t-de-t0-und-x0-medical' ) ||
        die( DateTime::Locale::FromCLDR->error );

    use Locale::Unicode;
    my $loc = Locale::Unicode->new( 'fr-FR' );
    my $locale = DateTime::Locale::FromCLDR->new( $loc ) ||
        die( DateTime::Locale::FromCLDR->error );

Specifying a calendar ID other than the default C<gregorian>:

    my $locale = DateTime::Locale::FromCLDR->new( 'ja-JP', calendar => 'japanese' ) ||
        die( DateTime::Locale::FromCLDR->error );

or, using an hash reference:

    my $locale = DateTime::Locale::FromCLDR->new( 'ja-JP', { calendar => 'japanese' } ) ||
        die( DateTime::Locale::FromCLDR->error );

Instantiate a new L<DateTime::Locale::FromCLDR> object based on a C<locale> provided, and returns it. By default, it uses the calendar C<gregorian>, but you can specify a different one with the C<calendar> option.

You can provide any C<locale>, even complex one as shown above, and only its core part will be retained. So, for example:

    my $locale = DateTime::Locale::FromCLDR->new( 'ryu-Kana-JP-t-de-t0-und-x0-medical' ) ||
        die( DateTime::Locale::FromCLDR->error );
    say $locale; # ryu-Kana-JP

If an error occurs, it sets an L<exception object|DateTime::Locale::FromCLDR::Exception> and returns C<undef> in scalar context, or an empty list in list context, or possibly a special C<DateTime::Locale::FromCLDR::NullObject> in object context. See ...

The object is overloaded and stringifies into the core part of the original string provided upon instantiation.

The core part is comprised of the C<language> ID, an optional C<script> ID, an optional C<territory> ID and zero or multiple C<variant> IDs. See L<Locale::Unicode> and the L<LDML specifications|https://unicode.org/reports/tr35/tr35.html#Locale> for m...

=head1 METHODS

All methods are read-only unless stated otherwise.

=head2 am_pm_abbreviated

This is an alias for L<am_pm_format_abbreviated|/am_pm_format_abbreviated>

=head2 am_pm_format_abbreviated

    my $array = $locale->am_pm_format_abbreviated;

Returns an array reference of the terms used to represent C<am> and C<pm>

The array reference could be empty if the C<locale> does not support specifying C<am>/C<pm>

For example:

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $ampm = $locale->am_pm_abbreviated
    say @$ampm; # AM, PM

    my $locale = DateTime::Locale::FromCLDR->new( 'ja' );
    my $ampm = $locale->am_pm_abbreviated
    say @$ampm; # 午前, 午後

    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    my $ampm = $locale->am_pm_abbreviated
    say @$ampm; # Empty

See L<Locale::Unicode::Data/calendar_term>

=head2 am_pm_format_narrow

Same as L<am_pm_format_abbreviated|/am_pm_format_abbreviated>, but returns the narrow format of the AM/PM terms.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->am_pm_format_narrow;

=head2 am_pm_format_wide

Same as L<am_pm_format_abbreviated|/am_pm_format_abbreviated>, but returns the wide format of the AM/PM terms.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->am_pm_format_wide;

=head2 am_pm_standalone_abbreviated

Same as L<am_pm_format_abbreviated|/am_pm_format_abbreviated>, but returns the abbreviated stand-alone format of the AM/PM terms.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->am_pm_standalone_abbreviated;

=head2 am_pm_standalone_narrow

Same as L<am_pm_format_abbreviated|/am_pm_format_abbreviated>, but returns the narrow stand-alone format of the AM/PM terms.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->am_pm_standalone_narrow;

=head2 am_pm_standalone_wide

Same as L<am_pm_format_abbreviated|/am_pm_format_abbreviated>, but returns the wide stand-alone format of the AM/PM terms.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->am_pm_standalone_wide;

=for Pod::Coverage as_string

=head2 available_formats

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->available_formats;

Returns an array reference of all the format ID available for this C<locale>

See L<Locale::Unicode::Data/calendar_available_format>

=head2 available_format_patterns

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $ref = $locale->available_format_patterns;

Returns an hash reference of all the available format ID to their corresponding pattern for the C<locale>

See L<Locale::Unicode::Data/calendar_available_format>

=head2 calendar

    my $locale = DateTime::Locale::FromCLDR->new( 'ja-Kana-JP', calendar => 'japanese' ) ||
        die( DateTime::Locale::FromCLDR->error );
    my $str = $locale->calendar; # japanese
    $locale->calendar( 'gregorian' );

Sets or gets the L<calendar ID|Locale::Unicode::Data/calendar> used to perform queries along with the given C<locale>

=head2 code

    my $obj = $locale->code;

Returns the L<Locale::Unicode> object either received or created upon object instantiation.

=head2 date_at_time_format_full

    my $str = $locale->date_at_time_format_full;

Returns the full L<date at time pattern|https://unicode.org/reports/tr35/tr35-dates.html#dateTimeFormats>

For example:

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->date_at_time_format_full;
    # EEEE, MMMM d, y 'at' h:mm:ss a zzzz
    # Tuesday, July 23, 2024 at 1:26:38 AM UTC

    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    say $locale->date_at_time_format_full;
    # EEEE d MMMM y 'à' HH:mm:ss zzzz
    # mardi 23 juillet 2024 à 01:27:11 UTC

=head2 date_at_time_format_long

Same as L<date_at_time_format_full|/date_at_time_format_full>, but returns the long format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->date_at_time_format_long;
    # MMMM d, y 'at' h:mm:ss a z
    # July 23, 2024 at 1:26:11 AM UTC

=head2 date_at_time_format_medium

Same as L<date_at_time_format_full|/date_at_time_format_full>, but returns the medium format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->date_at_time_format_medium;
    # MMM d, y 'at' h:mm:ss a
    # Jul 23, 2024 at 1:25:43 AM

=head2 date_at_time_format_short

Same as L<date_at_time_format_full|/date_at_time_format_full>, but returns the short format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->date_at_time_format_short;
    # M/d/yy 'at' h:mm a
    # 7/23/24 at 1:25 AM

=head2 date_format_default

This is an alias to L<date_format_medium|/date_format_medium>

=head2 date_format_full

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->date_format_full;
    # EEEE, MMMM d, y
    # Tuesday, July 23, 2024

Returns the L<full date pattern|https://unicode.org/reports/tr35/tr35-dates.html#dateFormats>

See also L<Locale::Unicode::Data/calendar_format_l10n>

=head2 date_format_long

Same as L<date_format_full|/date_format_full>, but returns the long format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->date_format_long;
    # MMMM d, y
    # July 23, 2024

=head2 date_format_medium

Same as L<date_format_full|/date_format_full>, but returns the medium format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->date_format_long;
    # MMM d, y
    # Jul 23, 2024

=head2 date_format_short

Same as L<date_format_full|/date_format_full>, but returns the short format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->date_format_short;
    # M/d/yy
    # 7/23/24

=head2 date_formats

    my $now = DateTime->now( locale => 'en' );
    my $ref = $locale->date_formats;
    foreach my $type ( sort( keys( %$ref ) ) )
    {
        say $type, ":";
        say $ref->{ $type };
        say $now->format_cldr( $ref->{ $type } ), "\n";
    }

Would produce:

    full:
    EEEE, MMMM d, y
    Tuesday, July 23, 2024

    long:
    MMMM d, y
    July 23, 2024

    medium:
    MMM d, y
    Jul 23, 2024

    short:
    M/d/yy
    7/23/24

Returns an hash reference with the keys being: C<full>, C<long>, C<medium>, C<short> and their value the result of their associated date format methods.

=head2 datetime_format

This is an alias for L<datetime_format_medium|/datetime_format_medium>

=head2 datetime_format_default

This is also an alias for L<datetime_format_medium|/datetime_format_medium>

=head2 datetime_format_full

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->datetime_format_full;
    # EEEE, MMMM d, y, h:mm:ss a zzzz
    # Tuesday, July 23, 2024, 1:53:27 AM UTC

Returns the L<full datetime pattern|https://unicode.org/reports/tr35/tr35-dates.html#dateTimeFormats>

See also L<Locale::Unicode::Data/calendar_datetime_format>

=head2 datetime_format_long

Same as L<datetime_format_full|/datetime_format_full>, but returns the long format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->datetime_format_long;
    # MMMM d, y, h:mm:ss a z
    # July 23, 2024, 1:57:02 AM UTC

=head2 datetime_format_medium

Same as L<datetime_format_full|/datetime_format_full>, but returns the medium format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->datetime_format_medium;
    # MMM d, y, h:mm:ss a
    # Jul 23, 2024, 2:03:16 AM

=head2 datetime_format_short

Same as L<datetime_format_full|/datetime_format_full>, but returns the short format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->datetime_format_short;
    # M/d/yy, h:mm a
    # 7/23/24, 2:04 AM

=head2 day_format_abbreviated

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $days = $locale->day_format_abbreviated;
    say @$days;
    # Mon, Tue, Wed, Thu, Fri, Sat, Sun

Returns an array reference of week day names abbreviated format with Monday first and Sunday last.

See also L<Locale::Unicode::Data/calendar_term>

=head2 day_format_narrow

Same as L<day_format_abbreviated|/day_format_abbreviated>, but returns the narrow format days.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $days = $locale->day_format_abbreviated;
    say @$days;
    # M, T, W, T, F, S, S

=head2 day_format_short

Same as L<day_format_abbreviated|/day_format_abbreviated>, but returns the short format days.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $days = $locale->day_format_short;
    say @$days;
    # Mo, Tu, We, Th, Fr, Sa, Su

=head2 day_format_wide

Same as L<day_format_abbreviated|/day_format_abbreviated>, but returns the wide format days.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $days = $locale->day_format_wide;
    say @$days;
    # Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

=head2 day_period_format_abbreviated

    my $dt = DateTime->new( year => 2024, hour => 7 );
    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->day_period_format_abbreviated( $dt );
    # in the morning

    my $dt = DateTime->new( year => 2024, hour => 13 );
    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->day_period_format_abbreviated( $dt );
    # in the afternoon

    my $dt = DateTime->new( year => 2024, hour => 7 );
    my $locale = DateTime::Locale::FromCLDR->new( 'ja-Kana-JP' );
    say $locale->day_period_format_abbreviated( $dt );
    # 朝
    # which means "morning" in Japanese

    my $dt = DateTime->new( year => 2024, hour => 13 );
    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    say $locale->day_period_format_abbreviated( $dt );
    # après-midi

Returns a string representing the localised expression of the period of day the L<DateTime> object provided is.

If nothing relevant could be found somehow, this will return an empty string. C<undef> is returned only if an error occurred.

This is used to provide the relevant value for the token C<B> or C<b> in the L<Unicode LDML format patterns|Locale::Unicode::Data/"Format Patterns">

See also L<Locale::Unicode::Data/calendar_term>, L<Locale::Unicode::Data/day_period> and L<DateTime::Format::Unicode>

=head2 day_period_format_narrow

Same as L<day_period_format_abbreviated|/day_period_format_abbreviated>, but returns the narrow format of day period.

    my $dt = DateTime->new( year => 2024, hour => 7 );
    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->day_period_format_narrow( $dt );
    # in the morning

=head2 day_period_format_wide

Same as L<day_period_format_abbreviated|/day_period_format_abbreviated>, but returns the wide format of day period.

    my $dt = DateTime->new( year => 2024, hour => 7 );
    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->day_period_format_wide( $dt );
    # in the morning

=head2 day_period_stand_alone_abbreviated

    my $dt = DateTime->new( year => 2024, hour => 7 );
    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->day_period_stand_alone_abbreviated( $dt );
    # morning

    my $dt = DateTime->new( year => 2024, hour => 13 );
    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->day_period_stand_alone_abbreviated( $dt );
    # afternoon

    my $dt = DateTime->new( year => 2024, hour => 7 );
    my $locale = DateTime::Locale::FromCLDR->new( 'ja-Kana-JP' );
    say $locale->day_period_stand_alone_abbreviated( $dt );
    # ""

The previous example would yield nothing, and as per L<the LDML specifications|https://unicode.org/reports/tr35/tr35-dates.html#dfst-period>, you would need to use the localised AM/PM instead.

    my $dt = DateTime->new( year => 2024, hour => 13 );
    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    say $locale->day_period_stand_alone_abbreviated( $dt );
    # ap.m.

Returns a string representing the localised expression of the period of day the L<DateTime> object provided is.

If nothing relevant could be found somehow, this will return an empty string. C<undef> is returned only if an error occurred.

This is used to provide a stand-alone word that can be used as a title, or in a different context.

See also L<Locale::Unicode::Data/calendar_term>, L<Locale::Unicode::Data/day_period> and L<DateTime::Format::Unicode>

=head2 day_period_stand_alone_narrow

Same as L<day_period_stand_alone_abbreviated|/day_period_stand_alone_abbreviated>, but returns the narrow stand-alone version of the day period.

    my $dt = DateTime->new( year => 2024, hour => 13 );
    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    say $locale->day_period_stand_alone_narrow( $dt );
    # ap.m.

=head2 day_period_stand_alone_wide

Same as L<day_period_stand_alone_abbreviated|/day_period_stand_alone_abbreviated>, but returns the wide stand-alone version of the day period.

    my $dt = DateTime->new( year => 2024, hour => 13 );
    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    say $locale->day_period_stand_alone_wide( $dt );
    # après-midi

=head2 day_periods

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $hash = $locale->day_periods;
    # Would return an hash reference like:
    {
        midnight => ["00:00", "00:00"],
        morning1 => ["06:00", "12:00"],
        noon => ["12:00", "12:00"],
        afternoon1 => ["12:00", "18:00"],
        evening1 => ["18:00", "21:00"],
        night1 => ["21:00", "06:00"],
    }

Returns an hash reference of day period token and values of 2-elements array (start time and end time in hours and minutes)

=head2 day_stand_alone_abbreviated

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $days = $locale->day_stand_alone_abbreviated;
    say @$days;
    # Mon, Tue, Wed, Thu, Fri, Sat, Sun

Returns an array reference of week day names in abbreviated format with Monday first and Sunday last.

This is often identical to the C<format> type.

See the L<LDML specifications|https://unicode.org/reports/tr35/tr35-dates.html#months_days_quarters_eras> for more information on the difference between the C<format> and C<stand-alone> types.

=head2 day_stand_alone_narrow

Same as L<day_stand_alone_abbreviated|/day_stand_alone_abbreviated>, but returns the narrow format days.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $days = $locale->day_stand_alone_narrow;
    say @$days;
    # M, T, W, T, F, S, S

=head2 day_stand_alone_short

Same as L<day_stand_alone_abbreviated|/day_stand_alone_abbreviated>, but returns the short format days.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $days = $locale->day_stand_alone_short;
    say @$days;
    # Mo, Tu, We, Th, Fr, Sa, Su

=head2 day_stand_alone_wide

Same as L<day_stand_alone_abbreviated|/day_stand_alone_abbreviated>, but returns the wide format days.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $days = $locale->day_stand_alone_wide;
    say @$days;
    # Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

=head2 default_date_format_length

This returns the string C<medium>

=head2 default_time_format_length

This returns the string C<medium>

=head2 era_abbreviated

    my $array = $locale->era_abbreviated;
    say @$array;
    # BC, AD

Returns an array reference of era names in abbreviated format.

See also L<Locale::Unicode::Data/calendar_eras_l10n>

=head2 era_narrow

Same as L<era_abbreviated|/era_abbreviated>, but returns the narrow format eras.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->era_narrow;
    say @$array;
    # B, A

=head2 era_wide

Same as L<era_abbreviated|/era_abbreviated>, but returns the wide format eras.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->era_wide;
    say @$array;
    # Before Christ, Anno Domini

=head2 error

Used as a mutator, this sets an L<exception object|DateTime::Locale::FromCLDR::Exception> and returns an C<DateTime::Locale::FromCLDR::NullObject> in object context (such as when chaining), or C<undef> in scalar context, or an empty list in list cont...

The C<DateTime::Locale::FromCLDR::NullObject> class prevents the perl error of C<Can't call method "%s" on an undefined value> (see L<perldiag>). Upon the last method chained, C<undef> is returned in scalar context or an empty list in list context.

=head2 fatal

    $cldr->fatal(1); # Enable fatal exceptions
    $cldr->fatal(0); # Disable fatal exceptions
    my $bool = $cldr->fatal;

Sets or get the boolean value, whether to die upon exception, or not. If set to true, then instead of setting an L<exception object|DateTime::Locale::FromCLDR::Exception>, this module will die with an L<exception object|DateTime::Locale::FromCLDR::Ex...

    use v.5.34; # to be able to use try-catch blocks in perl
    use experimental 'try';
    no warnings 'experimental';
    try
    {
        my $cldr = DateTime::Locale::FromCLDR->new( 'en', fatal => 1 );
        # Forgot the 'offset':
        my $str = $locale->format_gmt;
    }
    catch( $e )
    {
        say "Error occurred: ", $e->message;
        # Error occurred: No value for width was provided.
    }

=head2 first_day_of_week

    my $integer = $locale->first_day_of_week;

Returns an integer ranging from 1 to 7 where 1 means Monday and 7 means Sunday.

This represents what is the first day of the week for this C<locale>

Since the information on the first day of the week pertains to a C<territory>, if the C<locale> you provided does not have such information, this method will find out the L<likely subtag|Locale::Unicode::Data/likely_subtag> to get the C<locale>'s rig...

See the L<LDML specifications about likely subtags|https://unicode.org/reports/tr35/tr35.html#Likely_Subtags> for more information.

For example:

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );

Since there is no C<territory> associated, this will look up the likely subtag to find the target C<locale> is C<en-Latn-US>, and thus the C<territory> for C<en> is C<US> and first day of the week is C<7>

Another example:

    my $locale = DateTime::Locale::FromCLDR->new( 'fr-Latn' );

This will ultimately get the territory C<FR> and first day of the week is C<1>

    # Okinawan as spoken in the Japanese Southern islands
    my $locale = DateTime::Locale::FromCLDR->new( 'ryu' );

This will become C<ryu-Kana-JP> and thus the C<territory> would be C<JP> and first day of the week is C<7>

This information is cached in the current object, like for all the other methods in this API.

=head2 format_for

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $pattern = $locale->format_for( 'Bhm' );

Provided with the format ID of an L<available format|/available_formats> and this will return the localised C<CLDR> pattern.

Keep in mind that the C<CLDR> formatting method of L<DateTime|DateTime/format_cldr> does not recognise all the C<CLDR> pattern tokens. Thus, for example, if you chose the standard available pattern C<Bhm>, this method would return the localised patte...

    my $now = DateTime->now( locale => "en", time_zone => "Asia/Tokyo" );
    # Assuming $now = 2024-07-23T21:39:39
    say $now->format_cldr( 'h:mm B' );
    # 9:39 B

But C<B> is the day period, which can be looked up with L<Locale::Unicode::Data/day_period>, which provides us with the day period token C<night1>, which itself can be looked up with L<Locale::Unicode::Data/calendar_term> and gives us the localised s...

You can use L<DateTime::Format::Unicode> instead of the default L<DateTime> C<CLDR> formatting if you want to get better support for all L<CLDR pattern tokens|Locale::Unicode::Data/"Format Patterns">.

With Japanese:

    my $locale = DateTime::Locale::FromCLDR->new( 'ja' );
    my $pattern = $locale->format_for( 'Bhm' );
    # BK:mm
    my $now = DateTime->now( locale => "ja", time_zone => "Asia/Tokyo" );
    say $now->format_cldr( 'BK:mm' );
    # B9:54

But, this should have yielded: C<夜9:54> instead.

=head2 format_gmt

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    # Get the offset in seconds from the UTC
    my $offset = $dt->offset;
    my $str = $locale->gmt_format( $offset );
    # The 'width' is 'long' by default
    my $str = $locale->gmt_format( $offset, width => 'short' );

This returns a localised and formatted GMT timezone given an offset in seconds of the datetime from UTC.

For example:

=over 4

=item * C<GMT>

=item * C<UTC>

=item * C<Гринуич>

=back

Optionally, you can provide the C<width> option that may have the value C<long> (default), or C<short>

If the offset is C<0>, meaning this is the GMT time, then the localised representation of C<GMT> is returned using L<timezone_format_gmt_zero|/timezone_format_gmt_zero>, otherwise it will use the GMT format provided by L<timezone_format_gmt|/timezone...

Also, if the option C<width> is provided with a value C<short>, then the GMT hours, minutes, seconds formatting will not be zero padded.

For example:

=over 4

=item * C<GMT+03:30>

Long

=item * C<GMT+3:30>

Short

=item * C<UTC-03.00>

Long

=item * C<UTC-3>

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

This would return something like:

    {
        Bh => [qw( B h )],
        Bhm => [qw( B h m )],
        d => ["d"],
        default => ["default"],
        Gy => [qw( G y )],
        GyM => [qw( G M y )],
        GyMd => [qw( d G M y )],
        GyMEd => [qw( d G M y )],
        GyMMM => [qw( G M y )],
        GyMMMd => [qw( d G M y )],
        GyMMMEd => [qw( d G M y )],
        H => ["H"],
        h => [qw( a h )],
        hm => [qw( a h m )],
        Hm => [qw( H m )],
        hmv => [qw( a h m )],
        Hmv => [qw( H m )],
        Hv => ["H"],
        hv => [qw( a h )],
        M => ["M"],
        Md => [qw( d M )],
        MEd => [qw( d M )],
        MMM => ["M"],
        MMMd => [qw( d M )],
        MMMEd => [qw( d M )],
        y => ["y"],
        yM => [qw( M y )],
        yMd => [qw( d M y )],
        yMEd => [qw( d M y )],
        yMMM => [qw( M y )],
        yMMMd => [qw( d M y )],
        yMMMEd => [qw( d M y )],
        yMMMM => [qw( M y )],
    }

Returns an hash reference of all available interval format IDs and their associated L<greatest difference token|https://unicode.org/reports/tr35/tr35-dates.html#intervalFormats>

The C<default> interval format pattern is something like C<{0} – {1}>, but this changes depending on the C<locale> and is not always available.

C<{0}> is the placeholder for the first datetime and C<{1}> is the placeholder for the second one.

See L<Locale::Unicode::Data/interval_formats>

=head2 interval_greatest_diff

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $diff = $locale->interval_greatest_diff( $dt1, $dt2 );

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $diff = $locale->interval_greatest_diff( $dt1, $dt2, day_period_first => 1 );
    # or, using an hash reference instead:
    # my $diff = $locale->interval_greatest_diff( $dt1, $dt2, { day_period_first => 1 } );

Provided with 2 L<DateTime objects|DateTime>, and this will compute the L<greatest difference|https://unicode.org/reports/tr35/tr35-dates.html#intervalFormats>.

Quoting from the L<LDML specifications|https://unicode.org/reports/tr35/tr35-dates.html#intervalFormats>:

"The data supplied in CLDR requires the software to determine the calendar field with the greatest difference before using the format pattern. For example, the greatest difference in "Jan 10-12, 2008" is the day field, while the greatest difference i...

If both C<DateTime> objects are identical, this will return an empty string.

You can alter the inner working of the algorithm by providing the option C<day_period_first> with a true value. This will prioritise the day period over the AM/PM (morning vs afternoon). What this means, is that if you have two datetimes, one with an...

This is important, because of the way almost all, but 4 locales (C<bg>, C<id>, C<uz> and C<zu>), have sliced up their day periods. 

For the locale C<en>, for example, the day periods are:

=over 8

=item * C<midnight>

00:00  00:00

=item * C<morning1>

06:00  12:00

=item * C<noon>

12:00  12:00

=item * C<afternoon1>

12:00  18:00

=item * C<evening1>

18:00  21:00

=item * C<night1>

21:00  06:00

=back

As you can see, there are no occurrence of a day period that spans both morning and afternoon, and thus, because of those data, this method would always return, by default, C<a> instead of C<B>

For the table of the C<CLDR> components, see L<Locale::Unicode::Data/"Format Patterns">

If an error occurred, an L<exception object|DateTime::Locale::FromCLDR> is set and C<undef> is returned in scalar context, and an empty list in list context.

=head2 is_dst

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $dt = DateTime->new( year => 2024, month => 7, day => 1, time_zone => 'Asia/Tokyo' );
    my $bool = $locale->is_dst( $dt );
    # 0
    my $dt = DateTime->new( year => 2024, month => 7, day => 1, time_zone => 'America/Los_Angeles' );
    my $bool = $locale->is_dst( $dt );
    # 1

Returns true if the given C<timezone> is using daylight saving time, and false otherwise.

The result is cached to ensure repeating calls for the same C<timezone> are returned even faster.

If an error occurred, this will set an L<exception object|DateTime::Locale::FromCLDR::Exception>, and returns C<undef> in scalar context, or an empty list in list context.

How does it work? Very simply, this generates a L<DateTime> object based on the current year and given C<timezone> both for January 1st and July 1st, and get the C<timezone> offset for each. If they do not match, the C<timezone> has daylight saving t...

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN


If an error occurred, an L<exception object|DateTime::Format::FromCLDR::Exception> is set, and C<undef> is returned in scalar context, or an empty list in list context.

=head2 metazone_generic_short

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $str = $locale->metazone_generic_short( metazone => 'Atlantic' );
    # AT

    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    my $str = $locale->metazone_generic_short( metazone => 'Atlantic' );
    # HA

This returns the localised metazone name for the C<generic> time and C<short> format for the given C<metazone> ID.

If nothing can be found, an empty string is returned.

If an error occurred, an L<exception object|DateTime::Format::FromCLDR::Exception> is set, and C<undef> is returned in scalar context, or an empty list in list context.

=head2 metazone_standard_long

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $str = $locale->metazone_standard_long( metazone => 'Atlantic' );
    # Atlantic Standard Time

    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    my $str = $locale->metazone_standard_long( metazone => 'Atlantic' );
    # heure normale de l’Atlantique

This returns the localised metazone name for the C<standard> time and C<long> format for the given C<metazone> ID.

If nothing can be found, an empty string is returned.

If an error occurred, an L<exception object|DateTime::Format::FromCLDR::Exception> is set, and C<undef> is returned in scalar context, or an empty list in list context.

=head2 metazone_standard_short

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $str = $locale->metazone_standard_short( metazone => 'Atlantic' );
    # AST

    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    my $str = $locale->metazone_standard_short( metazone => 'Atlantic' );
    # HNA

This returns the localised metazone name for the C<standard> time and C<short> format for the given C<metazone> ID.

If nothing can be found, an empty string is returned.

If an error occurred, an L<exception object|DateTime::Format::FromCLDR::Exception> is set, and C<undef> is returned in scalar context, or an empty list in list context.

=head2 month_format_abbreviated

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->month_format_abbreviated;
    say @$array;
    # Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec

Returns an array reference of month names in abbreviated format from January to December.

See also L<Locale::Unicode::Data/calendar_term>

=head2 month_format_narrow

Same as L<month_format_abbreviated|/month_format_abbreviated>, but returns the months in narrow format.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->month_format_narrow;
    say @$array;
    # J, F, M, A, M, J, J, A, S, O, N, D

=head2 month_format_wide

Same as L<month_format_abbreviated|/month_format_abbreviated>, but returns the months in wide format.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->month_format_wide;
    say @$array;
    # January, February, March, April, May, June, July, August, September, October, November, December

=head2 month_stand_alone_abbreviated

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->month_stand_alone_abbreviated;
    say @$array;
    # Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec

Returns an array reference of month names in abbreviated stand-alone format from January to December.

See also L<Locale::Unicode::Data/calendar_term>

Note that there is often little difference between the C<format> and C<stand-alone> format types.

See the L<LDML specifications|https://unicode.org/reports/tr35/tr35-dates.html#months_days_quarters_eras> for more information on the difference between the C<format> and C<stand-alone> types.

=head2 month_stand_alone_narrow

Same as L<month_stand_alone_abbreviated|/month_stand_alone_abbreviated>, but returns the months in narrow format.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->month_stand_alone_narrow;
    say @$array;
    # J, F, M, A, M, J, J, A, S, O, N, D

=head2 month_stand_alone_wide

Same as L<month_format_abbreviated|/month_format_abbreviated>, but returns the months in wide format.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->month_stand_alone_wide;
    say @$array;
    # January, February, March, April, May, June, July, August, September, October, November, December

=head2 name

    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    say $locale->name; # French

    my $locale = DateTime::Locale::FromCLDR->new( 'fr-CH' );
    say $locale->name; # Swiss French

The C<locale>'s name in English.

See also L<native_name|/native_name>

=head2 native_language

    my $locale = DateTime::Locale::FromCLDR->new( 'fr-CH' );
    say $locale->native_language; # français

Returns the C<locale>'s C<language> name as written in the C<locale> own language.

If nothing can be found, it will return an empty string.

=head2 native_name

    my $locale = DateTime::Locale::FromCLDR->new( 'fr-CH' );
    say $locale->native_name; # français suisse

Returns the C<locale>'s name as written in the C<locale> own language.

If nothing can be found, it will return an empty string.

=head2 native_script

    my $locale = DateTime::Locale::FromCLDR->new( 'fr-Latn-CH' );
    say $locale->native_script; # latin

    my $locale = DateTime::Locale::FromCLDR->new( 'fr' );
    say $locale->native_script; # undef

lib/DateTime/Locale/FromCLDR.pm  view on Meta::CPAN

    my $locale = DateTime::Locale::FromCLDR->new( 'ar-EG' );
    my $ref = $locale->number_systems;
    # {
    #     finance => undef,
    #     native => undef,
    #     number_system => "arab",
    #     traditional => undef,
    # }

    my $locale = DateTime::Locale::FromCLDR->new( 'ja' );
    my $ref = $locale->number_systems;
    # {
    #     finance => "jpanfin",
    #     native => undef,
    #     number_system => undef,
    #     traditional => "jpan",
    # }

    my $locale = DateTime::Locale::FromCLDR->new( 'ja' );
    my $ref = $locale->number_systems;
    # {
    #     finance => "hantfin",
    #     native => "hanidec",
    #     number_system => undef,
    #     traditional => "hant",
    # }

Returns an hash reference containing the numbering systems for default (C<number_system>), C<finance>, C<native>, and C<traditional>. Note that not all of those properties would have a value, and thus some might be undefined.

=head2 number_system_digits

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $digits = $locale->number_system_digits( 'latn' );
    say $digits; # [0, 1, 2, 3, 4, 5, 6, 7, 8. 9];

    # Japanese traditional numbering system
    my $digits = $locale->number_system_digits( 'jpan' );
    say $digits; # ["〇","一","二","三","四","五","六","七","八","九"];

Provided with a valid number system ID, and this will return an array reference of digits for that number system, from 0 to 9.

It sets an L<exception object|DateTime::Locale::FromCLDR::Exception> upon error, and returns C<undef> in scalar context, or an empty list in list context.

=head2 prefers_24_hour_time

This checks whether the C<locale> prefers the 24H format or the 12H one and returns true (C<1>) if it prefers the 24 hours format or false (C<0>) otherwise.

How it finds out? It pulls the preferred time format from the CLDR data by calling L<time_format_preferred|/time_format_preferred>, and from there returns true (1) if the value is either C<H> or C<k>, or else false (0).

This is as specified by the L<Unicode LDML|https://www.unicode.org/reports/tr35/tr35-dates.html#availableFormats_appendItems>, which states: "the locale's actual preference for 12-hour or 24-hour time cycle is determined from the Time Data as describ...

=head2 quarter_format_abbreviated

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->quarter_format_abbreviated;
    say @$array;
    # Q1, Q2, Q3, Q4

Returns an array reference of quarter names in abbreviated format.

See also L<Locale::Unicode::Data/calendar_term>

=head2 quarter_format_narrow

Same as L<quarter_format_abbreviated|/quarter_format_abbreviated>, but returns the quarters in narrow format.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->quarter_format_narrow;
    say @$array;
    # 1, 2, 3, 4

=head2 quarter_format_wide

Same as L<quarter_format_abbreviated|/quarter_format_abbreviated>, but returns the quarters in wide format.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->quarter_format_wide;
    say @$array;
    # 1st quarter, 2nd quarter, 3rd quarter, 4th quarter

=head2 quarter_stand_alone_abbreviated

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->quarter_stand_alone_abbreviated;
    say @$array;
    # Q1, Q2, Q3, Q4

Returns an array reference of quarter names in abbreviated format.

See also L<Locale::Unicode::Data/calendar_term>

Note that there is often little difference between the C<format> and C<stand-alone> format types.

See the L<LDML specifications|https://unicode.org/reports/tr35/tr35-dates.html#months_days_quarters_eras> for more information on the difference between the C<format> and C<stand-alone> types.

=head2 quarter_stand_alone_narrow

Same as L<quarter_stand_alone_abbreviated|/quarter_stand_alone_abbreviated>, but returns the quarters in narrow format.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->quarter_stand_alone_narrow;
    say @$array;
    # 1, 2, 3, 4

=head2 quarter_stand_alone_wide

Same as L<quarter_stand_alone_abbreviated|/quarter_stand_alone_abbreviated>, but returns the quarters in wide format.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->quarter_stand_alone_wide;
    say @$array;
    # 1st quarter, 2nd quarter, 3rd quarter, 4th quarter

=head2 script

    my $locale = DateTime::Locale::FromCLDR->new( 'ja-Kana-JP' );
    my $str = $locale->script;
    # Katakana

Returns the name of the C<locale>'s C<script> in English.

If there is no C<script> specified in the C<locale>, it will return C<undef>

If there is a C<script> in the C<locale>, but, somehow, it cannot be found in the C<en> C<locale>'s L<language tree|Locale::Unicode::Data/make_inheritance_tree>, it will return an empty string.

=head2 script_code

    my $locale = DateTime::Locale::FromCLDR->new( 'ja-Kana-JP' );
    my $script = $locale->script_code;
    # Kana

    my $locale = DateTime::Locale::FromCLDR->new( 'ja-JP' );
    my $script = $locale->script_code;
    # undef

Returns the C<locale>'s C<script> ID, or C<undef> if there is none.

=head2 script_id

This is an alias for L<script_code|/script_code>

=head2 split_interval

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $ref = $locale->split_interval(
        pattern => $string,
        greatest_diff => 'd',
    ) || die( $locale->error );

This method actually calls L<Locale::Unicode::Data/split_interval> and passes it all the arguments it received, so please check its documentation.

It returns the array reference it received from L<Locale::Unicode::Data/split_interval>, or upon error, its sets an exception object, and returns C<undef> in scalar context or an empty list in list context.

=head2 territory

    my $locale = DateTime::Locale::FromCLDR->new( 'ja-JP' );
    my $script = $locale->territory;
    # Japan

    my $locale = DateTime::Locale::FromCLDR->new( 'zh-034' );
    my $script = $locale->territory;
    # Southern Asia

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $script = $locale->territory;
    # undef

    my $locale = DateTime::Locale::FromCLDR->new( 'en-XX' );
    my $script = $locale->territory;
    # ''

Returns the name of the C<locale>'s C<territory> in English.

If there is no C<territory> specified in the C<locale>, it will return C<undef>

If there is a C<territory> in the C<locale>, but, somehow, it cannot be found in the C<en> C<locale>'s L<language tree|Locale::Unicode::Data/make_inheritance_tree>, it will return an empty string.

=head2 territory_code

    my $locale = DateTime::Locale::FromCLDR->new( 'ja-JP' );
    my $script = $locale->territory_code;
    # JP

    my $locale = DateTime::Locale::FromCLDR->new( 'ja-Kana' );
    my $script = $locale->territory_code;
    # undef

Returns the C<locale>'s C<territory> ID, or C<undef> if there is none.

=head2 territory_id

This is an alias for L<territory_code|/territory_code>

=head2 territory_info

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $ref = $locale->territory_info;

which would yield:

    {
        calendars => undef,
        contains => undef,
        currency => "USD",
        first_day => 7,
        gdp => 19490000000000,
        languages => [qw(
            en es zh-Hant fr de fil it vi ko ru nv yi pdc hnj haw
            frc chr esu dak cho lkt ik mus io cic cad jbo osa zh
        )],
        literacy_percent => 99,
        min_days => 1,
        parent => "021",
        population => 332639000,
        status => "regular",
        territory => "US",
        territory_id => 297,
        weekend => undef,
    }

    my $locale = DateTime::Locale::FromCLDR->new( 'en-GB' );
    my $ref = $locale->territory_info;

which would yield:

    {
        calendars => undef,
        contains => undef,
        currency => "GBP",
        first_day => 1,
        gdp => 2925000000000,
        languages => [qw(
            en fr de es pl pa ur ta gu sco cy bn ar zh-Hant it lt pt
            so tr ga gd kw en-Shaw
        )],
        literacy_percent => 99,
        min_days => 4,
        parent => 154,
        population => 65761100,
        status => "regular",
        territory => "GB",
        territory_id => 121,
        weekend => undef,
    }

Returns an hash reference of information related to the ISO3166 country code associated with the C<locale>. If the C<locale> has no country code associated, it will expand it using the Unicode LDML rule with L<Locale::Unicode::Data/likely_subtag>

Keep in mind that the default or fallback data are stored in the special territory code C<001> (World). Thus, for example, if the C<calendars> field is empty, the default value would be in C<001>, and would be C<["gregorian"]>

=head2 time_format_allowed

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $array = $locale->time_format_allowed;

Returns an array reference of L<allowed time patterns|https://unicode.org/reports/tr35/tr35-dates.html#Time_Data> for the C<locale>'s associated territory. If the locale has no C<territory> associated with, it will check the L<likely subtag|Locale::U...

=head2 time_format_default

This is an alias for L<time_format_medium|/time_format_medium>

=head2 time_format_full

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->time_format_full;
    # h:mm:ss a zzzz
    # 10:44:07 PM UTC

Returns the L<full date pattern|https://unicode.org/reports/tr35/tr35-dates.html#dateFormats>

See also L<Locale::Unicode::Data/calendar_format_l10n>

=head2 time_format_long

Same as L<time_format_full|/time_format_full>, but returns the long format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->time_format_long;
    # h:mm:ss a z
    # 10:44:07 PM UTC

=head2 time_format_medium

Same as L<time_format_full|/time_format_full>, but returns the medium format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->time_format_medium;
    # h:mm:ss a
    # 10:44:07 PM

=head2 time_format_preferred

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    my $str = $locale->time_format_preferred;

Returns a string representing the L<time preferred pattern|https://unicode.org/reports/tr35/tr35-dates.html#Time_Data> for the C<locale>'s associated territory. If the locale has no C<territory> associated with, it will check the L<likely subtag|Loca...

=head2 time_format_short

Same as L<time_format_full|/time_format_full>, but returns the short format pattern.

    my $locale = DateTime::Locale::FromCLDR->new( 'en' );
    say $locale->time_format_short;
    # h:mm a
    # 10:44 PM

=head2 time_formats

    my $now = DateTime->now( locale => 'en' );
    my $ref = $locale->time_formats;
    foreach my $type ( sort( keys( %$ref ) ) )
    {
        say $type, ":";
        say $ref->{ $type };
        say $now->format_cldr( $ref->{ $type } ), "\n";
    }

Would produce:

    full:
    h:mm:ss a zzzz
    10:44:07 PM UTC

    long:
    h:mm:ss a z
    10:44:07 PM UTC

    medium:
    h:mm:ss a
    10:44:07 PM



( run in 1.401 second using v1.01-cache-2.11-cpan-39bf76dae61 )