Monday 9 November 2009

date to epoch stamp conversion

I have a requirement to see the utc timestamps(GMT) of a given date (YYYYMMDD) or a date range (YYYYMMDD-YYYYMMDD) format.This is how i have done it.


#!usr/bin/perl
use strict;use Time::Local;
my $usage = "------------Usage----------- \n $0 YYYYMMDD-YYYYMMDD time range to convert to epochtime)";
my $NOARGS=66;
my ($year,$mon,$day);
my @range = split(/-/,$ARGV[0]);

if( @ARGV != 1 )
{ print "$usage :
./epoch.pl 20091021
1256083200
or
./epoch.pl 20091015-20091022
1255564800
1255651200
1255737600
1255824000
1255910400
1255996800
1256083200
1256169600\n",sprintf("%s\n",'---------------------------') and exit $NOARGS;
}

#print "@range\n" ;
if( scalar(@range) == 2 )
{
foreach my $date ($range[0]..$range[1]) # expand range sequencially
{
($year,$mon,$day) = $date =~ /^(\d{4})(\d{2})(\d{2})$/ ; #collecting year,month,day into list
my $epoch = timegm(0,0,0,$day,$mon-1,$year-1900); #timegm function to return epochtime from the default variable list
print "$epoch\n";
}
}
else
{
($year,$mon,$day) = $range[0] =~ /^(\d{4})(\d{2})(\d{2})$/ ;
my $epoch = timegm(0,0,0,$day,$mon-1,$year-1900);
print "$epoch\n";
}


Sample output :

./epoch.pl 20091110
1257811200

No comments:

Post a Comment

Tweets by @sriramperumalla