# Current Time with 5 digits of milliseconds

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I need to get the current date-time with milliseconds upto 5 places of
precision.

That is, 20080512T12094565266 => YYYY MM DD T HH mm SS ms-5 digits

Here, 65266 is the milli-second with 5 places of precision.

I tried with the module DateTime, but that does not give the
milliseconds.

use DateTime;
my \$dt = DateTime->now( time_zone => 'floating' );

Any idea how this can be achieved in Perl?

Thanks.

## Re: Current Time with 5 digits of milliseconds

ambarish.mitra@gmail.com wrote:

\$ perl -le'
use Time::HiRes q/gettimeofday/;
use POSIX q/strftime/;
print substr strftime( q/%Y%m%dT%H%M%S/, localtime ) . ( gettimeofday )[
1 ] . q/00000/, 0, 20;
'
20080512T03134231838

Of course there is no guarantee that the microseconds will apply to the
seconds field that strftime produces.

John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

## Re: Current Time with 5 digits of milliseconds

[...]

That's because you are getting the "current time" twice: Once with
localtime and once with gettimeofday, and then you use the seconds from
the first call and the microseconds from the second call. Of course the
seconds may have changed between the calls. If you get the current time
only once that cannot happen:

my (\$seconds, \$microseconds) = gettimeofday;
print strftime( q/%Y%m%dT%H%M%S/, localtime(\$seconds)),
sprintf("%05d", \$microseconds/10);

(your code also prints the fractional part wrong: 2713 microseconds
should be printed as 00271 but is printed as 27130)

hp

## Re: Current Time with 5 digits of milliseconds

Peter J. Holzer wrote:

Thanks Peter, I didn't know how microseconds were represented.

John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

## Re: Current Time with 5 digits of milliseconds

ambarish.mitra@gmail.com wrote:

This may not be supported by your hardware and/or
O/S.

BugBear

## Re: Current Time with 5 digits of milliseconds

ambarish.mitra@gmail.com wrote:

No, 652.66 would be milliseconds with 5 digit (two decimal places) precision

C> perl -mDateTime -e "\$dt=DateTime->now; print \$dt->second"
29

C> perl -mDateTime -e "\$dt=DateTime->now; print \$dt->nanosecond"
0

http://perldoc.perl.org/Time/HiRes.html

--
RGB

## Re: Current Time with 5 digits of milliseconds

ambarish.mitra@gmail.com wrote:

Sorry, but the pure milliseconds can only have 3 digits.
I guess you mean you want the time with a resolution of
10 microseconds, i.e. 5 digits of resolution for the
sub-seconds part

But DateTime gives you the even better resolution of nano-
seconds (how useful that is is another question;-). Just
use the nanoseconds divided by 10_000 (or the milliseconds
method, divided by 10) and you get the sub-second part of
the time in 5-digits. so use

\$dt->nanosecond / 10000

or

\$dt->microsecond / 10

i.e., if you want to print something like "20080512T12094565266" do

printf "%sT%s%05d", \$dt->ymd( '' ), \$dt->hms( '' ), \$dt->microsecond / 10;

But: Initialisation of a DateTime object with 'now' doesn't seem
to set sub-second values. In order to get the current time with
better than second resolution into your DateTime object you thus
may have to use

use DateTime;
use Time::HiRes qw/ gettimeofday /;

my ( \$s, \$us ) = gettimeofday;
my \$dt = DateTime->from_epoch( epoch     => \$s + 1.0e-6 * \$us,
time_zone => 'floating' );
printf "%sT%s%05d", \$dt->ymd( '' ), \$dt->hms( '' ), \$dt->microsecond / 10;

Regards, Jens
--
\   Jens Thoms Toerring  ___      jt@toerring.de
\__________________________      http://toerring.de

## Re: Current Time with 5 digits of milliseconds

Since "milli" means "one thousandth", milliseconds by definition have
exactly 3 places of precision. The term you are looking for is
"fractional seconds".

Time::HiRes

hp