Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Current Time with 5 digits of milliseconds

1 view
Skip to first unread message

ambaris...@gmail.com

unread,
May 12, 2008, 3:39:17 AM5/12/08
to
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.

John W. Krahn

unread,
May 12, 2008, 6:22:12 AM5/12/08
to

$ 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

bugbear

unread,
May 12, 2008, 6:36:12 AM5/12/08
to
ambaris...@gmail.com wrote:
> I need to get the current date-time with milliseconds upto 5 places of
> precision.

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

BugBear

RedGrittyBrick

unread,
May 12, 2008, 6:44:59 AM5/12/08
to
ambaris...@gmail.com wrote:
> 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

--
RGB

Jens Thoms Toerring

unread,
May 12, 2008, 6:53:46 AM5/12/08
to
ambaris...@gmail.com wrote:
> 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
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

Peter J. Holzer

unread,
May 12, 2008, 6:58:17 AM5/12/08
to
On 2008-05-12 07:39, ambaris...@gmail.com <ambaris...@gmail.com> wrote:
> 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

Peter J. Holzer

unread,
May 12, 2008, 8:20:01 AM5/12/08
to
On 2008-05-12 10:22, John W. Krahn <som...@example.com> wrote:
> ambaris...@gmail.com wrote:
>> 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

John W. Krahn

unread,
May 12, 2008, 2:52:02 PM5/12/08
to

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

0 new messages