Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

code to get the date for "next Thursday"

by bfdi533 (Friar)
on Mar 20, 2006 at 20:37 UTC ( [id://538038]=perlquestion: print w/replies, xml ) Need Help??

bfdi533 has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code that I have been using to get the date for "next Thursday" but it runs VERY slow. From what I can tell it is because I am using Date::Manip.

Is there a better/different way to do this?

#!/usr/bin/perl use Date::Manip; $today_dt = &ParseDate("today"); $new_dt = &Date_GetNext($today_dt, 'Thu', 1); $date = &UnixDate($new_dt, "%Y-%m-%d"); print $date . "\n";

Replies are listed 'Best First'.
Re: code to get the date for "next Thursday"
by Albannach (Monsignor) on Mar 20, 2006 at 21:08 UTC
    Again it might not be any faster, but it is useful to note that ParseDate is smarter than you think:

    Use Date::Manip; print UnixDate(ParseDate('next thursday'),'%A %e %b %Y'),"\n";

    gives Thursday 23 Mar 2006

    Update: Here's a method using the usually-faster Date::Calc and a benchmark of some of the offerings... any guesses as to why Date::Manip is soooo slow? (1.7GHz P4 Win2k, 256MB RAM)

    Rate Manip bfdi ikegami Calc Manip 216/s -- -7% -99% -100% bfdi 231/s 7% -- -99% -100% ikegami 29540/s 13596% 12701% -- -65% Calc 84063/s 38874% 36327% 185% --

    Code follows:

    --
    I'd like to be able to assign to an luser

Re: code to get the date for "next Thursday"
by ikegami (Patriarch) on Mar 20, 2006 at 20:54 UTC

    It might not be any faster, but you can also do it as follows:

    use POSIX qw( strftime ); use Time::Local qw( timegm_nocheck ); my ($day, $mon, $year, $wday) = (localtime())[3..6]; # Defaults to now. $year += 1900; $day += (7 - $wday + 4) % 7; $next_th = timegm_nocheck(0,0,0,$day,$mon,$year); print(strftime('%Y-%m-%d', gmtime($next_th)), "\n");

    Only core modules are used.

    Update: Now using GMT functions for the date to avoid problems from daylight savings time. (This should speed things up a bit too!) The date is still in localtime since we used localtime to fetch the current date.

    Update: I had two names for the same variable ($day and $mday). Fixed.

      This method appears to be a little bit slower, though neither seem all that "slow" to me. 16+ million iterations per second on my little powerbook seems plenty fast...

      Are you sure this is the piece of your code that's slow?

      Rate ikegami code bfdi code ikegami code 16393443/s -- -20% bfdi code 20408163/s 24% --

        Well, the machine that I normally run this on takes 3-5 seconds to run the script. My current laptop runs this pretty quickly but it is also not the "production environment" for this script.

        I will take a look at some of the options as well as the test script to see which one will work best for the target system.

        Thanks!

Re: code to get the date for "next Thursday"
by saintmike (Vicar) on Mar 20, 2006 at 21:09 UTC
    Or, yet differently, if you want to score some software design points:
    use DateTime; use DateTime::Event::Recurrence; my $thu = DateTime::Event::Recurrence-> weekly(days => 4); # Thursday my $days = DateTime::Event::Recurrence-> daily(); my $set = $thu->intersection($days); my $it = $set->iterator( start => DateTime->today() ); if(my $dt = $it->next()) { print "Next Thursday from today: $dt\n"; }
Re: code to get the date for "next Thursday"
by duff (Parson) on Mar 20, 2006 at 21:00 UTC

    In the spirit of TMTOWTDI, here's a way to do it with GNU date:

    date -d 'Thursday + 7 days' +"%Y-%m-%d"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://538038]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-23 22:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found