TimeDate conversion to string.

T

Tony

I'm trying to use the output of the the date/time from localtime into a
label.:

($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime(time);
$year = ($year + 1900) - 2000;
$labelname = printf "REL_" . $proj[0] . "_%02d%02d%02d_%02d%02d%02d",
$hour,$min,$sec,$mon+1,$mday,$year;
print "$labelname\\n";

The problem is that when I input $labelname into a system command, an error
is returned saying

REL_Tony_021138_0824051
cleartool: Error: Name cannot be an integer: "1".
cleartool: Error: Unable to create label type "1".

Why do I get the extra 1 when adding \n?
How can I make this a string so it will be read as a string?

Tony
 
T

Toni Erdmann

Tony said:
I'm trying to use the output of the the date/time from localtime into a
label.:

($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime(time);
$year = ($year + 1900) - 2000;
$labelname = printf "REL_" . $proj[0] . "_%02d%02d%02d_%02d%02d%02d",
^^^^^^
Do you mean "sprintf".
$hour,$min,$sec,$mon+1,$mday,$year;
print "$labelname\\n";

The problem is that when I input $labelname into a system command, an error
is returned saying

REL_Tony_021138_0824051
cleartool: Error: Name cannot be an integer: "1".
cleartool: Error: Unable to create label type "1".

Why do I get the extra 1 when adding \n?
How can I make this a string so it will be read as a string?

Tony

Toni
 
G

goho

Tony said:
I'm trying to use the output of the the date/time from localtime into a
label.:

($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime(time);
$year = ($year + 1900) - 2000;
$labelname = printf "REL_" . $proj[0] . "_%02d%02d%02d_%02d%02d%02d",
$hour,$min,$sec,$mon+1,$mday,$year;
print "$labelname\\n";

The problem is that when I input $labelname into a system command, an error
is returned saying

REL_Tony_021138_0824051
cleartool: Error: Name cannot be an integer: "1".
cleartool: Error: Unable to create label type "1".

Why do I get the extra 1 when adding \n?
How can I make this a string so it will be read as a string?

Tony

As an alternative, try something like:

use strict;
use warnings;
use POSIX 'strftime';
my $label = strftime("REL_%Y%m%d_%H%M%S",localtime);
print "$label\n";
 
J

Joe Smith

Tony said:
$labelname = printf "REL_" . $proj[0] . "_%02d%02d%02d_%02d%02d%02d",

Why do I get the extra 1 when adding \n?

Because printf() returns true (1) when it succeeds.
The generated string is going directly to STDOUT, not to $labelname.
 
B

Brian McCauley

Toni said:
Tony said:
I'm trying to use the output of the the date/time from localtime into
a label.:

($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime(time);
$year = ($year + 1900) - 2000;
$labelname = printf "REL_" . $proj[0] . "_%02d%02d%02d_%02d%02d%02d",

^^^^^^
Do you mean "sprintf".
$hour,$min,$sec,$mon+1,$mday,$year;
print "$labelname\\n";

Also you should avoid putting data into the format string of sprintf()
just in case there's a '%' in there ever.

Put a '%s' in the format and then put $proj[0] as a separate item in
sprintf()'s argument list.

It may be that now you know $proj[0] does not contain '%' but getting
into the bad habit will get you burned eventually.
 
T

Tony

Thanks for the answers group. I got it to work.

Tony

Joe Smith said:
Tony said:
$labelname = printf "REL_" . $proj[0] . "_%02d%02d%02d_%02d%02d%02d",

Why do I get the extra 1 when adding \n?

Because printf() returns true (1) when it succeeds.
The generated string is going directly to STDOUT, not to $labelname.
 
E

Eric J. Roode

I'm trying to use the output of the the date/time from localtime into
a label.:

($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime(time);
$year = ($year + 1900) - 2000;
$labelname = printf "REL_" . $proj[0] . "_%02d%02d%02d_%02d%02d%02d",
$hour,$min,$sec,$mon+1,$mday,$year;
print "$labelname\\n";

May I suggest using the Time::Format module?

$labelname = "REL_$proj[0]_$time{hhmmss_mmddyy}";

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
B

Babacio

"Eric J. Roode"
May I suggest using the Time::Format module?

$labelname = "REL_$proj[0]_$time{hhmmss_mmddyy}";

Or DateTime and its friends ? It is very nice because it offers
(almost) everything you can dream of, like easter date computation, but
is still very simple to use (and there is more than a way to do
everything).

For example :

use DateTime;
use DateTime::Locale;
$dt = DateTime->now( locale => 'en' );
print $dt->strftime("This is %A, %B %d of %Y.\n");

Result :

This is Thursday, August 25 of 2005.

Other example :

$dt = DateTime->now( locale => 'es' );
print $dt->strftime("Estamos el %A %d de %B %Y.\n");

Result :

Estamos el jueves 25 de agosto 2005.
 
T

Tintin

Tony said:
I'm trying to use the output of the the date/time from localtime into a
label.:

($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime(time);
$year = ($year + 1900) - 2000;

Y21K bug!!!
$labelname = printf "REL_" . $proj[0] . "_%02d%02d%02d_%02d%02d%02d",
$hour,$min,$sec,$mon+1,$mday,$year;
print "$labelname\\n";

The problem is that when I input $labelname into a system command, an
error is returned saying

REL_Tony_021138_0824051
cleartool: Error: Name cannot be an integer: "1".
cleartool: Error: Unable to create label type "1".

Why do I get the extra 1 when adding \n?
How can I make this a string so it will be read as a string?

I'd do it like:

use POSIX 'stftime';
my $labelname = strftime "REL_$proj[0]_%H%M%S%m%d%y\n", localtime;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top