Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

newbie question Is there any way to have the localtime (or gm time) printed out in an integer format YYMMDDHHSS or something similar (e.g. 200202211500) for 3 pm today, instead of the text date output from code like this: $timeid = localtime; What I'm trying to do is creat an integer number from date stamp localtime or gmtime, etc.

Replies are listed 'Best First'.
Re: time as number
by steves (Curate) on Feb 21, 2003 at 19:44 UTC
    use strict; use POSIX qw(strftime); print strftime("%Y%m%d%H%M%S\n", localtime());
Re: time as number
by Limbic~Region (Chancellor) on Feb 21, 2003 at 19:55 UTC
    Since you indicated you are a "newbie" and did not indicate what operating system you were on let me expand on what steves has offered:

    use POSIX qw(strftime);

    This line exports ONLY the string format time function from the POSIX core module.

    If you do not recognize the "template" that steves used, they come from the posix date command.

    You may want to ask the oracle for answers on man date.

    This will explain what each formatting directive does.

    For example:

    %a Abbreviated weekday name. For example, Wed. %A Full weekday name. For example, Wednesday. %b Abbreviated month name. For example, Jan. %B Full month name. For example, January.
    Hope this helps - cheers - L~R

      It should be kept in mind that the POSIX module doesn't specify which %Formats strftime should support. The native Win32 version for instance doesn't support at least one that the Cygwin libraries do. From what i've seen from the web the strftime formats vary quite a bit. Don't expect them all to work. I put this together from one of the longest lists I could find on the web.

      Which outputs on Perl AS 633

      It'd be interesting to see how widely it varies, and which POSIX standard that perl is supposed to be complaint with. I found quite a few.

      ---
      demerphq


Re: time as number
by mojotoad (Monsignor) on Feb 21, 2003 at 20:03 UTC
    use Acme::Time::Asparagus qw(veggietime); print veggietime();

    Heh...or maybe you should try something like this:

    #!/usr/bin/perl ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(ti +me); $yy = sprintf("%02d", $year % 100); $str = $yy . join('', map(sprintf("%02d", $_), $mon, $mday, $hour, $ +min, $sec)); print $str, "\n";

    Although I'd recommend using the 4-digit form of the year, obtainable above as $yyyy = $year += 1900

    Matt

Re: time as number
by Cody Pendant (Prior) on Feb 22, 2003 at 09:32 UTC
    If you really do just need the time as an integer, why are we going the complicated way around?

    localtime is an integer, just not a very human-readable one. Just use that -- print localtime -- sort by it, do compares with it, and re-interpret it into human-readable format when it needs to be read by humans...
    --

    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D

      Just use that -- print localtime -- sort by it, do compares

      Er, I dont think thats right:

      D:\perl\PerlToc>perl -e "print ''.localtime" Mon Mar 3 18:12:15 2003
      Which doesn't provide a sortable format. Unless you want your dates sorted by day of week :-)

      ---
      demerphq


        But surely you've forced scalar context on it there by making it a string. What happens if you just do "print localtime"? And anyway, who needs localtime anyway. You can just do it with time and local it, again, later when you need your human-readable format.
        --
        “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
        M-J D