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 ambarish.mitra on May 12, 2008, 3:39 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.


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.

Posted by John W. Krahn on May 12, 2008, 6:22 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.
>
>
> 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
--
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

Posted by Peter J. Holzer on May 12, 2008, 8:20 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.
[...]
>> 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

Posted by John W. Krahn on May 12, 2008, 2:52 pm
Please log in for more thread options
Peter J. Holzer wrote:
>> 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.
> [...]
>>> 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
--
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

Posted by bugbear on May 12, 2008, 6:36 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.

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

BugBear

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