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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display a date 2

Status
Not open for further replies.

donny750

Programmer
Joined
Jul 13, 2006
Messages
145
Location
FR
hello;

I'want to make this;
the name of a file with the date
for example
test20061025115822.pl

i'dont know how to take the date in a variable with perl
and how to display the date like this
YYYYMMDDHHMMSS ?

because i want to add the variable containing the date to a file name;
test.$date.pl;



Thanks
 
You can use the "strftime" function in the core POSIX module.
 
Or if you'd rather not use a module to do this:

Code:
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon += 1;
 
my $datetime = sprintf "%02d/%02d/%04d %02d:%02d:%02d", $mon, $mday, $year, $hour, $min, $sec;

- George
 
Oops, guess I should have formatted the date correctly.

Code:
my $datetime = sprintf "%04d%02d%02d%02d%02d%02d", $year, $mday, $mon, $hour, $min, $sec;

Sorry about that. Too early here.
 
perldoc -f localtime

Code:
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

That will give you the system time in your variables.

Now just setup your date variable:

Code:
my $datestring = sprintf("%4d%02d%02d%02d%02d%02d",($year + 1900),($mon+1),$mday,$hour,$min,$sec);

Then setup your filename:

Code:
my $filename = "test.$datestring.pl";
 
wow - sorry about that double post... George must have submitted his while I was typing mine - at least are answers are consistent though!
 
My grammer skills seem to have left me - I meant "at least OUR answers are consistent
 
These give equivalent results:
Code:
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon += 1;
my $datetime = sprintf "%04d%02d%02d%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec;
print "$datetime\n";

use POSIX;
my $datetime2 = strftime( '%Y%m%d%H%M%S', localtime );
print "$datetime2\n";

Personally, I'd always prefer to use a standard module that's part of every Perl distribution rather than messing around with manipulating the output of localtime to display the year and month the way I want it.
 
Brian

Spelling skills also? Gramm[red]a[/red]r! Still at least you posted it to the right forum - I stuck a perl answer to a question on the VBA forum the other day by mistake. The only consolation was that one of the other posters remarked that it would be nice if the VB split function worked the same as perl...[smile]

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]
 
I should have just stayed in bed this morning - Nothing that I posted turned out right :)

Ishnid - all the years I've been doing my method to get a date string, and now I see your example and how easy it is! How have I never seen strftime method? It is much easier than manually pulling out each peice of the date/time!

I sure learn something new every day!
 
thanks
for display the date i've use this

Code:
my $displaydate= `date +'%Y%m%d%H%M%S'`;

if (-f $filename){

$f2= $f1.$displaydate;
	rename($f1,$f2);
}
but iive a file with the good date but the script add "?"
and i don't understand this :
Fic20061027115014?
 
donny, why would you call an external program when Perl is more than capable of performing the task itself? All that serves to do is to make your code less portable cross-platform.

The reason for your ? character is because date adds a newline to the end of the string. It mustn't be being interpreted correctly when you try to rename to a file with a newline in its name.

Just do it with strftime:
Code:
use POSIX qw(strftime);
my $displaydate= strftime('%Y%m%d%H%M%S', localtime);

if (-f $filename){

$f2= $f1.$displaydate;
    rename($f1,$f2);
}
 
Yep, to resolve Donny's problem, add a chomp($displaydate) after your first line. You will always need to chomp anything you read in using a system command.

But ultimately, yes - you should not be calling an external program when perl can do it, and as always, perl can do it many ways in this case.
 
ok
thanks guys
with use POSIX qw(strftime);
it's run very good
i haven't use posix because i don't know this module
 
thanks kevin
very interesting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top