Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
#-----------------------------------------------------------------------------
#
# Get time string
#
#-----------------------------------------------------------------------------
#
sub formatTime
    {
    my ( $seconds )  = @_;

    my( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
                                localtime( $seconds );
    $year += 1900;
    $mon += 1;

    return ( sprintf "%02d/%02d/%04d %02d:%02d:%02d",
                $mday,$mon,$year,$hour,$min,$sec );
    }

#-----------------------------------------------------------------------------
#
# Extract time from the string
#
#-----------------------------------------------------------------------------
#
sub extractTime
    {
    my ( $edate, $time ) = @_;

    my $date =  $1 if ($edate =~ /(\d+\/\d+\/\d+)/);    # remove leading '[' etc.
    my ( $mday, $mon, $year ) = split( /\//, $date );
    my ( $hour,$min, $sec ) = split( /:/, $time );

    return (timelocal( $sec, $min, $hour, $mday, ($mon - 1), ($year - 1900 )));
    }
share|improve this question
    
The callers of these functions will need to change. Eg whatever calls formatTime() will need to pass in milliseconds rather than seconds. Also, what have you tried? –  jordanm Dec 12 '12 at 2:27
    
@jordanm The script is actually pretty big, all I did was modify the date to include nano seconds %N, but it really didnt, I cant see anything to add along with what calls formatTime() that would help without changing the above –  I AM L Dec 12 '12 at 2:38
    
Please see How to Ask? –  mtk Dec 12 '12 at 4:54

1 Answer 1

up vote 2 down vote accepted

In order to to get a higher precision for you function formatTime, you need to take in miliseconds, rather than seconds. You can't take rounded seconds and make it more precise.

Since localtime() only accepts seconds since epoch, you will need to convert the miliseconds passed in and convert it to seconds. Here is an example:

use POSIX qw(floor);

sub formatTime {
    my ( $milliseconds )  = @_;

    my $seconds = floor($milliseconds / 1000);
    my $msec = $milliseconds % 1000;

    my( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
                                localtime( $seconds );
    $year += 1900;
    $mon += 1;

    return ( sprintf "%02d/%02d/%04d %02d:%02d:%02d:%04d",
                $mday,$mon,$year,$hour,$min,$sec,$msec );
}

The other option would be to use the DateTime. If you pass in seconds as a decimal, it can create a DateTime object with greater precision:

use DateTime;

sub formatTime {
    my ( $seconds ) = @_;
    my $dt = DateTime->from_epoch( epoch => $seconds );
    # ....
}
share|improve this answer
    
I tried both options with a bit of tweaks and managed to get millisecond timestamp for both, Thanks. –  I AM L Dec 13 '12 at 2:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.