# Current Time with 5 digits of milliseconds

Discussion in 'Perl Misc' started by ambarish.mitra, May 12, 2008.

1. ### ambarish.mitraGuest

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.

ambarish.mitra, May 12, 2008

2. ### John W. KrahnGuest

\$ 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

John W. Krahn, May 12, 2008

3. ### RedGrittyBrickGuest

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

RedGrittyBrick, May 12, 2008
4. ### Jens Thoms ToerringGuest

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, May 12, 2008
5. ### Peter J. HolzerGuest

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

Peter J. Holzer, May 12, 2008
6. ### Peter J. HolzerGuest

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

Peter J. Holzer, May 12, 2008
7. ### John W. KrahnGuest

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

John

John W. Krahn, May 12, 2008