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

format filename to yyyymmdd.hhmmss

Status
Not open for further replies.

rozzay

Programmer
Jan 3, 2002
142
US
Hi very new at perl and was looking at the date/time stuff and hopefully anyone can help I am trying to rename a file to sample_yyyymmdd.hhmmss.1

Thanks in advance!
 
1. Have a look to the function localtime();

It will return the year,month,day hh,mm,sec

2. And then have a look to the sprintf function in order to set the desirable format..

3. To rename the file use the function:
rename()

Keep in mind that we need to see at least a little effort before post some code..


dmazzini
GSM System and Telecomm Consultant

 
Thanks for your help. so far I was able to get the format for the filename with the current date and time. I am having a problem with the month though. The format is suppose to be: sample_yyyymmdd.hhmmss.1
what I keep getting is sample_2005425.153542.1 it should be sample_20050525.153542.1
the month is a single digit and from what i understand how perl works it goes for the array of 0-11 so i need to add 1 in order to get the current month but how do I make sure it includes the 0 for instance for may it would be 05

my ($sec,$min,$hours,$mday,$month,$year,$wday,$yday,$isdst)=localtime();
$nowyear=$year+1900;

$retekfname="sample_$nowyear$month$mday.$hours$min$sec.1";
 
Hmmm, Big issue here.
It's not just the month that will cause trouble, the day will too when it is in single figures (as will the time elements).
You might do better to use a sprintf so that you can control leading zero suppression.

Code:
$retekfname = sprintf("sample_%4d%02d%02d.%02d%02d%02d.1",$nowyear, $month, $mday, $hour, $min, $sec);

Trojan

 
Without sprint function...

my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year+=1900; $mon+=1;
$mday="0".$mday if ($mday<10);
$mon="0".$mon if ($mon<10);
$hour="0".$hour if ($hour<10);
$min="0".$min if ($min<10);
$sec="0".$sec if ($sec<10);

$DateTime="$year-$mon-$mday"."T"."$hour:$min:$sec";


dmazzini
GSM System and Telecomm Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top