Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How can I format the output of localtime?

by Anonymous Monk
on Jun 03, 2000 at 22:45 UTC ( #16212=perlquestion: print w/replies, xml ) Need Help??

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

When every I use stat() to get the date a file was last modified I get a weird number such as 959907928. Is there a way to format this into a normal date such as 6/3/00 without using Date::Manip?

Originally posted as a Categorized Question.

  • Comment on How can I format the output of localtime?

Replies are listed 'Best First'.
Re: How can I format the output of localtime?
by Zaxo (Archbishop) on Feb 12, 2004 at 03:48 UTC
    The most convenient formatter for localtime output is &POSIX::strftime. It is similar to an sprintf call, taking a format string as its first argument, and a localtime compatible list as the remaining arguments. The formats are specialized for time units, and extract the strings without need for displacements in the data. The complete list of formats is given in man 3 strftime. A locale-sensitive date can be formatted with the "%x" format string:
    use POSIX 'strftime'; my $datestring = strftime 'Today is %x.', localtime; print $datestring, $/;
    The format inquired of is given (with leading zeros) by the "%D" format string. Full names of months and days in the language of the current locale are given by the "%B" and "%A" formats, and their abbreviations by "%b" and "%a".

Re: How can I format the output of localtime?
by KM (Priest) on Jun 04, 2000 at 03:18 UTC
    Look at the POSIX module as well, it comes with Perl. Look at the POSIX::strftime method.

    Cheers,
    KM

Re: How can I format the output of localtime?
by reptile (Monk) on Jun 04, 2000 at 02:26 UTC

    localtime takes those big weird numbers (which btw are the number of seconds since the epoch, which is normally Jan 1. 1970) and returns either the ctime(3) string in scalar context, or an array of values describing the date. Look on the localtime manpage for more.

    You can also look at Date::Format on CPAN. With that, something like:

    time2str("%D", $time);

    will format $time to the "MM/DD/YY" format you were looking for. See Date::Format's documentation for all the format specifiers.

Re: How can I format the output of localtime?
by Sol-Invictus (Scribe) on Feb 08, 2004 at 22:51 UTC
    at it's simplest:

    print scalar(localtime(time)),"\n";
      Well, at its simplest, you can leave off the arg to localtime (and all the parens):
      print scalar localtime, "\n";
      Update: Well, several have found this post downvotable, but nobody has explained why. It's not incorrect. It offers an improvement over the post to which it is a reply (and which, inexplicably, has been significantly upvoted by comparison).

      You're not going to change its placement by downvoting it; it's attached to its parent, and it's the only response.

      In the helpful spirit of PerlMonks, could someone offer some reason that this post would be a negative vote magnet?


      The PerlMonk tr/// Advocate
        Who knows. FWIW, I like your solution better than the parent.

        Is there some unspoken rule about golfing in the Q&A section?

        Cheers,
        Matt

Re: How can I format the output of localtime?
by Roy Johnson (Monsignor) on Feb 11, 2004 at 16:52 UTC
    If you want to do it without using any modules, and you want the m/d/y (or d/m/y, season to taste) format,
    # Assuming you've got $timeval from a stat() call or some such my ($d,$m,$y) = (localtime($timeval))[3,4,5]; my $mdy = sprintf '%d/%d/%d', $m+1, $d, $y+1900;
Re: How can I format the output of localtime?
by jacques (Priest) on Oct 21, 2004 at 18:34 UTC
    This is an update of Roy Johnson's great answer, for those who just want the current time formatted in m/d/y.
    my ($d,$m,$y) = (localtime)[3,4,5]; my $mdy = sprintf '%d/%d/%d', $m+1, $d, $y+1900;
    Or if month and day should always be two digits:
    my $mdy = sprintf '%02d/%02d/%04d', $m+1, $d, $y+1900;
      if you want two digits for day/month like 04/03/2004, then
      my ($d,$m,$y) = (localtime(time))[3,4,5]; my $mdy = sprintf '%02d/%02d/%d', $m+1, $d, $y+1900;
        Update: Roy Johnson just notified me that localtime doesn't need any parameters to give the current time. On a different note, why aren't any of these replies appearing in the larger Q&A thread with the rest of the answers?
Re: How can I format the output of localtime?
by Mago (Parson) on Jul 08, 2003 at 20:35 UTC
    sub TimeStamp { my($format) = $_[0]; my($return); (my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday +, my $yday, my $isdst) = localtime(); $year = $year + 1900; $mon = $mon + 1; if (length($mon) == 1) {$mon = "0$mon";} if (length($mday) == 1) {$mday = "0$mday";} if (length($hour) == 1) {$hour = "0$hour";} if (length($min) == 1) {$min = "0$min";} if (length($sec) == 1) {$sec = "0$sec";} if ($format == 1) {$return = "$year\-$mon\-$mday $hour\:$min +\:$sec";} if ($format == 2) {$return = $mon . $mday . $year;} if ($format == 3) {$return = substr($year,2,2) . $mon . $mda +y;} if ($format == 4) {$return = $mon . $mday . substr($year,2,2 +);} if ($format == 5) {$return = $year . $mon . $mday . $hour . +$min . $sec;} if ($format == 6) {$return = $year . $mon . $mday;} if ($format == 7) {$return = $mday .'/'. $mon .'/'. $year .' + '. $hour .':'. $min .':'. $sec;} if ($format == 8) {$return = $year . $mon . $mday . $hour . +$min;} if ($format == 9) {$return = $mday . '/' . $mon . '/' . $yea +r;} return $return; }
      Dude, I really think you need to read up on sprintf!
      sub TimeStamp { my($format) = $_[0]; my ($sec,$min,$hour,$mday,$mon,$year) = localtime(); $year+=1900; $mon++; if ($format == 1) { return sprintf("%4d-%02d-%02d %02d:%02d:%02d",$year,$mon,$mday +,$hour,$min,$sec); } elsif ($format == 2) { return sprintf("%02d%02d%4d",$mon,$mday,$year); } elsif ($format == 3) { return sprintf("%02d%02d%02d",$year%100,$mon,$mday); } elsif ($format == 4) { return sprintf("%02d%02d%02d",$mon,$mday,$year%100); } elsif ($format == 5) { return sprintf("%4d%02d%02d%02d%02d%02d",$year,$mon,$mday,$hou +r,$min,$sec); } elsif ($format == 6) { return sprintf("%4d%02d%02d",$year,$mon,$mday); } elsif ($format == 7) { return sprintf("%02d/%02d/%4d %02d:%02d:%02d",$mday,$mon,$year +,$hour,$min,$sec); } elsif ($format == 8) { return sprintf("%4d%02d%02d%02d%02d",$year,$mon,$mday,$hour,$m +in); } else { return sprintf("%02d/%02d/%4d",$mday,$mon,$year); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://16212]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2022-01-27 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (70 votes). Check out past polls.

    Notices?