Weather::YR::Parser - Helps to parse special types of data.


Weather-YR documentation Contained in the Weather-YR distribution.

Index


Code Index:

NAME

Top

Weather::YR::Parser - Helps to parse special types of data.

DESCRIPTION

Top

This module is a helper to parse different types of data, such as dates.

METHODS

Top

parse_date(STRING)

This method parses a date string and returns a object reference to an instance of DateTime.

It takes the timestamp in the following format: YYYY-MM-DD HH:MM:SS

parse_date_iso8601

This method parses a date string and returns a object reference to an instance of DateTime.

It takes the timestamp in the format specified in ISO 8601.

Example: 2008-03-16T10:49:13Z

SEE ALSO

Top

AUTHOR

Top

Knut-Olav, <knut-olav@hoven.ws>

COPYRIGHT AND LICENSE

Top


Weather-YR documentation Contained in the Weather-YR distribution.
package Weather::YR::Parser;

use strict;
use warnings;

use DateTime;
use DateTime::Format::ISO8601;


sub parse_date {
    my ( $str ) = @_;

    my ($year, $month, $day, $hour, $minute, $sec)
        = $str =~ m{
                        (\d{4}) -
                        (\d{2}) -
                        (\d{2})
                        \s
                        (\d{2}) :
                        (\d{2}) :
                        (\d{2})
                }x;

    my $date = DateTime->new(
        'year'      => $year,
        'month'     => $month,
        'day'       => $day,
        'hour'      => $hour,
        'minute'    => $minute,
        'second'    => $sec,
    );

    return $date;
}

sub parse_date_iso8601 {
    my ( $str ) = @_;
    my $dt = DateTime::Format::ISO8601->parse_datetime($str);
    return $dt;
}


1;