• Index
  •  » Talend Open Studio for Data Integration » Installation
  •  » Greater or less than Date Routine?

#1 2007-05-29 22:07:26

David Weber
Member
Registered: 2007-04-12
Posts: 43

Greater or less than Date Routine?

Tags: [date, perl, routine]

I am looking to come up with a routine to return a date of < 1950/01/01 >  if the date is less than < 1950/01/01 > or Blank   So far I have to the routine below.   


sub testIssueDate {
      if ( $BondEdge_Raw[Issue_Date] gt "1950/01/01" )
            { return '$BondEdge_Raw[Issue_Date]'; }
       else
           { return '1950/01/01'; }
}


Below is my input:

9/29/1988
10/29/1944
9/29/1945

9/29/1988
9/29/1988

This is my output after the job is run:

9/29/1988
10/29/1944
9/29/1945
   0/00/00
9/29/1988
9/29/1988


Can anyone help me out with this?
Thanks,
David

Offline

#2 2007-05-30 01:45:12

c0utta
Member
Registered: 2007-04-13
Posts: 134

Re: Greater or less than Date Routine?

Hi David,

I'm an absolute amateur when it comes to Perl, but date values are handled as Epoch Seconds, not as character strings or dates.  I had to spend some time to get my head around this, but now it's second nature.  You're always returning $BondEdge_Raw[Issue_Date] because the gt comparison is irrelevant.

Have a look at http://www.unix.org.ua/orelly/perl/cookbook/ch03_03.htm if you want to use Epoch Seconds.  I assume you're interested in local time, so you'll need to do:

      my $date1950 = timelocal(0, 0, 0, 1, 0, 50);

You'll then need to convert your incoming date using timelocal as well.  Then you can easily do comparisons because you're dealing with numbers.

My disclaimer is that I'm no Perl programmer, so I tend to do things the long way, whereas a real Perl programmer would be able to this in one line of code!

Cheers,

c0utta

Offline

#3 2007-05-30 10:00:10

rbillerey
Talend team
Registered: 2006-09-22
Posts: 150

Re: Greater or less than Date Routine?

Hi David,

c0utta is right and the recipe from the Perl cookbook is one way to do it. Another way is to use Date::Manip cpan module : http://cpan.uwinnipeg.ca/htdocs/DateMan … anip.html.

Code:

use Date::Manip ;
# French summer time zone
$ENV{TZ} = 'FST' ;

# dateA cmp dateB : 
# -1 if dateA < dateB
#  0 if dateA = dateB
#  1 if dateA > dateB
# perl cmp operator works when dates are in the same time zone

print ParseDate("1950/01/01") cmp ParseDate("10/02/1988"), "\n";

# use Date_Cmp with dates from different timezone

print Date_Cmp( ParseDate("1950/01/01") , ParseDate("10/02/1988") ), "\n";

Hope this helps.

Last edited by rbillerey (2007-05-30 10:06:54)

Offline

#4 2007-05-30 10:01:38

plegall
Member
Registered: 2006-09-19
Posts: 1586
Website

Re: Greater or less than Date Routine?

My routine file has the following content:

Code:

use Exporter;

use vars qw(@EXPORT @ISA);

@ISA = qw(Exporter);
@EXPORT = qw(
    testIssueDate
);


sub testIssueDate {
    my $datestring = $_[0];
    
    if ((split('/', $datestring))[2] > 1950) {
        return $datestring;
    }
    else {
        return '1950/01/01';
    }
}

1;

The output of my job is:

Code:

Starting job topic792 at 09:58 30/05/2007.
.-------------------------.
|        tLogRow_1        |
+------------+------------+
| old_date   | new_date   |
+------------+------------+
| 9/29/1988  | 9/29/1988  |
| 10/29/1944 | 1950/01/01 |
| 9/29/1945  | 1950/01/01 |
| 9/29/1988  | 9/29/1988  |
| 9/29/1988  | 9/29/1988  |
'------------+------------'
Job topic792 ended at 09:58 30/05/2007. [exit code=0]

Uploaded Images

Offline

#5 2007-05-30 17:16:25

David Weber
Member
Registered: 2007-04-12
Posts: 43

Re: Greater or less than Date Routine?

Thanks for your help. I got the job to work!

Offline

  • Index
  •  » Talend Open Studio for Data Integration » Installation
  •  » Greater or less than Date Routine?

Board footer

Powered by FluxBB