fixed typo that caused MARC21 leader form to emit 25 characters instead of 24
[koha_fer] / C4 / Dates.pm
index 67430c9..320d04d 100644 (file)
@@ -24,7 +24,7 @@ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
 $VERSION = 0.03;
 @ISA = qw(Exporter);
-@EXPORT_OK = qw(DHTMLcalendar);
+@EXPORT_OK = qw(DHTMLcalendar format_date_in_iso format_date);
 
 my $prefformat = C4::Context->preference('dateformat');
 my $debug = $ENV{'DEBUG'} || 0;
@@ -45,7 +45,7 @@ our %posix_map = (
 );
 
 our %dmy_subs = (                      # strings to eval  (after using regular expression returned by regexp below)
-                                                       # make arrayrs for POSIX::strftime()
+                                                       # make arrays for POSIX::strftime()
          iso  => '[(0,0,0,$3, $2 - 1, $1 - 1900)]',            
        metric => '[(0,0,0,$1, $2 - 1, $3 - 1900)]',
          us   => '[(0,0,0,$2, $1 - 1, $3 - 1900)]',
@@ -99,7 +99,7 @@ sub init ($;$$) {
 sub output ($;$) {
        my $self = shift;
        my $newformat = (@_) ? _recognize_format(shift) : $self->{'dateformat'} ;
-       return POSIX::strftime($posix_map{$newformat}, @self::dmy_array);
+       return (eval {POSIX::strftime($posix_map{$newformat}, @self::dmy_array)} || undef);
 }
 sub today ($;$) {              # NOTE: sets date value to today (and returns it in the requested or current format)
        my $class = shift;
@@ -126,9 +126,18 @@ sub format {       # get or set dateformat: iso, metric, us, etc.
 sub visual {
        my $self = shift;
        if (@_) {
-               return $format_map{ shift };
+               return $format_map{ _recognize_format(shift) };
        }
-       return $format_map{$self->{'dateformat'} || $prefformat} ;
+       $self eq __PACKAGE__ and return $format_map{$prefformat};
+       return $format_map{ eval { $self->{'dateformat'} } || $prefformat} ;
+}
+
+# like the functions from the old C4::Date.pm
+sub format_date {
+       return __PACKAGE__ -> new(shift,'iso')->output((@_) ? shift : $prefformat);
+}
+sub format_date_in_iso {
+       return __PACKAGE__ -> new(shift,$prefformat)->output('iso');
 }
 
 1;
@@ -204,17 +213,32 @@ Or even:
 
                print C4::Dates->new("21-09-1989", "metric")->output("iso");
 
-=head2 ->DHMTLCalendar([date_format])
-
-Returns the format string for DHTML Calendar Display based on date_format.  
-If date_format is not supplied, the return is based on system preference.
+=head2 "syspref" -- System Preference(s)
 
-               C4::Dates->new()->DHTMLCalendar();      #  e.g., returns "%m/%d/%Y" for 'us' system preference
+Perhaps you want to force data obtained in a known format to display according to the user's system
+preference, without necessarily knowing what that preference is.  For this purpose, you can use the
+psuedo-format argument "syspref".  
 
-Format dates from database in ISO format into the <systempreference> format for display to user:
+For example, to print an ISO date (from the database) in the <systempreference> format:
 
                my $date = C4::Dates->new($date_from_database,"iso");
                my $datestring_for_display = $date->display("syspref");
+               print $datestring_for_display;
+
+Or even:
+
+               print C4::Dates->new($date_from_database,"iso")->display("syspref");
+
+If you just want to know what the <systempreferece> is, you can use:
+
+C4::Dates->
+
+=head2 ->DHMTLcalendar([date_format])
+
+Returns the format string for DHTML Calendar Display based on date_format.  
+If date_format is not supplied, the return is based on system preference.
+
+               C4::Dates->DHTMLcalendar();     #  e.g., returns "%m/%d/%Y" for 'us' system preference
 
 =head3 Error Handling