in reply to Re^2: Human Readable Date
in thread Human Readable Date

Here's the problem, and you get credit for discovering what I believe to be a bug in Date::Manip (or its documentation).

The documentation for Date::Manip correctly states that ParseDateString() will not correctly handle a raw number of seconds since epoch. Obviously, seconds since epoch is exactly what $s->mtime() is giving you. So you need a way of getting Date::Manip's ParseDateString() to recognize that what you're handing it is seconds since epoch.

The POD for Date::Manip goes on to tell you how to do that:

NOTE: One of the most frequently asked questions that I have gotten is how to parse seconds since the epoch. ParseDateString cannot simply parse a number as the seconds since the epoch (it conflicts with some ISO-8601 date formats). There are two ways to get this information. First, you can do the following:

$secs = ... # seconds since Jan 1, 1970 00:00:00 GMT $date = &DateCalc("Jan 1, 1970 00:00:00 GMT",$secs);

Second, you can call it directly as:

$date = &ParseDateString("epoch $secs");

You're attempting to use the second method, and at least for me on Win32 ActivePerl 5.8.6 with Date::Manip version 5.42, that second method simply doesn't work as advertised. It fails to convince Date::Manip that you've handed it a string of seconds since epoch, and the result is that ParseDateString doesn't know what it's looking at, and defaults to returning an empty string.

I have gone on to confirm that at least the first method suggested in the documentation for Date::Manip does work. That is,

printf "%s: Age [%s], size[%d]\n", $farm, DateCalc( "Jan 1, 1970 00:00:00 GMT",$s->mtime() ), $s->size();

Try that on for size. ;)

Update: Bug alert has been rescinded. See my followup below.


Dave

Replies are listed 'Best First'.
Re^4: Human Readable Date
by FireyIce01 (Novice) on Jan 18, 2005 at 06:06 UTC
    excellent! now how to get a space between the date and time...

    2005011717:58:52 is better than the unix time, but having at least a space between date and time would be useful... Perl ignores whitespace right?

    Aside: Not sure how I would submit the bug report, but you're more than welcome to

      Note, it also "doesn't work" to interpolate an object method call within a string like you're doing. In other words, "epoch $s->mtime()" (notice the function call interpolated within quotes) doesn't interpolate as a funciton call, it interpolates as "epoch File::stat=ARRAY(0x28fbe6c)->mtime()". In other words, $s is interpolated, but not dereferenced as a method call. It does work to say ParseDateString( "epoch " . $s->mtime() );

      This means that on second thought, you don't have a Date::Manip bug there, but rather, a bug in your own script, relying on a function of string interpolation that isn't there. See the following example:

      use strict; use warnings; use File::stat; use Date::Manip; my $st = stat( 'router.pl' ) or die "Bleah: $!\n"; my $secs = $st->mtime(); print "Your method of interpolation: $s->mtime()\n"; # See? Wrong. print "Stat info = ", scalar localtime( $secs ), "\n"; # This is just a reality check. print "Using the 'epoch' method: ", ParseDateString( "epoch $secs" ), "\n"; # This works correctly now. print "DateCalc method = ", DateCalc( "Jan 1, 1970 00:00:00 GMT", $secs ), "\n"; # And this works correctly too.

      Hope this helps. :)


      Dave

        Cool, so I've got the option of
        printf "%s: Age [%s], size [%d]\n", $farm, ParseDateString("epoch" . $ +s->mtime()), $s->size(); # the above line does the same thing as the next 4 commented lines # printf "%s: Age [%s], size[%s]\n", # $farm, # DateCalc( "Jan 1, 1970 00:00:00 GMT",$s->mtime() ), # $s->size();
        in my code right now, both are fairly clean looking... but now I need to do some formatting... all I really want to do is add a space between date and time... but also might want to add some / between the date -> YYYY/MM/DD or something like that... Any suggestions on the easiest way? (if you point me at good documentation that's suitable... my perl books are still packed away at the parent's house, so I won't be able to get at them for another 2-3 days)