Click here to get back home

Current Time with 5 digits of milliseconds

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Current Time with 5 digits of milliseconds ambarish.mitra 05-12-2008
Posted by RedGrittyBrick on May 12, 2008, 6:44 am
Please log in for more thread options
ambarish.mitra@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

Posted by Jens Thoms Toerring on May 12, 2008, 6:53 am
Please log in for more thread options
ambarish.mitra@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 ___ jt@toerring.de
\__________________________ http://toerring.de

Posted by Peter J. Holzer on May 12, 2008, 6:58 am
Please log in for more thread options
> 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

Similar ThreadsPosted
measure time (hopefully in milliseconds) October 25, 2004, 12:39 am
[newbie] Year in two digits; Time with leading zero May 18, 2007, 4:10 am
current time in different timezones incl DST August 28, 2004, 8:10 pm
GMT time to local time, according to timezone and summer/winter time. May 15, 2005, 10:45 pm
Converting milliseconds to seconds January 11, 2008, 3:11 am
regex matching exactly 10 digits November 28, 2006, 8:58 am
regular expression for digits November 15, 2007, 4:36 am
regex to match strings that don't contain any digits? August 9, 2005, 6:57 am
RegEx, how to seperate word from digits? January 23, 2008, 3:21 pm
matching only alphabetic chars and only digits with periods July 14, 2006, 5:38 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap