DateTime-Format-JP

 view release on metacpan or  search on metacpan

lib/DateTime/Format/JP.pm  view on Meta::CPAN

            {
                my $opts = {};
                @$opts{qw( year month day )} = @$ref;
                @$opts{qw( hour minute second )} = (0,0,0);
                $opts->{time_zone} = ( HAS_LOCAL_TZ ? 'local' : 'UTC' );
                return( DateTime::Lite->new( %$opts ) );
            }
            else
            {
                return( DateTime::Lite->now( time_zone => ( HAS_LOCAL_TZ ? 'local' : 'UTC' ) ) );
            }
        };
        if( $@ )
        {
            return( $self->error( $@ ) );
        }
        return( $rv );
    }
}

1;
# NOTE: POD
__END__

=encoding utf-8

=head1 NAME

DateTime::Format::JP - Japanese DateTime Parser and Formatter

=head1 SYNOPSIS

    use DateTime::Format::JP;
    my $fmt = DateTime::Format::JP->new(
        hankaku      => 1,
        pattern      => '%c', # default
        traditional  => 0,
        kanji_number => 0,
        zenkaku      => 0,
        time_zone    => 'local',
    );
    my $dt = DateTime->now or DateTime::Lite->now;
    $dt->set_formatter( $fmt );
    # set the encoding in and out to utf8
    use open ':std' => ':utf8';
    print "$dt\n"; # will print something like 令和3年7月12日午後2:30:20

    my $dt  = $fmt->parse_datetime( "令和3年7月12日午後2時30分" );
    
    my $str = $fmt->format_datetime( $dt );
    print "$str\n";

=head1 VERSION

    v0.1.7

=head1 DESCRIPTION

This module is used to parse and format Japanese date and time. It is lightweight and yet versatile.

It implements 2 main methods: L</parse_datetime> and L</format_datetime> both expect and return decoded utf8 string.

You can use L<Encode> to decode and encode from perl internal utf8 representation to real utf8 and vice versa.

=head1 METHODS

=head2 new

The constructor accepts the following parameters:

=over 4

=item I<hankaku> boolean

If true, the digits used will be "half-size" (半角), or roman numbers like 1, 2, 3, etc.

The opposite is I<zenkaku> (全角) or full-width. This will enable the use of double-byte Japanese numbers that still look like roman numbers, such as: 1, 2, 3, etc.

Defaults to true.

=item I<pattern> string

The pattern to use to format the date and time. See below the available L</"PATTERN TOKENS"> and their meanings.

Defaults to C<%c>

=item I<traditional> boolean

If true, then it will use a more traditional date/time representation. The effect of this parameter on the formatting is documented in L</"PATTERN TOKENS">

=item I<kanji_number> boolean

If true, this will have L</format_datetime> use numbers in kanji, such as: 一, 二, 三, 四, etc.

=item I<zenkaku> boolean

If true, this will use full-width, ie double-byte Japanese numbers that still look like roman numbers, such as: 1, 2, 3, etc.

=item I<time_zone> string

The time zone to use when creating a L<DateTime::Lite> object. Defaults to C<local> if L<DateTime::Lite::TimeZone> supports it, otherwise it will fallback on C<UTC>

=back

=head2 error

Returns the latest error set, if any.

All method in this module return C<undef> upon error and set an error that can be retrieved with this method.

=head2 format_datetime

Takes a L<DateTime::Lite> or L<DateTime> object and returns a formatted date and time based on the pattern specified, which defaults to C<%c>. 

You can call this method directly, or you can set this formatter object in L<DateTime::Lite/set_formatter> so that ie will be used for stringification of the L<DateTime::Lite> object.

See below L</"PATTERN TOKENS"> for the available tokens and their meanings.

=head2 hankaku

Sets or gets the boolean value for I<hankaku>.



( run in 2.883 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )