TimeDate conversion to string.

Discussion in 'Perl Misc' started by Tony, Aug 24, 2005.

  1. Tony

    Tony Guest

    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
     
    Tony, Aug 24, 2005
    #1
    1. Advertising

  2. Tony

    Toni Erdmann Guest

    Tony wrote:
    > 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
     
    Toni Erdmann, Aug 24, 2005
    #2
    1. Advertising

  3. Tony

    goho Guest

    Tony wrote:
    > 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";
     
    goho, Aug 24, 2005
    #3
  4. Tony

    Joe Smith Guest

    Tony wrote:

    > $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.
     
    Joe Smith, Aug 24, 2005
    #4
  5. Toni Erdmann wrote:

    > Tony wrote:
    >
    >> 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.
     
    Brian McCauley, Aug 24, 2005
    #5
  6. Tony

    Tony Guest

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

    Tony

    "Joe Smith" <> wrote in message
    news:...
    > Tony wrote:
    >
    >> $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.
     
    Tony, Aug 25, 2005
    #6
  7. "Tony" <> wrote in
    news:XdUOe.7758$:

    > 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
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
     
    Eric J. Roode, Aug 25, 2005
    #7
  8. Tony

    Babacio Guest

    "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.

    --
    Bé erre hue ixe eu elle, Bruxelles.
     
    Babacio, Aug 25, 2005
    #8
  9. Tony

    Tintin Guest

    "Tony" <> wrote in message
    news:XdUOe.7758$...
    > 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;
     
    Tintin, Aug 25, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Duenser

    Conversion: String to std_ulogic_vector

    Stefan Duenser, Dec 9, 2004, in forum: VHDL
    Replies:
    2
    Views:
    662
  2. =?ISO-8859-1?Q?Hel=E9ne?=

    Problem with string and base64binary conversion

    =?ISO-8859-1?Q?Hel=E9ne?=, Jul 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,904
  3. James Tillery
    Replies:
    2
    Views:
    496
    James Tillery
    Oct 25, 2004
  4. Alexander Eisenhuth
    Replies:
    5
    Views:
    546
    Bob Gailer
    Jul 25, 2003
  5. , India
    Replies:
    2
    Views:
    486
    Fraser Ross
    Sep 15, 2009
Loading...

Share This Page