Current Time with 5 digits of milliseconds

A

ambarish.mitra

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.
 
J

John W. Krahn

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?

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

RedGrittyBrick

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.

No, 652.66 would be milliseconds with 5 digit (two decimal places) precision
I tried with the module DateTime, but that does not give the
milliseconds.

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

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

C> perl -mDateTime -e "$dt=DateTime->now; print $dt->nanosecond"
0
Any idea how this can be achieved in Perl?

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

Jens Thoms Toerring

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.

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
I tried with the module DateTime, but that does not give the
milliseconds.

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
 
P

Peter J. Holzer

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.

Since "milli" means "one thousandth", milliseconds by definition have
exactly 3 places of precision. The term you are looking for is
"fractional seconds".
Any idea how this can be achieved in Perl?

Time::HiRes

hp
 
P

Peter J. Holzer

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. [...]
Any idea how this can be achieved in Perl?

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

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
 
J

John W. Krahn

Peter said:
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. [...]
Any idea how this can be achieved in Perl?
$ 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.

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)

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



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top