Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Setting up date to subtract specific number from it

by daxim (Chaplain)
on Nov 25, 2013 at 16:11 UTC ( #1064236=note: print w/ replies, xml ) Need Help??


in reply to Setting up date to subtract specific number from it

use 5.014;
use DateTime qw();

my %day2t = (
    Friday    => 'T',
    Saturday  => 'T-1',
    Sunday    => 'T-2',
    Monday    => 'T-3',
    Tuesday   => 'T-4',
    Wednesday => 'T-5',
    Thursday  => 'T-6',
);

{
    my $today = DateTime->now->truncate(to => 'day'); # 2013-11-25T00:00:00
    $today->day_name;   # Monday
    $day2t{$today->day_name};   # T-3
    my $into_the_past = ($day2t{$today->day_name} =~ s/T-?//r) || 0; # 3
    $today->clone->subtract(days => $into_the_past)->strftime('%F'); # 2013-11-22
}

{
    my $beginning_of_november = DateTime->new(year => 2013, month => 11, day => 1); # 2013-11-01T00:00:00
    $beginning_of_november->day_name; # Friday
    $day2t{$beginning_of_november->day_name};   # T
    my $into_the_past = ($day2t{$beginning_of_november->day_name} =~ s/T-?//r) || 0; # 0
    $beginning_of_november->clone->subtract(days => $into_the_past)->strftime('%F'); # 2013-11-01
}


Comment on Re: Setting up date to subtract specific number from it
Re^2: Setting up date to subtract specific number from it
by vihar (Acolyte) on Nov 25, 2013 at 16:25 UTC
    I really appreciate your help! Just wondering if there is a way to do this without using the Date::Time module? I am not a root user on my box and I am supposed to be avoiding the use of modules as much as possible. Thanks!

      "...as much as possible."

      Good, so they left you a little room to work with. I mean "as much as possible" isn't as restrictive as "none at all, ever, for any reason."

      Date manipulation is one of the situations where modules are invaluable. Hand rolled solutions usually miss something. Do they want date bugs, or date modules?

      Perhaps you could ask your client what date modules they already have installed, and work using one of those.


      Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1064236]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-04-20 00:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls