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:

use strict; use warnings; use Benchmark qw(cmpthese); use Date::Manip; use Date::Calc qw(Today Day_of_Week Add_Delta_Days); Date_Init('TZ=EDT'); # for ikegami use POSIX qw( strftime ); use Time::Local qw( timegm_nocheck ); sub ikegami { my ($day, $mon, $year, $wday) = (localtime())[3..6]; # Defaults to +now. $year += 1900; $day += (7 - $wday + 4) % 7; my $next_th = timegm_nocheck(0,0,0,$day,$mon,$year); # fixed, was $m +day rather than $day return strftime('%Y-%m-%d', gmtime($next_th)); } sub bfdi533 { my $today_dt = &ParseDate("today"); my $new_dt = &Date_GetNext($today_dt, 'Thu', 1); my $date = &UnixDate($new_dt, "%Y-%m-%d"); } sub Manip { return UnixDate(ParseDate('next thursday'),'%Y-%m-%e'); } sub Calc { my($year,$month,$day) = Today(); my $nextday = 4; # Thursday my $dow = Day_of_Week($year,$month,$day); my $delta = (7 + $nextday - $dow) % 7; my ($y,$m,$d) = Add_Delta_Days($year, $month, $day, $delta); return sprintf('%4i-%02i-%02i', $y, $m, $d); } cmpthese(-3, { bfdi => \&bfdi533, ikegami => \&ikegami, Manip => \&Manip, Calc => \&Calc, } );

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


In reply to Re: code to get the date for "next Thursday" by Albannach
in thread code to get the date for "next Thursday" by bfdi533

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.