date format

2004-08-16 Thread DBSMITH
All, 

I have this code:

my ($month, $day, $year) = (localtime)[4,3,5];
printf ("%02d/%02d/%02d\n", $month+1,$day,$year+1900);

which gives me 

08/16/2004

what I want is 08/16/04.  Should I just use Posix with strftime or is 
there a quicker way w/out having to load the Posix module?

also, why I ntoiced I had to may $month+1 otherwise it outputs a month 
back.  why is this?

thanks, 

derek



RE: date format

2004-08-16 Thread Bob Showalter
[EMAIL PROTECTED] wrote:
> All,
> 
> I have this code:
> 
> my ($month, $day, $year) = (localtime)[4,3,5];
> printf ("%02d/%02d/%02d\n", $month+1,$day,$year+1900);
> 
> which gives me
> 
> 08/16/2004
> 
> what I want is 08/16/04.  Should I just use Posix with strftime or is
> there a quicker way w/out having to load the Posix module?

   ($year + 1900) % 100

> 
> also, why I ntoiced I had to may $month+1 otherwise it outputs a month
> back.  why is this?

Historic localtime(3) semantics. Not really sure how it got that way, but
that's the way it is. Maybe because the value would typically be used to
index an array in C, which uses zero-based arrays?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: date format

2004-08-16 Thread Chris Devers
On Mon, 16 Aug 2004 [EMAIL PROTECTED] wrote:
I have this code:
my ($month, $day, $year) = (localtime)[4,3,5];
printf ("%02d/%02d/%02d\n", $month+1,$day,$year+1900);
which gives me
08/16/2004
what I want is 08/16/04.  Should I just use Posix with strftime or is
there a quicker way w/out having to load the Posix module?
POSIX is probably the "right" way to do it, though you could just use a 
regex to strip off the first two digits of the year:

$year =~ s/\d\d(\d\d)/$1/;
...or something to that effect.
also, why I ntoiced I had to may $month+1 otherwise it outputs a month
back.  why is this?
Because, as with many things in programming, it counts from zero, not 
one.


--
Chris Devers  [EMAIL PROTECTED]
http://devers.homeip.net:8080/blog/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: date format

2004-08-16 Thread Bob Showalter
Bob Showalter wrote:
>($year + 1900) % 100

Actually just

   $year % 100

is valid. The former makes it clearer what you're doing, if you're into that
:~)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: date format

2004-08-16 Thread Flemming Greve Skovengaard
[EMAIL PROTECTED] wrote:
All, 

I have this code:
my ($month, $day, $year) = (localtime)[4,3,5];
printf ("%02d/%02d/%02d\n", $month+1,$day,$year+1900);
which gives me 

08/16/2004
what I want is 08/16/04.  Should I just use Posix with strftime or is 
there a quicker way w/out having to load the Posix module?

also, why I ntoiced I had to may $month+1 otherwise it outputs a month 
back.  why is this?

thanks, 

derek

printf ("%02d/%02d/%02d\n", $month + 1, $day, $year - 100);
# Only works when $year > 1999.
Try 'perldoc -f localtime' to learn why this works.
--
Flemming Greve Skovengaard   FAITH, n.
a.k.a Greven, TuxPower   Belief without evidence in what is told
<[EMAIL PROTECTED]>  by one who speaks without knowledge,
4112.38 BogoMIPS of things without parallel.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: date format

2004-08-16 Thread DBSMITH
Ah yes the old modulus operator!  :  )  should of thought of that 
myself... but I am glad this list exists!

thanks a bunch!

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams






Bob Showalter <[EMAIL PROTECTED]>
08/16/2004 12:44 PM

 
To: "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>, [EMAIL PROTECTED]
cc: 
    Subject:RE: date format


Bob Showalter wrote:
>($year + 1900) % 100

Actually just

   $year % 100

is valid. The former makes it clearer what you're doing, if you're into 
that
:~)




RE: date format

2004-08-16 Thread Bob Showalter
Flemming Greve Skovengaard wrote:
> printf ("%02d/%02d/%02d\n", $month + 1, $day, $year - 100);
> # Only works when $year > 1999.

And when $year <= 2099 :~)

Stick to $year % 100;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: date format

2004-08-16 Thread Flemming Greve Skovengaard
Bob Showalter wrote:
Flemming Greve Skovengaard wrote:
printf ("%02d/%02d/%02d\n", $month + 1, $day, $year - 100);
# Only works when $year > 1999.

And when $year <= 2099 :~)
Stick to $year % 100;
Yes, you are correct. Your solution is fool proof.
--
Flemming Greve Skovengaard   FAITH, n.
a.k.a Greven, TuxPower   Belief without evidence in what is told
<[EMAIL PROTECTED]>  by one who speaks without knowledge,
4112.38 BogoMIPS of things without parallel.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: date format

2004-08-16 Thread DBSMITH
I have this value,  from the date format solution emails,  in a subroutine 
and I want to pass it to a if clause, how would I go about this?
Can I assign a literal such as 

sub datemanip {

my ( $month, $day, $year) = (localtime)[4,3,5];
my $foodate = printf ("%02d/%02d/%02d\n", $month + 1, $day, ($year %100));
}

while  ()
if ( $_ =~ $foodate) {

.

}






Flemming Greve Skovengaard <[EMAIL PROTECTED]>
08/16/2004 02:58 PM

 
To: "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
cc: Bob Showalter <[EMAIL PROTECTED]>
    Subject:Re: date format


Bob Showalter wrote:
> Flemming Greve Skovengaard wrote:
> 
>>printf ("%02d/%02d/%02d\n", $month + 1, $day, $year - 100);
>># Only works when $year > 1999.
> 
> 
> And when $year <= 2099 :~)
> 
> Stick to $year % 100;
> 

Yes, you are correct. Your solution is fool proof.

-- 
Flemming Greve Skovengaard   FAITH, n.
a.k.a Greven, TuxPower   Belief without evidence in what 
is told
<[EMAIL PROTECTED]>  by one who speaks without 
knowledge,
4112.38 BogoMIPS of things without parallel.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>






Re: date format

2004-08-16 Thread Chris Devers
On Mon, 16 Aug 2004 [EMAIL PROTECTED] wrote:
sub datemanip
A name like that screams a need for the Date::Manip CPAN module:

Look over the docs for that module, see if you can't use it to do what 
you need to do, and let the list know if you hit any obstacles.


--
Chris Devers  [EMAIL PROTECTED]
http://devers.homeip.net:8080/blog/
np: 'Nevermind'
 by Red Hot Chili Peppers
 from 'Freaky Styley'
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: date format

2004-08-16 Thread Flemming Greve Skovengaard
[EMAIL PROTECTED] wrote:
I have this value,  from the date format solution emails,  in a subroutine 
and I want to pass it to a if clause, how would I go about this?
Can I assign a literal such as 

sub datemanip {
my ( $month, $day, $year) = (localtime)[4,3,5];
my $foodate = printf ("%02d/%02d/%02d\n", $month + 1, $day, ($year %100));
Use sprintf for that.
I use sprintf in one of my programs like this:
my $d8_dato = sprintf( "%4d%02d%02d", ( substr( $dato, 4, 4 ),
substr( $dato, 2, 2 ),
substr( $dato, 0, 2 ) ) );

}
while  ()
if ( $_ =~ $foodate) {
It would work, if you use sprintf as shown above. But I would write:
if ( $_ =~ m/$foodate/ )
instead to eliminate confusesing when maintaining the code later.
.
}

--
Flemming Greve Skovengaard   FAITH, n.
a.k.a Greven, TuxPower   Belief without evidence in what is told
<[EMAIL PROTECTED]>  by one who speaks without knowledge,
4112.38 BogoMIPS of things without parallel.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



Re: date format

2004-08-17 Thread Gunnar Hjalmarsson
[EMAIL PROTECTED] wrote:
I have this code:
my ($month, $day, $year) = (localtime)[4,3,5];
printf ("%02d/%02d/%02d\n", $month+1,$day,$year+1900);
which gives me
08/16/2004
what I want is 08/16/04.
"perldoc -f localtime" describes very clearly how you get a two digit 
year. It's advisable to study the docs for the functions you are using 
before asking others for help.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Date format again

2002-03-03 Thread Troy May

Hello, this guy finally emailed his script to me.  The problem he is having
is with "$year".  Here's the dating part of the code:

---
$date = `/bin/date`;
chop($date);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$thisday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[(localtime) [6]];
$s = (localtime)[0];
$m = (localtime)[1];
$m = ($m + 35) ;

$h = (localtime)[2];

if ($m >=60)
{
$m = ($m - 60);
$h = ($h + 1);
}
if ($h >=24)
{
$h = 1;
}
else
{
$h = ($h + 10) ;
}

$month = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[(localtime)[4]];
$year = (localtime)[5];
$year = ($year + 1900);

$day = (localtime)[3];


# this coding is creating problem

($sec,$min,$hour,$mday,$mon,$year,undef,undef,undef) = localtime();
$mon++; # adjust from 0-11 to 1-12
$year %= 100;
$theDate = sprintf("%02u%02u%02u", $mday, $mon, $year);

--

Now, when he prints "$theDate", everything is ok with it (format-030302).
But when he prints just "$year" it displays "2" instead of "2002"

Any ideas?



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Help on Date Format

2001-04-24 Thread Arante, Susan

Could someone tell me why this is happening?  When I use this command, it
used to give me 20010405.doc (mmdd.doc), now it's giving me 2001 4 5.doc
- I'm losing the leading zeros.  
Command is on Perl 5 - printf("\%s%02s%02s.doc",$year,$month,$day).

Thanks.



date format using localtime()

2004-03-12 Thread Jeff Westman
Is there a way in perl to get the month/day/year using localtime
WITHOUT using 'use POSIX qw(strftime)' or a system "date" call.

Something using slices, maybe something like:

  print scalar ((localtime(time))[4,3,7])

expecting the result to be 03122004.

Trivial question, thanks in advance.


Jeff

__
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Date format again

2002-03-03 Thread Marcelo E. Magallon

[ Don't Cc: me, I read this mailing list, thank you ]

>> Troy May <[EMAIL PROTECTED]> writes:

 > ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
 > $thisday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[(localtime) [6]];
 > $s = (localtime)[0];
 > $m = (localtime)[1];
 > $m = ($m + 35) ;

 Is adding 35 minutes to the current date what this is all about?

 How about:

($sec, $min, $hour, $mday, $mon, $year) = (localtime(time+35*60))[0..5];

 and save yourself from all that adding and carrying?

 By the way, in the original program, if you happen to run it at, say
 23:59:59 and the clock changes to 0:00:00 while the program is still
 running, between one of the calls to localtime, you'll get horribly
 incorrect results.

 > $year %= 100;

 > Now, when he prints "$theDate", everything is ok with it (format-030302).
 > But when he prints just "$year" it displays "2" instead of "2002"

 2002 % 100 == 2

-- 
Marcelo

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Date format again

2002-03-03 Thread Alfred Wheeler

$year is a number. Numbers do not retain leading zeros. "$year %= 100;" does
not return the string "02"; it returns the number 2. Try something like
this -
$s_year = sprintf("%02u", $year);
print "$s_year";

The sprintf function basically converts the number into a string. 

"sprintf returns a string formatted by the usual printf conventions 
of the C library function sprintf." -- 
http://www.perldoc.com/perl5.6.1/pod/func/sprintf.html

It's just the same thing you did with the $theDate variable only applied to
a single item

of the list on the right side of the assignment.


- Original Message -
From: "Troy May" <[EMAIL PROTECTED]>
To: "Beginners CGI List" <[EMAIL PROTECTED]>
Sent: Sunday, March 03, 2002 12:52 AM
Subject: Date format again


> Hello, this guy finally emailed his script to me.  The problem he is
having
> is with "$year".  Here's the dating part of the code:
>
> ---
> $date = `/bin/date`;
> chop($date);
> ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
> $thisday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[(localtime) [6]];
> $s = (localtime)[0];
> $m = (localtime)[1];
> $m = ($m + 35) ;
>
> $h = (localtime)[2];
>
> if ($m >=60)
> {
> $m = ($m - 60);
> $h = ($h + 1);
> }
> if ($h >=24)
> {
> $h = 1;
> }
> else
> {
> $h = ($h + 10) ;
> }
>
> $month =
(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[(localtime)[4]];
> $year = (localtime)[5];
> $year = ($year + 1900);
>
> $day = (localtime)[3];
>
>
> # this coding is creating problem
>
> ($sec,$min,$hour,$mday,$mon,$year,undef,undef,undef) = localtime();
> $mon++; # adjust from 0-11 to 1-12
> $year %= 100;
> $theDate = sprintf("%02u%02u%02u", $mday, $mon, $year);
>
> --
>
> Now, when he prints "$theDate", everything is ok with it (format-030302).
> But when he prints just "$year" it displays "2" instead of "2002"
>
> Any ideas?
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Regex for date format

2018-06-29 Thread Mike Martin
Hi
I am trying to convert a string of the format
2018-01-01 16-45-21-654278

to a proper timestamp string

so basically I want to replace all -  after the date part

I am getting a bit stuck, lookbehind doesnt seem to work as it includes the
lookbehind on every occurence
last attempt is
s/(?<= )-/:/g;

any help appreciated

Mike


Re: Help on Date Format

2001-04-24 Thread John Joseph Trammell

On Tue, Apr 24, 2001 at 11:00:40AM -0500, Arante, Susan wrote:
> Could someone tell me why this is happening?  When I use this command, it
> used to give me 20010405.doc (mmdd.doc), now it's giving me 2001 4 5.doc
> - I'm losing the leading zeros.  
> Command is on Perl 5 - printf("\%s%02s%02s.doc",$year,$month,$day).

You probably want "%d%02d%02d.doc" as your format string.

 d => digits
 s => strings

And I don't think you need that leading "\".

-- 
Aren't you, at this point, cutting down a California Redwood using a
banana *and* a particle accelerator?
 - Bernard El-Hagin, in CLPM



Re: Help on Date Format

2001-04-24 Thread Casey West

On Tue, Apr 24, 2001 at 11:00:40AM -0500, Arante, Susan wrote:
: Could someone tell me why this is happening?  When I use this command, it
: used to give me 20010405.doc (mmdd.doc), now it's giving me 2001 4 5.doc
: - I'm losing the leading zeros.  
: Command is on Perl 5 - printf("\%s%02s%02s.doc",$year,$month,$day).


Try this:

printf( "%04d%02d%02d", $year, $month, $day );

for printf(), %d is used for integers and integers will be right
padded with zeros, like you want.

See:

perldoc -f printf

Enjoy!

-- 
Casey West



Re: Help on Date Format

2001-04-24 Thread Kevin Meltzer

Hi Susan,

I get what you expect:

perl -wle '$y=2001;$m=4;$d=5;printf("\%s%02s%02s.doc",$y,$m,$d)';
20010405.doc

Personally, I like POSIX.pm for dates.

# perl -MPOSIX -wle 'print strftime("%Y%m%d", localtime) . ".doc"';
20010424.doc

'perldoc POSIX' to learn more (look for strftime).

Cheers,
Kevin

On Tue, Apr 24, 2001 at 11:00:40AM -0500, Arante, Susan 
([EMAIL PROTECTED]) spew-ed forth:
> Could someone tell me why this is happening?  When I use this command, it
> used to give me 20010405.doc (mmdd.doc), now it's giving me 2001 4 5.doc
> - I'm losing the leading zeros.  
> Command is on Perl 5 - printf("\%s%02s%02s.doc",$year,$month,$day).
> 
> Thanks.
> 

-- 
I write the music I like. If other people like it, fine, they can go buy the
albums. And if they don't like it, there's always Michael Jackson for them to
listen to. -- Frank Zappa (about his music from the Yellow Shark)



RE: Help on Date Format

2001-04-24 Thread Arante, Susan

Thanks to all your responses.  It works now!!

Kevin, I'll try this one as well, but for now the d instead of s works
enough!!

-Original Message-
From: Kevin Meltzer [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, April 24, 2001 10:09 AM
To: Arante, Susan
Cc: [EMAIL PROTECTED]
Subject: Re: Help on Date Format


Hi Susan,

I get what you expect:

perl -wle '$y=2001;$m=4;$d=5;printf("\%s%02s%02s.doc",$y,$m,$d)';
20010405.doc

Personally, I like POSIX.pm for dates.

# perl -MPOSIX -wle 'print strftime("%Y%m%d", localtime) . ".doc"';
20010424.doc

'perldoc POSIX' to learn more (look for strftime).

Cheers,
Kevin

On Tue, Apr 24, 2001 at 11:00:40AM -0500, Arante, Susan
([EMAIL PROTECTED]) spew-ed forth:
> Could someone tell me why this is happening?  When I use this command, it
> used to give me 20010405.doc (mmdd.doc), now it's giving me 2001 4
5.doc
> - I'm losing the leading zeros.  
> Command is on Perl 5 - printf("\%s%02s%02s.doc",$year,$month,$day).
> 
> Thanks.
> 

-- 
I write the music I like. If other people like it, fine, they can go buy the
albums. And if they don't like it, there's always Michael Jackson for them
to
listen to. -- Frank Zappa (about his music from the Yellow Shark)



Re: Help on Date Format

2001-04-24 Thread John Joseph Trammell

On Tue, Apr 24, 2001 at 01:09:13PM -0400, Kevin Meltzer wrote:
> Hi Susan,
> 
> I get what you expect:
> 
> perl -wle '$y=2001;$m=4;$d=5;printf("\%s%02s%02s.doc",$y,$m,$d)';
> 20010405.doc

[snip]

Well I'll be damned.

[ ~ ] perl -e 'printf "%04s\n", 1'
0001
[ ~ ] perl -e 'printf "%04s\n", "1"'
0001
[ ~ ] perl -e 'printf "%04s\n", "   1"'
   1
[ ~ ] perl -e 'printf "%04s\n", "  1"'
0  1
[ ~ ]

And here I thought I knew it all.  :-)

-- 
Just Another Perl Hacker.



Re: Help on Date Format

2001-04-24 Thread Kevin Meltzer

In this case, it wont really matter. Since 1 and "1" is essentially  the same.
If you were actually using a signed integer (in decimal), then you would see
the difference:

>From perldoc -f sprintf:

   %s   a string
   %d   a signed integer, in decimal

[root@fluffhead /]# perl -e 'printf "%04s\n", 12.5'
12.5
[root@fluffhead /]# perl -e 'printf "%04d\n", 12.5'
0012

But, go back to what is basically a string (integer in string context??):

[root@fluffhead /]# perl -e 'printf "%04d\n", 12'
0012
[root@fluffhead /]# perl -e 'printf "%04s\n", 12'
0012

So, why her snippet suddenly freaked out, I don't know. We didn't actually see
what created the $year, $month and $day *shrug*

Cheers,
Kevin

On Tue, Apr 24, 2001 at 11:40:23AM -0500, John Joseph Trammell 
([EMAIL PROTECTED]) spew-ed forth:
> [snip]
> 
> Well I'll be damned.
> 
> [ ~ ] perl -e 'printf "%04s\n", 1'
> 0001
> [ ~ ] perl -e 'printf "%04s\n", "1"'
> 0001
> [ ~ ] perl -e 'printf "%04s\n", "   1"'
>1
> [ ~ ] perl -e 'printf "%04s\n", "  1"'
> 0  1
> [ ~ ]
> 
> And here I thought I knew it all.  :-)

-- 
"This Too Shall Pass"
-- inscription on the inside of King Solomon's Ring.



Re: date format using localtime()

2004-03-12 Thread Steve Mayer
Jeff,

  Check out
  http://www.users.voicenet.com/~corr/macsupt/macperl/localtime.html

Steve

On Fri, Mar 12, 2004 at 01:38:28PM -0800, Jeff Westman wrote:
> Is there a way in perl to get the month/day/year using localtime
> WITHOUT using 'use POSIX qw(strftime)' or a system "date" call.
> 
> Something using slices, maybe something like:
> 
>   print scalar ((localtime(time))[4,3,7])
> 
> expecting the result to be 03122004.
> 
> Trivial question, thanks in advance.
> 
> 
> Jeff
> 
> __
> Do you Yahoo!?
> Yahoo! Search - Find what you?re looking for faster
> http://search.yahoo.com
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
> 
> 

=
Steve Mayer Oracle Corporation
Project Lead1211 SW 5th Ave.
Portland Development Center Suite 900
[EMAIL PROTECTED]   Portland, OR 97204 
Phone:  503-525-3127
=

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: date format using localtime()

2004-03-12 Thread Owen Cook

On Fri, 12 Mar 2004, Jeff Westman wrote:

> Is there a way in perl to get the month/day/year using localtime
> WITHOUT using 'use POSIX qw(strftime)' or a system "date" call.
> 
> Something using slices, maybe something like:
> 
>   print scalar ((localtime(time))[4,3,7])
> 
> expecting the result to be 03122004.

Try something like this;

---
#!/usr/bin/perl

use strict;
use warnings;

my @t = localtime(time);
print (sprintf("%02d%02d%04d",$t[4] +1,$t[3],$t[5] +1900));



Owen


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Converting numbers into date format

2001-12-04 Thread Sandeep Pathare

I am a beginner of Perl.  How do I convert and print the following strings 
into a date format so that either the date is returned or "date not valid" 
is printed.
Input data is:

792910171010163200
552910171010163200
552913171010163200
552910171010163200
552909171010163200
552909171010163200

For each of the data value, the output should like:

552910171010163200  Sat Nov 17 10:29:55 2001

Here is some hint I read in the documentation, but still can't figure out 
how to use it:

The 552910171010163200 which is Sat Nov 17 10:29:55 2001,
(HINT: localtime) should be parsed into HH:MM:SS (Zero padded) and
WDay MMM DD,  - which should look like Sat Nov 17, 2001
55: 29:  10: 17:10:  101: 6:  320:0
HH:MM:SS:17:Nov: year (1900 +101= 2001):Sat:  320thday of the year

Thank you very much in advance.

Sandeep

_
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Changing date format using carpout

2017-03-25 Thread SSC_perl
I’ll sometimes use the following code at the beginning of a script to 
log errors while testing:

BEGIN {
use CGI::Carp qw(carpout);
open(_STDERR,'>&STDERR'); close STDERR;
open (my $log, '>>', 'logs/error.log') or warn("Couldn't open 
error.log: $! \n");
carpout($log);
close ($log);
}

However, I would like to change the date format.  I’m not wild about 
seeing the the full timestamp on every line:

[Sat Mar 25 08:05:58 2017]

Is there a way I can format that to my liking?  I see there's a 
‘noTimestamp’ option to stop it from printing altogether, but I don’t see a way 
to change the output.

Side question: is there a better way to accomplish what I’m doing 
above?  I’m happy with what I already have (except for the date) but only 
because I don’t know anything else.  I’d like to hear how others do this.

Thanks,
Frank
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Regex for date format

2018-06-29 Thread Uri Guttman

On 06/29/2018 09:32 AM, Mike Martin wrote:

Hi
I am trying to convert a string of the format
2018-01-01 16-45-21-654278

to a proper timestamp string

so basically I want to replace all -  after the date part


i am not sure what you are trying to do. show the after text that you 
want. a proper timestamp string is not specific enough.


if you want to really parse that string, then use Time::Piece and its 
strptime sub which can parse pretty much any time/date string.


uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Regex for date format

2018-06-29 Thread Uri Guttman

On 06/29/2018 10:41 AM, Mike Martin wrote:

sorry
-mm-dd hh:mm:ss.dd
eg:
2018-01-01 12-45-10-456789 to
2018-01-01 12:45:10.456789




please reply to the list and not to me!

then why did you want lookbehind? this is very easy if you just grab the 
time parts and reassemble them as you want. 


    $stamp =~ s/\s(\d\d)-(\d\d)-(\d\d)-/ $1:$2:$3./ ;

it uses the space to mark where the time part starts.

uri




Re: Regex for date format

2018-06-29 Thread Mike Martin
Thanks

On Fri, 29 Jun 2018, 15:48 Uri Guttman,  wrote:

> On 06/29/2018 10:41 AM, Mike Martin wrote:
>
> sorry
> -mm-dd hh:mm:ss.dd
> eg:
> 2018-01-01 12-45-10-456789 to
> 2018-01-01 12:45:10.456789
>
>
>
> please reply to the list and not to me!
>
> then why did you want lookbehind? this is very easy if you just grab the
> time parts and reassemble them as you want. 
>
> $stamp =~ s/\s(\d\d)-(\d\d)-(\d\d)-/ $1:$2:$3./ ;
>
> it uses the space to mark where the time part starts.
>
> uri
>
>
>


Re: Regex for date format

2018-06-29 Thread Mike Martin
Worked perfectly thanks, uri, and same technique works perfectly in
postgresql regexp_replace for info

On 29 June 2018 at 16:18, Mike Martin  wrote:

> Thanks
>
>
> On Fri, 29 Jun 2018, 15:48 Uri Guttman,  wrote:
>
>> On 06/29/2018 10:41 AM, Mike Martin wrote:
>>
>> sorry
>> -mm-dd hh:mm:ss.dd
>> eg:
>> 2018-01-01 12-45-10-456789 to
>> 2018-01-01 12:45:10.456789
>>
>>
>>
>> please reply to the list and not to me!
>>
>> then why did you want lookbehind? this is very easy if you just grab the
>> time parts and reassemble them as you want. 
>>
>> $stamp =~ s/\s(\d\d)-(\d\d)-(\d\d)-/ $1:$2:$3./ ;
>>
>> it uses the space to mark where the time part starts.
>>
>> uri
>>
>>
>>


Newbie needs help changing date format

2004-08-26 Thread John Bruin
Hi

I have a list of dates that have been converted to epoch seconds, processed
and then converted back to a string (using timelocal). The resulting date
format is:-

"Wed Mar 16 22:10:16 2004"

What is the easiest way to convert this format (or epoch seconds) to
"16-Mar-2004 22:10" - preferrably using a standard module as I don't have
administrator rights. 

Thanks
John



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Converting numbers into date format

2001-12-04 Thread nafiseh saberi

hi
you can do it with
gmtime
localtime
and many time function in...
http://www.perldoc.com/perl5.6/pod/func/gmtime.html
http://www.perldoc.com/perl5.6/pod/func/localtime.html
be successful.
your problem will solve.
_
  Best regards.
  Nafiseh Saberi
  www.iraninfocenter.net
  www.sorna.net
 ___
- Original Message -
From: "Sandeep Pathare" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, December 04, 2001 08:12 AM
Subject: Converting numbers into date format


> I am a beginner of Perl.  How do I convert and print the following strings
> into a date format so that either the date is returned or "date not valid"
> is printed.
> Input data is:
>
> 792910171010163200
> 552910171010163200
> 552913171010163200
> 552910171010163200
> 552909171010163200
> 552909171010163200
>
> For each of the data value, the output should like:
>
> 552910171010163200  Sat Nov 17 10:29:55 2001
>
> Here is some hint I read in the documentation, but still can't figure out
> how to use it:
>
> The 552910171010163200 which is Sat Nov 17 10:29:55 2001,
> (HINT: localtime) should be parsed into HH:MM:SS (Zero padded) and
> WDay MMM DD,  - which should look like Sat Nov 17, 2001
> 55: 29:  10: 17:10:  101: 6:
320:0
> HH:MM:SS:17:Nov: year (1900 +101= 2001):Sat:  320thday of the year
>
> Thank you very much in advance.
>
> Sandeep
>
> _
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Converting numbers into date format

2001-12-04 Thread Joel Divekar

Hi

use POSIX

Regards

Joel

At 11:54 AM 12/4/2001 +0330, nafiseh saberi wrote:
>hi
>you can do it with
>gmtime
>localtime
>and many time function in...
>http://www.perldoc.com/perl5.6/pod/func/gmtime.html
>http://www.perldoc.com/perl5.6/pod/func/localtime.html
>be successful.
>your problem will solve.
>_
>   Best regards.
>   Nafiseh Saberi
>   www.iraninfocenter.net
>   www.sorna.net
>  ___
>- Original Message -
>From: "Sandeep Pathare" <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>
>Sent: Tuesday, December 04, 2001 08:12 AM
>Subject: Converting numbers into date format
>
>
> > I am a beginner of Perl.  How do I convert and print the following strings
> > into a date format so that either the date is returned or "date not valid"
> > is printed.
> > Input data is:
> >
> > 792910171010163200
> > 552910171010163200
> > 552913171010163200
> > 552910171010163200
> > 552909171010163200
> > 552909171010163200
> >
> > For each of the data value, the output should like:
> >
> > 552910171010163200  Sat Nov 17 10:29:55 2001
> >
> > Here is some hint I read in the documentation, but still can't figure out
> > how to use it:
> >
> > The 552910171010163200 which is Sat Nov 17 10:29:55 2001,
> > (HINT: localtime) should be parsed into HH:MM:SS (Zero padded) and
> > WDay MMM DD,  - which should look like Sat Nov 17, 2001
> > 55: 29:  10: 17:10:  101: 6:
>320:0
> > HH:MM:SS:17:Nov: year (1900 +101= 2001):Sat:  320thday of the year
> >
> > Thank you very much in advance.
> >
> > Sandeep
> >
> > _
> > Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
> >
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>--
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]


--
QuantumLink Communications, Bombay, India



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Converting numbers into date format

2001-12-04 Thread John W. Krahn

Sandeep Pathare wrote:
> 
> I am a beginner of Perl.  How do I convert and print the following strings
> into a date format so that either the date is returned or "date not valid"
> is printed.
> Input data is:
> 
> 792910171010163200
> 552910171010163200
> 552913171010163200
> 552910171010163200
> 552909171010163200
> 552909171010163200
> 
> For each of the data value, the output should like:
> 
> 552910171010163200  Sat Nov 17 10:29:55 2001
> 
> Here is some hint I read in the documentation, but still can't figure out
> how to use it:
> 
> The 552910171010163200 which is Sat Nov 17 10:29:55 2001,
> (HINT: localtime) should be parsed into HH:MM:SS (Zero padded) and
> WDay MMM DD,  - which should look like Sat Nov 17, 2001
> 55: 29:  10: 17:10:  101: 6:  320:0
> HH:MM:SS:17:Nov: year (1900 +101= 2001):Sat:  320thday of the year


Here is one way to do it:

$ cat try.pl
#!/usr/bin/perl -w
use strict;
use Time::Local qw(timelocal_nocheck);

while (  ) {
chomp;
my $date = timelocal_nocheck( unpack 'A2A2A2A2A2A3', $_ );
if ( $_ eq join( '', localtime( $date ) )  ) {
print scalar localtime( $date ), "\n";
}
else {
print "Date not valid.\n";
}
}

__DATA__
792910171010163200
552910171010163200
552913171010163200
552910171010163200
552909171010163200
552909171010163200

$ ./try.pl
Date not valid.
Sat Nov 17 10:29:55 2001
Sat Nov 17 13:29:55 2001
Sat Nov 17 10:29:55 2001
Date not valid.
Date not valid.




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Converting numbers into date format

2001-12-04 Thread John W. Krahn

"John W. Krahn" wrote:
> 
> Sandeep Pathare wrote:
> >
> > I am a beginner of Perl.  How do I convert and print the following strings
> > into a date format so that either the date is returned or "date not valid"
> > is printed.
> > Input data is:
> >
> > 792910171010163200
> > 552910171010163200
> > 552913171010163200
> > 552910171010163200
> > 552909171010163200
> > 552909171010163200
> >
> > For each of the data value, the output should like:
> >
> > 552910171010163200  Sat Nov 17 10:29:55 2001
> >
> > Here is some hint I read in the documentation, but still can't figure out
> > how to use it:
> >
> > The 552910171010163200 which is Sat Nov 17 10:29:55 2001,
> > (HINT: localtime) should be parsed into HH:MM:SS (Zero padded) and
> > WDay MMM DD,  - which should look like Sat Nov 17, 2001
> > 55: 29:  10: 17:10:  101: 6:  320:0
> > HH:MM:SS:17:Nov: year (1900 +101= 2001):Sat:  320thday of the year
> 
> Here is one way to do it:
> 
> $ cat try.pl
> #!/usr/bin/perl -w
> use strict;
> use Time::Local qw(timelocal_nocheck);
> 
> while (  ) {
> chomp;
> my $date = timelocal_nocheck( unpack 'A2A2A2A2A2A3', $_ );
> if ( $_ eq join( '', localtime( $date ) )  ) {

Sorry, change the previous line to:

if ( $_ eq sprintf '%02d%02d%02d%02d%02d%03d%d%03d%d', localtime(
$date )   ) {


> print scalar localtime( $date ), "\n";
> }
> else {
> print "Date not valid.\n";
> }
> }
> 
> __DATA__
> 792910171010163200
> 552910171010163200
> 552913171010163200
> 552910171010163200
> 552909171010163200
> 552909171010163200
> 
> $ ./try.pl
> Date not valid.
> Sat Nov 17 10:29:55 2001
> Sat Nov 17 13:29:55 2001
> Sat Nov 17 10:29:55 2001
> Date not valid.
> Date not valid.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Date format search in the file

2008-11-22 Thread Sureshkumar M (HCL Financial Services)

Hi All,

 

  I want to find the string which are having the date inside the
file.

Please help me how do I match it,below is my program and it's not
returning anything.

 

 

 

#!/usr/bin/perl

open(DATA,"i")||die "Unable to open the file";

while()

{

if($_=~/(\d{2})([\W])\1\2\1]/)

{

print $_;

}

}

close(DATA);

exit 0;

~

Input file:

 

$cat i

15-06-79

05-06-1981

12-11-9

13-10-89

19-10-20009

1-10-0002

02-03-2008

03-nov-2008

 

$ 

 

 

Output should be:-

 

15-06-79

05-06-1981

13-10-89

02-03-2008

03-nov-2008

 

 

 

 



DISCLAIMER:
---
The contents of this e-mail and any attachment(s) are confidential and intended 
for the named recipient(s) only. 
It shall not attach any liability on the originator or HCL or its affiliates. 
Any views or opinions presented in 
this email are solely those of the author and may not necessarily reflect the 
opinions of HCL or its affiliates. 
Any form of reproduction, dissemination, copying, disclosure, modification, 
distribution and / or publication of 
this message without the prior written consent of the author of this e-mail is 
strictly prohibited. If you have 
received this email in error please delete it and notify the sender 
immediately. Before opening any mail and 
attachments please check them for viruses and defect.
---

date format search insdie the files

2008-11-29 Thread Sureshkumar M (HCL Financial Services)
Hi All,

I try to search the string which has the date format inside
the file,

But i am not able to get the desired files. Pls help me on this..

 

 

 

C)/tmp/sms/perl$ cat a1

 

115-06-1979

10-11-81

20-NOV-2008

05-07-1981

welcome

15-10-2008

12-03-20009

 

 

(C)/tmp/sms/perl$ cat 1

 

#/usr/bin/perl

open(DATA,"a1")||die"Unable to open the file";

 

while()

{

if($_=~/\d{2}-(\d{2}|\w{3})-\d{1,4}/)

{

print $_;

}

}

close(DATA);

exit 0;

(C)/tmp/sms/perl$

 

 

 

Output should be  :

 

10-11-81

20-NOV-2008

05-07-1981

15-10-2008

 

 

 

 

 



Re: Changing date format using carpout

2017-03-25 Thread Jim Gibson

> On Mar 25, 2017, at 8:51 AM, SSC_perl  wrote:
> 
>   I’ll sometimes use the following code at the beginning of a script to 
> log errors while testing:
> 
> BEGIN {
>   use CGI::Carp qw(carpout);
>   open(_STDERR,'>&STDERR'); close STDERR;
>   open (my $log, '>>', 'logs/error.log') or warn("Couldn't open 
> error.log: $! \n");
>   carpout($log);
>   close ($log);
> }
> 
>   However, I would like to change the date format.  I’m not wild about 
> seeing the the full timestamp on every line:
> 
> [Sat Mar 25 08:05:58 2017]
> 
>   Is there a way I can format that to my liking?  I see there's a 
> ‘noTimestamp’ option to stop it from printing altogether, but I don’t see a 
> way to change the output.

The timestamp is generated in the stamp() function, which starts on line 387 of 
the source file Carp.pm. You can copy the source of the modules and edit the 
stamp function to generate a shorter timestamp. You could also try overriding 
the supplied function of the imported module with your own version (not sure 
exactly how that is done).

> 
>   Side question: is there a better way to accomplish what I’m doing 
> above?  I’m happy with what I already have (except for the date) but only 
> because I don’t know anything else.  I’d like to hear how others do this.

I usually write my own log files using normal file output functions. However, 
that is harder, but not impossible, when you are writing CGI programs.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Changing date format using carpout

2017-03-26 Thread X Dungeness
Certainly an inefficient, unwieldy solution if you're dealing
with huge logfiles.  May be problematic on non-Unix too
but you could post-process the logfile in an END {} block


eg, get rid of hr:min:ss part of timestamp for example:

END {
$^I = '';
@ARGV = qw( /path/to/error.log);
while (<>) { s/\d\d:\d\d:\d\d//; print }
}




On Sat, Mar 25, 2017 at 8:51 AM, SSC_perl  wrote:
> I’ll sometimes use the following code at the beginning of a script to 
> log errors while testing:
>
> BEGIN {
> use CGI::Carp qw(carpout);
> open(_STDERR,'>&STDERR'); close STDERR;
> open (my $log, '>>', 'logs/error.log') or warn("Couldn't open 
> error.log: $! \n");
> carpout($log);
> close ($log);
> }
>
> However, I would like to change the date format.  I’m not wild about 
> seeing the the full timestamp on every line:
>
> [Sat Mar 25 08:05:58 2017]
>
> Is there a way I can format that to my liking?  I see there's a 
> ‘noTimestamp’ option to stop it from printing altogether, but I don’t see a 
> way to change the output.
>
> Side question: is there a better way to accomplish what I’m doing 
> above?  I’m happy with what I already have (except for the date) but only 
> because I don’t know anything else.  I’d like to hear how others do this.
>
> Thanks,
> Frank
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Changing date format using carpout

2017-03-26 Thread SSC_perl
> On Mar 25, 2017, at 8:58 PM, Jim Gibson  wrote:
> 
> You could also try overriding the supplied function of the imported module 
> with your own version (not sure exactly how that is done).

Hmm… me neither, but it’s a good idea.  I’ll contact the maintainer of 
CGI::Carp to see if that could be added as a feature.

> I usually write my own log files using normal file output functions. However, 
> that is harder, but not impossible, when you are writing CGI programs.

I do that as well, but that BEGIN statement catches things I wouldn’t 
normally look for myself.

Thanks,
Frank
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Changing date format using carpout

2017-03-26 Thread SSC_perl
> On Mar 26, 2017, at 1:20 AM, X Dungeness  wrote:
> 
> but you could post-process the logfile in an END {} block

I wouldn't have thought of that.  Thanks!

This now gives me a cleaner log to scan — streamlining the timestamp 
and adding a blank line between entries.  I also learned about $^I / 
$INPLACE_EDIT.  Very nice!

http://perldoc.perl.org/perlvar.html#SPECIAL-VARIABLES


END {
use Time::Piece;
$^I = '';
@ARGV = '/path/to/error.log';
while (<>) {
next if (m/^\s*$/);
if (m/\[(.+)\](.+)/) {
my ($timestamp, $message) = ($1, $2);
## [Wed Mar 22 12:43:20 2017] -- Original timestamp format.
my $dt = Time::Piece->strptime($timestamp, '%a %b %d %H:%M:%S %Y');
my $datetime = $dt->strftime('%Y-%m-%d %H:%M');
say "[$datetime] $message\n";
}
else {
say;
}
}
}
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Changing date format using carpout

2017-03-26 Thread Shawn H Corey
On Sun, 26 Mar 2017 11:28:44 -0700
SSC_perl  wrote:

> > On Mar 25, 2017, at 8:58 PM, Jim Gibson 
> > wrote:
> > 
> > You could also try overriding the supplied function of the imported
> > module with your own version (not sure exactly how that is done).  
> 
>   Hmm… me neither, but it’s a good idea.  I’ll contact the
> maintainer of CGI::Carp to see if that could be added as a feature.

In Perl, anything is possible. I haven't tried anything like this but
it would mean replacing the subroutine after the module was loaded.

This is untested.

use CGI::Carp;
{
no warnings;
local *CGI::Carp::stamp = sub {
# new code
}
}


-- 
Don't stop where the ink does.

Shawn H Corey

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Changing date format using carpout

2017-03-26 Thread SSC_perl
> On Mar 26, 2017, at 1:15 PM, Shawn H Corey  wrote:
> 
> it would mean replacing the subroutine after the module was loaded.

Thanks, Shawn, but I can’t get that to work.  Reading perldoc Core 
gives me the impression that I’d need to call the new sub, not the module.  If 
that’s true, I don’t see how it would work in this scenario.  Here’s what I 
tried:

BEGIN {
use CGI::Carp qw(carpout fatalsToBrowser );  # noTimestamp
{
no warnings;
use Time::Piece;
local *CGI::Carp::stamp = sub {
my $frame = 0;
my ($id, $pack, $file, $dev, $dirs);
if (defined($CGI::Carp::PROGNAME)) {
$id = $CGI::Carp::PROGNAME;
} else {
do {
$id = $file;
($pack, $file) = caller($frame++);
} until !$file;
}
if (! $CGI::Carp::FULL_PATH) {
($dev, $dirs, $id) = File::Spec->splitpath($id);
}
return "$id: " if $CGI::Carp::NO_TIMESTAMP;
my $time = scalar(localtime);
my $dt = Time::Piece->strptime($time, '%a %b %d %H:%M:%S %Y');
my $datetime = $dt->strftime('%Y-%m-%d %H:%M');
return "[$datetime] $id: ";
}
}
open (my $log, '>>', ‘/path/to/error.log');
carpout($log);
close ($log);
}
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Changing date format using carpout

2017-03-26 Thread X Dungeness
Shawn may have a different take but I think the "local" is
misplaced and goes out of scope when the call's made.

Here's a potential workaround:

out of scope

On Sun, Mar 26, 2017 at 2:02 PM, SSC_perl  wrote:
>> On Mar 26, 2017, at 1:15 PM, Shawn H Corey  wrote:
>>
>> it would mean replacing the subroutine after the module was loaded.
>
> Thanks, Shawn, but I can’t get that to work.  Reading perldoc Core 
> gives me the impression that I’d need to call the new sub, not the module.  
> If that’s true, I don’t see how it would work in this scenario.  Here’s what 
> I tried:
>
> BEGIN {
> use CGI::Carp qw(carpout fatalsToBrowser );  # noTimestamp
> {
> no warnings;
> use Time::Piece;
> local *CGI::Carp::stamp = sub {
> my $frame = 0;
> my ($id, $pack, $file, $dev, $dirs);
> if (defined($CGI::Carp::PROGNAME)) {
> $id = $CGI::Carp::PROGNAME;
> } else {
> do {
> $id = $file;
> ($pack, $file) = caller($frame++);
> } until !$file;
> }
> if (! $CGI::Carp::FULL_PATH) {
> ($dev, $dirs, $id) = File::Spec->splitpath($id);
> }
> return "$id: " if $CGI::Carp::NO_TIMESTAMP;
> my $time = scalar(localtime);
> my $dt = Time::Piece->strptime($time, '%a %b %d %H:%M:%S %Y');
> my $datetime = $dt->strftime('%Y-%m-%d %H:%M');
> return "[$datetime] $id: ";
> }
> }
> open (my $log, '>>', ‘/path/to/error.log');
> carpout($log);
> close ($log);
> }
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Changing date format using carpout

2017-03-26 Thread X Dungeness
The rest of it:

use Time::Piece;
use CGI::Carp (carpout);

{ local *CGI::Carp::stamp = sub {... };
  open( my $log, ">>", "/path/to/error.log") or die $!;
  carpout($log);
  carp("foo happened");
  # close($log)
 }
 carp("foo again but with module's timestamp");



On Sun, Mar 26, 2017 at 3:04 PM, X Dungeness  wrote:
> Shawn may have a different take but I think the "local" is
> misplaced and goes out of scope when the call's made.
>
> Here's a potential workaround:
>
> out of scope
>
> On Sun, Mar 26, 2017 at 2:02 PM, SSC_perl  wrote:
>>> On Mar 26, 2017, at 1:15 PM, Shawn H Corey  wrote:
>>>
>>> it would mean replacing the subroutine after the module was loaded.
>>
>> Thanks, Shawn, but I can’t get that to work.  Reading perldoc Core 
>> gives me the impression that I’d need to call the new sub, not the module.  
>> If that’s true, I don’t see how it would work in this scenario.  Here’s what 
>> I tried:
>>
>> BEGIN {
>> use CGI::Carp qw(carpout fatalsToBrowser );  # noTimestamp
>> {
>> no warnings;
>> use Time::Piece;
>> local *CGI::Carp::stamp = sub {
>> my $frame = 0;
>> my ($id, $pack, $file, $dev, $dirs);
>> if (defined($CGI::Carp::PROGNAME)) {
>> $id = $CGI::Carp::PROGNAME;
>> } else {
>> do {
>> $id = $file;
>> ($pack, $file) = caller($frame++);
>> } until !$file;
>> }
>> if (! $CGI::Carp::FULL_PATH) {
>> ($dev, $dirs, $id) = File::Spec->splitpath($id);
>> }
>> return "$id: " if $CGI::Carp::NO_TIMESTAMP;
>> my $time = scalar(localtime);
>> my $dt = Time::Piece->strptime($time, '%a %b %d %H:%M:%S %Y');
>> my $datetime = $dt->strftime('%Y-%m-%d %H:%M');
>> return "[$datetime] $id: ";
>> }
>> }
>> open (my $log, '>>', ‘/path/to/error.log');
>> carpout($log);
>> close ($log);
>> }
>> --
>> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
>> For additional commands, e-mail: beginners-h...@perl.org
>> http://learn.perl.org/
>>
>>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Newbie needs help changing date format

2004-08-26 Thread Wiggins d Anconia
> Hi
> 
> I have a list of dates that have been converted to epoch seconds,
processed
> and then converted back to a string (using timelocal). The resulting date
> format is:-
> 
> "Wed Mar 16 22:10:16 2004"
> 
> What is the easiest way to convert this format (or epoch seconds) to
> "16-Mar-2004 22:10" - preferrably using a standard module as I don't have
> administrator rights. 
> 
> Thanks
> John
> 

There are any number of modules that can do this.

Alternatively you can either use 'localtime' to get the pieces you want
and then do the conversions yourself, or you can use the 'strftime'
function of the POSIX (standard on POSIX systems) module.

perldoc POSIX
perldoc -f localtime

For more information. Note that POSIX relies on the underlying C header
for your system so you may have to 'man strftime' for the exact details,
despite the fact that it should be standard at least one part of it is
not across all systems (grrr... %z/%Z for the curious, I hit this one
before).

http://danconia.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Newbie needs help changing date format

2004-08-26 Thread Gunnar Hjalmarsson
John Bruin wrote:
I have a list of dates that have been converted to epoch seconds,
processed and then converted back to a string (using timelocal).
The resulting date format is:-
"Wed Mar 16 22:10:16 2004"
What is the easiest way to convert this format (or epoch seconds)
to "16-Mar-2004 22:10" -
The easiest way, I suppose, is to use epoch seconds as the starting
point and use some module. Pick one and give it a try.
Alternatively, the string is easily converted with the s/// operator:
s[\w{3}\s+(\w{3})\s+(\d{1,2})\s+(\d\d:\d\d):\d\d\s+(\d{4})]
 [$2-$1-$4 $3]
preferrably using a standard module as I don't have administrator
rights.
If you don't have root access, modules can be installed in a local
library.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



RE: Newbie needs help changing date format

2004-08-26 Thread John Bruin
Hi Wiggins and Gunnar

Thanks for your help. I come from an excel vba background where date
formatting is a simpler process. This is what I used:

$date_next_string = strftime("%d-%b-%Y %H:%M", localtime($date_next));



From: Wiggins d Anconia [mailto:[EMAIL PROTECTED] 
Sent: Friday, 27 August 2004 9:35 a.m.
To: John Bruin; [EMAIL PROTECTED]
Subject: Re: Newbie needs help changing date format

> Hi
> 
> I have a list of dates that have been converted to epoch seconds,
processed
> and then converted back to a string (using timelocal). The resulting 
> date format is:-
> 
> "Wed Mar 16 22:10:16 2004"
> 
> What is the easiest way to convert this format (or epoch seconds) to
> "16-Mar-2004 22:10" - preferrably using a standard module as I don't 
> have administrator rights.
> 
> Thanks
> John
> 

There are any number of modules that can do this.

Alternatively you can either use 'localtime' to get the pieces you want and
then do the conversions yourself, or you can use the 'strftime'
function of the POSIX (standard on POSIX systems) module.

perldoc POSIX
perldoc -f localtime

For more information. Note that POSIX relies on the underlying C header for
your system so you may have to 'man strftime' for the exact details, despite
the fact that it should be standard at least one part of it is not across
all systems (grrr... %z/%Z for the curious, I hit this one before).

http://danconia.org


--
To unsubscribe, e-mail: [EMAIL PROTECTED] For additional
commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/>
<http://learn.perl.org/first-response>






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Date format search in the file

2008-11-22 Thread Dermot
2008/11/22 Sureshkumar M (HCL Financial Services) <[EMAIL PROTECTED]>:
>
> Hi All,

Hi

>
> #!/usr/bin/perl

# Always use these, particularly when things aren't working as expected.
use strict;
use warnings;

> open(DATA,"i")||die "Unable to open the file";
>
> while()
>
> {
>
> if($_=~/(\d{2})([\W])\1\2\1]/)

I could be wrong but I don't think \w will not match a hypen "-" so
the test will fail.
This works for me:

if ($_=~/\d{1,2}-(\d{2}|\w{3})-\d+/)


HTH,
Dp.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Date format search in the file

2008-11-22 Thread John W. Krahn

Sureshkumar M (HCL Financial Services) wrote:

Hi All,


Hello,


I want to find the string which are having the date inside the
file.
Please help me how do I match it,below is my program and it's not
returning anything.

#!/usr/bin/perl


use warnings;
use strict;


open(DATA,"i")||die "Unable to open the file";


You should include the $! variable in the error message so you know 
*why* it failed to open.



while()
{
if($_=~/(\d{2})([\W])\1\2\1]/)


That regular expression says: match two digits anywhere in the string 
and store the results in $1, followed by a non-word character and store 
the results in $2, followed by the contents of the first capturing 
parentheses, followed by the contents of the second capturing 
parentheses, followed by the contents of the first capturing 
parentheses, followed by a ']' character.


So that will match, for example '15-15-15]'


{
print $_;
}
}
close(DATA);
exit 0;
~
Input file:

$cat i
15-06-79
05-06-1981
12-11-9
13-10-89
19-10-20009
1-10-0002
02-03-2008
03-nov-2008
$ 


Output should be:-

15-06-79
05-06-1981
13-10-89
02-03-2008
03-nov-2008


It looks like you want something like:

/^\d\d\D(?:\d\d|[a-zA-Z]{3})\D(?:\d\d|\d{4})$/




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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Date format search in the file

2008-11-22 Thread John W. Krahn

Dermot wrote:

2008/11/22 Sureshkumar M (HCL Financial Services) <[EMAIL PROTECTED]>:


#!/usr/bin/perl


# Always use these, particularly when things aren't working as expected.
use strict;
use warnings;


open(DATA,"i")||die "Unable to open the file";

while()

{

if($_=~/(\d{2})([\W])\1\2\1]/)


I could be wrong but I don't think \w will not match a hypen "-" so
the test will fail.


\w won't match '-', but \W will.


This works for me:

if ($_=~/\d{1,2}-(\d{2}|\w{3})-\d+/)


The OP wanted to match only two digit day values.  \w will also match 
digits so that will match a three digit month number.  The OP didn't 
want to match year values with one or five digits but that will match them.




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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Date format search in the file

2008-11-22 Thread sftriman
On Nov 22, 6:16 am, [EMAIL PROTECTED] (Dermot) wrote:
> 2008/11/22 Sureshkumar M (HCL Financial Services) <[EMAIL PROTECTED]>:
>
>
>
> > Hi All,
>
> Hi
>
>
>
> > #!/usr/bin/perl
>
> # Always use these, particularly when things aren't working as expected.
> use strict;
> use warnings;
>
> > open(DATA,"i")||die "Unable to open the file";
>
> > while()
>
> > {
>
> > if($_=~/(\d{2})([\W])\1\2\1]/)
>
> I could be wrong but I don't think \w will not match a hypen "-" so
> the test will fail.
> This works for me:
>
> if ($_=~/\d{1,2}-(\d{2}|\w{3})-\d+/)
>
> HTH,
> Dp.

Thanks for the reply.  I added strict and warning, and thankfully,
there were no messages.

The while loop on the file handle makes sense - I should do that.

What are you referring to in the if part?  I see I have an unescaped
hyphen which
I will make \- in the regexp compare.  But what is the compare you are
writing?

Also, I ran the script many times just now - it runs so fast, I can't
see why it's
causing the CPU surge:

$ time proc-js*pl
0.29s real 0.24s user 0.04s system
$ time proc-js*pl
0.33s real 0.28s user 0.03s system
$ time proc-js*pl
0.39s real 0.34s user 0.05s system

David


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Fwd: Date format search in the file

2008-11-24 Thread Dermot
2008/11/22 sftriman <[EMAIL PROTECTED]>:
> On Nov 22, 6:16 am, [EMAIL PROTECTED] (Dermot) wrote:
>> 2008/11/22 Sureshkumar M (HCL Financial Services) <[EMAIL PROTECTED]>:

>> I could be wrong but I don't think \w will not match a hypen "-" so
>> the test will fail.
>> This works for me:
>>
>> if ($_=~/\d{1,2}-(\d{2}|\w{3})-\d+/)
>>
>> HTH,
>> Dp.

Opps, yes John that correct. I didn't scroll down to the bit where it said

Output should be.

>
> Thanks for the reply.  I added strict and warning, and thankfully,
> there were no messages.
>
> The while loop on the file handle makes sense - I should do that.
>
> What are you referring to in the if part?  I see I have an unescaped
> hyphen which
> I will make \- in the regexp compare.

Who escaped a hypen? There is no need to escape a hypen. You escape
meta-characters and a hypen isn't.

But what is the compare you are
> writing?

My Regex was incorrect, as John pointed out. I was looking for

1 or 2 digit, a hypen, a 2 digit number of 3 character string, a hypen
and any number of  word characters.

You only want two digit day values and a 2-4 digit year value so
/\d{2}-(\d{2}|\w{3}-\d{2,4}/  would be the regex I would use.

Have a look at perldoc perlretut


> Also, I ran the script many times just now - it runs so fast, I can't
> see why it's
> causing the CPU surge:
>
> $ time proc-js*pl
>0.29s real 0.24s user 0.04s system
> $ time proc-js*pl
>0.33s real 0.28s user 0.03s system
> $ time proc-js*pl
>0.39s real 0.34s user 0.05s system

Can't help with that. :-/

Dp.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: date format search insdie the files

2008-11-29 Thread David Schmidt
You missed some parentheses

#!/usr/bin/perl

use strict;
use warnings;

open(DATA, '<', "data") || die"Unable to open the file";
while() {
#if($_=~/\d{2}-(\d{2}|\w{3})-\d{1,4}/) {
if(/(\d{2}-(\d{2})|(\w{3})-\d{1,4})/) {
print;
}
}
close(DATA);
exit 0;


On Sat, Nov 29, 2008 at 10:11 AM, Sureshkumar M (HCL Financial
Services) <[EMAIL PROTECTED]> wrote:
> Hi All,
>
>    I try to search the string which has the date format inside
> the file,
>
> But i am not able to get the desired files. Pls help me on this..
>
>
>
>
>
>
>
> C)/tmp/sms/perl$ cat a1
>
>
>
> 115-06-1979
>
> 10-11-81
>
> 20-NOV-2008
>
> 05-07-1981
>
> welcome
>
> 15-10-2008
>
> 12-03-20009
>
>
>
>
>
> (C)/tmp/sms/perl$ cat 1
>
>
>
> #/usr/bin/perl
>
> open(DATA,"a1")||die"Unable to open the file";
>
>
>
> while()
>
> {
>
> if($_=~/\d{2}-(\d{2}|\w{3})-\d{1,4}/)
>
> {
>
> print $_;
>
> }
>
> }
>
> close(DATA);
>
> exit 0;
>
> (C)/tmp/sms/perl$
>
>
>
>
>
>
>
> Output should be  :
>
>
>
> 10-11-81
>
> 20-NOV-2008
>
> 05-07-1981
>
> 15-10-2008
>
>
>
>
>
>
>
>
>
>
>
>



-- 
David Schmidt   |   http://www.fm5.at

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: date format search insdie the files

2008-11-29 Thread Sureshkumar M (HCL Financial Services)

Hi David,

 

  Thanks for the reply, but still I am not able to get expected
output from this.

Pls help me how to get this.

 

(C)/tmp/d$ cat a1

 

115-06-1979

10-11-81

20-NOV-2008

05-07-1981

welcome

20-03-20009

15-10-2008

 

 

(C)/tmp/d$ cat 1

#/usr/bin/perl

 

use strict;

use warnings;

 

open(DATA,"a1")||die"Unable to open the file";

 

while()

{

#if($_=~/\d\d\D(\d\d|\[a-zA-Z]{3})\D\d\d/)

if(/(\d{2}-(\d{2})|(\w{3})-\d{1,4})/)

{

#print $_;

print ;

}

}

close(DATA);

exit 0;

 

(C)/tmp/d$ perl 1   

115-06-1979

10-11-81

20-NOV-2008

05-07-1981

20-03-20009

15-10-2008

(C)/tmp/d$

 

 

 

 

 

 

 

 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of
David Schmidt
Sent: Saturday, November 29, 2008 3:12 PM
To: Sureshkumar M (HCL Financial Services); beginners@perl.org
Subject: Re: date format search insdie the files

 

You missed some parentheses

 

#!/usr/bin/perl

 

use strict;

use warnings;

 

open(DATA, '<', "data") || die"Unable to open the file";

while() {

#if($_=~/\d{2}-(\d{2}|\w{3})-\d{1,4}/) {

if(/(\d{2}-(\d{2})|(\w{3})-\d{1,4})/) {

print;

}

}

close(DATA);

exit 0;

 

 

On Sat, Nov 29, 2008 at 10:11 AM, Sureshkumar M (HCL Financial

Services) <[EMAIL PROTECTED]> wrote:

> Hi All,

> 

>I try to search the string which has the date format inside

> the file,

> 

> But i am not able to get the desired files. Pls help me on this..

> 

> 

> 

> 

> 

> 

> 

> C)/tmp/sms/perl$ cat a1

> 

> 

> 

> 115-06-1979

> 

> 10-11-81

> 

> 20-NOV-2008

> 

> 05-07-1981

> 

> welcome

> 

> 15-10-2008

> 

> 12-03-20009

> 

> 

> 

> 

> 

> (C)/tmp/sms/perl$ cat 1

> 

> 

> 

> #/usr/bin/perl

> 

> open(DATA,"a1")||die"Unable to open the file";

> 

> 

> 

> while()

> 

> {

> 

> if($_=~/\d{2}-(\d{2}|\w{3})-\d{1,4}/)

> 

> {

> 

> print $_;

> 

> }

> 

> }

> 

> close(DATA);

> 

> exit 0;

> 

> (C)/tmp/sms/perl$

> 

> 

> 

> 

> 

> 

> 

> Output should be  :

> 

> 

> 

> 10-11-81

> 

> 20-NOV-2008

> 

> 05-07-1981

> 

> 15-10-2008

> 

> 

> 

> 

> 

> 

> 

> 

> 

> 

> 

> 

 

 

 

-- 

David Schmidt   |   http://www.fm5.at



DISCLAIMER:
---
The contents of this e-mail and any attachment(s) are confidential and intended 
for the named recipient(s) only. 
It shall not attach any liability on the originator or HCL or its affiliates. 
Any views or opinions presented in 
this email are solely those of the author and may not necessarily reflect the 
opinions of HCL or its affiliates. 
Any form of reproduction, dissemination, copying, disclosure, modification, 
distribution and / or publication of 
this message without the prior written consent of the author of this e-mail is 
strictly prohibited. If you have 
received this email in error please delete it and notify the sender 
immediately. Before opening any mail and 
attachments please check them for viruses and defect.
---

RE: date format search insdie the files

2008-11-29 Thread Sureshkumar M (HCL Financial Services)
Hi all ,

 

Can someone help me on this

 



From: Sureshkumar M (HCL Financial Services) 
Sent: Saturday, November 29, 2008 4:21 PM
To: 'David Schmidt'
Cc: beginners@perl.org
Subject: RE: date format search insdie the files

 

Hi David,

 

  Thanks for the reply, but still I am not able to get expected
output from this.

Pls help me how to get this.

 

(C)/tmp/d$ cat a1

 

115-06-1979

10-11-81

20-NOV-2008

05-07-1981

welcome

20-03-20009

15-10-2008

 

 

(C)/tmp/d$ cat 1

#/usr/bin/perl

 

use strict;

use warnings;

 

open(DATA,"a1")||die"Unable to open the file";

 

while()

{

#if($_=~/\d\d\D(\d\d|\[a-zA-Z]{3})\D\d\d/)

if(/(\d{2}-(\d{2})|(\w{3})-\d{1,4})/)

{

#print $_;

print ;

}

}

close(DATA);

exit 0;

 

(C)/tmp/d$ perl 1   

115-06-1979

10-11-81

20-NOV-2008

05-07-1981

20-03-20009

15-10-2008

(C)/tmp/d$

 

 

 

 

 

 

 

 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of
David Schmidt
Sent: Saturday, November 29, 2008 3:12 PM
To: Sureshkumar M (HCL Financial Services); beginners@perl.org
Subject: Re: date format search insdie the files

 

You missed some parentheses

 

#!/usr/bin/perl

 

use strict;

use warnings;

 

open(DATA, '<', "data") || die"Unable to open the file";

while() {

#if($_=~/\d{2}-(\d{2}|\w{3})-\d{1,4}/) {

if(/(\d{2}-(\d{2})|(\w{3})-\d{1,4})/) {

print;

}

}

close(DATA);

exit 0;

 

 

On Sat, Nov 29, 2008 at 10:11 AM, Sureshkumar M (HCL Financial

Services) <[EMAIL PROTECTED]> wrote:

> Hi All,

> 

>I try to search the string which has the date format inside

> the file,

> 

> But i am not able to get the desired files. Pls help me on this..

> 

> 

> 

> 

> 

> 

> 

> C)/tmp/sms/perl$ cat a1

> 

> 

> 

> 115-06-1979

> 

> 10-11-81

> 

> 20-NOV-2008

> 

> 05-07-1981

> 

> welcome

> 

> 15-10-2008

> 

> 12-03-20009

> 

> 

> 

> 

> 

> (C)/tmp/sms/perl$ cat 1

> 

> 

> 

> #/usr/bin/perl

> 

> open(DATA,"a1")||die"Unable to open the file";

> 

> 

> 

> while()

> 

> {

> 

> if($_=~/\d{2}-(\d{2}|\w{3})-\d{1,4}/)

> 

> {

> 

> print $_;

> 

> }

> 

> }

> 

> close(DATA);

> 

> exit 0;

> 

> (C)/tmp/sms/perl$

> 

> 

> 

> 

> 

> 

> 

> Output should be  :

> 

> 

> 

> 10-11-81

> 

> 20-NOV-2008

> 

> 05-07-1981

> 

> 15-10-2008

> 

> 

> 

> 

> 

> 

> 

> 

> 

> 

> 

> 

 

 

 

-- 

David Schmidt   |   http://www.fm5.at



Re: date format search insdie the files

2008-11-29 Thread Dr.Ruud
"Sureshkumar M (HCL Financial Services)" schreef:

> #/usr/bin/perl
> open(DATA,"a1")||die"Unable to open the file";
> while()
> {
> if($_=~/\d{2}-(\d{2}|\w{3})-\d{1,4}/)
> {
> print $_;
> }
> }
> close(DATA);
> exit 0;

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $in_name = "data.in";
  {   open my $in_fh, "<", $in_name
or die "Error with '$in_name': $!";

  while ( <$in_fh> ) {
  print if /\b\d{2}-(?:\d{2}|\w{3})-\d{1,4}/;
  }
  }
  __END__

-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: date format search insdie the files

2008-11-29 Thread Sureshkumar M (HCL Financial Services)

Thanks a lot for your help,,


Can you explain me this part how it's works? 

 (?:\d{2}|\w{3})

?:   what this will do? 

And also, the output is like below


(C)/tmp/d$ perl 1
10-11-81
20-NOV-2008
05-07-1981
15-110-2008   this should not come as this as 3 digit month.
(C)/tmp/d$


Program:

(C)/tmp/d$ cat 1
#/usr/bin/perl
use strict;

open(DATA,")
{
print if /\b\d{2}\b-(\d{2}|\w{3})-\b\d{2,4}\b/;

}
close(DATA);
exit 0;
(C)/tmp/d$














-Original Message-
From: Dr.Ruud [mailto:[EMAIL PROTECTED] 
Sent: Saturday, November 29, 2008 5:30 PM
To: beginners@perl.org
Subject: Re: date format search insdie the files

"Sureshkumar M (HCL Financial Services)" schreef:

> #/usr/bin/perl
> open(DATA,"a1")||die"Unable to open the file";
> while()
> {
> if($_=~/\d{2}-(\d{2}|\w{3})-\d{1,4}/)
> {
> print $_;
> }
> }
> close(DATA);
> exit 0;

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $in_name = "data.in";
  {   open my $in_fh, "<", $in_name
or die "Error with '$in_name': $!";

  while ( <$in_fh> ) {
  print if /\b\d{2}-(?:\d{2}|\w{3})-\d{1,4}/;
  }
  }
  __END__

-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/



DISCLAIMER:
---
The contents of this e-mail and any attachment(s) are confidential and intended 
for the named recipient(s) only. 
It shall not attach any liability on the originator or HCL or its affiliates. 
Any views or opinions presented in 
this email are solely those of the author and may not necessarily reflect the 
opinions of HCL or its affiliates. 
Any form of reproduction, dissemination, copying, disclosure, modification, 
distribution and / or publication of 
this message without the prior written consent of the author of this e-mail is 
strictly prohibited. If you have 
received this email in error please delete it and notify the sender 
immediately. Before opening any mail and 
attachments please check them for viruses and defect.
---

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: date format search insdie the files

2008-11-29 Thread Dr.Ruud
"Sureshkumar M (HCL Financial Services)" schreef:

> Can you explain me this part how it's works?
>
>  (?:\d{2}|\w{3})
>
> ?:   what this will do?

Read perlre (and find out what (?:) means).



> And also, the output is like below
>
>
> (C)/tmp/d$ perl 1
> 10-11-81
> 20-NOV-2008
> 05-07-1981
> 15-110-2008   this should not come as this as 3 digit month.
> (C)/tmp/d$

Read perlre (and find out what \w means).


> Program:
>
> (C)/tmp/d$ cat 1
> #/usr/bin/perl
> use strict;
>
> open(DATA," while()
> {
> print if /\b\d{2}\b-(\d{2}|\w{3})-\b\d{2,4}\b/;
>
> }
> close(DATA);
> exit 0;

Compare your program to my version, and study what I changed about it.
Every change had a good reason.


Never fully quote a message, only quote (and summarize) what is relevant
for your reply.
React after each relevant part.

-- 
Affijn, Ruud

"Gewoon is een tijger."


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: date format search insdie the files

2008-11-29 Thread Dr.Ruud
"Sureshkumar M (HCL Financial Services)" schreef:

> Can someone help me on this

Impossible. 

-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Convert any date format to ISO

2013-07-24 Thread mimic...@gmail.com
I was trying to use Date::Simple to convert date from DD-MM- to ISO
standard -MM-DD,  but it produced error below because it returned undef
when the date passed is not ISO standard.

$ perl -e 'use Date::Simple (":all");$date =
Date::Simple->new("29-01-1972"); $x =  $date->as_iso;  print  "$x\n" or
"invalid\n";'
Can't call method "as_iso" on an undefined value at -e line 1.

 Based on the following two results, it's obvious the module does not
support date formats other than ISO.

perl -e 'use Date::Simple (":all");$date = Date::Simple->new("1972-01-29");
$x =  $date->as_iso;  print  "$x\n" or "invalid\n";'
1972-01-29

$ perl -e 'use Date::Simple (":all");$date = Date::Simple->new("19720129");
$x =  $date->as_iso;  print  "$x\n" or "invalid\n";'
1972-01-29

Most of the date modules on CPAN cannot do the job for me. I spent time
reading documentations but as it now stands, I have to do  this myself.

Mimi





On 24 July 2013 15:24, mimic...@gmail.com  wrote:

> I'm trying to convert dates (input from user in any format) to ISO and
> check it's valid.
>
> Can someone provide an example of this using Date::Simple or Date::Manip?
>
> Mimi
>


Re: Convert any date format to ISO

2013-07-24 Thread Michael Brader


On 07/25/2013 10:14 AM, mimic...@gmail.com wrote:
I was trying to use Date::Simple to convert date from DD-MM- to ISO 
standard -MM-DD,  but it produced error below because it returned 
undef when the date passed is not ISO standard.


Yeah on quick scan of the perldoc it looks like Date::Simple doesn't offer 
much in the way of parsing.



[...]

Most of the date modules on CPAN cannot do the job for me. I spent time 
reading documentations but as it now stands, I have to do  this myself.




There are at least 2 modules that can definitely do the job for you, 
Date::Manip::Date and DateTime (with DateTime::Format::Natural). I usually 
use the latter, but since you want the former:


Have a look at the documentation for Date::Manip::Date and look for 
parse_format and printf


If you are still stuck, don't hesitate to show your attempt and ask for 
more help.


Cheers,
Michael

--
Michael BraderSenior Software Engineer and Perl Person
Our World Wide Web has a World Wide Network  Technology/Softdev/DevOps
Internode   http://internode.on.net/  mbra...@internode.com.au
iiNet http://iinet.net.au/ m.bra...@staff.iinet.net.au



Re: Convert any date format to ISO

2013-07-24 Thread Jim Gibson

On Jul 24, 2013, at 5:44 PM, mimic...@gmail.com wrote:

> I was trying to use Date::Simple to convert date from DD-MM- to ISO 
> standard -MM-DD,  but it produced error below because it returned undef 
> when the date passed is not ISO standard.

You don't need a module to recognize a date in the form DD-MM- and change 
it into the form -MM-DD (untested):

if( $date =~ /^(\d\d)-(\d\d)-(\d\d\d\d)$/ ) {
  $date = "$3-$2-$1";
}else{
# try other conversions
}

The module Date::Parse does conversions of string dates into numerical ones 
(but not the one you need). You probably need to tell people what date formats 
are acceptable, and reject any that will not convert into an ISO form. Try as 
many conversions as you can in some reasonable order and stop if you get one 
that works.

> Most of the date modules on CPAN cannot do the job for me. I spent time 
> reading documentations but as it now stands, I have to do  this myself. 

The DateTime module has a lot of date/time manipulation functions, but not 
string conversions.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Convert any date format to ISO

2013-07-24 Thread Michael Brader


On 07/25/2013 03:26 PM, Jim Gibson wrote:

You don't need a module to recognize a date in the form DD-MM- and change 
it into the form -MM-DD (untested):

if( $date =~ /^(\d\d)-(\d\d)-(\d\d\d\d)$/ ) {
   $date = "$3-$2-$1";
}else{
 # try other conversions
}


Jim's right, but be aware that when you roll your own date code, you are 
taking all the validation work upon yourself. If you can trust your input, 
that's probably fine, but if you are dealing with user input or input from 
an untrusted source, you probably want to let CPAN do the work for you.


For example, the code above will happily turn '99-99-2013' into '2013-99-99'.

Cheers,
Michael

--
Michael BraderSenior Software Engineer and Perl Person
Our World Wide Web has a World Wide Network  Technology/Softdev/DevOps
Internode   http://internode.on.net/  mbra...@internode.com.au
iiNet http://iinet.net.au/ m.bra...@staff.iinet.net.au


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Convert any date format to ISO

2013-07-25 Thread Charles DeRykus
On Wed, Jul 24, 2013 at 10:56 PM, Michael Brader
wrote:

>
> On 07/25/2013 10:14 AM, mimic...@gmail.com wrote:
>
>  I was trying to use Date::Simple to convert date from DD-MM- to ISO
> standard -MM-DD,  but it produced error below because it returned undef
> when the date passed is not ISO standard.
>
>
> Yeah on quick scan of the perldoc it looks like Date::Simple doesn't offer
> much in the way of parsing.
>
>  [...]
>
> Most of the date modules on CPAN cannot do the job for me. I spent time
> reading documentations but as it now stands, I have to do  this myself.
>
>
> There are at least 2 modules that can definitely do the job for you,
> Date::Manip::Date and DateTime (with DateTime::Format::Natural). I usually
> use the latter, but since you want the former:
>
> Have a look at the documentation for Date::Manip::Date and look for
> parse_format and printf
>
>
>
Date::Manip and friends are wonderfully versatile but with even in this
simple case, it does start to get a bit twisty with special parse and
output methods:


use Date::Manip::Date;
my $date = Date::Manip::Date->new;

my $err=$date->parse_format("%d-%m-%Y","29-01-1972")
  or  die "can't parse date: $err";

print $date->printf("%m-%d-%Y");   # 01-29-1972

P.S.

IMO, it'd be superb if the earlier functional interface or even parse_date
in Date::Manip::Date would just accept a hash ref of non-standard  format
specs.  Then, you could just output with UnixDate as usual.


 eg,  ParseDate( ... ,   { fmt1="%d-%m-%Y", fmt2=... } );


-- 
Charles DeRykus


Re: Convert any date format to ISO

2013-07-27 Thread Charles DeRykus
On Fri, Jul 26, 2013 at 4:45 AM, Perl Beginners  wrote:

> On 07/25/2013 04:40 PM, Charles DeRykus wrote:
>
>>
>>
>>
>> On Wed, Jul 24, 2013 at 10:56 PM, Michael Brader <
>> mbra...@internode.com.au 
>> >
>> wrote:
>>
>>
>> On 07/25/2013 10:14 AM, mimic...@gmail.com
>>  wrote:
>>
>>> I was trying to use Date::Simple to convert date from DD-MM-
>>> to ISO standard -MM-DD,  but it produced error below because
>>> it returned undef when the date passed is not ISO standard.
>>>
>>
>> Yeah on quick scan of the perldoc it looks like Date::Simple
>> doesn't offer much in the way of parsing.
>>
>>  [...]
>>>
>>> Most of the date modules on CPAN cannot do the job for me. I
>>>
>>
>> Date::Manip and friends are wonderfully versatile but with even in this
>> simple case, it does start to get a bit twisty with special parse and
>> output methods:
>>
>> IMO, it'd be superb if the earlier functional interface or even
>> parse_date  in Date::Manip::Date would just accept a hash ref of
>> non-standard  format specs.  Then, you could just output with UnixDate as
>> usual.
>>
>>
>>  eg,  ParseDate( ... ,   { fmt1="%d-%m-%Y", fmt2=... } )
>>
>

>  Perhaps I'm missing something... but why do you want a hash ref?  Why not
> just a list (or listref) of formats. I'm not sure what the keys 'fmt1',
> 'fmt2', play in your suggestion (and if they don't play any role, a listref
> would definitely be better).
>
> So, if I understand your suggestion, what you want is this:
>
>$date->parse( $string, $listref, @opts );
>
> where $listref is optional, but if included, it would be a list of
> formats(similar to parse_format) thatthe date would be parsed against.  If
> they all failed, it could then fall back on the standard formats. If
> $listref were omitted, only the standard formats would be used.
>
> Does this sound like what you're suggesting?
>
>
Yes  the listref would be another simpler way.  I can't see
any value for the hashref unless someone had a need for a
shortcut identifier for the actual format.

-- 
Charles DeRykus


Re: Convert any date format to ISO

2013-07-27 Thread Michael Brader


On 07/26/2013 06:10 AM, Charles DeRykus wrote:



On Wed, Jul 24, 2013 at 10:56 PM, Michael Brader 
mailto:mbra...@internode.com.au>> wrote:


[...]
There are at least 2 modules that can definitely do the job for you,
Date::Manip::Date and DateTime (with DateTime::Format::Natural). I
usually use the latter, but since you want the former:

Have a look at the documentation for Date::Manip::Date and look for
parse_format and printf



Date::Manip and friends are wonderfully versatile but with even in this 
simple case, it does start to get a bit twisty with special parse and 
output methods:


[...]


Agreed, that's why I tend to stick with DateTime these days.

Cheers,
Michael

--
Michael BraderSenior Software Engineer and Perl Person
Our World Wide Web has a World Wide Network  Technology/Softdev/DevOps
Internode   http://internode.on.net/  mbra...@internode.com.au
iiNet http://iinet.net.au/ m.bra...@staff.iinet.net.au



WIN32::OLE problem while reading the date format

2006-02-08 Thread swayam panda

Hi ALL,

I am reading some values from the Excel file by using Win32::OLE,I 
am able to read  all fields bur not able able read if the date is in 
dd/mm/yy( i.e / slash) .If i am using backward slash then the date is 
working .Can any body tell me how to read the a date from the EXCEL file



Example  EXCEL file

 TESTERNAME Swayam
 DATE 9/2/2006
 DEVICETYPE SOLARIS
 HARDWAREVERSION vx34.56



if i am reading the file  then i am able to get the values of 
TESTER_NAME,DEVICE_TYPE,HARDWARE_VERSION but it's not able to take the value 
of DATE. but if i am using backward slash (\) then it's working .


Part  of my code



open (FILE,">>reportfile") or die "can't open $!";

for ($i = 1; $i <=12 ; $i++)
{
   # Skip over empty cells
   if ( (!defined($sheet->Range('A'.$i)->{'Value'}))  )
   {
  next;
   }

   elsif ($sheet->Range('A'.$i)->{'Value'} eq  "TESTERNAME")
   {

 print  FILE "#" . $sheet->Range('A'.$i)->{'Value'} . " :\t\t" 
.$sheet->Range('B'.$i)->{'Value'}. "\n";

   }

   elsif ($sheet->Range('A'.$i)->{'Value'} eq  "DATE")
   {
 print FILE "#" . $sheet->Range('A'.$i)->{'Value'} ." :\t\t\t" 
."$sheet->Range('B'.$i)->{'Value'}"."\n";

   }

   elsif ($sheet->Range('A'.$i)->{'Value'} eq  "DEVICETYPE")
   {
 print FILE "#" . $sheet->Range('A'.$i)->{'Value'} . " :\t\t" 
.$sheet->Range('B'.$i)->{'Value'}. "\n";

   }

   elsif ($sheet->Range('A'.$i)->{'Value'} eq  "HARDWAREVERSION")
   {
 print FILE "#" . $sheet->Range('A'.$i)->{'Value'} . " :\t" 
.$sheet->Range('B'.$i)->{'Value'}. "\n";

   }
}

But the same code is working if in the excel file (which i am reading ) i am 
using date format as 06\02\2006 ( back ward slash)



Thanks 



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: WIN32::OLE problem while reading the date format

2006-02-08 Thread Alfred Vahau
See Win32::OLE::Variant on ActiveState Perl Documentation on how to read 
the date correctly.


Alfred,

swayam panda wrote:


Hi ALL,

I am reading some values from the Excel file by using 
Win32::OLE,I am able to read  all fields bur not able able read if the 
date is in dd/mm/yy( i.e / slash) .If i am using backward slash then 
the date is working .Can any body tell me how to read the a date from 
the EXCEL file



Example  EXCEL file

 TESTERNAME Swayam
 DATE 9/2/2006
 DEVICETYPE SOLARIS
 HARDWAREVERSION vx34.56



if i am reading the file  then i am able to get the values of 
TESTER_NAME,DEVICE_TYPE,HARDWARE_VERSION but it's not able to take the 
value of DATE. but if i am using backward slash (\) then it's working .


Part  of my code



open (FILE,">>reportfile") or die "can't open $!";

for ($i = 1; $i <=12 ; $i++)
{
   # Skip over empty cells
   if ( (!defined($sheet->Range('A'.$i)->{'Value'}))  )
   {
  next;
   }

   elsif ($sheet->Range('A'.$i)->{'Value'} eq  "TESTERNAME")
   {

 print  FILE "#" . $sheet->Range('A'.$i)->{'Value'} . " :\t\t" 
.$sheet->Range('B'.$i)->{'Value'}. "\n";

   }

   elsif ($sheet->Range('A'.$i)->{'Value'} eq  "DATE")
   {
 print FILE "#" . $sheet->Range('A'.$i)->{'Value'} ." :\t\t\t" 
."$sheet->Range('B'.$i)->{'Value'}"."\n";

   }

   elsif ($sheet->Range('A'.$i)->{'Value'} eq  "DEVICETYPE")
   {
 print FILE "#" . $sheet->Range('A'.$i)->{'Value'} . " :\t\t" 
.$sheet->Range('B'.$i)->{'Value'}. "\n";

   }

   elsif ($sheet->Range('A'.$i)->{'Value'} eq  "HARDWAREVERSION")
   {
 print FILE "#" . $sheet->Range('A'.$i)->{'Value'} . " :\t" 
.$sheet->Range('B'.$i)->{'Value'}. "\n";

   }
}

But the same code is working if in the excel file (which i am reading 
) i am using date format as 06\02\2006 ( back ward slash)



Thanks



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>