You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl-cvs@perl.apache.org by do...@hyperreal.org on 1998/07/18 19:00:52 UTC

cvs commit: modperl/src/modules/perl Util.xs

dougm       98/07/18 10:00:52

  Modified:    Util     Util.pm
               t/net/perl util.pl
               src/modules/perl Util.xs
  Log:
  more Apache::Util stuff
  
  Revision  Changes    Path
  1.2       +27 -1     modperl/Util/Util.pm
  
  Index: Util.pm
  ===================================================================
  RCS file: /export/home/cvs/modperl/Util/Util.pm,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Util.pm	1998/07/18 14:44:15	1.1
  +++ Util.pm	1998/07/18 17:00:50	1.2
  @@ -7,7 +7,7 @@
   use DynaLoader ();
   
   @ISA = qw(Exporter DynaLoader);
  -@EXPORT_OK = qw(escape_html escape_uri);
  +@EXPORT_OK = qw(escape_html escape_uri parsedate ht_time);
   %EXPORT_TAGS = (all => \@EXPORT_OK);
   $VERSION = '0.01';
   
  @@ -70,6 +70,32 @@
   escape sequence and returns the result.
   
    my $esc = Apache::Util::escape_uri($uri);
  +
  +=item parsedate
  +
  +Parses an HTTP date in one of three standard forms:
  +
  + Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
  +
  + Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
  +
  + Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format       
  +
  +Example:
  +
  + my $secs = Apache::Util::parsedate($date_str);
  +
  +=item ht_time
  +
  +Format a time string.
  +
  +Examples:
  +
  + my $str = Apache::Util::ht_time(time);
  +
  + my $str = Apache::Util::ht_time(time, "%d %b %Y %T %Z");
  +
  + my $str = Apache::Util::ht_time(time, "%d %b %Y %T %Z", 0);
   
   =back
   
  
  
  
  1.2       +85 -3     modperl/t/net/perl/util.pl
  
  Index: util.pl
  ===================================================================
  RCS file: /export/home/cvs/modperl/t/net/perl/util.pl,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- util.pl	1998/07/18 14:44:17	1.1
  +++ util.pl	1998/07/18 17:00:51	1.2
  @@ -2,6 +2,7 @@
   use Apache::test;
   $|++;
   my $i = 0;
  +my $tests = 4;
   
   my $r = shift;
   $r->send_http_header('text/plain');
  @@ -18,6 +19,29 @@
       return;
   }
   
  +my $test_date_format = 0;
  +my $test_time_parsedate = 0;
  +
  +eval {
  +    require Date::Format;
  +};
  +
  +unless($@) {
  +    $test_date_format = 2;
  +    $tests += $test_date_format;
  +}
  +$@ = '';
  +
  +eval {
  +    require Time::ParseDate;
  +};
  +
  +unless($@) {
  +    $test_time_parsedate = 1;
  +    #$tests += $test_time_parsedate;
  +}
  +$@ = '';
  + 
   my $html = <<EOF;
   <html>
   <head>
  @@ -29,7 +53,7 @@
   </html>
   EOF
   
  -print "1..3\n";
  +print "1..$tests\n";
   
   my $esc = Apache::Util::escape_html($html);
   #print $esc;
  @@ -38,9 +62,9 @@
   
   #print $esc_2;
   test ++$i, $esc eq $esc_2;
  +use Benchmark;
   
   =pod
  -use Benchmark;
   timethese(1000, {
       C => sub { my $esc = Apache::Util::escape_html($html) },
       Perl => sub { my $esc = HTML::Entities::encode($html) },
  @@ -58,7 +82,6 @@
   test ++$i, lc($C) eq lc($Perl); 
   
   =pod
  -use Benchmark;
   timethese(10000, {
       C => sub { my $esc = Apache::Util::escape_uri($uri) },
       Perl => sub { my $esc = URI::Escape::uri_escape($uri) },
  @@ -82,4 +105,63 @@
       Perl => sub { my $d = HTTP::Date::time2str() },
       Perl_builtin => sub { my $d = scalar gmtime },
   });  
  +=cut
  +
  +my @formats = ("%d %b %Y %T %Z", "%a, %d %B %Y");
  +
  +if($test_date_format) {
  +    print "Testing Date::Format\n";
  +    for my $fmt (@formats) {
  +	my $c = Apache::Util::ht_time(time, $fmt, 0);
  +	my $p = Date::Format::time2str($fmt, time);
  +	print "C=$c\nPerl=$p\n";
  +	test ++$i, $c eq $p;
  +    }
  +}
  +=pod
  +timethese(10000, {
  +    C => sub {
  +	for my $fmt (@formats) {
  +	    my $c = Apache::Util::ht_time(time, $fmt, 0);
  +	}
  +    },
  +    Perl => sub {
  +	for my $fmt (@formats) {
  +	    my $p = Date::Format::time2str($fmt, time);
  +	}
  +    },
  +});
  +=cut
  +=pod
  +Benchmark: timing 10000 iterations of C, Perl...
  +  C:  3 secs ( 2.64 usr  0.05 sys =  2.69 cpu)
  +  Perl: 21 secs (20.57 usr  0.14 sys = 20.71 cpu) 
  +=cut
  +
  +my $date_str = "Sat, 18 Jul 1998 08:38:00 -0700";
  +
  +test ++$i, Apache::Util::parsedate($date_str);
  +
  +if($test_time_parsedate) {
  +    my $c = Apache::Util::parsedate($date_str);
  +    print "C says: ", scalar(localtime $c), "\n";
  +
  +    my $p = Time::ParseDate::parsedate($date_str);
  +    print "Perl says: ", scalar(localtime $p), "\n";
  +}
  +
  +=pod
  +timethese(10000, {
  +    C => sub {
  +	my $c = Apache::Util::parsedate($date_str);
  +    },
  +    Perl => sub {
  +	my $p = Time::ParseDate::parsedate($date_str);
  +    },
  +});
  +=cut
  +=pod
  +Benchmark: timing 10000 iterations of C, Perl...
  +  C:  1 secs ( 0.42 usr  0.00 sys =  0.42 cpu)
  +  Perl: 17 secs (16.76 usr  0.07 sys = 16.83 cpu)  
   =cut
  
  
  
  1.2       +8 -0      modperl/src/modules/perl/Util.xs
  
  Index: Util.xs
  ===================================================================
  RCS file: /export/home/cvs/modperl/src/modules/perl/Util.xs,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Util.xs	1998/07/18 14:44:16	1.1
  +++ Util.xs	1998/07/18 17:00:52	1.2
  @@ -6,6 +6,8 @@
   
   #define TIME_NOW time(NULL)
   #define DEFAULT_TIME_FORMAT "%a, %d %b %Y %H:%M:%S %Z"
  +
  +#define parsedate ap_parseHTTPdate
    
   static pool *util_pool(void)
   {
  @@ -56,3 +58,9 @@
   
       OUTPUT:
       RETVAL
  +
  +time_t
  +parsedate(date)
  +    const char *date
  +
  +