For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > last month in Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author last month in Perl
pinoy2ser@gmail.com

2006-10-02, 6:59 pm

Hello,

I'm very new in Perl I was wondering if any one can help me get
the last month's month ...

Thanks
pinoy

DJ Stunks

2006-10-02, 6:59 pm

pinoy2ser@gmail.com wrote:
> Hello,
>
> I'm very new in Perl I was wondering if any one can help me get
> the last month's month ...


C:\>perl -MDate::Manip -e "print UnixDate('last month','%B')"
September

-jp

pinoy2ser@gmail.com

2006-10-02, 6:59 pm


DJ Stunks wrote:
> pinoy2ser@gmail.com wrote:
>
> C:\>perl -MDate::Manip -e "print UnixDate('last month','%B')"
> September
>
> -jp


-jp

I im sorry i forgot to mension, use unix as my OS..Im not if that
matters?

Paul Lalli

2006-10-02, 6:59 pm

DJ Stunks wrote:
> pinoy2ser@gmail.com wrote:
>
> C:\>perl -MDate::Manip -e "print UnixDate('last month','%B')"
> September


Or, without using an insanely over-bloated non-standard module whose
own documentation says you should use it only as a last resort.... ;-)

perl -MPOSIX=strftime -le'@time = localtime; $time[4]--; print
strftime("%B", @time);'
September

Paul Lalli

Paul Lalli

2006-10-02, 6:59 pm

Paul Lalli wrote:
> perl -MPOSIX=strftime -le'@time = localtime; $time[4]--; print
> strftime("%B", @time);'
> September


Hrm. That won't necessarily work for every date. Wouldn't work if you
called it on March 30, for example. This one, I think, should:

perl -MPOSIX=strftime -le'@time = localtime; $time[3] = 0; print
strftime("%B", @time);'

Paul Lalli

pinoy2ser@gmail.com

2006-10-02, 6:59 pm

Paul,

this works perfectly......


Thank you very much

pinoy


Paul Lalli wrote:
> Paul Lalli wrote:
>
> Hrm. That won't necessarily work for every date. Wouldn't work if you
> called it on March 30, for example. This one, I think, should:
>
> perl -MPOSIX=strftime -le'@time = localtime; $time[3] = 0; print
> strftime("%B", @time);'
>
> Paul Lalli


pinoy2ser@gmail.com

2006-10-02, 6:59 pm


pinoy2...@gmail.com wrote:[color=darkred]
> Paul,
>
> this works perfectly......
>
>
> Thank you very much
>
> pinoy
>
>
> Paul Lalli wrote:


regarding the $time what does [3] represent?

Paul Lalli

2006-10-02, 6:59 pm

pinoy2ser@gmail.com wrote:
[color=darkred]
[color=darkred]
> regarding the $time what does [3] represent?


The localtime() function returns a list of values. Those values are
the current time's seconds, minutes, hours, days, months, and years.
In that order. @time is an array containing these values. $time[3] is
the fourth element of @time. Therefore, $time[3] is the number of days
in the current month of the current datetime. By setting this date to
0, we are effectively saying we want the day before the first date of
the current month (ie, the last day of the previous month). So if I
were to execute this script right now, the time returned by localtime
would be:
13, 40, 15, 2, 9, 106
(the months go from 0-11, and the years are the current year minus
1900) to represent a date time of
2006-10-02 15:40:13

By setting the fourth value to 0, the time we get is:
2006-09-30 15:40:13

so we are able to extract the month from that date time.

Hope that helps,
Paul Lalli

Paul Lalli

2006-10-02, 6:59 pm

pinoy2ser@gmail.com wrote:
[color=darkred]
[color=darkred]
> regarding the $time what does [3] represent?


The localtime() function returns a list of values. Those values are
the current time's seconds, minutes, hours, days, months, and years.
In that order. @time is an array containing these values. $time[3] is
the fourth element of @time. Therefore, $time[3] is the number of days
in the current month of the current datetime. By setting this date to
0, we are effectively saying we want the day before the first date of
the current month (ie, the last day of the previous month). So if I
were to execute this script right now, the time returned by localtime
would be:
13, 40, 15, 2, 9, 106
(the months go from 0-11, and the years are the current year minus
1900) to represent a date time of
2006-10-02 15:40:13

By setting the fourth value to 0, the time we get is:
2006-09-30 15:40:13

so we are able to extract the month from that date time.

Hope that helps,
Paul Lalli

DJ Stunks

2006-10-02, 6:59 pm

Paul Lalli wrote:
> DJ Stunks wrote:
>
> Or, without using an insanely over-bloated non-standard module whose
> own documentation says you should use it only as a last resort.... ;-)


hahaha I know, I know. In fact I was caught by this once, where I used
Date::Manip to alter a date for each line of a huge file. The script
was slower than molasses in January. Switched to Date::Calc and it
roared.

However, being able to say "last month" is so nice: self documenting
code :-)~

Plus, it's easier to use Date::Manip than it is to use Date::Parse and
use Date::Format.

-jp

pinoy2ser@gmail.com

2006-10-03, 8:02 am

Thank you both for your help and explanation...

Regards
pinoy!!!!


DJ Stunks wrote:
> Paul Lalli wrote:
>
> hahaha I know, I know. In fact I was caught by this once, where I used
> Date::Manip to alter a date for each line of a huge file. The script
> was slower than molasses in January. Switched to Date::Calc and it
> roared.
>
> However, being able to say "last month" is so nice: self documenting
> code :-)~
>
> Plus, it's easier to use Date::Manip than it is to use Date::Parse and
> use Date::Format.
>
> -jp


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com