home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Date.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-30  |  10.6 KB  |  362 lines

  1. #============================================================= -*-Perl-*-
  2. #
  3. # Template::Plugin::Date
  4. #
  5. # DESCRIPTION
  6. #
  7. #   Plugin to generate formatted date strings.
  8. #
  9. # AUTHORS
  10. #   Thierry-Michel Barral  <kktos@electron-libre.com>
  11. #   Andy Wardley           <abw@cre.canon.co.uk>
  12. #
  13. # COPYRIGHT
  14. #   Copyright (C) 2000 Thierry-Michel Barral, Andy Wardley.
  15. #
  16. #   This module is free software; you can redistribute it and/or
  17. #   modify it under the same terms as Perl itself.
  18. #
  19. #----------------------------------------------------------------------------
  20. #
  21. # $Id: Date.pm,v 2.71 2004/01/13 16:20:38 abw Exp $
  22. #
  23. #============================================================================
  24.  
  25. package Template::Plugin::Date;
  26.  
  27. use strict;
  28. use vars qw( $VERSION $FORMAT @LOCALE_SUFFIX );
  29. use base qw( Template::Plugin );
  30. use Template::Plugin;
  31.  
  32. use POSIX ();
  33.  
  34. $VERSION = sprintf("%d.%02d", q$Revision: 2.71 $ =~ /(\d+)\.(\d+)/);
  35. $FORMAT  = '%H:%M:%S %d-%b-%Y';    # default strftime() format
  36. @LOCALE_SUFFIX = qw( .ISO8859-1 .ISO_8859-15 .US-ASCII .UTF-8 );
  37.  
  38. #------------------------------------------------------------------------
  39. # new(\%options)
  40. #------------------------------------------------------------------------
  41.  
  42. sub new {
  43.     my ($class, $context, $params) = @_;
  44.     bless {
  45.     $params ? %$params : ()
  46.     }, $class;
  47. }
  48.  
  49.  
  50. #------------------------------------------------------------------------
  51. # now()
  52. # Call time() to return the current system time in seconds since the epoch.
  53. #------------------------------------------------------------------------
  54.  
  55. sub now {
  56.     return time();
  57. }
  58.  
  59.  
  60. #------------------------------------------------------------------------
  61. # format()                           
  62. # format($time)
  63. # format($time, $format)
  64. # format($time, $format, $locale)
  65. # format($time, $format, $locale, $gmt_flag)
  66. # format(\%named_params);
  67. # Returns a formatted time/date string for the specified time, $time, 
  68. # (or the current system time if unspecified) using the $format, $locale,
  69. # and $gmt values specified as arguments or internal values set defined
  70. # at construction time).  Specifying a Perl-true value for $gmt will
  71. # override the local time zone and force the output to be for GMT.
  72. # Any or all of the arguments may be specified as named parameters which 
  73. # get passed as a hash array reference as the final argument.
  74. # ------------------------------------------------------------------------
  75.  
  76. sub format {
  77.     my $self   = shift;
  78.     my $params = ref($_[$#_]) eq 'HASH' ? pop(@_) : { };
  79.     my $time   = shift(@_) || $params->{ time } || $self->{ time } 
  80.                || $self->now();
  81.     my $format = @_ ? shift(@_) 
  82.             : ($params->{ format } || $self->{ format } || $FORMAT);
  83.     my $locale = @_ ? shift(@_)
  84.             : ($params->{ locale } || $self->{ locale });
  85.     my $gmt = @_ ? shift(@_)
  86.             : ($params->{ gmt } || $self->{ gmt });
  87.     my (@date, $datestr);
  88.  
  89.     if ($time =~ /^\d+$/) {
  90.         # $time is now in seconds since epoch
  91.         if ($gmt) {
  92.             @date = (gmtime($time))[0..6];
  93.         }
  94.         else {
  95.             @date = (localtime($time))[0..6];
  96.         }
  97.     }
  98.     else {
  99.         # if $time is numeric, then we assume it's seconds since the epoch
  100.         # otherwise, we try to parse it as a 'H:M:S D:M:Y' string
  101.         @date = (split(/(?:\/| |:|-)/, $time))[2,1,0,3..5];
  102.         return (undef, Template::Exception->new('date',
  103.                "bad time/date string:  expects 'h:m:s d:m:y'  got: '$time'"))
  104.             unless @date >= 6 && defined $date[5];
  105.         $date[4] -= 1;     # correct month number 1-12 to range 0-11
  106.         $date[5] -= 1900;  # convert absolute year to years since 1900
  107.         $time = &POSIX::mktime(@date);
  108.     }
  109.     
  110.     if ($locale) {
  111.     # format the date in a specific locale, saving and subsequently
  112.         # restoring the current locale.
  113.         my $old_locale = &POSIX::setlocale(&POSIX::LC_ALL);
  114.  
  115.         # some systems expect locales to have a particular suffix
  116.         for my $suffix ('', @LOCALE_SUFFIX) {
  117.             my $try_locale = $locale.$suffix;
  118.         my $setlocale = &POSIX::setlocale(&POSIX::LC_ALL, $try_locale);
  119.             if (defined $setlocale && $try_locale eq $setlocale) {
  120.                 $locale = $try_locale;
  121.                 last;
  122.             }
  123.         }
  124.         $datestr = &POSIX::strftime($format, @date);
  125.         &POSIX::setlocale(&POSIX::LC_ALL, $old_locale);
  126.     }
  127.     else {
  128.         $datestr = &POSIX::strftime($format, @date);
  129.     }
  130.  
  131.     return $datestr;
  132. }
  133.  
  134. sub calc {
  135.     my $self = shift;
  136.     eval { require "Date/Calc.pm" };
  137.     $self->throw("failed to load Date::Calc: $@") if $@;
  138.     return Template::Plugin::Date::Calc->new('no context');
  139. }
  140.  
  141. sub manip {
  142.     my $self = shift;
  143.     eval { require "Date/Manip.pm" };
  144.     $self->throw("failed to load Date::Manip: $@") if $@;
  145.     return Template::Plugin::Date::Manip->new('no context');
  146. }
  147.  
  148.  
  149. sub throw {
  150.     my $self = shift;
  151.     die (Template::Exception->new('date', join(', ', @_)));
  152. }
  153.  
  154.  
  155. package Template::Plugin::Date::Calc;
  156. use base qw( Template::Plugin );
  157. use vars qw( $AUTOLOAD );
  158. *throw = \&Template::Plugin::Date::throw;
  159.  
  160. sub AUTOLOAD {
  161.     my $self = shift;
  162.     my $method = $AUTOLOAD;
  163.  
  164.     $method =~ s/.*:://;
  165.     return if $method eq 'DESTROY';
  166.  
  167.     my $sub = \&{"Date::Calc::$method"};
  168.     $self->throw("no such Date::Calc method: $method")
  169.     unless $sub;
  170.  
  171.     &$sub(@_);
  172. }
  173.  
  174. package Template::Plugin::Date::Manip;
  175. use base qw( Template::Plugin );
  176. use vars qw( $AUTOLOAD );
  177. *throw = \&Template::Plugin::Date::throw;
  178.  
  179. sub AUTOLOAD {
  180.     my $self = shift;
  181.     my $method = $AUTOLOAD;
  182.     
  183.     $method =~ s/.*:://;
  184.     return if $method eq 'DESTROY';
  185.     
  186.     my $sub = \&{"Date::Manip::$method"};
  187.     $self->throw("no such Date::Manip method: $method")
  188.         unless $sub;
  189.     
  190.     &$sub(@_);
  191. }
  192.     
  193.     
  194. 1;
  195.  
  196. __END__
  197.  
  198.  
  199. #------------------------------------------------------------------------
  200. # IMPORTANT NOTE
  201. #   This documentation is generated automatically from source
  202. #   templates.  Any changes you make here may be lost.
  203. #   The 'docsrc' documentation source bundle is available for download
  204. #   from http://www.template-toolkit.org/docs.html and contains all
  205. #   the source templates, XML files, scripts, etc., from which the
  206. #   documentation for the Template Toolkit is built.
  207. #------------------------------------------------------------------------
  208.  
  209. =head1 NAME
  210.  
  211. Template::Plugin::Date - Plugin to generate formatted date strings
  212.  
  213. =head1 SYNOPSIS
  214.  
  215.     [% USE date %]
  216.  
  217.     # use current time and default format
  218.     [% date.format %]
  219.  
  220.     # specify time as seconds since epoch or 'h:m:s d-m-y' string
  221.     [% date.format(960973980) %]
  222.     [% date.format('4:20:36 21/12/2000') %]
  223.  
  224.     # specify format
  225.     [% date.format(mytime, '%H:%M:%S') %]
  226.  
  227.     # specify locale
  228.     [% date.format(date.now, '%a %d %b %y', 'en_GB') %]
  229.  
  230.     # named parameters 
  231.     [% date.format(mytime, format = '%H:%M:%S') %]
  232.     [% date.format(locale = 'en_GB') %]
  233.     [% date.format(time   = date.now, 
  234.            format = '%H:%M:%S', 
  235.                    locale = 'en_GB) %]
  236.    
  237.     # specify default format to plugin
  238.     [% USE date(format = '%H:%M:%S', locale = 'de_DE') %]
  239.  
  240.     [% date.format %]
  241.     ...
  242.  
  243. =head1 DESCRIPTION
  244.  
  245. The Date plugin provides an easy way to generate formatted time and date
  246. strings by delegating to the POSIX strftime() routine.  
  247.  
  248. The plugin can be loaded via the familiar USE directive.
  249.  
  250.     [% USE date %]
  251.  
  252. This creates a plugin object with the default name of 'date'.  An alternate
  253. name can be specified as such:
  254.  
  255.     [% USE myname = date %]
  256.  
  257. The plugin provides the format() method which accepts a time value, a
  258. format string and a locale name.  All of these parameters are optional
  259. with the current system time, default format ('%H:%M:%S %d-%b-%Y') and
  260. current locale being used respectively, if undefined.  Default values
  261. for the time, format and/or locale may be specified as named parameters 
  262. in the USE directive.
  263.  
  264.     [% USE date(format = '%a %d-%b-%Y', locale = 'fr_FR') %]
  265.  
  266. When called without any parameters, the format() method returns a string
  267. representing the current system time, formatted by strftime() according 
  268. to the default format and for the default locale (which may not be the
  269. current one, if locale is set in the USE directive).
  270.  
  271.     [% date.format %]
  272.  
  273. The plugin allows a time/date to be specified as seconds since the epoch,
  274. as is returned by time().
  275.  
  276.     File last modified: [% date.format(filemod_time) %]
  277.  
  278. The time/date can also be specified as a string of the form 'h:m:s d/m/y'.
  279. Any of the characters : / - or space may be used to delimit fields.
  280.  
  281.     [% USE day = date(format => '%A', locale => 'en_GB') %]
  282.     [% day.format('4:20:00 9-13-2000') %]  
  283.  
  284. Output:
  285.  
  286.     Tuesday
  287.  
  288. A format string can also be passed to the format() method, and a locale
  289. specification may follow that.
  290.  
  291.     [% date.format(filemod, '%d-%b-%Y') %]
  292.     [% date.format(filemod, '%d-%b-%Y', 'en_GB') %]
  293.  
  294. A fourth parameter allows you to force output in GMT, in the case of 
  295. seconds-since-the-epoch input:
  296.  
  297.     [% date.format(filemod, '%d-%b-%Y', 'en_GB', 1) %]
  298.  
  299. Note that in this case, if the local time is not GMT, then also specifying
  300. '%Z' (time zone) in the format parameter will lead to an extremely 
  301. misleading result.
  302.  
  303. Any or all of these parameters may be named.  Positional parameters
  304. should always be in the order ($time, $format, $locale).
  305.  
  306.     [% date.format(format => '%H:%M:%S') %]
  307.     [% date.format(time => filemod, format => '%H:%M:%S') %]
  308.     [% date.format(mytime, format => '%H:%M:%S') %]
  309.     [% date.format(mytime, format => '%H:%M:%S', locale => 'fr_FR') %]
  310.     [% date.format(mytime, format => '%H:%M:%S', gmt => 1) %]
  311.     ...etc...
  312.  
  313. The now() method returns the current system time in seconds since the 
  314. epoch.  
  315.  
  316.     [% date.format(date.now, '%A') %]
  317.  
  318. The calc() method can be used to create an interface to the Date::Calc
  319. module (if installed on your system).
  320.  
  321.     [% calc = date.calc %]
  322.     [% calc.Monday_of_Week(22, 2001).join('/') %]
  323.  
  324. The manip() method can be used to create an interface to the Date::Manip
  325. module (if installed on your system).
  326.  
  327.     [% manip = date.manip %]
  328.     [% manip.UnixDate("Noon Yesterday","%Y %b %d %H:%M") %]
  329.  
  330. =head1 AUTHORS
  331.  
  332. Thierry-Michel Barral E<lt>kktos@electron-libre.comE<gt> wrote the original
  333. plugin.
  334.  
  335. Andy Wardley E<lt>abw@cre.canon.co.ukE<gt> provided some minor
  336. fixups/enhancements, a test script and documentation.
  337.  
  338. Mark D. Mills E<lt>mark@hostile.orgE<gt> cloned Date::Manip from the
  339. cute Date::Calc sub-plugin.
  340.  
  341. =head1 VERSION
  342.  
  343. 2.71, distributed as part of the
  344. Template Toolkit version 2.13, released on 30 January 2004.
  345.  
  346.  
  347.  
  348. =head1 COPYRIGHT
  349.  
  350. Copyright (C) 2000 Thierry-Michel Barral, Andy Wardley.
  351.  
  352. This module is free software; you can redistribute it and/or
  353. modify it under the same terms as Perl itself.
  354.  
  355. =head1 SEE ALSO
  356.  
  357. L<Template::Plugin|Template::Plugin>, L<POSIX|POSIX>
  358.  
  359.