Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filename with Yesterdays date

Status
Not open for further replies.

Bonestein

Technical User
May 13, 2003
67
US
I am trying to run a report and output it to a file with yesterdays date, the only problem I am having at the moment is getting the filename to be yesterdays dates.
I have 2 methods I am currently trying and it's not working very well.



first is:

#!/usr/local/bin/perl -w

use strict;
use DBI;
use DBD::Oracle;
use Text::CSV;
$ENV{ORACLE_HOME}="/oracle";

my $user = "fyiuser";
my $password = "fyiuser";
my $dbh;

$dbh = DBI->connect("dbi:Oracle:fyidocs.com", $user, $password, { RaiseError => 0, AutoCommit => 0 }) or die ("Could not conn
ect to DB");

my $yest_date = $dbh->prepare("select to_char(sysdate-1,'YYYYMMDD') from dual");
my $filename = $yest_date . ".csv";

print "$filename\n";


And this outputs: DBI::st=HASH(0x2758ec).csv
but is supposed to output: 20080203.csv

I don't know what I am doing wrong with this one. The next thing I am trying, I don't know how to format the date correctly is:

#!/usr/bin/perl

$yesterday = time() - ( 24 * 60 * 60 );

my $lastday = (localtime $yesterday);

print "$lastday \n";

This outputs: Sun Feb 3 20:28:57 2008

but I can't really use that as the filename, I would like just the year/month/day possibly "2008Feb3" would be nice.


Does anyone have any ideas on how to create a file and give it yesterdays date as a name?
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]POSIX[/green] [red]qw([/red][purple]strftime[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] strftime [red]"[/red][purple]%Y%m%d[/purple][red]"[/red], [url=http://perldoc.perl.org/functions/localtime.html][black][b]localtime[/b][/black][/url][red]([/red][url=http://perldoc.perl.org/functions/time.html][black][b]time[/b][/black][/url] - [fuchsia]60[/fuchsia][blue]*[/blue][fuchsia]60[/fuchsia][blue]*[/blue][fuchsia]24[/fuchsia][red])[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.10.0) Modules used :
[ul]
[li]POSIX - Perl interface to IEEE Std 1003.1[/li]
[/ul]
[/tt]

- Miller
 
You are my Hero!!


used this to create the filename and the script is working great!!


my $filename = (strftime "%Y%m%d", localtime(time - 60*60*24)) . ".csv";


Thanks again for the help.
 
Bonestein

For future reference, your original scheme would have worked, except that after you prepared the SQL statement
Code:
my $yest_date = $dbh->prepare("select to_char(sysdate-1,'YYYYMMDD') from dual");
$yest_date contains a reference to a statement handle which prints out as DBI::st=HASH(0x2758ec) as you have seen. You still have to execute this statement and read the result to get the date.

But unless you need to use the date in some kind of SQL selection predicate, MillerH's solution is probably about 100 times more efficient...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top