CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Getting specific Dates by numerical values

 



regex2012
User

Oct 7, 2015, 8:50 AM

Post #1 of 5 (1317 views)
Getting specific Dates by numerical values Can't Post

I am experimenting with dates and date modules and I found with Date::Manip I could do the following:

Code
$main::TZ= 'GMT'; 
print UnixDate(ParseDate("first Friday in October 2015"),"First Friday
+of the month is %m/%d/%Y");


However, I want to get the first Friday by using numbers - not the "first Friday string." there is a command, "cal" in bash, that will get a calendar and from there I can do it. But I want to do it in Perl. Is there any way using the following modules that I see are available in our binaries:


Code
use Date::Calc; 
use Date::Parse;
use Date::Manip;


If not, is there any way by any other means?


Laurent_R
Veteran / Moderator

Oct 7, 2015, 10:35 AM

Post #2 of 5 (1307 views)
Re: [regex2012] Getting specific Dates by numerical values [In reply to] Can't Post

Hum, can you explain what you mean by "the first Friday by using numbers", i.e. show what these "numbers" ought to be? Date in the month? Please say what you need exactly.


FishMonger
Veteran / Moderator

Oct 7, 2015, 10:52 AM

Post #3 of 5 (1305 views)
Re: [regex2012] Getting specific Dates by numerical values [In reply to] Can't Post

Either of these 2 modules might do what you need, but I can't say for sure because you haven't made clear what you need to do.

Time::Date - A time and date object for Perl
http://search.cpan.org/~jacobg/Time-Date-0.03/lib/Time/Date.pm

Time::Piece - Object Oriented time objects
http://search.cpan.org/~rjbs/Time-Piece-1.30/Piece.pm


regex2012
User

Oct 7, 2015, 11:49 AM

Post #4 of 5 (1302 views)
Re: [Laurent_R] Getting specific Dates by numerical values [In reply to] Can't Post

for example, you need to write:
first Friday in October 2015"
I want to write the first Friday in 10/2015 or 10 2015


Laurent_R
Veteran / Moderator

Oct 8, 2015, 1:55 AM

Post #5 of 5 (1275 views)
Re: [regex2012] Getting specific Dates by numerical values [In reply to] Can't Post

There might be a simpler way, perhaps using another module, but you could use Time::Piece calculate the weekday for the first day of the month and, from there, add the required number of days to get to the first Friday in the month. The idea is shown in the following session under the Perl debugger:

Code
  DB<1> use Time::Local; # used for initializing the first day in the month 

DB<2> use Time::Piece;

DB<3> use Time::Seconds; # used only to import the ONE_DAY constant (number of seconds in a day)

DB<4> $month = 10 - 1; # months start at 0 in Time::Local. October -> 09

DB<5> $friday = 6; # 6th day in the week (Sunday = 1). Probably use a weekday hash for the more general case

DB<6> $t =localtime timelocal(O, O, O, 1, $month, 2015);

DB<7> p $t->cdate; # check that we really have Oct 1, 2015
Thu Oct 1 00:00:00 2015
DB<8> $delta = $friday - $t->wday;

DB<9> p $delta;
1
DB<10> $delta = 7 + $delta if $delta < 0;

DB<11> $t1 = $t + $delta * ONE_DAY; # ONE_DAY defined in Time::Seconds; could use 60 * 60 * 24 instead

DB<12> print $t1->cdate;
Fri Oct 2 00:00:00 2015


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives