Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Providing Fractions of a second to DateTime?

by cmv (Chaplain)
on Jun 12, 2012 at 13:41 UTC ( #975807=perlquestion: print w/ replies, xml ) Need Help??
cmv has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks-

I'm parsing a logfile that gives me Timezone, Month, Day, Year, and the number of milliseconds since midnight. I would like to create a timestamp string down to the milliseconds of the form:

mm/dd/yyyy hh:mm:ss.xxx

I fancy using DateTime to help me out with this, but I'm having trouble figuring out how to generate and provide the fraction of a second to it. I am forced to use perl5.8 - any help is very much appreciated!

Thanks

-Craig

Here is my current code that does not handle fractions of a second:

use strict; use warnings; # Get Data... my ($tZone, $pMonth, $pDay, $pYear, $pStart) = qw(-04:00 05 30 2012 50 +460904); print STDERR "tZone=$tZone pMonth=$pMonth pDay=$pDay pYear=$pYear pSta +rt=$pStart\n"; use DateTime; # Start from midnight... my $dt = DateTime->new(year=>$pYear, month=>$pMonth, day=>$pDay, hour= +>00, minute=>00, second=>00, time_zone=>$tZone); # Add in milliseconds since midnight (pStart)... $dt->add(seconds=>int($pStart/1000)); my $timestamp = $dt->strftime('%D %T.%3N'); print STDERR "timestamp=$timestamp\n";

Comment on Providing Fractions of a second to DateTime?
Download Code
Replies are listed 'Best First'.
Re: Providing Fractions of a second to DateTime?
by tobyink (Abbot) on Jun 12, 2012 at 14:41 UTC
    use DateTime; my $timezone = 'UTC'; my $year = 2012; my $month = 6; my $day = 12; my $millisecond = 3600000; # 1.00 am my $dt = DateTime->new( year => $year, month => $month, day => $day, hour => 0, minute => 0, second => 0, nanosecond => 0, time_zone => $timezone, )->add( nanoseconds => ($millisecond * 1_000_000), ); print $dt;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Providing Fractions of a second to DateTime?
by ansh batra (Friar) on Jun 12, 2012 at 14:13 UTC

    use DateTime; $dt = DateTime->new( year => 1964, month => 10, day => 16, hour => 16, minute => 12, second => 47, nanosecond => 500000000, #see this time_zone => 'Asia/Taipei', );

    this example is given on CPAN

      Thanks for the pointer to nanosecond. I originally saw that and tried various things that made sense to me. For instance:
      my $dt = DateTime->new(year=>$pYear, month=>$pMonth, day=>$pDay, millisecond=>$pStart, time_zone=>$tZone);
      Well, that didn't work, since it doesn't take millisecond as an argument to new, so I tried converting $pStart to nanoseconds-since-midnight and substituting in that, but alas - it seems that the nanosecond value is only for providing the intra-second-delta.

      See my above response to what I think I need to do. I'm hoping someone will show me a better way...

        $dt = DateTime->new( year=>2003, month=>6, day=>12, hour=>7, minute=>22, second=>12, millisecond=>345, microsecond=>678, nanosecond=>901 );

        this should also work and use $dt->millisecond() for your calculations
        refer cpan

Re: Providing Fractions of a second to DateTime?
by daxim (Chaplain) on Jun 12, 2012 at 13:55 UTC
      Thanks for the pointer to HiRes, but I don't think that helps. I already have the number of milliseconds since midnight from the log.

      What I think I need to do is to get DateTime to tell me the number of milliseconds from midnight to 14:01:00.000 on this date, then subtract that from the total number of milliseconds since midnight, then tell DateTime to add that result to the original timestamp that I calculated.

      When I read this back, it sounds too complicated. I must be missing something...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://975807]
Approved by toolic
Front-paged by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (16)
As of 2015-07-30 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls