[ic] Modification of a passed Date Value

Kevin Walsh kevin at cursor.biz
Tue Mar 2 11:31:21 EST 2004


Gary Norton [gnorton at broadgap.com] wrote:
> If I have a stored in the following format: 20040218 (YYYYMMDD)
> 
> I would like to modify that format by adding 3 days, or perhaps
> subtracting 2. 
> 
> > From the docs it looks like I can use the  convert_date tag to do this,
> > but only if I do not pass a date: 
> 
> [convert_date -2][/convert_date] will subtract 2 days from the current
> date/time. 
> 
> [convert_date -2]20040218[/convert_date] ignores the -2 and just formats
> the date. 
> 
> The time tag looks like it only works with the current time as well. I
> don't' seem to be able to pass it a date value.
> 
> Am I using these tags incorrectly?
> 
It seems that tag wasn't written to apply the "days" offset to a
supplied date value - only to the current date (when no overriding
date value has been supplied).

The following patch seems to work, so I will apply it to the 5.1.x
development CVS unless anyone has an objection, or can suggest a better
way to do it:

======================================================================
*** convert_date.tag    29 Jan 2002 05:52:42 -0000      1.1
--- convert_date.tag    2 Mar 2004 16:26:38 -0000
***************
*** 8,13 ****
--- 8,14 ----
  sub {
      my ($days, $opt, $text) = @_;
      my @t;
+     my $now;

        if(! ref $opt) {
                my $raw = $opt ? 1 : 0;
***************
*** 32,42 ****
                                        $t[5] -= 1900;
        }
        else {
!                                       my $now = time();
!                                       if ($days) {
!                                                                       $now += $days * 86400;
!                                       }
!                                       @t = localtime($now);
        }

        if (defined $opt->{raw} and Vend::Util::is_yes($opt->{raw})) {
--- 33,46 ----
                                        $t[5] -= 1900;
        }
        else {
!                                       $now = time();
!                                       @t = localtime($now) unless $days;
!       }
!
!       if ($days) {
!               $now ||= mktime(@t);
!               $now += $days * 86400;
!               @t = localtime($now);
        }

        if (defined $opt->{raw} and Vend::Util::is_yes($opt->{raw})) {
======================================================================

Test page:

    No args: [convert-date][/convert-date]<br>
    2 days before now: [convert-date days="-2"][/convert-date]<br>
    2 days before 20040218: [convert-date days="-2"]20040218[/convert-date]<br>
    2 days before 2004-02-18: [convert-date days="-2"]2004-02-18[/convert-date]<br>
    3 days after 2004-02-18: [convert-date days="3"]2004-02-18[/convert-date]<br>
    20040218: [convert-date]20040218[/convert-date]<br>
    2004-02-18: [convert-date]2004-02-18[/convert-date]<br>

Test output:

    No args: 02-Mar-2004 04:29PM
    2 days before now: 29-Feb-2004 04:29PM
    2 days before 20040218: 16-Feb-2004
    2 days before 2004-02-18: 16-Feb-2004
    3 days after 2004-02-18: 21-Feb-2004
    20040218: 18-Feb-2004
    2004-02-18: 18-Feb-2004

-- 
   _/   _/  _/_/_/_/  _/    _/  _/_/_/  _/    _/
  _/_/_/   _/_/      _/    _/    _/    _/_/  _/   K e v i n   W a l s h
 _/ _/    _/          _/ _/     _/    _/  _/_/    kevin at cursor.biz
_/   _/  _/_/_/_/      _/    _/_/_/  _/    _/



More information about the interchange-users mailing list