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

Hello,

Under unix basic I can convert the following internal time to external time: 11982 to 10/20/00 with:

OCONV("11982","D")

How would I do that in Perl?

Thanks...

Replies are listed 'Best First'.
Re: How to convert time in perl?
by Thelonius (Priest) on Jun 05, 2003 at 13:58 UTC
    With cbro's hint, here's a function to do what you want:
    sub pick2mmddyy { my ($day, $mo, $year) = (gmtime(($_[0]-732)*86400))[3,4,5]; return sprintf("%02d/%02d/%02d", $mo+1, $day, $year%100); } print pick2mmddyy(11982), "\n";
        Perfect :) And thanks again for the help...

        Steve
        Yep, that's a fine function. You might even say I have an affinity for this type of transformation. (Obscure math puns.)
Re: How to convert time in perl?
by cbro (Pilgrim) on Jun 05, 2003 at 13:30 UTC
    By the use of the oconv function, and your mention of "unix basic", it seems as though your internal time is based on the Pick calandar. Where, day "0" (in "internal" format) on the Pick calendar is December 31, 1967. For example, June 26, 1987 has an internal value of 7117, which is the number of days since 12/31/67.

    Is this correct? If so, please let us know, because there may be a module that deals with such a format explicitly.

    UPDATE: After doing some minor calculations (e.g. 11982/365 = 32.827 and 11982%365 = 302; 302/30 = 10.0667), I see approximately 32 years and ten month, which from 12/31/67, is 10/20/00. Now, by RFC99 (I think), Perl is going to always be standardized on the UNIX epoch, and everyone here (including myself) is in that mindset. I don't want to go through all of the trouble of trying to find a Pick calandar module until you 100% confirm that this is what your dealing with. Actually, I have already gone to the trouble and haven't found one yet and don't want to continue looking until I've received a confirmation. Of course, you can look too if that is, in fact, what you need. Try here.
Re: How to convert time in perl?
by fokat (Deacon) on Jun 05, 2003 at 12:50 UTC

    I don't know what kind of representation is that you mention. The customary way to represent time at the OS level, is the number of seconds elapsed since Jan 1 1970 00:00:00 (the Epoch). This is, for instance, what time() returns.

    I guess you can get your conversion by using some snippet such as:

    my $time = some_function_returning_the_time_you_want(); print "The interesting time was ", scalar localtime($time), "\n";

    localtime() in a scalar context, will give you a nicely formatted string such as:

    bash-2.05a$ perl -e 'print scalar localtime(), "\n"' Thu Jun 5 08:48:44 2003

    In an array context, it will return the components of the date stamp. Please check your friendly perldoc -f localtime for the details.

    Best regards

    -lem, but some call me fokat

Re: How to convert time in perl?
by Aragorn (Curate) on Jun 05, 2003 at 12:51 UTC
    The internal time in Unix is the number of seconds since Jan 1, 1970:

    $ perl -e 'print time()' gives 1054817016, for example. To get a string representation of that, use localtime:

    $ perl -e '$timestr = localtime(time()); print $timestr'

    For See also the documentation on the time() and localtime() functions. Using the localtime function in list context gives you the different time parts (seconds, minutes, hours, etc.) which you can format with printf or sprintf to get the format you want. See the documentation for examples.

    Arjen

Re: How to convert time in perl?
by hmerrill (Friar) on Jun 05, 2003 at 13:20 UTC
    Learn about Perl's time and localtime functions by doing
    perldoc -f time perldoc -f localtime
    at a comand prompt.

    You can even search the perldocs for a keyword by doing something like
    perldoc -q time
    and you will get a listing displayed of all the matching topics.

    HTH.
Re: How to convert time in perl?
by gellyfish (Monsignor) on Jun 06, 2003 at 21:49 UTC
    I would look at the strftime() method in the POSIX module.