Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get micro seconds with time stamp in Perl?

Tags:

perl

This might be a silly question but I am not able to find answer to it:

I have a Python code which is printing time-stamp like this:

2015-05-19 22:27:00.688441

The code which produces this string looks something like this:

print str(datetime.datetime.strptime(time_value, '%Y-%m-%d %H:%M:%S.%f'))

I would like to do the same in Perl (version 5.14). I can get all parts of this string except the last fractional part (%f).

How do I get exact same string in Perl?

PS: I tried using Time::HiRes module.

use Time::HiRes;
my ($seconds, $microseconds) = gettimeofday;

If somehow I can use $seconds to localtime(), is it possible? If so, then how?

Extension to original question: If I have a variable $time_value -- lets assume that I have retrieved a time stamp string in this variable from somewhere, how can I format this to required format i.e. time stamp with microseconds?

like image 778
Kedar Joshi Avatar asked Oct 20 '15 21:10

Kedar Joshi


People also ask

How do I get milliseconds Perl?

In Perl if you want to calculate time in milliseconds (thousandths of a second) you can use Time::HiRes and the time() function.

How do I print a timestamp in Perl?

Timestamp can be created by creating a DateTime object and then calling the now constructor. my $datetime = DateTime->now; print "$datetime\n" ; A DateTime object can also be created by providing all the details part wise like date, hour, minute, second, etc.

What is $@ in Perl?

$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.

What is Strftime in Perl?

Perl POSIX Function strftime() The Perl POSIX strftime() function is used to format date and time with the specifiers preceded with (%) sign. There are two types of specifiers, one is for local time and other is for gmt time zone.


2 Answers

The way most similar to how Python does it is to install and use DateTime. Note that microseconds are not a part of the standard strptime format. DateTime uses %N for fractional seconds. %6N means factional seconds to 6 places (microseconds).

# `DateTime->now` uses seconds, so we have to supply our own
# hires time.
use Time::HiRes qw(time);
use DateTime;

print DateTime->from_epoch( epoch => time )->strftime('%Y-%m-%d %H:%M:%S.%6N');
like image 94
Schwern Avatar answered Sep 22 '22 04:09

Schwern


$ perl -E'
   use DateTime qw( );

   my $time = 1445400376.20984;

   my $dt = DateTime->from_epoch( epoch => $time, time_zone => "local" );
   say $dt->strftime("%Y-%m-%d %H:%M:%S.%6N");
'
2015-10-20 21:06:16.209840

(Remove time_zone => "local" for UTC.)

But DateTime is a bit heavy. POSIX offers the lightest solution.

$ perl -E'
   use POSIX qw( strftime );

   my $time = 1445400376.20984;

   my $microsecs = ($time - int($time)) * 1e6;
   say sprintf("%s.%06.0f", strftime("%Y-%m-%d %H:%M:%S", localtime($time)), $microsecs);
'
2015-10-20 21:06:16.209840

(Change localtime to gmtime for UTC.)

If you don't already have the time, you can use Time::HiRes's time in the first snippet and gettimeofday for the second.

like image 42
ikegami Avatar answered Sep 22 '22 04:09

ikegami