| 1 | # -*- perl -*- | ||
| 2 | ##---------------------------------------------------------------------------- | ||
| 3 | ## REST API Framework - ~/lib/Net/API/REST/DateTime.pm | ||
| 4 | ## Version v1.0.0 | ||
| 5 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 7 | ## Created 2019/12/15 | ||
| 8 | ## Modified 2023/06/10 | ||
| 9 | ## All rights reserved | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package Net::API::REST::DateTime; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use common::sense; | ||
| 20 | use parent qw( Apache2::API::DateTime ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | our $VERSION = 'v1.0.0'; | ||
| 23 | }; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | 1; | ||
| 29 | # NOTE: pod | ||
| 30 | __END__ | ||
| 31 | |||
| 32 | =encoding utf8 | ||
| 33 | |||
| 34 | =head1 NAME | ||
| 35 | |||
| 36 | Net::API::REST::DateTime - HTTP DateTime Manipulation and Formatting | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | use Net::API::REST::DateTime; | ||
| 41 | my $d = Net::API::REST::DateTime->new( debug => 3 ); | ||
| 42 | my $dt = DateTime->now; | ||
| 43 | $dt->set_formatter( $d ); | ||
| 44 | print( "$dt\n" ); | ||
| 45 | ## will produce | ||
| 46 | Sun, 15 Dec 2019 15:32:12 GMT | ||
| 47 | |||
| 48 | my( @parts ) = $d->parse_date( $date_string ); | ||
| 49 | |||
| 50 | my $datetime_object = $d->str2datetime( $date_string ); | ||
| 51 | $datetime_object->set_formatter( $d ); | ||
| 52 | my $timestamp_in_seconds = $d->str2time( $date_string ); | ||
| 53 | my $datetime_object = $d->time2datetime( $timestamp_in_seconds ); | ||
| 54 | my $datetime_string = $d->time2str( $timestamp_in_seconds ); | ||
| 55 | |||
| 56 | =head1 VERSION | ||
| 57 | |||
| 58 | v1.0.0 | ||
| 59 | |||
| 60 | =head1 DESCRIPTION | ||
| 61 | |||
| 62 | This module contains methods to create and manipulate datetime representation from and to C<DateTime> object or unix timestamps. | ||
| 63 | |||
| 64 | When using it as a formatter to a C<DateTime> object, this will make sure it is properly formatted for its use in http headers and cookies. | ||
| 65 | |||
| 66 | As of version <v1.0.0> it completely inherits from L<Apache2::API::DateTime> | ||
| 67 | |||
| 68 | =head1 METHODS | ||
| 69 | |||
| 70 | =head2 new( hash ) | ||
| 71 | |||
| 72 | This initiates the package and take the following parameters: | ||
| 73 | |||
| 74 | =over 4 | ||
| 75 | |||
| 76 | =item I<debug> | ||
| 77 | |||
| 78 | Optional. If set with a positive integer, this will activate verbose debugging message | ||
| 79 | |||
| 80 | =back | ||
| 81 | |||
| 82 | =head2 format_datetime( $date_time_object ) | ||
| 83 | |||
| 84 | Provided a C<DateTime> object, this returns a http compliant string representation, such as: | ||
| 85 | |||
| 86 | Sun, 15 Dec 2019 15:32:12 GMT | ||
| 87 | |||
| 88 | that can be used in http headers and cookies' expires property as per rfc6265. | ||
| 89 | |||
| 90 | =head2 parse_date( string ) | ||
| 91 | |||
| 92 | Given a datetime string, this returns, in list context, a list of day, month, year, hour, minute, second and time zone or an iso 8601 datetime string in scalar context. | ||
| 93 | |||
| 94 | This is used by the method B<str2datetime> | ||
| 95 | |||
| 96 | =head2 str2datetime( string ) | ||
| 97 | |||
| 98 | Given a string that looks like a date, this will parse it and return a C<DateTime> object. | ||
| 99 | |||
| 100 | =head2 str2time( string ) | ||
| 101 | |||
| 102 | Given a string that looks like a date, this returns its representation as a unix timestamp in second since epoch. | ||
| 103 | |||
| 104 | In the background, it calls B<str2datetime> for parsing. | ||
| 105 | |||
| 106 | =head2 time2datetime( timestamp in seconds ) | ||
| 107 | |||
| 108 | Given a unix timestamp in seconds since epoch, this returns a C<DateTime> object. | ||
| 109 | |||
| 110 | =head2 time2str( timestamp in seconds ) | ||
| 111 | |||
| 112 | Given a unix timestamp in seconds since epoch, this returns a string representation of the timestamp suitable for http headers and cookies. The format is like C<Sat, 14 Dec 2019 22:12:30 GMT> | ||
| 113 | |||
| 114 | =head1 AUTHOR | ||
| 115 | |||
| 116 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 117 | |||
| 118 | CPAN ID: jdeguest | ||
| 119 | |||
| 120 | L<https://gitlab.com/jackdeguest/Net-API-REST> | ||
| 121 | |||
| 122 | =head1 SEE ALSO | ||
| 123 | |||
| 124 | C<DateTime> | ||
| 125 | |||
| 126 | =head1 COPYRIGHT & LICENSE | ||
| 127 | |||
| 128 | Copyright (c) 2018-2019 DEGUEST Pte. Ltd. | ||
| 129 | |||
| 130 | You can use, copy, modify and redistribute this package and associated | ||
| 131 | files under the same terms as Perl itself. | ||
| 132 | |||
| 133 | =cut |