| 1 | package Apache::HeavyCGI::Date; | ||
| 2 | use 5.005; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | use vars qw($VERSION); | ||
| 6 | $VERSION = sprintf "%d.%03d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/; | ||
| 7 | |||
| 8 | use HTTP::Date (); | ||
| 9 | use overload '""' => "http"; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my($class,%arg) = @_; | ||
| 13 | $arg{unix} = time unless %arg; | ||
| 14 | my $self = bless {}, $class; | ||
| 15 | while (my($k,$v) = each %arg) { | ||
| 16 | $self->{$k} = $v; | ||
| 17 | } | ||
| 18 | $self; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub unix { | ||
| 22 | my $self = shift; | ||
| 23 | my($set) = @_; | ||
| 24 | if (defined $set) { | ||
| 25 | $self->{unix} = $set; | ||
| 26 | $self->{http} = undef; | ||
| 27 | } | ||
| 28 | return $self->{unix} if defined $self->{unix}; # can be 0 | ||
| 29 | $self->{unix} = HTTP::Date::str2time($self->{http}); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub http { | ||
| 33 | my $self = shift; | ||
| 34 | my($set) = @_; | ||
| 35 | if (defined $set) { | ||
| 36 | $self->{http} = $set; | ||
| 37 | $self->{unix} = undef; | ||
| 38 | } | ||
| 39 | unless (defined $self->{unix}) { | ||
| 40 | require Carp; | ||
| 41 | Carp::confess("No time in my object"); | ||
| 42 | } | ||
| 43 | $self->{http} ||= HTTP::Date::time2str($self->{unix}); # can't be 0 or "" | ||
| 44 | } | ||
| 45 | |||
| 46 | 1; | ||
| 47 | |||
| 48 | =head1 NAME | ||
| 49 | |||
| 50 | Apache::HeavyCGI::Date - represent a date as both unix time and HTTP time | ||
| 51 | |||
| 52 | =head1 SYNOPSIS | ||
| 53 | |||
| 54 | my $date = Apache::HeavyCGI::Date->new; | ||
| 55 | |||
| 56 | $date->unix(time); # set | ||
| 57 | print $date->unix; # get | ||
| 58 | print $date->http; # get as http | ||
| 59 | print $date; # same thing due to overloading | ||
| 60 | |||
| 61 | =head1 DESCRIPTION | ||
| 62 | |||
| 63 | This class implements a simple dual value date variable. There are | ||
| 64 | only two accessor methods that let you set and get dates. unix() sets | ||
| 65 | and gets the UNIX time, and http() gets and sets the HTTP time. | ||
| 66 | Whenever a time is set the other time gets undefined. Retrieving an | ||
| 67 | undefined time triggers a conversion from the other time. That way the | ||
| 68 | two times are always synced. | ||
| 69 | |||
| 70 | =head1 PREREQUISITES | ||
| 71 | |||
| 72 | The class uses HTTP::Date internally. | ||
| 73 | |||
| 74 | =head1 AUTHOR | ||
| 75 | |||
| 76 | andreas koenig <andreas.koenig@anima.de> | ||
| 77 | |||
| 78 | =cut | ||
| 79 |