Time in perl

Time in perl

am 04.10.2004 16:34:58 von Kamal Ahmed

Hi List,

I am trying to print time in a format HH:MM:SS.nnnnnn
E.g. 08:06:26.464649

Any ideas how I can do it ?

Any help would be appreciated.

Thanks,

-Kamal.

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Time in perl

am 04.10.2004 16:47:46 von Bill Curnow

On 4 Oct 2004 at 10:34, Kamal Ahmed wrote:

> I am trying to print time in a format HH:MM:SS.nnnnnn
> E.g. 08:06:26.464649
>
> Any ideas how I can do it ?

use Time::HiRes qw(gettimeofday);

($epochseconds, $microseconds) = gettimeofday;
($second, $minute, $hour) = localtime($epochseconds);
print sprintf("%02d:%02d:%02d.%06d %s",
$hour, $minute, $second, $microseconds);

One caveat, Win32 systems do not appear to be able to give you
anything smaller than milliseconds (the first three decimal places).


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Time in perl

am 04.10.2004 23:15:21 von dbecoll

Kamal Ahmed wrote:

> Hi List,
>
> I am trying to print time in a format HH:MM:SS.nnnnnn
> E.g. 08:06:26.464649
>
> Any ideas how I can do it ?

Not without knowing what time format you are starting with.

epoch time doesn't go below seconds, so it would have to be some
other time format.

--
,-/- __ _ _ $Bill Luebkert Mailto:dbecoll@adelphia.net
(_/ / ) // // DBE Collectibles Mailto:dbe@todbe.com
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_ _______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Time in perl

am 04.10.2004 23:20:21 von Kamal Ahmed

Bill,

When I use your approach, I am getting an error:

25/02/2001-Use of uninitialized value in sprintf at ./exploitD.pl line
208.
17:14:33.016000 time="" action="" orig="bedm-edn-650-pri"
i/f_dir="inbound" i/f_

Line 208 and few lines around it are as below:

sub genOne {
my $index = shift;

my %data;
$data{'name'} = "test";

my $num = int(rand() * 100);
my $year = 1997 + intRand(5);
my $mon = intRand(12);
my $day = intRand(28) + 1;

my $date = "";
$date .= "0" if (int($day) < 10);
$date .= $day;
$date .= "\/$months[$mon]\/$year-";

print "$date";

$data{'LogFileID'} = '1044611703';
$data{'LogRecNum'} = $recNum++;
##$data{'time'} = "$date";

##-------------------
my $epochseconds = "0";
my $microseconds = "0";
($epochseconds, $microseconds) = gettimeofday;
($second, $minute, $hour) = localtime($epochseconds);
print sprintf("%02d:%02d:%02d.%06d %s", ## ----- THIS IS Line 208
$hour, $minute, $second, $microseconds);
##----------------------

Thanks,

-Kamal.


-----Original Message-----
From: Bill Curnow [mailto:bill.curnow@pcca.com]
Sent: Monday, October 04, 2004 10:48 AM
To: Kamal Ahmed; activeperl@listserv.ActiveState.com
Subject: Re: Time in perl


On 4 Oct 2004 at 10:34, Kamal Ahmed wrote:

> I am trying to print time in a format HH:MM:SS.nnnnnn
> E.g. 08:06:26.464649
>
> Any ideas how I can do it ?

use Time::HiRes qw(gettimeofday);

($epochseconds, $microseconds) = gettimeofday;
($second, $minute, $hour) = localtime($epochseconds);
print sprintf("%02d:%02d:%02d.%06d %s",
$hour, $minute, $second, $microseconds);

One caveat, Win32 systems do not appear to be able to give you
anything smaller than milliseconds (the first three decimal places).



_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Time in perl

am 04.10.2004 23:49:28 von dbecoll

Kamal Ahmed wrote:

> Bill,
>
> When I use your approach, I am getting an error:
>
> 25/02/2001-Use of uninitialized value in sprintf at ./exploitD.pl line
> 208.
> 17:14:33.016000 time="" action="" orig="bedm-edn-650-pri"
> i/f_dir="inbound" i/f_
>
> Line 208 and few lines around it are as below:

> my $epochseconds = "0";
> my $microseconds = "0";
> ($epochseconds, $microseconds) = gettimeofday;
> ($second, $minute, $hour) = localtime($epochseconds);
> print sprintf("%02d:%02d:%02d.%06d %s", ## ----- THIS IS Line 208
> $hour, $minute, $second, $microseconds);

Drop the last %s on that line (it was probably for the timezone in
the original code).

use Time::HiRes qw(gettimeofday);

my ($epoch, $usecs) = gettimeofday;
my ($second, $minute, $hour) = localtime $epoch;
print sprintf "%02d:%02d:%02d.%06d\n", $hour, $minute, $second, $usecs;

__END__


--
,-/- __ _ _ $Bill Luebkert Mailto:dbecoll@adelphia.net
(_/ / ) // // DBE Collectibles Mailto:dbe@todbe.com
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_ _______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Time in perl

am 05.10.2004 00:00:24 von Kamal Ahmed

Thanks, Bill and Basil.

It works now. I did close the sub, it was just not included, sorry.

Stdout:

$ ./exploitD.pl 1 0
16/09/1997-17:26:45.562062time="" action="" orig="bedm-edn-650-pri"
i/f_dir="inb
ound" i/f_name="eth-s1p2c3" proto="udp" src="00.00.00.00"
dst="172.16.0.65" serv
ice="1023" s_port="1548" len="3" rule="3" has_accounting="0"
LogFileID="10446117
03" LogRecNum="1980000"

Code Snippit:
sub genOne {
my $index = shift;

my %data;
$data{'name'} = "test";

my $num = int(rand() * 100);
my $year = 1997 + intRand(5);
my $mon = intRand(12);
my $day = intRand(28) + 1;

my $date = "";
$date .= "0" if (int($day) < 10);
$date .= $day;
$date .= "\/$months[$mon]\/$year-";

print "$date";

$data{'LogFileID'} = '1044611703';
$data{'LogRecNum'} = $recNum++;
##$data{'time'} = "$date";

##-------------------
my $epochseconds = "0";
my $microseconds = "0";
my $second = "0";
($epochseconds, $microseconds) = gettimeofday;
($second, $minute, $hour) = localtime($epochseconds);
print sprintf("%02d:%02d:%02d.%06d", $hour, $minute, $second,
$microseconds);
##----------------------






#change the action value to be one of the alerts from the array
#$data{'action'} = (intRand(12) < 11) ? 'accept' : 'drop';
$data{'action'} = $alerts[intRand(45)];
if (needError($actionErrRate)) {
if (intRand(20) < 3) {
$data{'action'} = 'deny';
} elsif (intRand(20) < 5) {
$data{'action'} = ' accept ';
} elsif (intRand(20) < 5) {
$data{'action'} = 'a c c e p t';
} elsif (intRand(20) < 5) {
$data{'action'} = 'acc\'eptdeny';
} else {
$data{'action'} = badString();
}
}


-Kamal.

-----Original Message-----
From: $Bill Luebkert [mailto:dbecoll@adelphia.net]
Sent: Monday, October 04, 2004 5:49 PM
To: Kamal Ahmed
Cc: activeperl@listserv.ActiveState.com
Subject: Re: Time in perl


Kamal Ahmed wrote:

> Bill,
>
> When I use your approach, I am getting an error:
>
> 25/02/2001-Use of uninitialized value in sprintf at ./exploitD.pl line

> 208. 17:14:33.016000 time="" action="" orig="bedm-edn-650-pri"
> i/f_dir="inbound" i/f_
>
> Line 208 and few lines around it are as below:

> my $epochseconds = "0";
> my $microseconds = "0";
> ($epochseconds, $microseconds) = gettimeofday;
> ($second, $minute, $hour) = localtime($epochseconds);
> print sprintf("%02d:%02d:%02d.%06d %s", ## ----- THIS IS Line
208
> $hour, $minute, $second, $microseconds);

Drop the last %s on that line (it was probably for the timezone in the
original code).

use Time::HiRes qw(gettimeofday);

my ($epoch, $usecs) = gettimeofday;
my ($second, $minute, $hour) = localtime $epoch;
print sprintf "%02d:%02d:%02d.%06d\n", $hour, $minute, $second, $usecs;

__END__


--
,-/- __ _ _ $Bill Luebkert
Mailto:dbecoll@adelphia.net
(_/ / ) // // DBE Collectibles Mailto:dbe@todbe.com
/ ) /--< o // // Castle of Medieval Myth & Magic
http://www.todbe.com/
-/-' /___/_<_
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Time in perl

am 05.10.2004 15:09:00 von Deane.Rothenmaier

This is a multipart message in MIME format.
--===============1601191524==
Content-Type: multipart/alternative;
boundary="=_alternative 00483C0F86256F24_="

This is a multipart message in MIME format.
--=_alternative 00483C0F86256F24_=
Content-Type: text/plain; charset="us-ascii"

I think it's the " %s" at the end of the format string--it's a fifth
format and only four "arguments" are presented....

print sprintf("%02d:%02d:%02d.%06d %s", ## ----- THIS IS Line 208
$hour, $minute, $second, $microseconds);

The first %02d is for $hour, the second %02d is for $minute, the third
%02d is for $second, the %06d is for $microseconds, so what is that %s
hanging off the end supposed to get?

Deane





"Kamal Ahmed"
Sent by: activeperl-bounces@listserv.ActiveState.com
10/04/2004 16:20


To:
cc:
Subject: RE: Time in perl


Bill,

When I use your approach, I am getting an error:

25/02/2001-Use of uninitialized value in sprintf at ./exploitD.pl line
208.
17:14:33.016000 time="" action="" orig="bedm-edn-650-pri"
i/f_dir="inbound" i/f_

Line 208 and few lines around it are as below:

sub genOne {
my $index = shift;

my %data;
$data{'name'} = "test";

my $num = int(rand() * 100);
my $year = 1997 + intRand(5);
my $mon = intRand(12);
my $day = intRand(28) + 1;

my $date = "";
$date .= "0" if (int($day) < 10);
$date .= $day;
$date .= "\/$months[$mon]\/$year-";

print "$date";

$data{'LogFileID'} = '1044611703';
$data{'LogRecNum'} = $recNum++;
##$data{'time'} = "$date";

##-------------------
my $epochseconds = "0";
my $microseconds = "0";
($epochseconds, $microseconds) = gettimeofday;
($second, $minute, $hour) = localtime($epochseconds);
print sprintf("%02d:%02d:%02d.%06d %s", ## ----- THIS IS Line 208
$hour, $minute, $second, $microseconds);
##----------------------

Thanks,

-Kamal.


-----Original Message-----
From: Bill Curnow [mailto:bill.curnow@pcca.com]
Sent: Monday, October 04, 2004 10:48 AM
To: Kamal Ahmed; activeperl@listserv.ActiveState.com
Subject: Re: Time in perl


On 4 Oct 2004 at 10:34, Kamal Ahmed wrote:

> I am trying to print time in a format HH:MM:SS.nnnnnn
> E.g. 08:06:26.464649
>
> Any ideas how I can do it ?

use Time::HiRes qw(gettimeofday);

($epochseconds, $microseconds) = gettimeofday;
($second, $minute, $hour) = localtime($epochseconds);
print sprintf("%02d:%02d:%02d.%06d %s",
$hour, $minute, $second, $microseconds);

One caveat, Win32 systems do not appear to be able to give you
anything smaller than milliseconds (the first three decimal places).



_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



--=_alternative 00483C0F86256F24_=
Content-Type: text/html; charset="us-ascii"



I think it's the " %s" at the end of the format string--it's a fifth format and only four "arguments" are presented....



      print sprintf("%02d:%02d:%02d.%06d %s",  ## ----- THIS IS Line 208

               $hour, $minute, $second, $microseconds);  



The first %02d is for $hour, the second %02d is for $minute, the third %02d is for $second, the %06d is for $microseconds, so what is that %s hanging off the end supposed to get?



Deane











"Kamal Ahmed" <Kamal.Ahmed@esecurity.net>

Sent by: activeperl-bounces@listserv.ActiveState.com

10/04/2004 16:20


       

        To:        <activeperl@listserv.ActiveState.com>

        cc:        

        Subject:        RE: Time in perl






Bill,



When I use your approach, I am getting an error:



25/02/2001-Use of uninitialized value in sprintf at ./exploitD.pl line

208.

17:14:33.016000 time="" action="" orig="bedm-edn-650-pri"

i/f_dir="inbound" i/f_



Line 208 and few lines around it are as below:



sub genOne {

   my $index = shift;



   my %data;

   $data{'name'} = "test";



   my $num = int(rand() * 100);

   my $year = 1997 + intRand(5);

   my $mon =  intRand(12);

   my $day =  intRand(28) + 1;



   my $date = "";

   $date .= "0" if (int($day) < 10);

   $date .= $day;

   $date .= "\/$months[$mon]\/$year-";

   

   print "$date";



   $data{'LogFileID'} = '1044611703';

   $data{'LogRecNum'} = $recNum++;

   ##$data{'time'} = "$date";

   

   ##-------------------

   my $epochseconds = "0";

   my $microseconds = "0";

   ($epochseconds, $microseconds) = gettimeofday;

     ($second, $minute, $hour) = localtime($epochseconds);

     print sprintf("%02d:%02d:%02d.%06d %s",  ## ----- THIS IS Line 208

               $hour, $minute, $second, $microseconds);  

               ##----------------------

   

Thanks,



-Kamal.





-----Original Message-----

From: Bill Curnow [mailto:bill.curnow@pcca.com]

Sent: Monday, October 04, 2004 10:48 AM

To: Kamal Ahmed; activeperl@listserv.ActiveState.com

Subject: Re: Time in perl





On 4 Oct 2004 at 10:34, Kamal Ahmed wrote:



> I am trying to print time in a format HH:MM:SS.nnnnnn

> E.g. 08:06:26.464649

>

> Any ideas how I can do it ?



 use Time::HiRes qw(gettimeofday);  



 ($epochseconds, $microseconds) = gettimeofday;

 ($second, $minute, $hour) = localtime($epochseconds);

 print sprintf("%02d:%02d:%02d.%06d %s",

               $hour, $minute, $second, $microseconds);  



One caveat, Win32 systems do not appear to be able to give you

anything smaller than milliseconds (the first three decimal places).







_______________________________________________

ActivePerl mailing list

ActivePerl@listserv.ActiveState.com

To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs






--=_alternative 00483C0F86256F24_=--


--===============1601191524==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1601191524==--

RE: Time in perl

am 05.10.2004 16:46:22 von Bill Curnow

On 4 Oct 2004 at 17:20, Kamal Ahmed wrote:

> When I use your approach, I am getting an error:

As several others pointed out the trailing %s was causing the
problem. The code was a nearly completely stripped-down example from
our job tracking system. I should have spent a few extra minutes
cleaning it up.

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Time in perl

am 05.10.2004 18:44:55 von joe

Depending on how the module was written for Win32, you can get a resolution
of about 3-5ms or about 10-12ms on Windows systems. Pretty tough to get any
better than that if it is possible at all.

joe



-----Original Message-----
From: Bill Curnow [mailto:bill.curnow@pcca.com]
Sent: Monday, October 04, 2004 10:48 AM
To: Kamal Ahmed; activeperl@listserv.ActiveState.com
Subject: Re: Time in perl



One caveat, Win32 systems do not appear to be able to give you
anything smaller than milliseconds (the first three decimal places).


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: time

am 31.10.2005 21:44:40 von JPerlmutter

Deane-

let me first copy the substance from your email to cite it right


****quote*****
A simple one: what's the best, quickest, way to get the difference between

two dates. They're both in the same format: "YYYYMMDD". One of them gets
ginned up into that format starting from a call to localtime (that is, @x
= localtime; stuff happens to @x; $y ends up "20051031";), the other's
read in as a string from a file.
****quote*****


ok well
my $time = localtime(); returns : Mon Oct 31 15:35:34 2005

which is nice when you're doing a report for a human, but not for
differences.

aside from checking date/time modules that exist, i would suggest looking
into converting to epoch. at this point it is in seconds since midnight
Jan 1 1970. from there you find the difference and then you have the
seconds difference. converting to another form is as simple as:

----pseudocode ---

$t1 = localtime();
$t2= ;
$t3=convert($t1, epoch);
$t4=convert($t2, epoch);

$seconds_difference=$t3-$t4;
$minutes_difference=$seconds_difference/60;
$hours_difference=$minutes_difference/60;
$days_difference=$hours_difference/24

----pseudocode ---

not sure of the built in converter off the top of my head. figure I'd show
you what I'd do and you can decide if it will work for you.

HTH

-josh
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs