currency.csv contains using the code below.  The date has been
adjusted from 2000-12-30 00:16:19 UTC to PST.  The rest of the file is
still not being processed.

2000-12-29,16:16:19,PST,,,,


#!/usr/bin/perl
#
# cur2csv.pl
#

use strict;
use vars qw($started);
use vars qw($quote_date $cur_sym $cur_desc $usd_unit $units_usd);
use vars qw($year $month $mday $hour $minute $second $timezone);
use vars qw($conv_date $date $time $tz);


use Date::Manip;
use String::Strip;

use DBI;
use DBD::Pg;

open (OUTFILE, ">", "currency.csv") || die "Can not open currency.csv
for writing";

printf STDERR "Reading currency file...";
open (INFILE, "curtest") || die "Can not open /var/spool/mail/currency
for reading";

while (<INFILE>) {

        
        # Extract date and time of Currency Rate Quotation
    
        ($year, $month, $mday, $hour, $minute, $second, $timezone) =
        /^Rates as of (\d+).(\d+).(\d+) (\d+):(\d+):(\d+) (\w+) (.*)$/; 

        # Convert date from UTC (GMT) to PST8PDT and adjust date and time
accordingly.

        $tz = &Date_TimeZone;   
        $conv_date = "$year-$month-$mday $hour:$minute:$second";
        $conv_date = &ParseDate($conv_date);
        $conv_date = &Date_ConvTZ($conv_date, $timezone, $tz);  
        $date      = &UnixDate($conv_date,"%Y-%m-%d");
        $time      = &UnixDate($conv_date,"%H:%M:%S");
        $tz        = &UnixDate($conv_date,"%Z");

        $year and last;    # If we've matched the data line, then bail out.
        
        eof and print STDERR "Didn't find the date line";
        
        }

        # Extract the ISO 4217 Code for Currencies and Funds (1995)
        # Extract the Currency Description, and trim the trailing spaces
        # Extract US Dollars to Units rate, and trim the leading/trailing
spaces
        # Extract Units to US Dollars rate, and trim the leading/trailing
spaces
        
        while (<INFILE>) {
        
            ($cur_sym, $cur_desc, $usd_unit, $units_usd) =
            /^([A-Z]{3})( [A-Za-z])+\s+(\d+\.\d+)\s+(\d+\.\d+)\s*$/;
        
            $cur_sym and $started++;
        
            not $cur_sym and ($started and last) or next;
            
            substr($cur_desc, 0, 1) = '';  # Delete leading space
        
        }       
            

        printf OUTFILE "%s\,%s\,%s\,%s\,%s\,%s\,%s\n",
                $date, $time, $tz, $cur_sym, $cur_desc, $usd_unit, $units_usd;
    
close(INFILE);
close(OUTFILE);
print STDERR "\n";

1;

Reply via email to