Fix handling of extra_query_params in Business::OnlinePayment wrapper.
[interchange.git] / code / UserTag / convert_date.tag
1 # Copyright 2002-2007 Interchange Development Group and others
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.  See the LICENSE file for details.
7
8 # $Id: convert_date.tag,v 1.9 2009-05-01 13:50:00 pajamian Exp $
9
10 UserTag convert-date Order       adjust
11 UserTag convert-date PosNumber   1
12 UserTag convert-date addAttr
13 UserTag convert-date AttrAlias   fmt format
14 UserTag convert-date AttrAlias   days adjust
15 UserTag convert-date HasEndTag
16 UserTag convert-date Interpolate
17 UserTag convert-date Version     $Revision: 1.9 $
18 UserTag convert-date Routine     <<EOR
19 sub {
20     my ($adjust, $opt, $text) = @_;
21     my @t;
22     my $now;
23
24         if(! ref $opt) {
25                 my $raw = $opt ? 1 : 0;
26                 $opt = {};
27                 $opt->{raw} = 1 if $raw;
28         }
29
30         my $fmt = $opt->{format} || '';
31         if($text =~ /^(\d\d\d\d)-(\d?\d)-(\d?\d)$/) {
32                 $t[5] = $1 - 1900;
33                 $t[4] = $2 - 1;
34                 $t[3] = $3;
35         } 
36         elsif($text =~ /\d/) {
37                                         $text =~ s/\D//g;
38                                         $text =~ /(\d\d\d\d)(\d\d)(\d\d)(?:(\d\d)(\d\d))?/;
39                                         $t[2] = $4 || undef;
40                                         $t[1] = $5 || undef;
41                                         $t[3] = $3;
42                                         $t[4] = $2 - 1;
43                                         $t[5] = $1;
44                                         $t[5] -= 1900;
45         }
46         elsif (exists $opt->{empty}) {
47                 return $opt->{empty};
48         }
49         else {
50                                         $now = time();
51                                         @t = localtime($now) unless $adjust;
52         }
53
54         if ($adjust) {
55                 if ($#t < 8) {
56                         $t[8] = -1;
57                 }
58                 $now ||= POSIX::mktime(@t);
59                 $adjust .= ' days' if $adjust =~ /^[-\s\d]+$/;
60                 @t = localtime(adjust_time($adjust, $now, $opt->{compensate_dst}));
61         }
62
63         if (defined $opt->{raw} and Vend::Util::is_yes($opt->{raw})) {
64                                         $fmt = $t[2] && $text ?  '%Y%m%d%H%M' : '%Y%m%d';
65         }
66
67         if (! $fmt) {
68                 if ($t[1] || $t[2]) {
69                         $fmt = '%d-%b-%Y %I:%M%p';
70                 } else {
71                         $fmt = '%d-%b-%Y';
72                 }
73         }
74
75         my ($current, $out);
76         my $locale = $opt->{locale} || $Scratch->{mv_locale};
77         if ($locale) {
78                 $current = POSIX::setlocale(&POSIX::LC_TIME);
79                 POSIX::setlocale(&POSIX::LC_TIME, $locale);
80                 $out = POSIX::strftime($fmt, @t);
81                 POSIX::setlocale(&POSIX::LC_TIME, $current);
82         } else {
83                 $out = POSIX::strftime($fmt, @t);
84         }
85         $out =~ s/\b0(\d)\b/$1/g if $opt->{zerofix};
86         return $out;
87 }
88 EOR