Reputation: 4725
I need convert any given date or datetime to microseconds using perl. I tried the following code it works only for the current time you run it.
use Time::HiRes qw(gettimeofday);
my $timestamp = int (gettimeofday * 1000);
print STDOUT "timestamp = $timestamp\n";
I am wondering if there a way to convert any given date or datetime into microseconds in perl, how? btw, I don't have Parse module installed. my perl version is v5.8.8 built for i386-linux-thread-multi.
Upvotes: 3
Views: 722
Reputation: 4725
I finally found a way to convert any given date to microseconds. That is, using system date function like below code snippet . suppose I want to convert a date of 100 days ago to microseconds.
my $anydate = strftime "%Y%m%d", localtime( time - 100 * 24 * 60 * 60 ); # a date of 100 days ago;
my $microsecs = `date -d "$anydate" +%s%N | cut -b1-13`;
print "$microsecs\n";
This really works in any perl version without installing any module.
Upvotes: 0
Reputation: 29975
DateTime
can do that easily:
my $dt = DateTime->new( year => 2012, nanosecond => 4 );
print $dt->hires_epoch();
For more examples see the docs on cpan
Upvotes: 3