http://qs321.pair.com?node_id=27921

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (dates and times)

Is there a way of storing 600 in a scalar as 0600 (24 hour clock style)?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Is there a way of storing 600 in a scalar as 0600 (24 hour clock style)?
by nardo (Friar) on Aug 15, 2000 at 17:58 UTC
    sprintf('%04d', 600);
      This way works, but you'll want to be careful with this method.
      sprintf("%04s %04s %04s", 600, "0600", 0600);
      Results in: "0600 0600 0384"
      Octal can get you.
Re: Is there a way of storing 600 in a scalar as 0600 (24 hour clock style)?
by davorg (Chancellor) on Aug 15, 2000 at 18:00 UTC
Re: Is there a way of storing 600 in a scalar as 0600 (24 hour clock style)?
by tenatious (Beadle) on Aug 21, 2000 at 05:29 UTC
    The above answers are adequate, but I'd like to add that all you have to do is treat the 600 like a string instead of a number. So if you wanted to make a "24 hour" variable, all you'd have to do is something like;
    $time = 600; ($time = "0".$time) if ($time < 1000);
      what if $time < 100?

      Heh. Then, I suppose, the code becomes:

      $time = 600; ($time = "0".$time) if ($time < 1000); ($time = "0".$time) if ($time < 100); ($time = "0".$time) if ($time < 10);

      p.s. Don't do this -- stick with sprintf