Wednesday, October 15, 2008

Retrieving Date and Time from Unix Timestamp

In Unix, we can retrieve Date and Time from Timestamp by using following command:
$ date -r 1220000000
Fri Aug 29 14:23:20 IST 2008

But in Linux, there is no '-r' option. Therefore, I have written a Perl script to
retrieve date and time from timestamp. Here is the script:

$ vi ~/bin/dater.pl

#!/usr/bin/perl -w

use strict;
use warnings;

my $ts = time;
if (@ARGV >= 1)
{
    $ts = $ARGV[0];
}
my ($sec, $min, $hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
++$mon;
$year+=1900;
print "$mday-$mon-$year $hour:$min:$sec\n";


# end dater.
pl


Sample runs of above program:
$ dater.pl
15-10-2008 12:34:37
$ dater.pl 1220000000
29-8-2008 14:23:20

PS: you can change parameters in print function in the program
according to output you desire.



No comments: