tags:

views:

399

answers:

6

For ex : from date : 10/02/2010

How to convert equal time stamp for 10/02/2010 00:00:00 in Perl

I cant use local time or time .. is there any other way to achieve this ..

+3  A: 

Without localtime():

use Time::Local;    
$time = timelocal($sec,$min,$hour,$mday,$mon,$year);

( see perldoc )

A standard way would be something like:

use POSIX;

use strict;
use warnings;

my $sec  = 0;
my $min  = 0;
my $hour = 0;
my $day  = 10;
my $mon  = 2 - 1;
my $year = 2010 - 1900;
my $wday = 0;
my $yday = 0;

my $unixtime = mktime ($sec, $min, $hour, $day, $mon, $year, $wday, $yday);
print "$unixtime\n";

my $readable_time = localtime($unixtime);
print "$readable_time\n"

( From http://www.adp-gmbh.ch/perl/posix/convert_time.html )

mre
+2  A: 

The DateTime module should be helpful here. In particular, I believe the DateTime::Format::Natural module can parse a user-supplied date string. From there, you have a DateTime object and can print it out or transform it as you like.

dsolimano
The DateTime module is extremely flexible and is my preferred choice for complex date and time manipulations (in particular, calculating sunrise and sunset for different locations when I travel around the world). However DateTime is heavy-weight and has a lot of dependencies. So if it's simplicity you want then using the built-in Time::Local module might be an approach to consider.
PP
+1  A: 

Depending on where your initial date is coming from you might be able to parse it using

Date::Manip

and calling

ParseDate("10/02/2010")

You can then take that output and convert it into whatever format you wish.

Mike
+3  A: 

I think you want the built-in module Time::Local.

Arkadiy
+1  A: 

You could use Date::Parse:

use Date::Parse;
print str2time('10/02/2010 00:00:00');

On my machine this prints 1285970400, which corresponds to October 2nd, 2010 (I live in +1 GMT with +1 Wintertime.)

Andomar
+4  A: 

You can use the Time::Local core module:

use Time::Local;
my ($d, $m, $y) = split '/', '10/02/2010';
my $time = timelocal(0,0,0,$d,$m,$y);
eugene y
I prefer this approach.
PP